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++) { |
vandebo@chromium.org | b862204 | 2012-10-22 20:12:40 +0000 | [diff] [blame] | 77 | // If the next components needs t and this component will consume a |
| 78 | // copy, make another copy. |
| 79 | if (dupInput[i] && multiplier[i] != 0) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 80 | result->append("dup "); |
| 81 | } |
| 82 | |
| 83 | if (multiplier[i] == 0) { |
| 84 | result->appendScalar(prevColor[i]); |
| 85 | result->append(" "); |
| 86 | } else { |
| 87 | if (multiplier[i] != 1) { |
| 88 | result->appendScalar(multiplier[i]); |
| 89 | result->append(" mul "); |
| 90 | } |
| 91 | if (prevColor[i] != 0) { |
| 92 | result->appendScalar(prevColor[i]); |
| 93 | result->append(" add "); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (dupInput[i]) { |
| 98 | result->append("exch\n"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* Generate Type 4 function code to map t=[0,1) to the passed gradient, |
| 104 | clamping at the edges of the range. The generated code will be of the form: |
| 105 | if (t < 0) { |
| 106 | return colorData[0][r,g,b]; |
| 107 | } else { |
| 108 | if (t < info.fColorOffsets[1]) { |
| 109 | return linearinterpolation(colorData[0][r,g,b], |
| 110 | colorData[1][r,g,b]); |
| 111 | } else { |
| 112 | if (t < info.fColorOffsets[2]) { |
| 113 | return linearinterpolation(colorData[1][r,g,b], |
| 114 | colorData[2][r,g,b]); |
| 115 | } else { |
| 116 | |
| 117 | ... } else { |
| 118 | return colorData[info.fColorCount - 1][r,g,b]; |
| 119 | } |
| 120 | ... |
| 121 | } |
| 122 | } |
| 123 | */ |
| 124 | static void gradientFunctionCode(const SkShader::GradientInfo& info, |
| 125 | SkString* result) { |
| 126 | /* We want to linearly interpolate from the previous color to the next. |
| 127 | Scale the colors from 0..255 to 0..1 and determine the multipliers |
| 128 | for interpolation. |
| 129 | C{r,g,b}(t, section) = t - offset_(section-1) + t * Multiplier{r,g,b}. |
| 130 | */ |
| 131 | static const int kColorComponents = 3; |
twiz@google.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 132 | typedef SkScalar ColorTuple[kColorComponents]; |
| 133 | SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount); |
| 134 | ColorTuple *colorData = colorDataAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 135 | const SkScalar scale = SkScalarInvert(SkIntToScalar(255)); |
| 136 | for (int i = 0; i < info.fColorCount; i++) { |
| 137 | colorData[i][0] = SkScalarMul(SkColorGetR(info.fColors[i]), scale); |
| 138 | colorData[i][1] = SkScalarMul(SkColorGetG(info.fColors[i]), scale); |
| 139 | colorData[i][2] = SkScalarMul(SkColorGetB(info.fColors[i]), scale); |
| 140 | } |
| 141 | |
| 142 | // Clamp the initial color. |
| 143 | result->append("dup 0 le {pop "); |
| 144 | result->appendScalar(colorData[0][0]); |
| 145 | result->append(" "); |
| 146 | result->appendScalar(colorData[0][1]); |
| 147 | result->append(" "); |
| 148 | result->appendScalar(colorData[0][2]); |
| 149 | result->append(" }\n"); |
| 150 | |
| 151 | // The gradient colors. |
| 152 | for (int i = 1 ; i < info.fColorCount; i++) { |
| 153 | result->append("{dup "); |
| 154 | result->appendScalar(info.fColorOffsets[i]); |
| 155 | result->append(" le {"); |
| 156 | if (info.fColorOffsets[i - 1] != 0) { |
| 157 | result->appendScalar(info.fColorOffsets[i - 1]); |
| 158 | result->append(" sub\n"); |
| 159 | } |
| 160 | |
| 161 | interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1], |
| 162 | colorData[i], colorData[i - 1], kColorComponents, |
| 163 | result); |
| 164 | result->append("}\n"); |
| 165 | } |
| 166 | |
| 167 | // Clamp the final color. |
| 168 | result->append("{pop "); |
| 169 | result->appendScalar(colorData[info.fColorCount - 1][0]); |
| 170 | result->append(" "); |
| 171 | result->appendScalar(colorData[info.fColorCount - 1][1]); |
| 172 | result->append(" "); |
| 173 | result->appendScalar(colorData[info.fColorCount - 1][2]); |
| 174 | |
| 175 | for (int i = 0 ; i < info.fColorCount; i++) { |
| 176 | result->append("} ifelse\n"); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */ |
| 181 | static void tileModeCode(SkShader::TileMode mode, SkString* result) { |
| 182 | if (mode == SkShader::kRepeat_TileMode) { |
| 183 | result->append("dup truncate sub\n"); // Get the fractional part. |
| 184 | result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1) |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (mode == SkShader::kMirror_TileMode) { |
| 189 | // Map t mod 2 into [0, 1, 1, 0]. |
| 190 | // Code Stack |
| 191 | result->append("abs " // Map negative to positive. |
| 192 | "dup " // t.s t.s |
| 193 | "truncate " // t.s t |
| 194 | "dup " // t.s t t |
| 195 | "cvi " // t.s t T |
| 196 | "2 mod " // t.s t (i mod 2) |
| 197 | "1 eq " // t.s t true|false |
| 198 | "3 1 roll " // true|false t.s t |
| 199 | "sub " // true|false 0.s |
| 200 | "exch " // 0.s true|false |
| 201 | "{1 exch sub} if\n"); // 1 - 0.s|0.s |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static SkString linearCode(const SkShader::GradientInfo& info) { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 206 | SkString function("{pop\n"); // Just ditch the y value. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 207 | tileModeCode(info.fTileMode, &function); |
| 208 | gradientFunctionCode(info, &function); |
| 209 | function.append("}"); |
| 210 | return function; |
| 211 | } |
| 212 | |
| 213 | static SkString radialCode(const SkShader::GradientInfo& info) { |
| 214 | SkString function("{"); |
| 215 | // Find the distance from the origin. |
| 216 | function.append("dup " // x y y |
| 217 | "mul " // x y^2 |
| 218 | "exch " // y^2 x |
| 219 | "dup " // y^2 x x |
| 220 | "mul " // y^2 x^2 |
| 221 | "add " // y^2+x^2 |
| 222 | "sqrt\n"); // sqrt(y^2+x^2) |
| 223 | |
| 224 | tileModeCode(info.fTileMode, &function); |
| 225 | gradientFunctionCode(info, &function); |
| 226 | function.append("}"); |
| 227 | return function; |
| 228 | } |
| 229 | |
| 230 | /* The math here is all based on the description in Two_Point_Radial_Gradient, |
| 231 | with one simplification, the coordinate space has been scaled so that |
| 232 | Dr = 1. This means we don't need to scale the entire equation by 1/Dr^2. |
| 233 | */ |
| 234 | static SkString twoPointRadialCode(const SkShader::GradientInfo& info) { |
| 235 | SkScalar dx = info.fPoint[0].fX - info.fPoint[1].fX; |
| 236 | SkScalar dy = info.fPoint[0].fY - info.fPoint[1].fY; |
| 237 | SkScalar sr = info.fRadius[0]; |
| 238 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - SK_Scalar1; |
| 239 | bool posRoot = info.fRadius[1] > info.fRadius[0]; |
| 240 | |
| 241 | // We start with a stack of (x y), copy it and then consume one copy in |
| 242 | // order to calculate b and the other to calculate c. |
| 243 | SkString function("{"); |
| 244 | function.append("2 copy "); |
| 245 | |
| 246 | // Calculate -b and b^2. |
| 247 | function.appendScalar(dy); |
| 248 | function.append(" mul exch "); |
| 249 | function.appendScalar(dx); |
| 250 | function.append(" mul add "); |
| 251 | function.appendScalar(sr); |
| 252 | function.append(" sub 2 mul neg dup dup mul\n"); |
| 253 | |
| 254 | // Calculate c |
| 255 | function.append("4 2 roll dup mul exch dup mul add "); |
| 256 | function.appendScalar(SkScalarMul(sr, sr)); |
| 257 | function.append(" sub\n"); |
| 258 | |
| 259 | // Calculate the determinate |
| 260 | function.appendScalar(SkScalarMul(SkIntToScalar(4), a)); |
| 261 | function.append(" mul sub abs sqrt\n"); |
| 262 | |
| 263 | // And then the final value of t. |
| 264 | if (posRoot) { |
| 265 | function.append("sub "); |
| 266 | } else { |
| 267 | function.append("add "); |
| 268 | } |
| 269 | function.appendScalar(SkScalarMul(SkIntToScalar(2), a)); |
| 270 | function.append(" div\n"); |
| 271 | |
| 272 | tileModeCode(info.fTileMode, &function); |
| 273 | gradientFunctionCode(info, &function); |
| 274 | function.append("}"); |
| 275 | return function; |
| 276 | } |
| 277 | |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 278 | /* Conical gradient shader, based on the Canvas spec for radial gradients |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 279 | See: http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 280 | */ |
| 281 | static SkString twoPointConicalCode(const SkShader::GradientInfo& info) { |
| 282 | SkScalar dx = info.fPoint[1].fX - info.fPoint[0].fX; |
| 283 | SkScalar dy = info.fPoint[1].fY - info.fPoint[0].fY; |
| 284 | SkScalar r0 = info.fRadius[0]; |
| 285 | SkScalar dr = info.fRadius[1] - info.fRadius[0]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 286 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 287 | SkScalarMul(dr, dr); |
| 288 | |
| 289 | // First compute t, if the pixel falls outside the cone, then we'll end |
| 290 | // with 'false' on the stack, otherwise we'll push 'true' with t below it |
| 291 | |
| 292 | // We start with a stack of (x y), copy it and then consume one copy in |
| 293 | // order to calculate b and the other to calculate c. |
| 294 | SkString function("{"); |
| 295 | function.append("2 copy "); |
| 296 | |
| 297 | // Calculate b and b^2; b = -2 * (y * dy + x * dx + r0 * dr). |
| 298 | function.appendScalar(dy); |
| 299 | function.append(" mul exch "); |
| 300 | function.appendScalar(dx); |
| 301 | function.append(" mul add "); |
| 302 | function.appendScalar(SkScalarMul(r0, dr)); |
| 303 | function.append(" add -2 mul dup dup mul\n"); |
| 304 | |
| 305 | // c = x^2 + y^2 + radius0^2 |
| 306 | function.append("4 2 roll dup mul exch dup mul add "); |
| 307 | function.appendScalar(SkScalarMul(r0, r0)); |
| 308 | function.append(" sub dup 4 1 roll\n"); |
| 309 | |
| 310 | // Contents of the stack at this point: c, b, b^2, c |
| 311 | |
| 312 | // if a = 0, then we collapse to a simpler linear case |
| 313 | if (a == 0) { |
| 314 | |
| 315 | // t = -c/b |
| 316 | function.append("pop pop div neg dup "); |
| 317 | |
| 318 | // compute radius(t) |
| 319 | function.appendScalar(dr); |
| 320 | function.append(" mul "); |
| 321 | function.appendScalar(r0); |
| 322 | function.append(" add\n"); |
| 323 | |
| 324 | // if r(t) < 0, then it's outside the cone |
rileya@google.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 325 | function.append("0 lt {pop false} {true} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 326 | |
| 327 | } else { |
| 328 | |
| 329 | // quadratic case: the Canvas spec wants the largest |
| 330 | // root t for which radius(t) > 0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 331 | |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 332 | // compute the discriminant (b^2 - 4ac) |
| 333 | function.appendScalar(SkScalarMul(SkIntToScalar(4), a)); |
| 334 | function.append(" mul sub dup\n"); |
| 335 | |
| 336 | // if d >= 0, proceed |
| 337 | function.append("0 ge {\n"); |
| 338 | |
| 339 | // an intermediate value we'll use to compute the roots: |
| 340 | // q = -0.5 * (b +/- sqrt(d)) |
| 341 | function.append("sqrt exch dup 0 lt {exch -1 mul} if"); |
| 342 | function.append(" add -0.5 mul dup\n"); |
| 343 | |
| 344 | // first root = q / a |
| 345 | function.appendScalar(a); |
| 346 | function.append(" div\n"); |
| 347 | |
| 348 | // second root = c / q |
| 349 | function.append("3 1 roll div\n"); |
| 350 | |
| 351 | // put the larger root on top of the stack |
| 352 | function.append("2 copy gt {exch} if\n"); |
| 353 | |
| 354 | // compute radius(t) for larger root |
| 355 | function.append("dup "); |
| 356 | function.appendScalar(dr); |
| 357 | function.append(" mul "); |
| 358 | function.appendScalar(r0); |
| 359 | function.append(" add\n"); |
| 360 | |
| 361 | // if r(t) > 0, we have our t, pop off the smaller root and we're done |
| 362 | function.append(" 0 gt {exch pop true}\n"); |
| 363 | |
| 364 | // otherwise, throw out the larger one and try the smaller root |
| 365 | function.append("{pop dup\n"); |
| 366 | function.appendScalar(dr); |
| 367 | function.append(" mul "); |
| 368 | function.appendScalar(r0); |
| 369 | function.append(" add\n"); |
| 370 | |
| 371 | // if r(t) < 0, push false, otherwise the smaller root is our t |
rileya@google.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 372 | function.append("0 le {pop false} {true} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 373 | function.append("} ifelse\n"); |
| 374 | |
| 375 | // d < 0, clear the stack and push false |
rileya@google.com | ab2fe82 | 2012-07-10 20:41:38 +0000 | [diff] [blame] | 376 | function.append("} {pop pop pop false} ifelse\n"); |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // if the pixel is in the cone, proceed to compute a color |
| 380 | function.append("{"); |
| 381 | tileModeCode(info.fTileMode, &function); |
| 382 | gradientFunctionCode(info, &function); |
| 383 | |
| 384 | // otherwise, just write black |
| 385 | function.append("} {0 0 0} ifelse }"); |
| 386 | |
| 387 | return function; |
| 388 | } |
| 389 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 390 | static SkString sweepCode(const SkShader::GradientInfo& info) { |
| 391 | SkString function("{exch atan 360 div\n"); |
| 392 | tileModeCode(info.fTileMode, &function); |
| 393 | gradientFunctionCode(info, &function); |
| 394 | function.append("}"); |
| 395 | return function; |
| 396 | } |
| 397 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 398 | class SkPDFShader::State { |
| 399 | public: |
| 400 | SkShader::GradientType fType; |
| 401 | SkShader::GradientInfo fInfo; |
| 402 | SkAutoFree fColorData; |
| 403 | SkMatrix fCanvasTransform; |
| 404 | SkMatrix fShaderTransform; |
| 405 | SkIRect fBBox; |
| 406 | |
| 407 | SkBitmap fImage; |
| 408 | uint32_t fPixelGeneration; |
| 409 | SkShader::TileMode fImageTileModes[2]; |
| 410 | |
| 411 | explicit State(const SkShader& shader, const SkMatrix& canvasTransform, |
| 412 | const SkIRect& bbox); |
| 413 | bool operator==(const State& b) const; |
| 414 | }; |
| 415 | |
| 416 | class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader { |
| 417 | public: |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 418 | explicit SkPDFFunctionShader(SkPDFShader::State* state); |
robertphillips@google.com | 05141c2 | 2012-04-27 12:54:01 +0000 | [diff] [blame] | 419 | virtual ~SkPDFFunctionShader() { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 420 | if (isValid()) { |
| 421 | RemoveShader(this); |
| 422 | } |
| 423 | fResources.unrefAll(); |
| 424 | } |
| 425 | |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 426 | virtual bool isValid() { return fResources.count() > 0; } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 427 | |
| 428 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 429 | GetResourcesHelper(&fResources, resourceList); |
| 430 | } |
| 431 | |
| 432 | private: |
| 433 | static SkPDFObject* RangeObject(); |
| 434 | |
| 435 | SkTDArray<SkPDFObject*> fResources; |
| 436 | SkAutoTDelete<const SkPDFShader::State> fState; |
| 437 | |
| 438 | SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain); |
| 439 | }; |
| 440 | |
| 441 | class SkPDFImageShader : public SkPDFStream, public SkPDFShader { |
| 442 | public: |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 443 | explicit SkPDFImageShader(SkPDFShader::State* state); |
robertphillips@google.com | 05141c2 | 2012-04-27 12:54:01 +0000 | [diff] [blame] | 444 | virtual ~SkPDFImageShader() { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 445 | RemoveShader(this); |
| 446 | fResources.unrefAll(); |
| 447 | } |
| 448 | |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 449 | virtual bool isValid() { return size() > 0; } |
| 450 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 451 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 452 | GetResourcesHelper(&fResources, resourceList); |
| 453 | } |
| 454 | |
| 455 | private: |
| 456 | SkTDArray<SkPDFObject*> fResources; |
| 457 | SkAutoTDelete<const SkPDFShader::State> fState; |
| 458 | }; |
| 459 | |
| 460 | SkPDFShader::SkPDFShader() {} |
| 461 | |
| 462 | // static |
| 463 | void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 464 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 465 | ShaderCanonicalEntry entry(shader, NULL); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 466 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 467 | SkASSERT(index >= 0); |
| 468 | CanonicalShaders().removeShuffle(index); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 472 | SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 473 | const SkMatrix& matrix, |
| 474 | const SkIRect& surfaceBBox) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 475 | SkPDFObject* result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 476 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 477 | SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox)); |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 478 | if (shaderState.get()->fType == SkShader::kNone_GradientType && |
| 479 | shaderState.get()->fImage.isNull()) { |
| 480 | // TODO(vandebo) This drops SKComposeShader on the floor. We could |
| 481 | // handle compose shader by pulling things up to a layer, drawing with |
| 482 | // the first shader, applying the xfer mode and drawing again with the |
| 483 | // second shader, then applying the layer to the original drawing. |
| 484 | return NULL; |
| 485 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 486 | |
| 487 | ShaderCanonicalEntry entry(NULL, shaderState.get()); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 488 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 489 | if (index >= 0) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 490 | result = CanonicalShaders()[index].fPDFShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 491 | result->ref(); |
| 492 | return result; |
| 493 | } |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 494 | |
| 495 | bool valid = false; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 496 | // The PDFShader takes ownership of the shaderSate. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 497 | if (shaderState.get()->fType == SkShader::kNone_GradientType) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 498 | SkPDFImageShader* imageShader = |
| 499 | new SkPDFImageShader(shaderState.detach()); |
| 500 | valid = imageShader->isValid(); |
| 501 | result = imageShader; |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 502 | } else { |
| 503 | SkPDFFunctionShader* functionShader = |
| 504 | new SkPDFFunctionShader(shaderState.detach()); |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 505 | valid = functionShader->isValid(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 506 | result = functionShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 507 | } |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 508 | if (!valid) { |
| 509 | delete result; |
| 510 | return NULL; |
| 511 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 512 | entry.fPDFShader = result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 513 | CanonicalShaders().push(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 514 | return result; // return the reference that came from new. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // static |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 518 | SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 519 | // This initialization is only thread safe with gcc. |
| 520 | static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders; |
| 521 | return gCanonicalShaders; |
| 522 | } |
| 523 | |
| 524 | // static |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 525 | SkBaseMutex& SkPDFShader::CanonicalShadersMutex() { |
| 526 | // This initialization is only thread safe with gcc or when |
| 527 | // POD-style mutex initialization is used. |
| 528 | SK_DECLARE_STATIC_MUTEX(gCanonicalShadersMutex); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 529 | return gCanonicalShadersMutex; |
| 530 | } |
| 531 | |
| 532 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 533 | SkPDFObject* SkPDFFunctionShader::RangeObject() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 534 | // This initialization is only thread safe with gcc. |
| 535 | static SkPDFArray* range = NULL; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 536 | // 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] | 537 | // populate domain. |
| 538 | if (range == NULL) { |
| 539 | range = new SkPDFArray; |
| 540 | range->reserve(6); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 541 | range->appendInt(0); |
| 542 | range->appendInt(1); |
| 543 | range->appendInt(0); |
| 544 | range->appendInt(1); |
| 545 | range->appendInt(0); |
| 546 | range->appendInt(1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 547 | } |
| 548 | return range; |
| 549 | } |
| 550 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 551 | SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) |
| 552 | : SkPDFDict("Pattern"), |
| 553 | fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 554 | SkString (*codeFunction)(const SkShader::GradientInfo& info) = NULL; |
| 555 | SkPoint transformPoints[2]; |
| 556 | |
| 557 | // Depending on the type of the gradient, we want to transform the |
| 558 | // coordinate space in different ways. |
| 559 | const SkShader::GradientInfo* info = &fState.get()->fInfo; |
| 560 | transformPoints[0] = info->fPoint[0]; |
| 561 | transformPoints[1] = info->fPoint[1]; |
| 562 | switch (fState.get()->fType) { |
| 563 | case SkShader::kLinear_GradientType: |
| 564 | codeFunction = &linearCode; |
| 565 | break; |
| 566 | case SkShader::kRadial_GradientType: |
| 567 | transformPoints[1] = transformPoints[0]; |
| 568 | transformPoints[1].fX += info->fRadius[0]; |
| 569 | codeFunction = &radialCode; |
| 570 | break; |
| 571 | case SkShader::kRadial2_GradientType: { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 572 | // Bail out if the radii are the same. Empty fResources signals |
| 573 | // an error and isValid will return false. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 574 | if (info->fRadius[0] == info->fRadius[1]) { |
| 575 | return; |
| 576 | } |
| 577 | transformPoints[1] = transformPoints[0]; |
| 578 | SkScalar dr = info->fRadius[1] - info->fRadius[0]; |
| 579 | transformPoints[1].fX += dr; |
| 580 | codeFunction = &twoPointRadialCode; |
| 581 | break; |
| 582 | } |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 583 | case SkShader::kConical_GradientType: { |
| 584 | transformPoints[1] = transformPoints[0]; |
| 585 | transformPoints[1].fX += SK_Scalar1; |
| 586 | codeFunction = &twoPointConicalCode; |
| 587 | break; |
| 588 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 589 | case SkShader::kSweep_GradientType: |
| 590 | transformPoints[1] = transformPoints[0]; |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 591 | transformPoints[1].fX += SK_Scalar1; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 592 | codeFunction = &sweepCode; |
| 593 | break; |
| 594 | case SkShader::kColor_GradientType: |
| 595 | case SkShader::kNone_GradientType: |
vandebo@chromium.org | 020798a | 2011-12-21 01:49:35 +0000 | [diff] [blame] | 596 | default: |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
| 600 | // Move any scaling (assuming a unit gradient) or translation |
| 601 | // (and rotation for linear gradient), of the final gradient from |
| 602 | // info->fPoints to the matrix (updating bbox appropriately). Now |
| 603 | // the gradient can be drawn on on the unit segment. |
| 604 | SkMatrix mapperMatrix; |
| 605 | unitToPointsMatrix(transformPoints, &mapperMatrix); |
| 606 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
| 607 | finalMatrix.preConcat(mapperMatrix); |
| 608 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
| 609 | SkRect bbox; |
| 610 | bbox.set(fState.get()->fBBox); |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 611 | if (!transformBBox(finalMatrix, &bbox)) { |
| 612 | return; |
| 613 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 614 | |
| 615 | SkRefPtr<SkPDFArray> domain = new SkPDFArray; |
| 616 | domain->unref(); // SkRefPtr and new both took a reference. |
| 617 | domain->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 618 | domain->appendScalar(bbox.fLeft); |
| 619 | domain->appendScalar(bbox.fRight); |
| 620 | domain->appendScalar(bbox.fTop); |
| 621 | domain->appendScalar(bbox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 622 | |
| 623 | SkString functionCode; |
| 624 | // The two point radial gradient further references fState.get()->fInfo |
| 625 | // in translating from x, y coordinates to the t parameter. So, we have |
| 626 | // to transform the points and radii according to the calculated matrix. |
| 627 | if (fState.get()->fType == SkShader::kRadial2_GradientType) { |
| 628 | SkShader::GradientInfo twoPointRadialInfo = *info; |
| 629 | SkMatrix inverseMapperMatrix; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 630 | if (!mapperMatrix.invert(&inverseMapperMatrix)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 631 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 632 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 633 | inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2); |
| 634 | twoPointRadialInfo.fRadius[0] = |
| 635 | inverseMapperMatrix.mapRadius(info->fRadius[0]); |
| 636 | twoPointRadialInfo.fRadius[1] = |
| 637 | inverseMapperMatrix.mapRadius(info->fRadius[1]); |
| 638 | functionCode = codeFunction(twoPointRadialInfo); |
| 639 | } else { |
| 640 | functionCode = codeFunction(*info); |
| 641 | } |
| 642 | |
| 643 | SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get()); |
| 644 | // Pass one reference to fResources, SkRefPtr and new both took a reference. |
| 645 | fResources.push(function.get()); |
| 646 | |
| 647 | SkRefPtr<SkPDFDict> pdfShader = new SkPDFDict; |
| 648 | pdfShader->unref(); // SkRefPtr and new both took a reference. |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 649 | pdfShader->insertInt("ShadingType", 1); |
| 650 | pdfShader->insertName("ColorSpace", "DeviceRGB"); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 651 | pdfShader->insert("Domain", domain.get()); |
| 652 | pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref(); |
| 653 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 654 | insertInt("PatternType", 2); |
| 655 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
| 656 | insert("Shading", pdfShader.get()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 657 | } |
| 658 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 659 | SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 660 | fState.get()->fImage.lockPixels(); |
| 661 | |
| 662 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
| 663 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
| 664 | SkRect surfaceBBox; |
| 665 | surfaceBBox.set(fState.get()->fBBox); |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 666 | if (!transformBBox(finalMatrix, &surfaceBBox)) { |
| 667 | return; |
| 668 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 669 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 670 | SkMatrix unflip; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 671 | unflip.setTranslate(0, SkScalarRoundToScalar(surfaceBBox.height())); |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 672 | unflip.preScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 673 | SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()), |
| 674 | SkScalarRound(surfaceBBox.height())); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 675 | SkPDFDevice pattern(size, size, unflip); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 676 | SkCanvas canvas(&pattern); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 677 | canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop); |
| 678 | finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 679 | |
| 680 | const SkBitmap* image = &fState.get()->fImage; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 681 | SkScalar width = SkIntToScalar(image->width()); |
| 682 | SkScalar height = SkIntToScalar(image->height()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 683 | SkShader::TileMode tileModes[2]; |
| 684 | tileModes[0] = fState.get()->fImageTileModes[0]; |
| 685 | tileModes[1] = fState.get()->fImageTileModes[1]; |
| 686 | |
| 687 | canvas.drawBitmap(*image, 0, 0); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 688 | SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop, |
| 689 | width, height); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 690 | |
| 691 | // Tiling is implied. First we handle mirroring. |
| 692 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 693 | SkMatrix xMirror; |
| 694 | xMirror.setScale(-1, 1); |
| 695 | xMirror.postTranslate(2 * width, 0); |
| 696 | canvas.drawBitmapMatrix(*image, xMirror); |
| 697 | patternBBox.fRight += width; |
| 698 | } |
| 699 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
| 700 | SkMatrix yMirror; |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 701 | yMirror.setScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 702 | yMirror.postTranslate(0, 2 * height); |
| 703 | canvas.drawBitmapMatrix(*image, yMirror); |
| 704 | patternBBox.fBottom += height; |
| 705 | } |
| 706 | if (tileModes[0] == SkShader::kMirror_TileMode && |
| 707 | tileModes[1] == SkShader::kMirror_TileMode) { |
| 708 | SkMatrix mirror; |
| 709 | mirror.setScale(-1, -1); |
| 710 | mirror.postTranslate(2 * width, 2 * height); |
| 711 | canvas.drawBitmapMatrix(*image, mirror); |
| 712 | } |
| 713 | |
| 714 | // Then handle Clamping, which requires expanding the pattern canvas to |
| 715 | // cover the entire surfaceBBox. |
| 716 | |
| 717 | // If both x and y are in clamp mode, we start by filling in the corners. |
| 718 | // (Which are just a rectangles of the corner colors.) |
| 719 | if (tileModes[0] == SkShader::kClamp_TileMode && |
| 720 | tileModes[1] == SkShader::kClamp_TileMode) { |
| 721 | SkPaint paint; |
| 722 | SkRect rect; |
| 723 | rect = SkRect::MakeLTRB(surfaceBBox.fLeft, surfaceBBox.fTop, 0, 0); |
| 724 | if (!rect.isEmpty()) { |
| 725 | paint.setColor(image->getColor(0, 0)); |
| 726 | canvas.drawRect(rect, paint); |
| 727 | } |
| 728 | |
| 729 | rect = SkRect::MakeLTRB(width, surfaceBBox.fTop, surfaceBBox.fRight, 0); |
| 730 | if (!rect.isEmpty()) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 731 | paint.setColor(image->getColor(image->width() - 1, 0)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 732 | canvas.drawRect(rect, paint); |
| 733 | } |
| 734 | |
| 735 | rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight, |
| 736 | surfaceBBox.fBottom); |
| 737 | if (!rect.isEmpty()) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 738 | paint.setColor(image->getColor(image->width() - 1, |
| 739 | image->height() - 1)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 740 | canvas.drawRect(rect, paint); |
| 741 | } |
| 742 | |
| 743 | rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0, |
| 744 | surfaceBBox.fBottom); |
| 745 | if (!rect.isEmpty()) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 746 | paint.setColor(image->getColor(0, image->height() - 1)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 747 | canvas.drawRect(rect, paint); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // Then expand the left, right, top, then bottom. |
| 752 | if (tileModes[0] == SkShader::kClamp_TileMode) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 753 | SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, image->height()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 754 | if (surfaceBBox.fLeft < 0) { |
| 755 | SkBitmap left; |
| 756 | SkAssertResult(image->extractSubset(&left, subset)); |
| 757 | |
| 758 | SkMatrix leftMatrix; |
| 759 | leftMatrix.setScale(-surfaceBBox.fLeft, 1); |
| 760 | leftMatrix.postTranslate(surfaceBBox.fLeft, 0); |
| 761 | canvas.drawBitmapMatrix(left, leftMatrix); |
| 762 | |
| 763 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 764 | leftMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 765 | leftMatrix.postTranslate(0, 2 * height); |
| 766 | canvas.drawBitmapMatrix(left, leftMatrix); |
| 767 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 768 | patternBBox.fLeft = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (surfaceBBox.fRight > width) { |
| 772 | SkBitmap right; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 773 | subset.offset(image->width() - 1, 0); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 774 | SkAssertResult(image->extractSubset(&right, subset)); |
| 775 | |
| 776 | SkMatrix rightMatrix; |
| 777 | rightMatrix.setScale(surfaceBBox.fRight - width, 1); |
| 778 | rightMatrix.postTranslate(width, 0); |
| 779 | canvas.drawBitmapMatrix(right, rightMatrix); |
| 780 | |
| 781 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 782 | rightMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 783 | rightMatrix.postTranslate(0, 2 * height); |
| 784 | canvas.drawBitmapMatrix(right, rightMatrix); |
| 785 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 786 | patternBBox.fRight = surfaceBBox.width(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
| 790 | if (tileModes[1] == SkShader::kClamp_TileMode) { |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 791 | SkIRect subset = SkIRect::MakeXYWH(0, 0, image->width(), 1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 792 | if (surfaceBBox.fTop < 0) { |
| 793 | SkBitmap top; |
| 794 | SkAssertResult(image->extractSubset(&top, subset)); |
| 795 | |
| 796 | SkMatrix topMatrix; |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 797 | topMatrix.setScale(SK_Scalar1, -surfaceBBox.fTop); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 798 | topMatrix.postTranslate(0, surfaceBBox.fTop); |
| 799 | canvas.drawBitmapMatrix(top, topMatrix); |
| 800 | |
| 801 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 802 | topMatrix.postScale(-1, 1); |
| 803 | topMatrix.postTranslate(2 * width, 0); |
| 804 | canvas.drawBitmapMatrix(top, topMatrix); |
| 805 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 806 | patternBBox.fTop = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (surfaceBBox.fBottom > height) { |
| 810 | SkBitmap bottom; |
vandebo@chromium.org | 54ff85c | 2012-03-09 17:18:50 +0000 | [diff] [blame] | 811 | subset.offset(0, image->height() - 1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 812 | SkAssertResult(image->extractSubset(&bottom, subset)); |
| 813 | |
| 814 | SkMatrix bottomMatrix; |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 815 | bottomMatrix.setScale(SK_Scalar1, surfaceBBox.fBottom - height); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 816 | bottomMatrix.postTranslate(0, height); |
| 817 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
| 818 | |
| 819 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 820 | bottomMatrix.postScale(-1, 1); |
| 821 | bottomMatrix.postTranslate(2 * width, 0); |
| 822 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
| 823 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 824 | patternBBox.fBottom = surfaceBBox.height(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray; |
| 829 | patternBBoxArray->unref(); // SkRefPtr and new both took a reference. |
| 830 | patternBBoxArray->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 831 | patternBBoxArray->appendScalar(patternBBox.fLeft); |
| 832 | patternBBoxArray->appendScalar(patternBBox.fTop); |
| 833 | patternBBoxArray->appendScalar(patternBBox.fRight); |
| 834 | patternBBoxArray->appendScalar(patternBBox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 835 | |
| 836 | // Put the canvas into the pattern stream (fContent). |
| 837 | SkRefPtr<SkStream> content = pattern.content(); |
| 838 | content->unref(); // SkRefPtr and content() both took a reference. |
vandebo@chromium.org | 188838c | 2012-03-09 22:16:58 +0000 | [diff] [blame] | 839 | pattern.getResources(&fResources, false); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 840 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 841 | setData(content.get()); |
| 842 | insertName("Type", "Pattern"); |
| 843 | insertInt("PatternType", 1); |
| 844 | insertInt("PaintType", 1); |
| 845 | insertInt("TilingType", 1); |
| 846 | insert("BBox", patternBBoxArray.get()); |
| 847 | insertScalar("XStep", patternBBox.width()); |
| 848 | insertScalar("YStep", patternBBox.height()); |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame] | 849 | insert("Resources", pattern.getResourceDict()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 850 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 851 | |
| 852 | fState.get()->fImage.unlockPixels(); |
| 853 | } |
| 854 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 855 | SkPDFStream* SkPDFFunctionShader::makePSFunction(const SkString& psCode, |
| 856 | SkPDFArray* domain) { |
| 857 | SkAutoDataUnref funcData(SkData::NewWithCopy(psCode.c_str(), |
| 858 | psCode.size())); |
| 859 | SkPDFStream* result = new SkPDFStream(funcData.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 860 | result->insertInt("FunctionType", 4); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 861 | result->insert("Domain", domain); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 862 | result->insert("Range", RangeObject()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 863 | return result; |
| 864 | } |
| 865 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 866 | SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader, |
| 867 | const State* state) |
| 868 | : fPDFShader(pdfShader), |
| 869 | fState(state) { |
| 870 | } |
| 871 | |
| 872 | bool SkPDFShader::ShaderCanonicalEntry::operator==( |
| 873 | const ShaderCanonicalEntry& b) const { |
| 874 | return fPDFShader == b.fPDFShader || |
| 875 | (fState != NULL && b.fState != NULL && *fState == *b.fState); |
| 876 | } |
| 877 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 878 | bool SkPDFShader::State::operator==(const SkPDFShader::State& b) const { |
| 879 | if (fType != b.fType || |
| 880 | fCanvasTransform != b.fCanvasTransform || |
| 881 | fShaderTransform != b.fShaderTransform || |
| 882 | fBBox != b.fBBox) { |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | if (fType == SkShader::kNone_GradientType) { |
| 887 | if (fPixelGeneration != b.fPixelGeneration || |
| 888 | fPixelGeneration == 0 || |
| 889 | fImageTileModes[0] != b.fImageTileModes[0] || |
| 890 | fImageTileModes[1] != b.fImageTileModes[1]) { |
| 891 | return false; |
| 892 | } |
| 893 | } else { |
| 894 | if (fInfo.fColorCount != b.fInfo.fColorCount || |
| 895 | memcmp(fInfo.fColors, b.fInfo.fColors, |
| 896 | sizeof(SkColor) * fInfo.fColorCount) != 0 || |
| 897 | memcmp(fInfo.fColorOffsets, b.fInfo.fColorOffsets, |
| 898 | sizeof(SkScalar) * fInfo.fColorCount) != 0 || |
| 899 | fInfo.fPoint[0] != b.fInfo.fPoint[0] || |
| 900 | fInfo.fTileMode != b.fInfo.fTileMode) { |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | switch (fType) { |
| 905 | case SkShader::kLinear_GradientType: |
| 906 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1]) { |
| 907 | return false; |
| 908 | } |
| 909 | break; |
| 910 | case SkShader::kRadial_GradientType: |
| 911 | if (fInfo.fRadius[0] != b.fInfo.fRadius[0]) { |
| 912 | return false; |
| 913 | } |
| 914 | break; |
| 915 | case SkShader::kRadial2_GradientType: |
reed@google.com | 4908533 | 2012-06-11 18:54:07 +0000 | [diff] [blame] | 916 | case SkShader::kConical_GradientType: |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 917 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1] || |
| 918 | fInfo.fRadius[0] != b.fInfo.fRadius[0] || |
| 919 | fInfo.fRadius[1] != b.fInfo.fRadius[1]) { |
| 920 | return false; |
| 921 | } |
| 922 | break; |
| 923 | case SkShader::kSweep_GradientType: |
| 924 | case SkShader::kNone_GradientType: |
| 925 | case SkShader::kColor_GradientType: |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | SkPDFShader::State::State(const SkShader& shader, |
| 933 | const SkMatrix& canvasTransform, const SkIRect& bbox) |
| 934 | : fCanvasTransform(canvasTransform), |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 935 | fBBox(bbox), |
| 936 | fPixelGeneration(0) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 937 | fInfo.fColorCount = 0; |
| 938 | fInfo.fColors = NULL; |
| 939 | fInfo.fColorOffsets = NULL; |
bsalomon@google.com | f94b3a4 | 2012-10-31 18:09:01 +0000 | [diff] [blame^] | 940 | fShaderTransform = shader.getLocalMatrix(); |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 941 | fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 942 | |
| 943 | fType = shader.asAGradient(&fInfo); |
| 944 | |
| 945 | if (fType == SkShader::kNone_GradientType) { |
| 946 | SkShader::BitmapType bitmapType; |
| 947 | SkMatrix matrix; |
rileya@google.com | 91f319c | 2012-07-25 17:18:31 +0000 | [diff] [blame] | 948 | bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 949 | if (bitmapType != SkShader::kDefault_BitmapType) { |
| 950 | fImage.reset(); |
| 951 | return; |
| 952 | } |
| 953 | SkASSERT(matrix.isIdentity()); |
| 954 | fPixelGeneration = fImage.getGenerationID(); |
| 955 | } else { |
| 956 | fColorData.set(sk_malloc_throw( |
| 957 | fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 958 | fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
vandebo@chromium.org | 020798a | 2011-12-21 01:49:35 +0000 | [diff] [blame] | 959 | fInfo.fColorOffsets = |
| 960 | reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 961 | shader.asAGradient(&fInfo); |
| 962 | } |
| 963 | } |