blob: 84c429105e7042a4f3675c1c0fba8780a5d36505 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 The Android Open Source Project
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000010#ifndef SkPDFGraphicState_DEFINED
11#define SkPDFGraphicState_DEFINED
12
13#include "SkPaint.h"
14#include "SkPDFTypes.h"
15#include "SkTemplates.h"
16#include "SkThread.h"
17
vandebo@chromium.org6112c212011-05-13 03:50:38 +000018class SkPDFFormXObject;
19
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000020/** \class SkPDFGraphicState
21 SkPaint objects roughly correspond to graphic state dictionaries that can
vandebo@chromium.org6112c212011-05-13 03:50:38 +000022 be installed. So that a given dictionary is only output to the pdf file
23 once, we want to canonicalize them. Static methods in this class manage
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000024 a weakly referenced set of SkPDFGraphicState objects: when the last
25 reference to a SkPDFGraphicState is removed, it removes itself from the
26 static set of objects.
27
28*/
29class SkPDFGraphicState : public SkPDFDict {
30public:
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +000031 enum SkPDFSMaskMode {
32 kAlpha_SMaskMode,
33 kLuminosity_SMaskMode
34 };
35
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000036 virtual ~SkPDFGraphicState();
37
edisonn@google.com6addb192013-04-02 15:33:08 +000038 virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
39 SkTSet<SkPDFObject*>* newResourceObjects);
vandebo@chromium.org6112c212011-05-13 03:50:38 +000040
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000041 // Override emitObject and getOutputSize so that we can populate
42 // the dictionary on demand.
43 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
44 bool indirect);
45 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
46
47 /** Get the graphic state for the passed SkPaint. The reference count of
48 * the object is incremented and it is the caller's responsibility to
vandebo@chromium.org6112c212011-05-13 03:50:38 +000049 * unreference it when done. This is needed to accommodate the weak
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000050 * reference pattern used when the returned object is new and has no
51 * other references.
52 * @param paint The SkPaint to emulate.
53 */
reed@google.comf6c3ebd2011-07-20 17:20:28 +000054 static SkPDFGraphicState* GetGraphicStateForPaint(const SkPaint& paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055
vandebo@chromium.org6112c212011-05-13 03:50:38 +000056 /** Make a graphic state that only sets the passed soft mask. The
57 * reference count of the object is incremented and it is the caller's
58 * responsibility to unreference it when done.
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +000059 * @param sMask The form xobject to use as a soft mask.
60 * @param invert Indicates if the alpha of the sMask should be inverted.
61 * @param sMaskMode Whether to use alpha or luminosity for the sMask.
vandebo@chromium.org6112c212011-05-13 03:50:38 +000062 */
reed@google.comf6c3ebd2011-07-20 17:20:28 +000063 static SkPDFGraphicState* GetSMaskGraphicState(SkPDFFormXObject* sMask,
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +000064 bool invert,
65 SkPDFSMaskMode sMaskMode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +000066
67 /** Get a graphic state that only unsets the soft mask. The reference
68 * count of the object is incremented and it is the caller's responsibility
69 * to unreference it when done. This is needed to accommodate the weak
70 * reference pattern used when the returned object is new and has no
71 * other references.
72 */
reed@google.comf6c3ebd2011-07-20 17:20:28 +000073 static SkPDFGraphicState* GetNoSMaskGraphicState();
vandebo@chromium.org6112c212011-05-13 03:50:38 +000074
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000075private:
76 const SkPaint fPaint;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000077 SkTDArray<SkPDFObject*> fResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000078 bool fPopulated;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000079 bool fSMask;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000080
81 class GSCanonicalEntry {
82 public:
83 SkPDFGraphicState* fGraphicState;
84 const SkPaint* fPaint;
85
86 bool operator==(const GSCanonicalEntry& b) const;
87 explicit GSCanonicalEntry(SkPDFGraphicState* gs)
88 : fGraphicState(gs),
89 fPaint(&gs->fPaint) {}
vandebo@chromium.org73322072011-06-21 21:19:41 +000090 explicit GSCanonicalEntry(const SkPaint* paint)
91 : fGraphicState(NULL),
92 fPaint(paint) {}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000093 };
94
95 // This should be made a hash table if performance is a problem.
reed@google.comf6c3ebd2011-07-20 17:20:28 +000096 static SkTDArray<GSCanonicalEntry>& CanonicalPaints();
digit@google.com1771cbf2012-01-26 21:26:40 +000097 static SkBaseMutex& CanonicalPaintsMutex();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000098
vandebo@chromium.org6112c212011-05-13 03:50:38 +000099 SkPDFGraphicState();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000100 explicit SkPDFGraphicState(const SkPaint& paint);
101
102 void populateDict();
103
vandebo@chromium.org19e3c1e2011-05-25 00:41:30 +0000104 static SkPDFObject* GetInvertFunction();
105
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000106 static int Find(const SkPaint& paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000107};
108
109#endif