epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 10 | #include "SkPDFShader.h" |
| 11 | |
| 12 | #include "SkCanvas.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 14 | #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.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 20 | #include "SkTemplates.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 21 | #include "SkThread.h" |
| 22 | #include "SkTypes.h" |
| 23 | |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 24 | static bool transformBBox(const SkMatrix& matrix, SkRect* bbox) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 25 | SkMatrix inverse; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 26 | if (!matrix.invert(&inverse)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 27 | return false; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 28 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 29 | inverse.mapRect(bbox); |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 30 | return true; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static 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 | */ |
| 52 | static 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.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 56 | SkAutoSTMalloc<4, SkScalar> multiplierAlloc(components); |
| 57 | SkScalar *multiplier = multiplierAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 58 | 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.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 65 | SkAutoSTMalloc<4, bool> dupInputAlloc(components); |
| 66 | bool *dupInput = dupInputAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 67 | 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 | */ |
| 123 | static 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.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 131 | typedef SkScalar ColorTuple[kColorComponents]; |
| 132 | SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount); |
| 133 | ColorTuple *colorData = colorDataAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 134 | 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. */ |
| 180 | static 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 | |
| 204 | static SkString linearCode(const SkShader::GradientInfo& info) { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 205 | SkString function("{pop\n"); // Just ditch the y value. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 206 | tileModeCode(info.fTileMode, &function); |
| 207 | gradientFunctionCode(info, &function); |
| 208 | function.append("}"); |
| 209 | return function; |
| 210 | } |
| 211 | |
| 212 | static 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 | */ |
| 233 | static 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.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 277 | /* Conical gradient shader, based on the Canvas spec for radial gradients |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 278 | See: http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 279 | */ |
| 280 | static 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 285 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 286 | 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.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 324 | function.append("0 lt {pop false} {true} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 325 | |
| 326 | } else { |
| 327 | |
| 328 | // quadratic case: the Canvas spec wants the largest |
| 329 | // root t for which radius(t) > 0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 330 | |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 331 | // 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.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 371 | function.append("0 le {pop false} {true} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 372 | function.append("} ifelse\n"); |
| 373 | |
| 374 | // d < 0, clear the stack and push false |
rileya@google.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 375 | function.append("} {pop pop pop false} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 376 | } |
| 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.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 389 | static 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.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 397 | class SkPDFShader::State { |
| 398 | public: |
| 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 | |
| 415 | class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader { |
| 416 | public: |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 417 | explicit SkPDFFunctionShader(SkPDFShader::State* state); |
robertphillips@google.com | 05141c2 | 2012-04-27 12:54:01 +0000 | [diff] [blame] | 418 | virtual ~SkPDFFunctionShader() { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 419 | if (isValid()) { |
| 420 | RemoveShader(this); |
| 421 | } |
| 422 | fResources.unrefAll(); |
| 423 | } |
| 424 | |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 425 | virtual bool isValid() { return fResources.count() > 0; } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 426 | |
| 427 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 428 | GetResourcesHelper(&fResources, resourceList); |
| 429 | } |
| 430 | |
| 431 | private: |
| 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 | |
| 440 | class SkPDFImageShader : public SkPDFStream, public SkPDFShader { |
| 441 | public: |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 442 | explicit SkPDFImageShader(SkPDFShader::State* state); |
robertphillips@google.com | 05141c2 | 2012-04-27 12:54:01 +0000 | [diff] [blame] | 443 | virtual ~SkPDFImageShader() { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 444 | RemoveShader(this); |
| 445 | fResources.unrefAll(); |
| 446 | } |
| 447 | |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 448 | virtual bool isValid() { return size() > 0; } |
| 449 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 450 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 451 | GetResourcesHelper(&fResources, resourceList); |
| 452 | } |
| 453 | |
| 454 | private: |
| 455 | SkTDArray<SkPDFObject*> fResources; |
| 456 | SkAutoTDelete<const SkPDFShader::State> fState; |
| 457 | }; |
| 458 | |
| 459 | SkPDFShader::SkPDFShader() {} |
| 460 | |
| 461 | // static |
| 462 | void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 463 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 464 | ShaderCanonicalEntry entry(shader, NULL); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 465 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 466 | SkASSERT(index >= 0); |
| 467 | CanonicalShaders().removeShuffle(index); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 471 | SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 472 | const SkMatrix& matrix, |
| 473 | const SkIRect& surfaceBBox) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 474 | SkPDFObject* result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 475 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 476 | SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox)); |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 477 | 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.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 485 | |
| 486 | ShaderCanonicalEntry entry(NULL, shaderState.get()); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 487 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 488 | if (index >= 0) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 489 | result = CanonicalShaders()[index].fPDFShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 490 | result->ref(); |
| 491 | return result; |
| 492 | } |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 493 | |
| 494 | bool valid = false; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 495 | // The PDFShader takes ownership of the shaderSate. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 496 | if (shaderState.get()->fType == SkShader::kNone_GradientType) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 497 | SkPDFImageShader* imageShader = |
| 498 | new SkPDFImageShader(shaderState.detach()); |
| 499 | valid = imageShader->isValid(); |
| 500 | result = imageShader; |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 501 | } else { |
| 502 | SkPDFFunctionShader* functionShader = |
| 503 | new SkPDFFunctionShader(shaderState.detach()); |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 504 | valid = functionShader->isValid(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 505 | result = functionShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 506 | } |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 507 | if (!valid) { |
| 508 | delete result; |
| 509 | return NULL; |
| 510 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 511 | entry.fPDFShader = result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 512 | CanonicalShaders().push(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 513 | return result; // return the reference that came from new. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // static |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 517 | SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 518 | // This initialization is only thread safe with gcc. |
| 519 | static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders; |
| 520 | return gCanonicalShaders; |
| 521 | } |
| 522 | |
| 523 | // static |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 524 | SkBaseMutex& 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.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 528 | return gCanonicalShadersMutex; |
| 529 | } |
| 530 | |
| 531 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 532 | SkPDFObject* SkPDFFunctionShader::RangeObject() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 533 | // This initialization is only thread safe with gcc. |
| 534 | static SkPDFArray* range = NULL; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 535 | // This method is only used with CanonicalShadersMutex, so it's safe to |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 536 | // populate domain. |
| 537 | if (range == NULL) { |
| 538 | range = new SkPDFArray; |
| 539 | range->reserve(6); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 540 | 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.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 546 | } |
| 547 | return range; |
| 548 | } |
| 549 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 550 | SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) |
| 551 | : SkPDFDict("Pattern"), |
| 552 | fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 553 | 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.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 571 | // Bail out if the radii are the same. Empty fResources signals |
| 572 | // an error and isValid will return false. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 573 | 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.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 582 | case SkShader::kConical_GradientType: { |
| 583 | transformPoints[1] = transformPoints[0]; |
| 584 | transformPoints[1].fX += SK_Scalar1; |
| 585 | codeFunction = &twoPointConicalCode; |
| 586 | break; |
| 587 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 588 | case SkShader::kSweep_GradientType: |
| 589 | transformPoints[1] = transformPoints[0]; |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 590 | transformPoints[1].fX += SK_Scalar1; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 591 | codeFunction = &sweepCode; |
| 592 | break; |
| 593 | case SkShader::kColor_GradientType: |
| 594 | case SkShader::kNone_GradientType: |
vandebo@chromium.org | 020798a | 2011-12-21 01:49:35 +0000 | [diff] [blame] | 595 | default: |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 596 | 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.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 610 | if (!transformBBox(finalMatrix, &bbox)) { |
| 611 | return; |
| 612 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 613 | |
| 614 | SkRefPtr<SkPDFArray> domain = new SkPDFArray; |
| 615 | domain->unref(); // SkRefPtr and new both took a reference. |
| 616 | domain->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 617 | domain->appendScalar(bbox.fLeft); |
| 618 | domain->appendScalar(bbox.fRight); |
| 619 | domain->appendScalar(bbox.fTop); |
| 620 | domain->appendScalar(bbox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 621 | |
| 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.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 629 | if (!mapperMatrix.invert(&inverseMapperMatrix)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 630 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 631 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 632 | 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.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 648 | pdfShader->insertInt("ShadingType", 1); |
| 649 | pdfShader->insertName("ColorSpace", "DeviceRGB"); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 650 | pdfShader->insert("Domain", domain.get()); |
| 651 | pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref(); |
| 652 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 653 | insertInt("PatternType", 2); |
| 654 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
| 655 | insert("Shading", pdfShader.get()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 656 | } |
| 657 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 658 | SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 659 | 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.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 665 | if (!transformBBox(finalMatrix, &surfaceBBox)) { |
| 666 | return; |
| 667 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 668 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 669 | SkMatrix unflip; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 670 | unflip.setTranslate(0, SkScalarRoundToScalar(surfaceBBox.height())); |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 671 | unflip.preScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 672 | SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()), |
| 673 | SkScalarRound(surfaceBBox.height())); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 674 | SkPDFDevice pattern(size, size, unflip); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 675 | SkCanvas canvas(&pattern); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 676 | canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop); |
| 677 | finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 678 | |
| 679 | const SkBitmap* image = &fState.get()->fImage; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 680 | SkScalar width = SkIntToScalar(image->width()); |
| 681 | SkScalar height = SkIntToScalar(image->height()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 682 | 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.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 687 | SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop, |
| 688 | width, height); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 689 | |
| 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.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 700 | yMirror.setScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 701 | 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.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 730 | paint.setColor(image->getColor(image->width() - 1, 0)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 731 | canvas.drawRect(rect, paint); |
| 732 | } |
| 733 | |
| 734 | rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight, |
| 735 | surfaceBBox.fBottom); |
| 736 | if (!rect.isEmpty()) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 737 | paint.setColor(image->getColor(image->width() - 1, |
| 738 | image->height() - 1)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 739 | canvas.drawRect(rect, paint); |
| 740 | } |
| 741 | |
| 742 | rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0, |
| 743 | surfaceBBox.fBottom); |
| 744 | if (!rect.isEmpty()) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 745 | paint.setColor(image->getColor(0, image->height() - 1)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 746 | 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.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 752 | SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, image->height()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 753 | 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.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 763 | leftMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 764 | leftMatrix.postTranslate(0, 2 * height); |
| 765 | canvas.drawBitmapMatrix(left, leftMatrix); |
| 766 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 767 | patternBBox.fLeft = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (surfaceBBox.fRight > width) { |
| 771 | SkBitmap right; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 772 | subset.offset(image->width() - 1, 0); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 773 | 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.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 781 | rightMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 782 | rightMatrix.postTranslate(0, 2 * height); |
| 783 | canvas.drawBitmapMatrix(right, rightMatrix); |
| 784 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 785 | patternBBox.fRight = surfaceBBox.width(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
| 789 | if (tileModes[1] == SkShader::kClamp_TileMode) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 790 | SkIRect subset = SkIRect::MakeXYWH(0, 0, image->width(), 1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 791 | if (surfaceBBox.fTop < 0) { |
| 792 | SkBitmap top; |
| 793 | SkAssertResult(image->extractSubset(&top, subset)); |
| 794 | |
| 795 | SkMatrix topMatrix; |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 796 | topMatrix.setScale(SK_Scalar1, -surfaceBBox.fTop); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 797 | 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.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 805 | patternBBox.fTop = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (surfaceBBox.fBottom > height) { |
| 809 | SkBitmap bottom; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 810 | subset.offset(0, image->height() - 1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 811 | SkAssertResult(image->extractSubset(&bottom, subset)); |
| 812 | |
| 813 | SkMatrix bottomMatrix; |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 814 | bottomMatrix.setScale(SK_Scalar1, surfaceBBox.fBottom - height); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 815 | 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.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 823 | patternBBox.fBottom = surfaceBBox.height(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray; |
| 828 | patternBBoxArray->unref(); // SkRefPtr and new both took a reference. |
| 829 | patternBBoxArray->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 830 | patternBBoxArray->appendScalar(patternBBox.fLeft); |
| 831 | patternBBoxArray->appendScalar(patternBBox.fTop); |
| 832 | patternBBoxArray->appendScalar(patternBBox.fRight); |
| 833 | patternBBoxArray->appendScalar(patternBBox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 834 | |
| 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.org | 188838c | 2012-03-09 22:16:58 +0000 | [diff] [blame] | 838 | pattern.getResources(&fResources, false); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 839 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 840 | 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.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 848 | insert("Resources", pattern.getResourceDict()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 849 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 850 | |
| 851 | fState.get()->fImage.unlockPixels(); |
| 852 | } |
| 853 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 854 | SkPDFStream* 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.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 859 | result->insertInt("FunctionType", 4); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 860 | result->insert("Domain", domain); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 861 | result->insert("Range", RangeObject()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 862 | return result; |
| 863 | } |
| 864 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 865 | SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader, |
| 866 | const State* state) |
| 867 | : fPDFShader(pdfShader), |
| 868 | fState(state) { |
| 869 | } |
| 870 | |
| 871 | bool 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.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 877 | bool 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.com | 4908533 | 2012-06-11 18:54:07 +0000 | [diff] [blame] | 915 | case SkShader::kConical_GradientType: |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 916 | 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 | |
| 931 | SkPDFShader::State::State(const SkShader& shader, |
| 932 | const SkMatrix& canvasTransform, const SkIRect& bbox) |
| 933 | : fCanvasTransform(canvasTransform), |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 934 | fBBox(bbox), |
| 935 | fPixelGeneration(0) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 936 | fInfo.fColorCount = 0; |
| 937 | fInfo.fColors = NULL; |
| 938 | fInfo.fColorOffsets = NULL; |
| 939 | shader.getLocalMatrix(&fShaderTransform); |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 940 | fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 941 | |
| 942 | fType = shader.asAGradient(&fInfo); |
| 943 | |
| 944 | if (fType == SkShader::kNone_GradientType) { |
| 945 | SkShader::BitmapType bitmapType; |
| 946 | SkMatrix matrix; |
rileya@google.com | 91f319c | 2012-07-25 17:18:31 +0000 | [diff] [blame] | 947 | bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 948 | 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.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 957 | fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
vandebo@chromium.org | 020798a | 2011-12-21 01:49:35 +0000 | [diff] [blame] | 958 | fInfo.fColorOffsets = |
| 959 | reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 960 | shader.asAGradient(&fInfo); |
| 961 | } |
| 962 | } |