blob: e6f1d7f10802e3c1f78027c03f21721636dd9c1f [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++) {
77 // If the next components needs t, make a copy.
78 if (dupInput[i]) {
79 result->append("dup ");
80 }
81
82 if (multiplier[i] == 0) {
83 result->appendScalar(prevColor[i]);
84 result->append(" ");
85 } else {
86 if (multiplier[i] != 1) {
87 result->appendScalar(multiplier[i]);
88 result->append(" mul ");
89 }
90 if (prevColor[i] != 0) {
91 result->appendScalar(prevColor[i]);
92 result->append(" add ");
93 }
94 }
95
96 if (dupInput[i]) {
97 result->append("exch\n");
98 }
99 }
100}
101
102/* Generate Type 4 function code to map t=[0,1) to the passed gradient,
103 clamping at the edges of the range. The generated code will be of the form:
104 if (t < 0) {
105 return colorData[0][r,g,b];
106 } else {
107 if (t < info.fColorOffsets[1]) {
108 return linearinterpolation(colorData[0][r,g,b],
109 colorData[1][r,g,b]);
110 } else {
111 if (t < info.fColorOffsets[2]) {
112 return linearinterpolation(colorData[1][r,g,b],
113 colorData[2][r,g,b]);
114 } else {
115
116 ... } else {
117 return colorData[info.fColorCount - 1][r,g,b];
118 }
119 ...
120 }
121 }
122 */
123static void gradientFunctionCode(const SkShader::GradientInfo& info,
124 SkString* result) {
125 /* We want to linearly interpolate from the previous color to the next.
126 Scale the colors from 0..255 to 0..1 and determine the multipliers
127 for interpolation.
128 C{r,g,b}(t, section) = t - offset_(section-1) + t * Multiplier{r,g,b}.
129 */
130 static const int kColorComponents = 3;
twiz@google.com316338a2011-03-09 23:14:04 +0000131 typedef SkScalar ColorTuple[kColorComponents];
132 SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount);
133 ColorTuple *colorData = colorDataAlloc.get();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000134 const SkScalar scale = SkScalarInvert(SkIntToScalar(255));
135 for (int i = 0; i < info.fColorCount; i++) {
136 colorData[i][0] = SkScalarMul(SkColorGetR(info.fColors[i]), scale);
137 colorData[i][1] = SkScalarMul(SkColorGetG(info.fColors[i]), scale);
138 colorData[i][2] = SkScalarMul(SkColorGetB(info.fColors[i]), scale);
139 }
140
141 // Clamp the initial color.
142 result->append("dup 0 le {pop ");
143 result->appendScalar(colorData[0][0]);
144 result->append(" ");
145 result->appendScalar(colorData[0][1]);
146 result->append(" ");
147 result->appendScalar(colorData[0][2]);
148 result->append(" }\n");
149
150 // The gradient colors.
151 for (int i = 1 ; i < info.fColorCount; i++) {
152 result->append("{dup ");
153 result->appendScalar(info.fColorOffsets[i]);
154 result->append(" le {");
155 if (info.fColorOffsets[i - 1] != 0) {
156 result->appendScalar(info.fColorOffsets[i - 1]);
157 result->append(" sub\n");
158 }
159
160 interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1],
161 colorData[i], colorData[i - 1], kColorComponents,
162 result);
163 result->append("}\n");
164 }
165
166 // Clamp the final color.
167 result->append("{pop ");
168 result->appendScalar(colorData[info.fColorCount - 1][0]);
169 result->append(" ");
170 result->appendScalar(colorData[info.fColorCount - 1][1]);
171 result->append(" ");
172 result->appendScalar(colorData[info.fColorCount - 1][2]);
173
174 for (int i = 0 ; i < info.fColorCount; i++) {
175 result->append("} ifelse\n");
176 }
177}
178
179/* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */
180static void tileModeCode(SkShader::TileMode mode, SkString* result) {
181 if (mode == SkShader::kRepeat_TileMode) {
182 result->append("dup truncate sub\n"); // Get the fractional part.
183 result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1)
184 return;
185 }
186
187 if (mode == SkShader::kMirror_TileMode) {
188 // Map t mod 2 into [0, 1, 1, 0].
189 // Code Stack
190 result->append("abs " // Map negative to positive.
191 "dup " // t.s t.s
192 "truncate " // t.s t
193 "dup " // t.s t t
194 "cvi " // t.s t T
195 "2 mod " // t.s t (i mod 2)
196 "1 eq " // t.s t true|false
197 "3 1 roll " // true|false t.s t
198 "sub " // true|false 0.s
199 "exch " // 0.s true|false
200 "{1 exch sub} if\n"); // 1 - 0.s|0.s
201 }
202}
203
204static SkString linearCode(const SkShader::GradientInfo& info) {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000205 SkString function("{pop\n"); // Just ditch the y value.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000206 tileModeCode(info.fTileMode, &function);
207 gradientFunctionCode(info, &function);
208 function.append("}");
209 return function;
210}
211
212static SkString radialCode(const SkShader::GradientInfo& info) {
213 SkString function("{");
214 // Find the distance from the origin.
215 function.append("dup " // x y y
216 "mul " // x y^2
217 "exch " // y^2 x
218 "dup " // y^2 x x
219 "mul " // y^2 x^2
220 "add " // y^2+x^2
221 "sqrt\n"); // sqrt(y^2+x^2)
222
223 tileModeCode(info.fTileMode, &function);
224 gradientFunctionCode(info, &function);
225 function.append("}");
226 return function;
227}
228
229/* The math here is all based on the description in Two_Point_Radial_Gradient,
230 with one simplification, the coordinate space has been scaled so that
231 Dr = 1. This means we don't need to scale the entire equation by 1/Dr^2.
232 */
233static SkString twoPointRadialCode(const SkShader::GradientInfo& info) {
234 SkScalar dx = info.fPoint[0].fX - info.fPoint[1].fX;
235 SkScalar dy = info.fPoint[0].fY - info.fPoint[1].fY;
236 SkScalar sr = info.fRadius[0];
237 SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - SK_Scalar1;
238 bool posRoot = info.fRadius[1] > info.fRadius[0];
239
240 // We start with a stack of (x y), copy it and then consume one copy in
241 // order to calculate b and the other to calculate c.
242 SkString function("{");
243 function.append("2 copy ");
244
245 // Calculate -b and b^2.
246 function.appendScalar(dy);
247 function.append(" mul exch ");
248 function.appendScalar(dx);
249 function.append(" mul add ");
250 function.appendScalar(sr);
251 function.append(" sub 2 mul neg dup dup mul\n");
252
253 // Calculate c
254 function.append("4 2 roll dup mul exch dup mul add ");
255 function.appendScalar(SkScalarMul(sr, sr));
256 function.append(" sub\n");
257
258 // Calculate the determinate
259 function.appendScalar(SkScalarMul(SkIntToScalar(4), a));
260 function.append(" mul sub abs sqrt\n");
261
262 // And then the final value of t.
263 if (posRoot) {
264 function.append("sub ");
265 } else {
266 function.append("add ");
267 }
268 function.appendScalar(SkScalarMul(SkIntToScalar(2), a));
269 function.append(" div\n");
270
271 tileModeCode(info.fTileMode, &function);
272 gradientFunctionCode(info, &function);
273 function.append("}");
274 return function;
275}
276
rileya@google.com62197282012-07-10 20:05:23 +0000277/* Conical gradient shader, based on the Canvas spec for radial gradients
rmistry@google.comd6176b02012-08-23 18:14:13 +0000278 See: http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient
rileya@google.com62197282012-07-10 20:05:23 +0000279 */
280static SkString twoPointConicalCode(const SkShader::GradientInfo& info) {
281 SkScalar dx = info.fPoint[1].fX - info.fPoint[0].fX;
282 SkScalar dy = info.fPoint[1].fY - info.fPoint[0].fY;
283 SkScalar r0 = info.fRadius[0];
284 SkScalar dr = info.fRadius[1] - info.fRadius[0];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000285 SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) -
rileya@google.com62197282012-07-10 20:05:23 +0000286 SkScalarMul(dr, dr);
287
288 // First compute t, if the pixel falls outside the cone, then we'll end
289 // with 'false' on the stack, otherwise we'll push 'true' with t below it
290
291 // We start with a stack of (x y), copy it and then consume one copy in
292 // order to calculate b and the other to calculate c.
293 SkString function("{");
294 function.append("2 copy ");
295
296 // Calculate b and b^2; b = -2 * (y * dy + x * dx + r0 * dr).
297 function.appendScalar(dy);
298 function.append(" mul exch ");
299 function.appendScalar(dx);
300 function.append(" mul add ");
301 function.appendScalar(SkScalarMul(r0, dr));
302 function.append(" add -2 mul dup dup mul\n");
303
304 // c = x^2 + y^2 + radius0^2
305 function.append("4 2 roll dup mul exch dup mul add ");
306 function.appendScalar(SkScalarMul(r0, r0));
307 function.append(" sub dup 4 1 roll\n");
308
309 // Contents of the stack at this point: c, b, b^2, c
310
311 // if a = 0, then we collapse to a simpler linear case
312 if (a == 0) {
313
314 // t = -c/b
315 function.append("pop pop div neg dup ");
316
317 // compute radius(t)
318 function.appendScalar(dr);
319 function.append(" mul ");
320 function.appendScalar(r0);
321 function.append(" add\n");
322
323 // if r(t) < 0, then it's outside the cone
rileya@google.comab2fe822012-07-10 20:41:38 +0000324 function.append("0 lt {pop false} {true} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000325
326 } else {
327
328 // quadratic case: the Canvas spec wants the largest
329 // root t for which radius(t) > 0
rmistry@google.comd6176b02012-08-23 18:14:13 +0000330
rileya@google.com62197282012-07-10 20:05:23 +0000331 // compute the discriminant (b^2 - 4ac)
332 function.appendScalar(SkScalarMul(SkIntToScalar(4), a));
333 function.append(" mul sub dup\n");
334
335 // if d >= 0, proceed
336 function.append("0 ge {\n");
337
338 // an intermediate value we'll use to compute the roots:
339 // q = -0.5 * (b +/- sqrt(d))
340 function.append("sqrt exch dup 0 lt {exch -1 mul} if");
341 function.append(" add -0.5 mul dup\n");
342
343 // first root = q / a
344 function.appendScalar(a);
345 function.append(" div\n");
346
347 // second root = c / q
348 function.append("3 1 roll div\n");
349
350 // put the larger root on top of the stack
351 function.append("2 copy gt {exch} if\n");
352
353 // compute radius(t) for larger root
354 function.append("dup ");
355 function.appendScalar(dr);
356 function.append(" mul ");
357 function.appendScalar(r0);
358 function.append(" add\n");
359
360 // if r(t) > 0, we have our t, pop off the smaller root and we're done
361 function.append(" 0 gt {exch pop true}\n");
362
363 // otherwise, throw out the larger one and try the smaller root
364 function.append("{pop dup\n");
365 function.appendScalar(dr);
366 function.append(" mul ");
367 function.appendScalar(r0);
368 function.append(" add\n");
369
370 // if r(t) < 0, push false, otherwise the smaller root is our t
rileya@google.comab2fe822012-07-10 20:41:38 +0000371 function.append("0 le {pop false} {true} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000372 function.append("} ifelse\n");
373
374 // d < 0, clear the stack and push false
rileya@google.comab2fe822012-07-10 20:41:38 +0000375 function.append("} {pop pop pop false} ifelse\n");
rileya@google.com62197282012-07-10 20:05:23 +0000376 }
377
378 // if the pixel is in the cone, proceed to compute a color
379 function.append("{");
380 tileModeCode(info.fTileMode, &function);
381 gradientFunctionCode(info, &function);
382
383 // otherwise, just write black
384 function.append("} {0 0 0} ifelse }");
385
386 return function;
387}
388
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000389static SkString sweepCode(const SkShader::GradientInfo& info) {
390 SkString function("{exch atan 360 div\n");
391 tileModeCode(info.fTileMode, &function);
392 gradientFunctionCode(info, &function);
393 function.append("}");
394 return function;
395}
396
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000397class SkPDFShader::State {
398public:
399 SkShader::GradientType fType;
400 SkShader::GradientInfo fInfo;
401 SkAutoFree fColorData;
402 SkMatrix fCanvasTransform;
403 SkMatrix fShaderTransform;
404 SkIRect fBBox;
405
406 SkBitmap fImage;
407 uint32_t fPixelGeneration;
408 SkShader::TileMode fImageTileModes[2];
409
410 explicit State(const SkShader& shader, const SkMatrix& canvasTransform,
411 const SkIRect& bbox);
412 bool operator==(const State& b) const;
413};
414
415class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader {
416public:
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000417 explicit SkPDFFunctionShader(SkPDFShader::State* state);
robertphillips@google.com05141c22012-04-27 12:54:01 +0000418 virtual ~SkPDFFunctionShader() {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000419 if (isValid()) {
420 RemoveShader(this);
421 }
422 fResources.unrefAll();
423 }
424
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000425 virtual bool isValid() { return fResources.count() > 0; }
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000426
427 void getResources(SkTDArray<SkPDFObject*>* resourceList) {
428 GetResourcesHelper(&fResources, resourceList);
429 }
430
431private:
432 static SkPDFObject* RangeObject();
433
434 SkTDArray<SkPDFObject*> fResources;
435 SkAutoTDelete<const SkPDFShader::State> fState;
436
437 SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain);
438};
439
440class SkPDFImageShader : public SkPDFStream, public SkPDFShader {
441public:
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000442 explicit SkPDFImageShader(SkPDFShader::State* state);
robertphillips@google.com05141c22012-04-27 12:54:01 +0000443 virtual ~SkPDFImageShader() {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000444 RemoveShader(this);
445 fResources.unrefAll();
446 }
447
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000448 virtual bool isValid() { return size() > 0; }
449
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000450 void getResources(SkTDArray<SkPDFObject*>* resourceList) {
451 GetResourcesHelper(&fResources, resourceList);
452 }
453
454private:
455 SkTDArray<SkPDFObject*> fResources;
456 SkAutoTDelete<const SkPDFShader::State> fState;
457};
458
459SkPDFShader::SkPDFShader() {}
460
461// static
462void SkPDFShader::RemoveShader(SkPDFObject* shader) {
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000463 SkAutoMutexAcquire lock(CanonicalShadersMutex());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000464 ShaderCanonicalEntry entry(shader, NULL);
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000465 int index = CanonicalShaders().find(entry);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000466 SkASSERT(index >= 0);
467 CanonicalShaders().removeShuffle(index);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000468}
469
470// static
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000471SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader,
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000472 const SkMatrix& matrix,
473 const SkIRect& surfaceBBox) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000474 SkPDFObject* result;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000475 SkAutoMutexAcquire lock(CanonicalShadersMutex());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000476 SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox));
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +0000477 if (shaderState.get()->fType == SkShader::kNone_GradientType &&
478 shaderState.get()->fImage.isNull()) {
479 // TODO(vandebo) This drops SKComposeShader on the floor. We could
480 // handle compose shader by pulling things up to a layer, drawing with
481 // the first shader, applying the xfer mode and drawing again with the
482 // second shader, then applying the layer to the original drawing.
483 return NULL;
484 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000485
486 ShaderCanonicalEntry entry(NULL, shaderState.get());
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000487 int index = CanonicalShaders().find(entry);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000488 if (index >= 0) {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000489 result = CanonicalShaders()[index].fPDFShader;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000490 result->ref();
491 return result;
492 }
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000493
494 bool valid = false;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000495 // The PDFShader takes ownership of the shaderSate.
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000496 if (shaderState.get()->fType == SkShader::kNone_GradientType) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000497 SkPDFImageShader* imageShader =
498 new SkPDFImageShader(shaderState.detach());
499 valid = imageShader->isValid();
500 result = imageShader;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000501 } else {
502 SkPDFFunctionShader* functionShader =
503 new SkPDFFunctionShader(shaderState.detach());
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000504 valid = functionShader->isValid();
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000505 result = functionShader;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000506 }
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000507 if (!valid) {
508 delete result;
509 return NULL;
510 }
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000511 entry.fPDFShader = result;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000512 CanonicalShaders().push(entry);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000513 return result; // return the reference that came from new.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000514}
515
516// static
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000517SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000518 // This initialization is only thread safe with gcc.
519 static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders;
520 return gCanonicalShaders;
521}
522
523// static
digit@google.com1771cbf2012-01-26 21:26:40 +0000524SkBaseMutex& SkPDFShader::CanonicalShadersMutex() {
525 // This initialization is only thread safe with gcc or when
526 // POD-style mutex initialization is used.
527 SK_DECLARE_STATIC_MUTEX(gCanonicalShadersMutex);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000528 return gCanonicalShadersMutex;
529}
530
531// static
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000532SkPDFObject* SkPDFFunctionShader::RangeObject() {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000533 // This initialization is only thread safe with gcc.
534 static SkPDFArray* range = NULL;
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +0000535 // This method is only used with CanonicalShadersMutex, so it's safe to
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000536 // populate domain.
537 if (range == NULL) {
538 range = new SkPDFArray;
539 range->reserve(6);
reed@google.comc789cf12011-07-20 12:14:33 +0000540 range->appendInt(0);
541 range->appendInt(1);
542 range->appendInt(0);
543 range->appendInt(1);
544 range->appendInt(0);
545 range->appendInt(1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000546 }
547 return range;
548}
549
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000550SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state)
551 : SkPDFDict("Pattern"),
552 fState(state) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000553 SkString (*codeFunction)(const SkShader::GradientInfo& info) = NULL;
554 SkPoint transformPoints[2];
555
556 // Depending on the type of the gradient, we want to transform the
557 // coordinate space in different ways.
558 const SkShader::GradientInfo* info = &fState.get()->fInfo;
559 transformPoints[0] = info->fPoint[0];
560 transformPoints[1] = info->fPoint[1];
561 switch (fState.get()->fType) {
562 case SkShader::kLinear_GradientType:
563 codeFunction = &linearCode;
564 break;
565 case SkShader::kRadial_GradientType:
566 transformPoints[1] = transformPoints[0];
567 transformPoints[1].fX += info->fRadius[0];
568 codeFunction = &radialCode;
569 break;
570 case SkShader::kRadial2_GradientType: {
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000571 // Bail out if the radii are the same. Empty fResources signals
572 // an error and isValid will return false.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000573 if (info->fRadius[0] == info->fRadius[1]) {
574 return;
575 }
576 transformPoints[1] = transformPoints[0];
577 SkScalar dr = info->fRadius[1] - info->fRadius[0];
578 transformPoints[1].fX += dr;
579 codeFunction = &twoPointRadialCode;
580 break;
581 }
rileya@google.com62197282012-07-10 20:05:23 +0000582 case SkShader::kConical_GradientType: {
583 transformPoints[1] = transformPoints[0];
584 transformPoints[1].fX += SK_Scalar1;
585 codeFunction = &twoPointConicalCode;
586 break;
587 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000588 case SkShader::kSweep_GradientType:
589 transformPoints[1] = transformPoints[0];
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +0000590 transformPoints[1].fX += SK_Scalar1;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000591 codeFunction = &sweepCode;
592 break;
593 case SkShader::kColor_GradientType:
594 case SkShader::kNone_GradientType:
vandebo@chromium.org020798a2011-12-21 01:49:35 +0000595 default:
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000596 return;
597 }
598
599 // Move any scaling (assuming a unit gradient) or translation
600 // (and rotation for linear gradient), of the final gradient from
601 // info->fPoints to the matrix (updating bbox appropriately). Now
602 // the gradient can be drawn on on the unit segment.
603 SkMatrix mapperMatrix;
604 unitToPointsMatrix(transformPoints, &mapperMatrix);
605 SkMatrix finalMatrix = fState.get()->fCanvasTransform;
606 finalMatrix.preConcat(mapperMatrix);
607 finalMatrix.preConcat(fState.get()->fShaderTransform);
608 SkRect bbox;
609 bbox.set(fState.get()->fBBox);
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000610 if (!transformBBox(finalMatrix, &bbox)) {
611 return;
612 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000613
614 SkRefPtr<SkPDFArray> domain = new SkPDFArray;
615 domain->unref(); // SkRefPtr and new both took a reference.
616 domain->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000617 domain->appendScalar(bbox.fLeft);
618 domain->appendScalar(bbox.fRight);
619 domain->appendScalar(bbox.fTop);
620 domain->appendScalar(bbox.fBottom);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000621
622 SkString functionCode;
623 // The two point radial gradient further references fState.get()->fInfo
624 // in translating from x, y coordinates to the t parameter. So, we have
625 // to transform the points and radii according to the calculated matrix.
626 if (fState.get()->fType == SkShader::kRadial2_GradientType) {
627 SkShader::GradientInfo twoPointRadialInfo = *info;
628 SkMatrix inverseMapperMatrix;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000629 if (!mapperMatrix.invert(&inverseMapperMatrix)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000630 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000631 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000632 inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2);
633 twoPointRadialInfo.fRadius[0] =
634 inverseMapperMatrix.mapRadius(info->fRadius[0]);
635 twoPointRadialInfo.fRadius[1] =
636 inverseMapperMatrix.mapRadius(info->fRadius[1]);
637 functionCode = codeFunction(twoPointRadialInfo);
638 } else {
639 functionCode = codeFunction(*info);
640 }
641
642 SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get());
643 // Pass one reference to fResources, SkRefPtr and new both took a reference.
644 fResources.push(function.get());
645
646 SkRefPtr<SkPDFDict> pdfShader = new SkPDFDict;
647 pdfShader->unref(); // SkRefPtr and new both took a reference.
reed@google.comc789cf12011-07-20 12:14:33 +0000648 pdfShader->insertInt("ShadingType", 1);
649 pdfShader->insertName("ColorSpace", "DeviceRGB");
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000650 pdfShader->insert("Domain", domain.get());
651 pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref();
652
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000653 insertInt("PatternType", 2);
654 insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref();
655 insert("Shading", pdfShader.get());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000656}
657
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000658SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000659 fState.get()->fImage.lockPixels();
660
661 SkMatrix finalMatrix = fState.get()->fCanvasTransform;
662 finalMatrix.preConcat(fState.get()->fShaderTransform);
663 SkRect surfaceBBox;
664 surfaceBBox.set(fState.get()->fBBox);
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000665 if (!transformBBox(finalMatrix, &surfaceBBox)) {
666 return;
667 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000668
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000669 SkMatrix unflip;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000670 unflip.setTranslate(0, SkScalarRoundToScalar(surfaceBBox.height()));
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000671 unflip.preScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000672 SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()),
673 SkScalarRound(surfaceBBox.height()));
ctguil@chromium.org15261292011-04-29 17:54:16 +0000674 SkPDFDevice pattern(size, size, unflip);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000675 SkCanvas canvas(&pattern);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000676 canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop);
677 finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000678
679 const SkBitmap* image = &fState.get()->fImage;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000680 SkScalar width = SkIntToScalar(image->width());
681 SkScalar height = SkIntToScalar(image->height());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000682 SkShader::TileMode tileModes[2];
683 tileModes[0] = fState.get()->fImageTileModes[0];
684 tileModes[1] = fState.get()->fImageTileModes[1];
685
686 canvas.drawBitmap(*image, 0, 0);
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000687 SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop,
688 width, height);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000689
690 // Tiling is implied. First we handle mirroring.
691 if (tileModes[0] == SkShader::kMirror_TileMode) {
692 SkMatrix xMirror;
693 xMirror.setScale(-1, 1);
694 xMirror.postTranslate(2 * width, 0);
695 canvas.drawBitmapMatrix(*image, xMirror);
696 patternBBox.fRight += width;
697 }
698 if (tileModes[1] == SkShader::kMirror_TileMode) {
699 SkMatrix yMirror;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000700 yMirror.setScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000701 yMirror.postTranslate(0, 2 * height);
702 canvas.drawBitmapMatrix(*image, yMirror);
703 patternBBox.fBottom += height;
704 }
705 if (tileModes[0] == SkShader::kMirror_TileMode &&
706 tileModes[1] == SkShader::kMirror_TileMode) {
707 SkMatrix mirror;
708 mirror.setScale(-1, -1);
709 mirror.postTranslate(2 * width, 2 * height);
710 canvas.drawBitmapMatrix(*image, mirror);
711 }
712
713 // Then handle Clamping, which requires expanding the pattern canvas to
714 // cover the entire surfaceBBox.
715
716 // If both x and y are in clamp mode, we start by filling in the corners.
717 // (Which are just a rectangles of the corner colors.)
718 if (tileModes[0] == SkShader::kClamp_TileMode &&
719 tileModes[1] == SkShader::kClamp_TileMode) {
720 SkPaint paint;
721 SkRect rect;
722 rect = SkRect::MakeLTRB(surfaceBBox.fLeft, surfaceBBox.fTop, 0, 0);
723 if (!rect.isEmpty()) {
724 paint.setColor(image->getColor(0, 0));
725 canvas.drawRect(rect, paint);
726 }
727
728 rect = SkRect::MakeLTRB(width, surfaceBBox.fTop, surfaceBBox.fRight, 0);
729 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000730 paint.setColor(image->getColor(image->width() - 1, 0));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000731 canvas.drawRect(rect, paint);
732 }
733
734 rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight,
735 surfaceBBox.fBottom);
736 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000737 paint.setColor(image->getColor(image->width() - 1,
738 image->height() - 1));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000739 canvas.drawRect(rect, paint);
740 }
741
742 rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0,
743 surfaceBBox.fBottom);
744 if (!rect.isEmpty()) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000745 paint.setColor(image->getColor(0, image->height() - 1));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000746 canvas.drawRect(rect, paint);
747 }
748 }
749
750 // Then expand the left, right, top, then bottom.
751 if (tileModes[0] == SkShader::kClamp_TileMode) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000752 SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, image->height());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000753 if (surfaceBBox.fLeft < 0) {
754 SkBitmap left;
755 SkAssertResult(image->extractSubset(&left, subset));
756
757 SkMatrix leftMatrix;
758 leftMatrix.setScale(-surfaceBBox.fLeft, 1);
759 leftMatrix.postTranslate(surfaceBBox.fLeft, 0);
760 canvas.drawBitmapMatrix(left, leftMatrix);
761
762 if (tileModes[1] == SkShader::kMirror_TileMode) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000763 leftMatrix.postScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000764 leftMatrix.postTranslate(0, 2 * height);
765 canvas.drawBitmapMatrix(left, leftMatrix);
766 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000767 patternBBox.fLeft = 0;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000768 }
769
770 if (surfaceBBox.fRight > width) {
771 SkBitmap right;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000772 subset.offset(image->width() - 1, 0);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000773 SkAssertResult(image->extractSubset(&right, subset));
774
775 SkMatrix rightMatrix;
776 rightMatrix.setScale(surfaceBBox.fRight - width, 1);
777 rightMatrix.postTranslate(width, 0);
778 canvas.drawBitmapMatrix(right, rightMatrix);
779
780 if (tileModes[1] == SkShader::kMirror_TileMode) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000781 rightMatrix.postScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000782 rightMatrix.postTranslate(0, 2 * height);
783 canvas.drawBitmapMatrix(right, rightMatrix);
784 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000785 patternBBox.fRight = surfaceBBox.width();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000786 }
787 }
788
789 if (tileModes[1] == SkShader::kClamp_TileMode) {
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000790 SkIRect subset = SkIRect::MakeXYWH(0, 0, image->width(), 1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000791 if (surfaceBBox.fTop < 0) {
792 SkBitmap top;
793 SkAssertResult(image->extractSubset(&top, subset));
794
795 SkMatrix topMatrix;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000796 topMatrix.setScale(SK_Scalar1, -surfaceBBox.fTop);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000797 topMatrix.postTranslate(0, surfaceBBox.fTop);
798 canvas.drawBitmapMatrix(top, topMatrix);
799
800 if (tileModes[0] == SkShader::kMirror_TileMode) {
801 topMatrix.postScale(-1, 1);
802 topMatrix.postTranslate(2 * width, 0);
803 canvas.drawBitmapMatrix(top, topMatrix);
804 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000805 patternBBox.fTop = 0;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000806 }
807
808 if (surfaceBBox.fBottom > height) {
809 SkBitmap bottom;
vandebo@chromium.org54ff85c2012-03-09 17:18:50 +0000810 subset.offset(0, image->height() - 1);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000811 SkAssertResult(image->extractSubset(&bottom, subset));
812
813 SkMatrix bottomMatrix;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000814 bottomMatrix.setScale(SK_Scalar1, surfaceBBox.fBottom - height);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000815 bottomMatrix.postTranslate(0, height);
816 canvas.drawBitmapMatrix(bottom, bottomMatrix);
817
818 if (tileModes[0] == SkShader::kMirror_TileMode) {
819 bottomMatrix.postScale(-1, 1);
820 bottomMatrix.postTranslate(2 * width, 0);
821 canvas.drawBitmapMatrix(bottom, bottomMatrix);
822 }
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +0000823 patternBBox.fBottom = surfaceBBox.height();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000824 }
825 }
826
827 SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray;
828 patternBBoxArray->unref(); // SkRefPtr and new both took a reference.
829 patternBBoxArray->reserve(4);
reed@google.comc789cf12011-07-20 12:14:33 +0000830 patternBBoxArray->appendScalar(patternBBox.fLeft);
831 patternBBoxArray->appendScalar(patternBBox.fTop);
832 patternBBoxArray->appendScalar(patternBBox.fRight);
833 patternBBoxArray->appendScalar(patternBBox.fBottom);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000834
835 // Put the canvas into the pattern stream (fContent).
836 SkRefPtr<SkStream> content = pattern.content();
837 content->unref(); // SkRefPtr and content() both took a reference.
vandebo@chromium.org188838c2012-03-09 22:16:58 +0000838 pattern.getResources(&fResources, false);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000839
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000840 setData(content.get());
841 insertName("Type", "Pattern");
842 insertInt("PatternType", 1);
843 insertInt("PaintType", 1);
844 insertInt("TilingType", 1);
845 insert("BBox", patternBBoxArray.get());
846 insertScalar("XStep", patternBBox.width());
847 insertScalar("YStep", patternBBox.height());
reed@google.com1feb3302011-07-20 18:43:19 +0000848 insert("Resources", pattern.getResourceDict());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000849 insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000850
851 fState.get()->fImage.unlockPixels();
852}
853
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000854SkPDFStream* SkPDFFunctionShader::makePSFunction(const SkString& psCode,
855 SkPDFArray* domain) {
856 SkAutoDataUnref funcData(SkData::NewWithCopy(psCode.c_str(),
857 psCode.size()));
858 SkPDFStream* result = new SkPDFStream(funcData.get());
reed@google.comc789cf12011-07-20 12:14:33 +0000859 result->insertInt("FunctionType", 4);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000860 result->insert("Domain", domain);
reed@google.comf6c3ebd2011-07-20 17:20:28 +0000861 result->insert("Range", RangeObject());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000862 return result;
863}
864
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000865SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader,
866 const State* state)
867 : fPDFShader(pdfShader),
868 fState(state) {
869}
870
871bool SkPDFShader::ShaderCanonicalEntry::operator==(
872 const ShaderCanonicalEntry& b) const {
873 return fPDFShader == b.fPDFShader ||
874 (fState != NULL && b.fState != NULL && *fState == *b.fState);
875}
876
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000877bool SkPDFShader::State::operator==(const SkPDFShader::State& b) const {
878 if (fType != b.fType ||
879 fCanvasTransform != b.fCanvasTransform ||
880 fShaderTransform != b.fShaderTransform ||
881 fBBox != b.fBBox) {
882 return false;
883 }
884
885 if (fType == SkShader::kNone_GradientType) {
886 if (fPixelGeneration != b.fPixelGeneration ||
887 fPixelGeneration == 0 ||
888 fImageTileModes[0] != b.fImageTileModes[0] ||
889 fImageTileModes[1] != b.fImageTileModes[1]) {
890 return false;
891 }
892 } else {
893 if (fInfo.fColorCount != b.fInfo.fColorCount ||
894 memcmp(fInfo.fColors, b.fInfo.fColors,
895 sizeof(SkColor) * fInfo.fColorCount) != 0 ||
896 memcmp(fInfo.fColorOffsets, b.fInfo.fColorOffsets,
897 sizeof(SkScalar) * fInfo.fColorCount) != 0 ||
898 fInfo.fPoint[0] != b.fInfo.fPoint[0] ||
899 fInfo.fTileMode != b.fInfo.fTileMode) {
900 return false;
901 }
902
903 switch (fType) {
904 case SkShader::kLinear_GradientType:
905 if (fInfo.fPoint[1] != b.fInfo.fPoint[1]) {
906 return false;
907 }
908 break;
909 case SkShader::kRadial_GradientType:
910 if (fInfo.fRadius[0] != b.fInfo.fRadius[0]) {
911 return false;
912 }
913 break;
914 case SkShader::kRadial2_GradientType:
reed@google.com49085332012-06-11 18:54:07 +0000915 case SkShader::kConical_GradientType:
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000916 if (fInfo.fPoint[1] != b.fInfo.fPoint[1] ||
917 fInfo.fRadius[0] != b.fInfo.fRadius[0] ||
918 fInfo.fRadius[1] != b.fInfo.fRadius[1]) {
919 return false;
920 }
921 break;
922 case SkShader::kSweep_GradientType:
923 case SkShader::kNone_GradientType:
924 case SkShader::kColor_GradientType:
925 break;
926 }
927 }
928 return true;
929}
930
931SkPDFShader::State::State(const SkShader& shader,
932 const SkMatrix& canvasTransform, const SkIRect& bbox)
933 : fCanvasTransform(canvasTransform),
vandebo@chromium.orge1bc2742011-06-21 22:26:39 +0000934 fBBox(bbox),
935 fPixelGeneration(0) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000936 fInfo.fColorCount = 0;
937 fInfo.fColors = NULL;
938 fInfo.fColorOffsets = NULL;
939 shader.getLocalMatrix(&fShaderTransform);
vandebo@chromium.orge1bc2742011-06-21 22:26:39 +0000940 fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000941
942 fType = shader.asAGradient(&fInfo);
943
944 if (fType == SkShader::kNone_GradientType) {
945 SkShader::BitmapType bitmapType;
946 SkMatrix matrix;
rileya@google.com91f319c2012-07-25 17:18:31 +0000947 bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000948 if (bitmapType != SkShader::kDefault_BitmapType) {
949 fImage.reset();
950 return;
951 }
952 SkASSERT(matrix.isIdentity());
953 fPixelGeneration = fImage.getGenerationID();
954 } else {
955 fColorData.set(sk_malloc_throw(
956 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar))));
ctguil@chromium.orga5c72342011-08-15 23:55:03 +0000957 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get());
vandebo@chromium.org020798a2011-12-21 01:49:35 +0000958 fInfo.fColorOffsets =
959 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000960 shader.asAGradient(&fInfo);
961 }
962}