blob: db5beb8fc86cada9fe6c46241f72eaaf31d9ee77 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +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.orgda912d62011-03-08 18:31:02 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000010#include "SkPDFShader.h"
11
12#include "SkCanvas.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000013#include "SkData.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000014#include "SkPDFCatalog.h"
15#include "SkPDFDevice.h"
16#include "SkPDFTypes.h"
17#include "SkPDFUtils.h"
18#include "SkScalar.h"
19#include "SkStream.h"
twiz@google.com316338a2011-03-09 23:14:04 +000020#include "SkTemplates.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000021#include "SkThread.h"
22#include "SkTypes.h"
23
vandebo@chromium.org386dfc02012-04-17 22:31:52 +000024static bool transformBBox(const SkMatrix& matrix, SkRect* bbox) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000025 SkMatrix inverse;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +000026 if (!matrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +000027 return false;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +000028 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000029 inverse.mapRect(bbox);
vandebo@chromium.org386dfc02012-04-17 22:31:52 +000030 return true;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000031}
32
33static void unitToPointsMatrix(const SkPoint pts[2], SkMatrix* matrix) {
34 SkVector vec = pts[1] - pts[0];
35 SkScalar mag = vec.length();
36 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
37
38 vec.scale(inv);
39 matrix->setSinCos(vec.fY, vec.fX);
40 matrix->preTranslate(pts[0].fX, pts[0].fY);
41 matrix->preScale(mag, mag);
42}
43
44/* Assumes t + startOffset is on the stack and does a linear interpolation on t
45 between startOffset and endOffset from prevColor to curColor (for each color
46 component), leaving the result in component order on the stack.
47 @param range endOffset - startOffset
48 @param curColor[components] The current color components.
49 @param prevColor[components] The previous color components.
50 @param result The result ps function.
51 */
52static void interpolateColorCode(SkScalar range, SkScalar* curColor,
53 SkScalar* prevColor, int components,
54 SkString* result) {
55 // Figure out how to scale each color component.
twiz@google.com316338a2011-03-09 23:14:04 +000056 SkAutoSTMalloc<4, SkScalar> multiplierAlloc(components);
57 SkScalar *multiplier = multiplierAlloc.get();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000058 for (int i = 0; i < components; i++) {
59 multiplier[i] = SkScalarDiv(curColor[i] - prevColor[i], range);
60 }
61
62 // Calculate when we no longer need to keep a copy of the input parameter t.
63 // If the last component to use t is i, then dupInput[0..i - 1] = true
64 // and dupInput[i .. components] = false.
twiz@google.com316338a2011-03-09 23:14:04 +000065 SkAutoSTMalloc<4, bool> dupInputAlloc(components);
66 bool *dupInput = dupInputAlloc.get();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000067 dupInput[components - 1] = false;
68 for (int i = components - 2; i >= 0; i--) {
69 dupInput[i] = dupInput[i + 1] || multiplier[i + 1] != 0;
70 }
71
72 if (!dupInput[0] && multiplier[0] == 0) {
73 result->append("pop ");
74 }
75
76 for (int i = 0; i < components; i++) {
vandebo@chromium.orgb8622042012-10-22 20:12:40 +000077 // If the next components needs t and this component will consume a
78 // copy, make another copy.
79 if (dupInput[i] && multiplier[i] != 0) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000080 result->append("dup ");
81 }
82
83 if (multiplier[i] == 0) {
84 result->appendScalar(prevColor[i]);
85 result->append(" ");
86 } else {
87 if (multiplier[i] != 1) {
88 result->appendScalar(multiplier[i]);
89 result->append(" mul ");
90 }
91 if (prevColor[i] != 0) {
92 result->appendScalar(prevColor[i]);
93 result->append(" add ");
94 }
95 }
96
97 if (dupInput[i]) {
98 result->append("exch\n");
99 }
100 }
101}
102
103/* Generate Type 4 function code to map t=[0,1) to the passed gradient,
104 clamping at the edges of the range. The generated code will be of the form:
105 if (t < 0) {
106 return colorData[0][r,g,b];
107 } else {
108 if (t < info.fColorOffsets[1]) {
109 return linearinterpolation(colorData[0][r,g,b],
110 colorData[1][r,g,b]);
111 } else {
112 if (t < info.fColorOffsets[2]) {
113 return linearinterpolation(colorData[1][r,g,b],
114 colorData[2][r,g,b]);
115 } else {
116
117 ... } else {
118 return colorData[info.fColorCount - 1][r,g,b];
119 }
120 ...
121 }
122 }
123 */
124static void gradientFunctionCode(const SkShader::GradientInfo& info,
125 SkString* result) {
126 /* We want to linearly interpolate from the previous color to the next.
127 Scale the colors from 0..255 to 0..1 and determine the multipliers
128 for interpolation.
129 C{r,g,b}(t, section) = t - offset_(section-1) + t * Multiplier{r,g,b}.
130 */
131 static const int kColorComponents = 3;
twiz@google.com316338a2011-03-09 23:14:04 +0000132 typedef SkScalar ColorTuple[kColorComponents];
133 SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount);
134 ColorTuple *colorData = colorDataAlloc.get();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000135 const SkScalar scale = SkScalarInvert(SkIntToScalar(255));
136 for (int i = 0; i < info.fColorCount; i++) {
137 colorData[i][0] = SkScalarMul(SkColorGetR(info.fColors[i]), scale);
138 colorData[i][1] = SkScalarMul(SkColorGetG(info.fColors[i]), scale);
139 colorData[i][2] = SkScalarMul(SkColorGetB(info.fColors[i]), scale);
140 }
141
142 // Clamp the initial color.
143 result->append("dup 0 le {pop ");
144 result->appendScalar(colorData[0][0]);
145 result->append(" ");
146 result->appendScalar(colorData[0][1]);
147 result->append(" ");
148 result->appendScalar(colorData[0][2]);
149 result->append(" }\n");
150
151 // The gradient colors.
152 for (int i = 1 ; i < info.fColorCount; i++) {
153 result->append("{dup ");
154 result->appendScalar(info.fColorOffsets[i]);
155 result->append(" le {");
156 if (info.fColorOffsets[i - 1] != 0) {
157 result->appendScalar(info.fColorOffsets[i - 1]);
158 result->append(" sub\n");
159 }
160
161 interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1],
162 colorData[i], colorData[i - 1], kColorComponents,
163 result);
164 result->append("}\n");
165 }
166
167 // Clamp the final color.
168 result->append("{pop ");
169 result->appendScalar(colorData[info.fColorCount - 1][0]);
170 result->append(" ");
171 result->appendScalar(colorData[info.fColorCount - 1][1]);
172 result->append(" ");
173 result->appendScalar(colorData[info.fColorCount - 1][2]);
174
175 for (int i = 0 ; i < info.fColorCount; i++) {
176 result->append("} ifelse\n");
177 }
178}
179
180/* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */
181static void tileModeCode(SkShader::TileMode mode, SkString* result) {
182 if (mode == SkShader::kRepeat_TileMode) {
183 result->append("dup truncate sub\n"); // Get the fractional part.
184 result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1)
185 return;
186 }
187
188 if (mode == SkShader::kMirror_TileMode) {
189 // Map t mod 2 into [0, 1, 1, 0].
190 // Code Stack
191 result->append("abs " // Map negative to positive.
192 "dup " // t.s t.s
193 "truncate " // t.s t
194 "dup " // t.s t t
195 "cvi " // t.s t T
196 "2 mod " // t.s t (i mod 2)
197 "1 eq " // t.s t true|false
198 "3 1 roll " // true|false t.s t
199 "sub " // true|false 0.s
200 "exch " // 0.s true|false
201 "{1 exch sub} if\n"); // 1 - 0.s|0.s
202 }
203}
204
205static SkString linearCode(const SkShader::GradientInfo& info) {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000206 SkString function("{pop\n"); // Just ditch the y value.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000207 tileModeCode(info.fTileMode, &function);
208 gradientFunctionCode(info, &function);
209 function.append("}");
210 return function;
211}
212
213static SkString radialCode(const SkShader::GradientInfo& info) {
214 SkString function("{");
215 // Find the distance from the origin.
216 function.append("dup " // x y y
217 "mul " // x y^2
218 "exch " // y^2 x
219 "dup " // y^2 x x
220 "mul " // y^2 x^2
221 "add " // y^2+x^2
222 "sqrt\n"); // sqrt(y^2+x^2)
223
224 tileModeCode(info.fTileMode, &function);
225 gradientFunctionCode(info, &function);
226 function.append("}");
227 return function;
228}
229
230/* The math here is all based on the description in Two_Point_Radial_Gradient,
231 with one simplification, the coordinate space has been scaled so that
232 Dr = 1. This means we don't need to scale the entire equation by 1/Dr^2.
233 */
234static SkString twoPointRadialCode(const SkShader::GradientInfo& info) {
235 SkScalar dx = info.fPoint[0].fX - info.fPoint[1].fX;
236 SkScalar dy = info.fPoint[0].fY - info.fPoint[1].fY;
237 SkScalar sr = info.fRadius[0];
238 SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - SK_Scalar1;
239 bool posRoot = info.fRadius[1] > info.fRadius[0];
240
241 // We start with a stack of (x y), copy it and then consume one copy in
242 // order to calculate b and the other to calculate c.
243 SkString function("{");
244 function.append("2 copy ");
245
246 // Calculate -b and b^2.
247 function.appendScalar(dy);
248 function.append(" mul exch ");
249 function.appendScalar(dx);
250 function.append(" mul add ");
251 function.appendScalar(sr);
252 function.append(" sub 2 mul neg dup dup mul\n");
253
254 // Calculate c
255 function.append("4 2 roll dup mul exch dup mul add ");
256 function.appendScalar(SkScalarMul(sr, sr));
257 function.append(" sub\n");
258
259 // Calculate the determinate
260 function.appendScalar(SkScalarMul(SkIntToScalar(4), a));
261 function.append(" mul sub abs sqrt\n");
262
263 // And then the final value of t.
264 if (posRoot) {
265 function.append("sub ");
266 } else {
267 function.append("add ");
268 }
269 function.appendScalar(SkScalarMul(SkIntToScalar(2), a));
270 function.append(" div\n");
271
272 tileModeCode(info.fTileMode, &function);
273 gradientFunctionCode(info, &function);
274 function.append("}");
275 return function;
276}
277
rileya@google.com62197282012-07-10 20:05:23 +0000278/* Conical gradient shader, based on the Canvas spec for radial gradients
rmistry@google.comd6176b02012-08-23 18:14:13 +0000279 See: http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient
rileya@google.com62197282012-07-10 20:05:23 +0000280 */
281static SkString twoPointConicalCode(const SkShader::GradientInfo& info) {
282 SkScalar dx = info.fPoint[1].fX - info.fPoint[0].fX;
283 SkScalar dy = info.fPoint[1].fY - info.fPoint[0].fY;
284 SkScalar r0 = info.fRadius[0];
285 SkScalar dr = info.fRadius[1] - info.fRadius[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000286 SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) -
rileya@google.com62197282012-07-10 20:05:23 +0000287 SkScalarMul(dr, dr);
288
289 // First compute t, if the pixel falls outside the cone, then we'll end
290 // with 'false' on the stack, otherwise we'll push 'true' with t below it
291
292 // We start with a stack of (x y), copy it and then consume one copy in
293 // order to calculate b and the other to calculate c.
294 SkString function("{");
295 function.append("2 copy ");
296
297 // Calculate b and b^2; b = -2 * (y * dy + x * dx + r0 * dr).
298 function.appendScalar(dy);
299 function.append(" mul exch ");
300 function.appendScalar(dx);
301 function.append(" mul add ");
302 function.appendScalar(SkScalarMul(r0, dr));
303 function.append(" add -2 mul dup dup mul\n");
304
305 // c = x^2 + y^2 + radius0^2
306 function.append("4 2 roll dup mul exch dup mul add ");
307 function.appendScalar(SkScalarMul(r0, r0));
308 function.append(" sub dup 4 1 roll\n");
309
310 // Contents of the stack at this point: c, b, b^2, c
311
312 // if a = 0, then we collapse to a simpler linear case
313 if (a == 0) {
314
315 // t = -c/b
316 function.append("pop pop div neg dup ");
317
318 // compute radius(t)
319 function.appendScalar(dr);
320 function.append(" mul ");
321 function.appendScalar(r0);
322 function.append(" add\n");
323
324 // if r(t) < 0, then it's outside the cone
rileya@google.comab2fe822012-07-10 20:41:38 +0000325 function.append("0 lt {pop false} {true} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000326
327 } else {
328
329 // quadratic case: the Canvas spec wants the largest
330 // root t for which radius(t) > 0
rmistry@google.comd6176b02012-08-23 18:14:13 +0000331
rileya@google.com62197282012-07-10 20:05:23 +0000332 // compute the discriminant (b^2 - 4ac)
333 function.appendScalar(SkScalarMul(SkIntToScalar(4), a));
334 function.append(" mul sub dup\n");
335
336 // if d >= 0, proceed
337 function.append("0 ge {\n");
338
339 // an intermediate value we'll use to compute the roots:
340 // q = -0.5 * (b +/- sqrt(d))
341 function.append("sqrt exch dup 0 lt {exch -1 mul} if");
342 function.append(" add -0.5 mul dup\n");
343
344 // first root = q / a
345 function.appendScalar(a);
346 function.append(" div\n");
347
348 // second root = c / q
349 function.append("3 1 roll div\n");
350
351 // put the larger root on top of the stack
352 function.append("2 copy gt {exch} if\n");
353
354 // compute radius(t) for larger root
355 function.append("dup ");
356 function.appendScalar(dr);
357 function.append(" mul ");
358 function.appendScalar(r0);
359 function.append(" add\n");
360
361 // if r(t) > 0, we have our t, pop off the smaller root and we're done
362 function.append(" 0 gt {exch pop true}\n");
363
364 // otherwise, throw out the larger one and try the smaller root
365 function.append("{pop dup\n");
366 function.appendScalar(dr);
367 function.append(" mul ");
368 function.appendScalar(r0);
369 function.append(" add\n");
370
371 // if r(t) < 0, push false, otherwise the smaller root is our t
rileya@google.comab2fe822012-07-10 20:41:38 +0000372 function.append("0 le {pop false} {true} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000373 function.append("} ifelse\n");
374
375 // d < 0, clear the stack and push false
rileya@google.comab2fe822012-07-10 20:41:38 +0000376 function.append("} {pop pop pop false} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000377 }
378
379 // if the pixel is in the cone, proceed to compute a color
380 function.append("{");
381 tileModeCode(info.fTileMode, &function);
382 gradientFunctionCode(info, &function);
383
384 // otherwise, just write black
385 function.append("} {0 0 0} ifelse }");
386
387 return function;
388}
389
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000390static SkString sweepCode(const SkShader::GradientInfo& info) {
391 SkString function("{exch atan 360 div\n");
392 tileModeCode(info.fTileMode, &function);
393 gradientFunctionCode(info, &function);
394 function.append("}");
395 return function;
396}
397
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000398class SkPDFShader::State {
399public:
400 SkShader::GradientType fType;
401 SkShader::GradientInfo fInfo;
402 SkAutoFree fColorData;
403 SkMatrix fCanvasTransform;
404 SkMatrix fShaderTransform;
405 SkIRect fBBox;
406
407 SkBitmap fImage;
408 uint32_t fPixelGeneration;
409 SkShader::TileMode fImageTileModes[2];
410
411 explicit State(const SkShader& shader, const SkMatrix& canvasTransform,
412 const SkIRect& bbox);
413 bool operator==(const State& b) const;
414};
415
416class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader {
417public:
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000418 explicit SkPDFFunctionShader(SkPDFShader::State* state);
robertphillips@google.com05141c22012-04-27 12:54:01 +0000419 virtual ~SkPDFFunctionShader() {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000420 if (isValid()) {
421 RemoveShader(this);
422 }
423 fResources.unrefAll();
424 }
425
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000426 virtual bool isValid() { return fResources.count() > 0; }
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000427
428 void getResources(SkTDArray<SkPDFObject*>* resourceList) {
429 GetResourcesHelper(&fResources, resourceList);
430 }
431
432private:
433 static SkPDFObject* RangeObject();
434
435 SkTDArray<SkPDFObject*> fResources;
436 SkAutoTDelete<const SkPDFShader::State> fState;
437
438 SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain);
439};
440
441class SkPDFImageShader : public SkPDFStream, public SkPDFShader {
442public:
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000443 explicit SkPDFImageShader(SkPDFShader::State* state);
robertphillips@google.com05141c22012-04-27 12:54:01 +0000444 virtual ~SkPDFImageShader() {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000445 RemoveShader(this);
446 fResources.unrefAll();
447 }
448
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000449 virtual bool isValid() { return size() > 0; }
450
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000451 void getResources(SkTDArray<SkPDFObject*>* resourceList) {
452 GetResourcesHelper(&fResources, resourceList);
453 }
454
455private:
456 SkTDArray<SkPDFObject*> fResources;
457 SkAutoTDelete<const SkPDFShader::State> fState;
458};
459
460SkPDFShader::SkPDFShader() {}
461
462// static
463void SkPDFShader::RemoveShader(SkPDFObject* shader) {
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000464 SkAutoMutexAcquire lock(CanonicalShadersMutex());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000465 ShaderCanonicalEntry entry(shader, NULL);
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000466 int index = CanonicalShaders().find(entry);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000467 SkASSERT(index >= 0);
468 CanonicalShaders().removeShuffle(index);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000469}
470
471// static
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000472SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader,
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000473 const SkMatrix& matrix,
474 const SkIRect& surfaceBBox) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000475 SkPDFObject* result;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000476 SkAutoMutexAcquire lock(CanonicalShadersMutex());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000477 SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox));
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +0000478 if (shaderState.get()->fType == SkShader::kNone_GradientType &&
479 shaderState.get()->fImage.isNull()) {
480 // TODO(vandebo) This drops SKComposeShader on the floor. We could
481 // handle compose shader by pulling things up to a layer, drawing with
482 // the first shader, applying the xfer mode and drawing again with the
483 // second shader, then applying the layer to the original drawing.
484 return NULL;
485 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000486
487 ShaderCanonicalEntry entry(NULL, shaderState.get());
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000488 int index = CanonicalShaders().find(entry);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000489 if (index >= 0) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000490 result = CanonicalShaders()[index].fPDFShader;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000491 result->ref();
492 return result;
493 }
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000494
495 bool valid = false;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000496 // The PDFShader takes ownership of the shaderSate.
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000497 if (shaderState.get()->fType == SkShader::kNone_GradientType) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000498 SkPDFImageShader* imageShader =
499 new SkPDFImageShader(shaderState.detach());
500 valid = imageShader->isValid();
501 result = imageShader;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000502 } else {
503 SkPDFFunctionShader* functionShader =
504 new SkPDFFunctionShader(shaderState.detach());
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000505 valid = functionShader->isValid();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000506 result = functionShader;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000507 }
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000508 if (!valid) {
509 delete result;
510 return NULL;
511 }
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000512 entry.fPDFShader = result;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000513 CanonicalShaders().push(entry);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000514 return result; // return the reference that came from new.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000515}
516
517// static
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000518SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000519 // This initialization is only thread safe with gcc.
520 static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders;
521 return gCanonicalShaders;
522}
523
524// static
digit@google.com1771cbf2012-01-26 21:26:40 +0000525SkBaseMutex& SkPDFShader::CanonicalShadersMutex() {
526 // This initialization is only thread safe with gcc or when
527 // POD-style mutex initialization is used.
528 SK_DECLARE_STATIC_MUTEX(gCanonicalShadersMutex);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000529 return gCanonicalShadersMutex;
530}
531
532// static
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000533SkPDFObject* SkPDFFunctionShader::RangeObject() {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000534 // This initialization is only thread safe with gcc.
535 static SkPDFArray* range = NULL;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000536 // This method is only used with CanonicalShadersMutex, so it's safe to
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000537 // populate domain.
538 if (range == NULL) {
539 range = new SkPDFArray;
540 range->reserve(6);
reed@google.comc789cf12011-07-20 12:14:33 +0000541 range->appendInt(0);
542 range->appendInt(1);
543 range->appendInt(0);
544 range->appendInt(1);
545 range->appendInt(0);
546 range->appendInt(1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000547 }
548 return range;
549}
550
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000551SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state)
552 : SkPDFDict("Pattern"),
553 fState(state) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000554 SkString (*codeFunction)(const SkShader::GradientInfo& info) = NULL;
555 SkPoint transformPoints[2];
556
557 // Depending on the type of the gradient, we want to transform the
558 // coordinate space in different ways.
559 const SkShader::GradientInfo* info = &fState.get()->fInfo;
560 transformPoints[0] = info->fPoint[0];
561 transformPoints[1] = info->fPoint[1];
562 switch (fState.get()->fType) {
563 case SkShader::kLinear_GradientType:
564 codeFunction = &linearCode;
565 break;
566 case SkShader::kRadial_GradientType:
567 transformPoints[1] = transformPoints[0];
568 transformPoints[1].fX += info->fRadius[0];
569 codeFunction = &radialCode;
570 break;
571 case SkShader::kRadial2_GradientType: {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000572 // Bail out if the radii are the same. Empty fResources signals
573 // an error and isValid will return false.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000574 if (info->fRadius[0] == info->fRadius[1]) {
575 return;
576 }
577 transformPoints[1] = transformPoints[0];
578 SkScalar dr = info->fRadius[1] - info->fRadius[0];
579 transformPoints[1].fX += dr;
580 codeFunction = &twoPointRadialCode;
581 break;
582 }
rileya@google.com62197282012-07-10 20:05:23 +0000583 case SkShader::kConical_GradientType: {
584 transformPoints[1] = transformPoints[0];
585 transformPoints[1].fX += SK_Scalar1;
586 codeFunction = &twoPointConicalCode;
587 break;
588 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000589 case SkShader::kSweep_GradientType:
590 transformPoints[1] = transformPoints[0];
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +0000591 transformPoints[1].fX += SK_Scalar1;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000592 codeFunction = &sweepCode;
593 break;
594 case SkShader::kColor_GradientType:
595 case SkShader::kNone_GradientType:
vandebo@chromium.org020798a2011-12-21 01:49:35 +0000596 default:
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000597 return;
598 }
599
600 // Move any scaling (assuming a unit gradient) or translation
601 // (and rotation for linear gradient), of the final gradient from
602 // info->fPoints to the matrix (updating bbox appropriately). Now
603 // the gradient can be drawn on on the unit segment.
604 SkMatrix mapperMatrix;
605 unitToPointsMatrix(transformPoints, &mapperMatrix);
606 SkMatrix finalMatrix = fState.get()->fCanvasTransform;
607 finalMatrix.preConcat(mapperMatrix);
608 finalMatrix.preConcat(fState.get()->fShaderTransform);
609 SkRect bbox;
610 bbox.set(fState.get()->fBBox);
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000611 if (!transformBBox(finalMatrix, &bbox)) {
612 return;
613 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000614
615 SkRefPtr<SkPDFArray> domain = new SkPDFArray;
616 domain->unref(); // SkRefPtr and new both took a reference.
617 domain->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000618 domain->appendScalar(bbox.fLeft);
619 domain->appendScalar(bbox.fRight);
620 domain->appendScalar(bbox.fTop);
621 domain->appendScalar(bbox.fBottom);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000622
623 SkString functionCode;
624 // The two point radial gradient further references fState.get()->fInfo
625 // in translating from x, y coordinates to the t parameter. So, we have
626 // to transform the points and radii according to the calculated matrix.
627 if (fState.get()->fType == SkShader::kRadial2_GradientType) {
628 SkShader::GradientInfo twoPointRadialInfo = *info;
629 SkMatrix inverseMapperMatrix;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000630 if (!mapperMatrix.invert(&inverseMapperMatrix)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000631 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000632 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000633 inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2);
634 twoPointRadialInfo.fRadius[0] =
635 inverseMapperMatrix.mapRadius(info->fRadius[0]);
636 twoPointRadialInfo.fRadius[1] =
637 inverseMapperMatrix.mapRadius(info->fRadius[1]);
638 functionCode = codeFunction(twoPointRadialInfo);
639 } else {
640 functionCode = codeFunction(*info);
641 }
642
643 SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get());
644 // Pass one reference to fResources, SkRefPtr and new both took a reference.
645 fResources.push(function.get());
646
647 SkRefPtr<SkPDFDict> pdfShader = new SkPDFDict;
648 pdfShader->unref(); // SkRefPtr and new both took a reference.
reed@google.comc789cf12011-07-20 12:14:33 +0000649 pdfShader->insertInt("ShadingType", 1);
650 pdfShader->insertName("ColorSpace", "DeviceRGB");
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000651 pdfShader->insert("Domain", domain.get());
652 pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref();
653
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000654 insertInt("PatternType", 2);
655 insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref();
656 insert("Shading", pdfShader.get());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000657}
658
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000659SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000660 fState.get()->fImage.lockPixels();
661
662 SkMatrix finalMatrix = fState.get()->fCanvasTransform;
663 finalMatrix.preConcat(fState.get()->fShaderTransform);
664 SkRect surfaceBBox;
665 surfaceBBox.set(fState.get()->fBBox);
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000666 if (!transformBBox(finalMatrix, &surfaceBBox)) {
667 return;
668 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000669
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000670 SkMatrix unflip;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000671 unflip.setTranslate(0, SkScalarRoundToScalar(surfaceBBox.height()));
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000672 unflip.preScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000673 SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()),
674 SkScalarRound(surfaceBBox.height()));
ctguil@chromium.org15261292011-04-29 17:54:16 +0000675 SkPDFDevice pattern(size, size, unflip);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000676 SkCanvas canvas(&pattern);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000677 canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop);
678 finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000679
680 const SkBitmap* image = &fState.get()->fImage;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000681 SkScalar width = SkIntToScalar(image->width());
682 SkScalar height = SkIntToScalar(image->height());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000683 SkShader::TileMode tileModes[2];
684 tileModes[0] = fState.get()->fImageTileModes[0];
685 tileModes[1] = fState.get()->fImageTileModes[1];
686
687 canvas.drawBitmap(*image, 0, 0);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000688 SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop,
689 width, height);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000690
691 // Tiling is implied. First we handle mirroring.
692 if (tileModes[0] == SkShader::kMirror_TileMode) {
693 SkMatrix xMirror;
694 xMirror.setScale(-1, 1);
695 xMirror.postTranslate(2 * width, 0);
696 canvas.drawBitmapMatrix(*image, xMirror);
697 patternBBox.fRight += width;
698 }
699 if (tileModes[1] == SkShader::kMirror_TileMode) {
700 SkMatrix yMirror;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000701 yMirror.setScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000702 yMirror.postTranslate(0, 2 * height);
703 canvas.drawBitmapMatrix(*image, yMirror);
704 patternBBox.fBottom += height;
705 }
706 if (tileModes[0] == SkShader::kMirror_TileMode &&
707 tileModes[1] == SkShader::kMirror_TileMode) {
708 SkMatrix mirror;
709 mirror.setScale(-1, -1);
710 mirror.postTranslate(2 * width, 2 * height);
711 canvas.drawBitmapMatrix(*image, mirror);
712 }
713
714 // Then handle Clamping, which requires expanding the pattern canvas to
715 // cover the entire surfaceBBox.
716
717 // If both x and y are in clamp mode, we start by filling in the corners.
718 // (Which are just a rectangles of the corner colors.)
719 if (tileModes[0] == SkShader::kClamp_TileMode &&
720 tileModes[1] == SkShader::kClamp_TileMode) {
721 SkPaint paint;
722 SkRect rect;
723 rect = SkRect::MakeLTRB(surfaceBBox.fLeft, surfaceBBox.fTop, 0, 0);
724 if (!rect.isEmpty()) {
725 paint.setColor(image->getColor(0, 0));
726 canvas.drawRect(rect, paint);
727 }
728
729 rect = SkRect::MakeLTRB(width, surfaceBBox.fTop, surfaceBBox.fRight, 0);
730 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000731 paint.setColor(image->getColor(image->width() - 1, 0));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000732 canvas.drawRect(rect, paint);
733 }
734
735 rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight,
736 surfaceBBox.fBottom);
737 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000738 paint.setColor(image->getColor(image->width() - 1,
739 image->height() - 1));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000740 canvas.drawRect(rect, paint);
741 }
742
743 rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0,
744 surfaceBBox.fBottom);
745 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000746 paint.setColor(image->getColor(0, image->height() - 1));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000747 canvas.drawRect(rect, paint);
748 }
749 }
750
751 // Then expand the left, right, top, then bottom.
752 if (tileModes[0] == SkShader::kClamp_TileMode) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000753 SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, image->height());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000754 if (surfaceBBox.fLeft < 0) {
755 SkBitmap left;
756 SkAssertResult(image->extractSubset(&left, subset));
757
758 SkMatrix leftMatrix;
759 leftMatrix.setScale(-surfaceBBox.fLeft, 1);
760 leftMatrix.postTranslate(surfaceBBox.fLeft, 0);
761 canvas.drawBitmapMatrix(left, leftMatrix);
762
763 if (tileModes[1] == SkShader::kMirror_TileMode) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000764 leftMatrix.postScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000765 leftMatrix.postTranslate(0, 2 * height);
766 canvas.drawBitmapMatrix(left, leftMatrix);
767 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000768 patternBBox.fLeft = 0;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000769 }
770
771 if (surfaceBBox.fRight > width) {
772 SkBitmap right;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000773 subset.offset(image->width() - 1, 0);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000774 SkAssertResult(image->extractSubset(&right, subset));
775
776 SkMatrix rightMatrix;
777 rightMatrix.setScale(surfaceBBox.fRight - width, 1);
778 rightMatrix.postTranslate(width, 0);
779 canvas.drawBitmapMatrix(right, rightMatrix);
780
781 if (tileModes[1] == SkShader::kMirror_TileMode) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000782 rightMatrix.postScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000783 rightMatrix.postTranslate(0, 2 * height);
784 canvas.drawBitmapMatrix(right, rightMatrix);
785 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000786 patternBBox.fRight = surfaceBBox.width();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000787 }
788 }
789
790 if (tileModes[1] == SkShader::kClamp_TileMode) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000791 SkIRect subset = SkIRect::MakeXYWH(0, 0, image->width(), 1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000792 if (surfaceBBox.fTop < 0) {
793 SkBitmap top;
794 SkAssertResult(image->extractSubset(&top, subset));
795
796 SkMatrix topMatrix;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000797 topMatrix.setScale(SK_Scalar1, -surfaceBBox.fTop);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000798 topMatrix.postTranslate(0, surfaceBBox.fTop);
799 canvas.drawBitmapMatrix(top, topMatrix);
800
801 if (tileModes[0] == SkShader::kMirror_TileMode) {
802 topMatrix.postScale(-1, 1);
803 topMatrix.postTranslate(2 * width, 0);
804 canvas.drawBitmapMatrix(top, topMatrix);
805 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000806 patternBBox.fTop = 0;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000807 }
808
809 if (surfaceBBox.fBottom > height) {
810 SkBitmap bottom;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000811 subset.offset(0, image->height() - 1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000812 SkAssertResult(image->extractSubset(&bottom, subset));
813
814 SkMatrix bottomMatrix;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000815 bottomMatrix.setScale(SK_Scalar1, surfaceBBox.fBottom - height);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000816 bottomMatrix.postTranslate(0, height);
817 canvas.drawBitmapMatrix(bottom, bottomMatrix);
818
819 if (tileModes[0] == SkShader::kMirror_TileMode) {
820 bottomMatrix.postScale(-1, 1);
821 bottomMatrix.postTranslate(2 * width, 0);
822 canvas.drawBitmapMatrix(bottom, bottomMatrix);
823 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000824 patternBBox.fBottom = surfaceBBox.height();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000825 }
826 }
827
828 SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray;
829 patternBBoxArray->unref(); // SkRefPtr and new both took a reference.
830 patternBBoxArray->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000831 patternBBoxArray->appendScalar(patternBBox.fLeft);
832 patternBBoxArray->appendScalar(patternBBox.fTop);
833 patternBBoxArray->appendScalar(patternBBox.fRight);
834 patternBBoxArray->appendScalar(patternBBox.fBottom);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000835
836 // Put the canvas into the pattern stream (fContent).
837 SkRefPtr<SkStream> content = pattern.content();
838 content->unref(); // SkRefPtr and content() both took a reference.
vandebo@chromium.org188838c2012-03-09 22:16:58 +0000839 pattern.getResources(&fResources, false);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000840
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000841 setData(content.get());
842 insertName("Type", "Pattern");
843 insertInt("PatternType", 1);
844 insertInt("PaintType", 1);
845 insertInt("TilingType", 1);
846 insert("BBox", patternBBoxArray.get());
847 insertScalar("XStep", patternBBox.width());
848 insertScalar("YStep", patternBBox.height());
reed@google.com1feb3302011-07-20 18:43:19 +0000849 insert("Resources", pattern.getResourceDict());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000850 insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000851
852 fState.get()->fImage.unlockPixels();
853}
854
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000855SkPDFStream* SkPDFFunctionShader::makePSFunction(const SkString& psCode,
856 SkPDFArray* domain) {
857 SkAutoDataUnref funcData(SkData::NewWithCopy(psCode.c_str(),
858 psCode.size()));
859 SkPDFStream* result = new SkPDFStream(funcData.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000860 result->insertInt("FunctionType", 4);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000861 result->insert("Domain", domain);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000862 result->insert("Range", RangeObject());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000863 return result;
864}
865
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000866SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader,
867 const State* state)
868 : fPDFShader(pdfShader),
869 fState(state) {
870}
871
872bool SkPDFShader::ShaderCanonicalEntry::operator==(
873 const ShaderCanonicalEntry& b) const {
874 return fPDFShader == b.fPDFShader ||
875 (fState != NULL && b.fState != NULL && *fState == *b.fState);
876}
877
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000878bool SkPDFShader::State::operator==(const SkPDFShader::State& b) const {
879 if (fType != b.fType ||
880 fCanvasTransform != b.fCanvasTransform ||
881 fShaderTransform != b.fShaderTransform ||
882 fBBox != b.fBBox) {
883 return false;
884 }
885
886 if (fType == SkShader::kNone_GradientType) {
887 if (fPixelGeneration != b.fPixelGeneration ||
888 fPixelGeneration == 0 ||
889 fImageTileModes[0] != b.fImageTileModes[0] ||
890 fImageTileModes[1] != b.fImageTileModes[1]) {
891 return false;
892 }
893 } else {
894 if (fInfo.fColorCount != b.fInfo.fColorCount ||
895 memcmp(fInfo.fColors, b.fInfo.fColors,
896 sizeof(SkColor) * fInfo.fColorCount) != 0 ||
897 memcmp(fInfo.fColorOffsets, b.fInfo.fColorOffsets,
898 sizeof(SkScalar) * fInfo.fColorCount) != 0 ||
899 fInfo.fPoint[0] != b.fInfo.fPoint[0] ||
900 fInfo.fTileMode != b.fInfo.fTileMode) {
901 return false;
902 }
903
904 switch (fType) {
905 case SkShader::kLinear_GradientType:
906 if (fInfo.fPoint[1] != b.fInfo.fPoint[1]) {
907 return false;
908 }
909 break;
910 case SkShader::kRadial_GradientType:
911 if (fInfo.fRadius[0] != b.fInfo.fRadius[0]) {
912 return false;
913 }
914 break;
915 case SkShader::kRadial2_GradientType:
reed@google.com49085332012-06-11 18:54:07 +0000916 case SkShader::kConical_GradientType:
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000917 if (fInfo.fPoint[1] != b.fInfo.fPoint[1] ||
918 fInfo.fRadius[0] != b.fInfo.fRadius[0] ||
919 fInfo.fRadius[1] != b.fInfo.fRadius[1]) {
920 return false;
921 }
922 break;
923 case SkShader::kSweep_GradientType:
924 case SkShader::kNone_GradientType:
925 case SkShader::kColor_GradientType:
926 break;
927 }
928 }
929 return true;
930}
931
932SkPDFShader::State::State(const SkShader& shader,
933 const SkMatrix& canvasTransform, const SkIRect& bbox)
934 : fCanvasTransform(canvasTransform),
vandebo@chromium.orge1bc2742011-06-21 22:26:39 +0000935 fBBox(bbox),
936 fPixelGeneration(0) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000937 fInfo.fColorCount = 0;
938 fInfo.fColors = NULL;
939 fInfo.fColorOffsets = NULL;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000940 fShaderTransform = shader.getLocalMatrix();
vandebo@chromium.orge1bc2742011-06-21 22:26:39 +0000941 fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000942
943 fType = shader.asAGradient(&fInfo);
944
945 if (fType == SkShader::kNone_GradientType) {
946 SkShader::BitmapType bitmapType;
947 SkMatrix matrix;
rileya@google.com91f319c2012-07-25 17:18:31 +0000948 bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000949 if (bitmapType != SkShader::kDefault_BitmapType) {
950 fImage.reset();
951 return;
952 }
953 SkASSERT(matrix.isIdentity());
954 fPixelGeneration = fImage.getGenerationID();
955 } else {
956 fColorData.set(sk_malloc_throw(
957 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar))));
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000958 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get());
vandebo@chromium.org020798a2011-12-21 01:49:35 +0000959 fInfo.fColorOffsets =
960 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000961 shader.asAGradient(&fInfo);
962 }
963}