blob: b43c1076c4d70015afd05d24b90f53c059f20439 [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkPDFGraphicState_DEFINED
18#define SkPDFGraphicState_DEFINED
19
20#include "SkPaint.h"
21#include "SkPDFTypes.h"
22#include "SkTemplates.h"
23#include "SkThread.h"
24
vandebo@chromium.org6112c212011-05-13 03:50:38 +000025class SkPDFFormXObject;
26
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027/** \class SkPDFGraphicState
28 SkPaint objects roughly correspond to graphic state dictionaries that can
vandebo@chromium.org6112c212011-05-13 03:50:38 +000029 be installed. So that a given dictionary is only output to the pdf file
30 once, we want to canonicalize them. Static methods in this class manage
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000031 a weakly referenced set of SkPDFGraphicState objects: when the last
32 reference to a SkPDFGraphicState is removed, it removes itself from the
33 static set of objects.
34
35*/
36class SkPDFGraphicState : public SkPDFDict {
37public:
38 virtual ~SkPDFGraphicState();
39
vandebo@chromium.org6112c212011-05-13 03:50:38 +000040 virtual void getResources(SkTDArray<SkPDFObject*>* resourceList);
41
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000042 // Override emitObject and getOutputSize so that we can populate
43 // the dictionary on demand.
44 virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
45 bool indirect);
46 virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
47
48 /** Get the graphic state for the passed SkPaint. The reference count of
49 * the object is incremented and it is the caller's responsibility to
vandebo@chromium.org6112c212011-05-13 03:50:38 +000050 * unreference it when done. This is needed to accommodate the weak
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000051 * reference pattern used when the returned object is new and has no
52 * other references.
53 * @param paint The SkPaint to emulate.
54 */
55 static SkPDFGraphicState* getGraphicStateForPaint(const SkPaint& paint);
56
vandebo@chromium.org6112c212011-05-13 03:50:38 +000057 /** Make a graphic state that only sets the passed soft mask. The
58 * reference count of the object is incremented and it is the caller's
59 * responsibility to unreference it when done.
60 * @param sMask The form xobject to use as a soft mask.
61 * @param invert Indicates if the alpha of the sMask should be inverted.
62 */
63 static SkPDFGraphicState* getSMaskGraphicState(SkPDFFormXObject* sMask,
64 bool invert);
65
66 /** Get a graphic state that only unsets the soft mask. The reference
67 * count of the object is incremented and it is the caller's responsibility
68 * to unreference it when done. This is needed to accommodate the weak
69 * reference pattern used when the returned object is new and has no
70 * other references.
71 */
72 static SkPDFGraphicState* getNoSMaskGraphicState();
73
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000074private:
75 const SkPaint fPaint;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000076 SkTDArray<SkPDFObject*> fResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000077 bool fPopulated;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000078 bool fSMask;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000079
80 class GSCanonicalEntry {
81 public:
82 SkPDFGraphicState* fGraphicState;
83 const SkPaint* fPaint;
84
85 bool operator==(const GSCanonicalEntry& b) const;
86 explicit GSCanonicalEntry(SkPDFGraphicState* gs)
87 : fGraphicState(gs),
88 fPaint(&gs->fPaint) {}
89 explicit GSCanonicalEntry(const SkPaint* paint) : fPaint(paint) {}
90 };
91
92 // This should be made a hash table if performance is a problem.
93 static SkTDArray<GSCanonicalEntry>& canonicalPaints();
94 static SkMutex& canonicalPaintsMutex();
95
vandebo@chromium.org6112c212011-05-13 03:50:38 +000096 SkPDFGraphicState();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000097 explicit SkPDFGraphicState(const SkPaint& paint);
98
99 void populateDict();
100
101 static int find(const SkPaint& paint);
102};
103
104#endif