OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkData.h" | 8 #include "SkData.h" |
| 9 #include "SkLazyPtr.h" |
9 #include "SkPDFCanon.h" | 10 #include "SkPDFCanon.h" |
10 #include "SkPDFFormXObject.h" | 11 #include "SkPDFFormXObject.h" |
11 #include "SkPDFGraphicState.h" | 12 #include "SkPDFGraphicState.h" |
12 #include "SkPDFUtils.h" | 13 #include "SkPDFUtils.h" |
13 #include "SkTypes.h" | 14 #include "SkTypes.h" |
14 | 15 |
15 static const char* as_blend_mode(SkXfermode::Mode mode) { | 16 static const char* as_blend_mode(SkXfermode::Mode mode) { |
16 switch (mode) { | 17 switch (mode) { |
17 case SkXfermode::kSrcOver_Mode: | 18 case SkXfermode::kSrcOver_Mode: |
18 return "Normal"; | 19 return "Normal"; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 SkPDFGraphicState* pdfGraphicState = | 138 SkPDFGraphicState* pdfGraphicState = |
138 SkPDFCanon::GetCanon().findGraphicState(paint); | 139 SkPDFCanon::GetCanon().findGraphicState(paint); |
139 if (pdfGraphicState) { | 140 if (pdfGraphicState) { |
140 return SkRef(pdfGraphicState); | 141 return SkRef(pdfGraphicState); |
141 } | 142 } |
142 pdfGraphicState = new SkPDFGraphicState(paint); | 143 pdfGraphicState = new SkPDFGraphicState(paint); |
143 SkPDFCanon::GetCanon().addGraphicState(pdfGraphicState); | 144 SkPDFCanon::GetCanon().addGraphicState(pdfGraphicState); |
144 return pdfGraphicState; | 145 return pdfGraphicState; |
145 } | 146 } |
146 | 147 |
147 // static | 148 namespace { |
148 SkPDFObject* SkPDFGraphicState::GetInvertFunction() { | 149 SkPDFObject* create_invert_function() { |
149 // This assumes that canonicalPaintsMutex is held. | 150 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use |
150 SkPDFCanon::GetPaintMutex().assertHeld(); | 151 // a type 2 function, so we use a type 4 function. |
151 static SkPDFStream* invertFunction = NULL; | 152 SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray); |
152 if (!invertFunction) { | 153 domainAndRange->reserve(2); |
153 // Acrobat crashes if we use a type 0 function, kpdf crashes if we use | 154 domainAndRange->appendInt(0); |
154 // a type 2 function, so we use a type 4 function. | 155 domainAndRange->appendInt(1); |
155 SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray); | |
156 domainAndRange->reserve(2); | |
157 domainAndRange->appendInt(0); | |
158 domainAndRange->appendInt(1); | |
159 | 156 |
160 static const char psInvert[] = "{1 exch sub}"; | 157 static const char psInvert[] = "{1 exch sub}"; |
161 // Do not copy the trailing '\0' into the SkData. | 158 // Do not copy the trailing '\0' into the SkData. |
162 SkAutoTUnref<SkData> psInvertStream( | 159 SkAutoTUnref<SkData> psInvertStream( |
163 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); | 160 SkData::NewWithoutCopy(psInvert, strlen(psInvert))); |
164 | 161 |
165 invertFunction = new SkPDFStream(psInvertStream.get()); | 162 SkPDFStream* invertFunction = SkNEW_ARGS( |
166 invertFunction->insertInt("FunctionType", 4); | 163 SkPDFStream, (psInvertStream.get())); |
167 invertFunction->insert("Domain", domainAndRange.get()); | 164 invertFunction->insertInt("FunctionType", 4); |
168 invertFunction->insert("Range", domainAndRange.get()); | 165 invertFunction->insert("Domain", domainAndRange.get()); |
169 } | 166 invertFunction->insert("Range", domainAndRange.get()); |
170 return invertFunction; | 167 return invertFunction; |
171 } | 168 } |
172 | 169 |
| 170 template <typename T> void unref(T* ptr) { ptr->unref(); } |
| 171 } // namespace |
| 172 |
| 173 SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject, invertFunction, |
| 174 create_invert_function, unref<SkPDFObject>); |
| 175 |
173 // static | 176 // static |
| 177 SkPDFObject* SkPDFGraphicState::GetInvertFunction() { |
| 178 return invertFunction.get(); |
| 179 } |
| 180 |
| 181 // static |
174 SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState( | 182 SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState( |
175 SkPDFFormXObject* sMask, bool invert, SkPDFSMaskMode sMaskMode) { | 183 SkPDFFormXObject* sMask, bool invert, SkPDFSMaskMode sMaskMode) { |
176 // The practical chances of using the same mask more than once are unlikely | 184 // The practical chances of using the same mask more than once are unlikely |
177 // enough that it's not worth canonicalizing. | 185 // enough that it's not worth canonicalizing. |
178 SkAutoMutexAcquire lock(SkPDFCanon::GetPaintMutex()); | |
179 SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask")); | 186 SkAutoTUnref<SkPDFDict> sMaskDict(new SkPDFDict("Mask")); |
180 if (sMaskMode == kAlpha_SMaskMode) { | 187 if (sMaskMode == kAlpha_SMaskMode) { |
181 sMaskDict->insertName("S", "Alpha"); | 188 sMaskDict->insertName("S", "Alpha"); |
182 } else if (sMaskMode == kLuminosity_SMaskMode) { | 189 } else if (sMaskMode == kLuminosity_SMaskMode) { |
183 sMaskDict->insertName("S", "Luminosity"); | 190 sMaskDict->insertName("S", "Luminosity"); |
184 } | 191 } |
185 sMaskDict->insert("G", new SkPDFObjRef(sMask))->unref(); | 192 sMaskDict->insert("G", new SkPDFObjRef(sMask))->unref(); |
186 | 193 |
187 SkPDFGraphicState* result = new SkPDFGraphicState; | 194 SkPDFGraphicState* result = new SkPDFGraphicState; |
188 result->fPopulated = true; | 195 result->fPopulated = true; |
189 result->fSMask = true; | 196 result->fSMask = true; |
190 result->insertName("Type", "ExtGState"); | 197 result->insertName("Type", "ExtGState"); |
191 result->insert("SMask", sMaskDict.get()); | 198 result->insert("SMask", sMaskDict.get()); |
192 result->fResources.push(sMask); | 199 result->fResources.push(sMask); |
193 sMask->ref(); | 200 sMask->ref(); |
194 | 201 |
195 if (invert) { | 202 if (invert) { |
196 SkPDFObject* invertFunction = GetInvertFunction(); | 203 SkPDFObject* invertFunction = GetInvertFunction(); |
197 result->fResources.push(invertFunction); | 204 result->fResources.push(invertFunction); |
198 invertFunction->ref(); | 205 invertFunction->ref(); |
199 sMaskDict->insert("TR", new SkPDFObjRef(invertFunction))->unref(); | 206 sMaskDict->insert("TR", new SkPDFObjRef(invertFunction))->unref(); |
200 } | 207 } |
201 | 208 |
202 return result; | 209 return result; |
203 } | 210 } |
204 | 211 |
205 // static | 212 SkPDFGraphicState* SkPDFGraphicState::CreateNoSMaskGraphicState() { |
206 SkPDFGraphicState* SkPDFGraphicState::GetNoSMaskGraphicState() { | 213 SkPDFGraphicState* noSMaskGS = SkNEW(SkPDFGraphicState); |
207 SkAutoMutexAcquire lock(SkPDFCanon::GetPaintMutex()); | 214 noSMaskGS->fPopulated = true; |
208 static SkPDFGraphicState* noSMaskGS = NULL; | 215 noSMaskGS->fSMask = true; |
209 if (!noSMaskGS) { | 216 noSMaskGS->insertName("Type", "ExtGState"); |
210 noSMaskGS = new SkPDFGraphicState; | 217 noSMaskGS->insertName("SMask", "None"); |
211 noSMaskGS->fPopulated = true; | |
212 noSMaskGS->fSMask = true; | |
213 noSMaskGS->insertName("Type", "ExtGState"); | |
214 noSMaskGS->insertName("SMask", "None"); | |
215 } | |
216 noSMaskGS->ref(); | |
217 return noSMaskGS; | 218 return noSMaskGS; |
218 } | 219 } |
219 | 220 |
| 221 SK_DECLARE_STATIC_LAZY_PTR( |
| 222 SkPDFGraphicState, noSMaskGraphicState, |
| 223 SkPDFGraphicState::CreateNoSMaskGraphicState, unref<SkPDFGraphicState>); |
| 224 |
| 225 // static |
| 226 SkPDFGraphicState* SkPDFGraphicState::GetNoSMaskGraphicState() { |
| 227 return SkRef(noSMaskGraphicState.get()); |
| 228 } |
| 229 |
220 SkPDFGraphicState::SkPDFGraphicState() | 230 SkPDFGraphicState::SkPDFGraphicState() |
221 : fPopulated(false), | 231 : fPopulated(false), |
222 fSMask(false) { | 232 fSMask(false) { |
223 } | 233 } |
224 | 234 |
225 SkPDFGraphicState::SkPDFGraphicState(const SkPaint& paint) | 235 SkPDFGraphicState::SkPDFGraphicState(const SkPaint& paint) |
226 : fPaint(paint), | 236 : fPaint(paint), |
227 fPopulated(false), | 237 fPopulated(false), |
228 fSMask(false) { | 238 fSMask(false) { |
229 } | 239 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 fPaint.getXfermode()->asMode(&xfermode); | 273 fPaint.getXfermode()->asMode(&xfermode); |
264 // If we don't support the mode, just use kSrcOver_Mode. | 274 // If we don't support the mode, just use kSrcOver_Mode. |
265 if (xfermode < 0 || xfermode > SkXfermode::kLastMode || | 275 if (xfermode < 0 || xfermode > SkXfermode::kLastMode || |
266 as_blend_mode(xfermode) == NULL) { | 276 as_blend_mode(xfermode) == NULL) { |
267 xfermode = SkXfermode::kSrcOver_Mode; | 277 xfermode = SkXfermode::kSrcOver_Mode; |
268 NOT_IMPLEMENTED("unsupported xfermode", false); | 278 NOT_IMPLEMENTED("unsupported xfermode", false); |
269 } | 279 } |
270 insertName("BM", as_blend_mode(xfermode)); | 280 insertName("BM", as_blend_mode(xfermode)); |
271 } | 281 } |
272 } | 282 } |
273 | |
OLD | NEW |