Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkPDFShader.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "SkData.h" |
michael@0 | 13 | #include "SkPDFCatalog.h" |
michael@0 | 14 | #include "SkPDFDevice.h" |
michael@0 | 15 | #include "SkPDFFormXObject.h" |
michael@0 | 16 | #include "SkPDFGraphicState.h" |
michael@0 | 17 | #include "SkPDFResourceDict.h" |
michael@0 | 18 | #include "SkPDFUtils.h" |
michael@0 | 19 | #include "SkScalar.h" |
michael@0 | 20 | #include "SkStream.h" |
michael@0 | 21 | #include "SkTemplates.h" |
michael@0 | 22 | #include "SkThread.h" |
michael@0 | 23 | #include "SkTSet.h" |
michael@0 | 24 | #include "SkTypes.h" |
michael@0 | 25 | |
michael@0 | 26 | static bool inverseTransformBBox(const SkMatrix& matrix, SkRect* bbox) { |
michael@0 | 27 | SkMatrix inverse; |
michael@0 | 28 | if (!matrix.invert(&inverse)) { |
michael@0 | 29 | return false; |
michael@0 | 30 | } |
michael@0 | 31 | inverse.mapRect(bbox); |
michael@0 | 32 | return true; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | static void unitToPointsMatrix(const SkPoint pts[2], SkMatrix* matrix) { |
michael@0 | 36 | SkVector vec = pts[1] - pts[0]; |
michael@0 | 37 | SkScalar mag = vec.length(); |
michael@0 | 38 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
michael@0 | 39 | |
michael@0 | 40 | vec.scale(inv); |
michael@0 | 41 | matrix->setSinCos(vec.fY, vec.fX); |
michael@0 | 42 | matrix->preScale(mag, mag); |
michael@0 | 43 | matrix->postTranslate(pts[0].fX, pts[0].fY); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /* Assumes t + startOffset is on the stack and does a linear interpolation on t |
michael@0 | 47 | between startOffset and endOffset from prevColor to curColor (for each color |
michael@0 | 48 | component), leaving the result in component order on the stack. It assumes |
michael@0 | 49 | there are always 3 components per color. |
michael@0 | 50 | @param range endOffset - startOffset |
michael@0 | 51 | @param curColor[components] The current color components. |
michael@0 | 52 | @param prevColor[components] The previous color components. |
michael@0 | 53 | @param result The result ps function. |
michael@0 | 54 | */ |
michael@0 | 55 | static void interpolateColorCode(SkScalar range, SkScalar* curColor, |
michael@0 | 56 | SkScalar* prevColor, SkString* result) { |
michael@0 | 57 | SkASSERT(range != SkIntToScalar(0)); |
michael@0 | 58 | static const int kColorComponents = 3; |
michael@0 | 59 | |
michael@0 | 60 | // Figure out how to scale each color component. |
michael@0 | 61 | SkScalar multiplier[kColorComponents]; |
michael@0 | 62 | for (int i = 0; i < kColorComponents; i++) { |
michael@0 | 63 | multiplier[i] = SkScalarDiv(curColor[i] - prevColor[i], range); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Calculate when we no longer need to keep a copy of the input parameter t. |
michael@0 | 67 | // If the last component to use t is i, then dupInput[0..i - 1] = true |
michael@0 | 68 | // and dupInput[i .. components] = false. |
michael@0 | 69 | bool dupInput[kColorComponents]; |
michael@0 | 70 | dupInput[kColorComponents - 1] = false; |
michael@0 | 71 | for (int i = kColorComponents - 2; i >= 0; i--) { |
michael@0 | 72 | dupInput[i] = dupInput[i + 1] || multiplier[i + 1] != 0; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | if (!dupInput[0] && multiplier[0] == 0) { |
michael@0 | 76 | result->append("pop "); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | for (int i = 0; i < kColorComponents; i++) { |
michael@0 | 80 | // If the next components needs t and this component will consume a |
michael@0 | 81 | // copy, make another copy. |
michael@0 | 82 | if (dupInput[i] && multiplier[i] != 0) { |
michael@0 | 83 | result->append("dup "); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | if (multiplier[i] == 0) { |
michael@0 | 87 | result->appendScalar(prevColor[i]); |
michael@0 | 88 | result->append(" "); |
michael@0 | 89 | } else { |
michael@0 | 90 | if (multiplier[i] != 1) { |
michael@0 | 91 | result->appendScalar(multiplier[i]); |
michael@0 | 92 | result->append(" mul "); |
michael@0 | 93 | } |
michael@0 | 94 | if (prevColor[i] != 0) { |
michael@0 | 95 | result->appendScalar(prevColor[i]); |
michael@0 | 96 | result->append(" add "); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | if (dupInput[i]) { |
michael@0 | 101 | result->append("exch\n"); |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | /* Generate Type 4 function code to map t=[0,1) to the passed gradient, |
michael@0 | 107 | clamping at the edges of the range. The generated code will be of the form: |
michael@0 | 108 | if (t < 0) { |
michael@0 | 109 | return colorData[0][r,g,b]; |
michael@0 | 110 | } else { |
michael@0 | 111 | if (t < info.fColorOffsets[1]) { |
michael@0 | 112 | return linearinterpolation(colorData[0][r,g,b], |
michael@0 | 113 | colorData[1][r,g,b]); |
michael@0 | 114 | } else { |
michael@0 | 115 | if (t < info.fColorOffsets[2]) { |
michael@0 | 116 | return linearinterpolation(colorData[1][r,g,b], |
michael@0 | 117 | colorData[2][r,g,b]); |
michael@0 | 118 | } else { |
michael@0 | 119 | |
michael@0 | 120 | ... } else { |
michael@0 | 121 | return colorData[info.fColorCount - 1][r,g,b]; |
michael@0 | 122 | } |
michael@0 | 123 | ... |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | */ |
michael@0 | 127 | static void gradientFunctionCode(const SkShader::GradientInfo& info, |
michael@0 | 128 | SkString* result) { |
michael@0 | 129 | /* We want to linearly interpolate from the previous color to the next. |
michael@0 | 130 | Scale the colors from 0..255 to 0..1 and determine the multipliers |
michael@0 | 131 | for interpolation. |
michael@0 | 132 | C{r,g,b}(t, section) = t - offset_(section-1) + t * Multiplier{r,g,b}. |
michael@0 | 133 | */ |
michael@0 | 134 | static const int kColorComponents = 3; |
michael@0 | 135 | typedef SkScalar ColorTuple[kColorComponents]; |
michael@0 | 136 | SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount); |
michael@0 | 137 | ColorTuple *colorData = colorDataAlloc.get(); |
michael@0 | 138 | const SkScalar scale = SkScalarInvert(SkIntToScalar(255)); |
michael@0 | 139 | for (int i = 0; i < info.fColorCount; i++) { |
michael@0 | 140 | colorData[i][0] = SkScalarMul(SkColorGetR(info.fColors[i]), scale); |
michael@0 | 141 | colorData[i][1] = SkScalarMul(SkColorGetG(info.fColors[i]), scale); |
michael@0 | 142 | colorData[i][2] = SkScalarMul(SkColorGetB(info.fColors[i]), scale); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // Clamp the initial color. |
michael@0 | 146 | result->append("dup 0 le {pop "); |
michael@0 | 147 | result->appendScalar(colorData[0][0]); |
michael@0 | 148 | result->append(" "); |
michael@0 | 149 | result->appendScalar(colorData[0][1]); |
michael@0 | 150 | result->append(" "); |
michael@0 | 151 | result->appendScalar(colorData[0][2]); |
michael@0 | 152 | result->append(" }\n"); |
michael@0 | 153 | |
michael@0 | 154 | // The gradient colors. |
michael@0 | 155 | int gradients = 0; |
michael@0 | 156 | for (int i = 1 ; i < info.fColorCount; i++) { |
michael@0 | 157 | if (info.fColorOffsets[i] == info.fColorOffsets[i - 1]) { |
michael@0 | 158 | continue; |
michael@0 | 159 | } |
michael@0 | 160 | gradients++; |
michael@0 | 161 | |
michael@0 | 162 | result->append("{dup "); |
michael@0 | 163 | result->appendScalar(info.fColorOffsets[i]); |
michael@0 | 164 | result->append(" le {"); |
michael@0 | 165 | if (info.fColorOffsets[i - 1] != 0) { |
michael@0 | 166 | result->appendScalar(info.fColorOffsets[i - 1]); |
michael@0 | 167 | result->append(" sub\n"); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1], |
michael@0 | 171 | colorData[i], colorData[i - 1], result); |
michael@0 | 172 | result->append("}\n"); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | // Clamp the final color. |
michael@0 | 176 | result->append("{pop "); |
michael@0 | 177 | result->appendScalar(colorData[info.fColorCount - 1][0]); |
michael@0 | 178 | result->append(" "); |
michael@0 | 179 | result->appendScalar(colorData[info.fColorCount - 1][1]); |
michael@0 | 180 | result->append(" "); |
michael@0 | 181 | result->appendScalar(colorData[info.fColorCount - 1][2]); |
michael@0 | 182 | |
michael@0 | 183 | for (int i = 0 ; i < gradients + 1; i++) { |
michael@0 | 184 | result->append("} ifelse\n"); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | /* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */ |
michael@0 | 189 | static void tileModeCode(SkShader::TileMode mode, SkString* result) { |
michael@0 | 190 | if (mode == SkShader::kRepeat_TileMode) { |
michael@0 | 191 | result->append("dup truncate sub\n"); // Get the fractional part. |
michael@0 | 192 | result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1) |
michael@0 | 193 | return; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | if (mode == SkShader::kMirror_TileMode) { |
michael@0 | 197 | // Map t mod 2 into [0, 1, 1, 0]. |
michael@0 | 198 | // Code Stack |
michael@0 | 199 | result->append("abs " // Map negative to positive. |
michael@0 | 200 | "dup " // t.s t.s |
michael@0 | 201 | "truncate " // t.s t |
michael@0 | 202 | "dup " // t.s t t |
michael@0 | 203 | "cvi " // t.s t T |
michael@0 | 204 | "2 mod " // t.s t (i mod 2) |
michael@0 | 205 | "1 eq " // t.s t true|false |
michael@0 | 206 | "3 1 roll " // true|false t.s t |
michael@0 | 207 | "sub " // true|false 0.s |
michael@0 | 208 | "exch " // 0.s true|false |
michael@0 | 209 | "{1 exch sub} if\n"); // 1 - 0.s|0.s |
michael@0 | 210 | } |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | /** |
michael@0 | 214 | * Returns PS function code that applies inverse perspective |
michael@0 | 215 | * to a x, y point. |
michael@0 | 216 | * The function assumes that the stack has at least two elements, |
michael@0 | 217 | * and that the top 2 elements are numeric values. |
michael@0 | 218 | * After executing this code on a PS stack, the last 2 elements are updated |
michael@0 | 219 | * while the rest of the stack is preserved intact. |
michael@0 | 220 | * inversePerspectiveMatrix is the inverse perspective matrix. |
michael@0 | 221 | */ |
michael@0 | 222 | static SkString apply_perspective_to_coordinates( |
michael@0 | 223 | const SkMatrix& inversePerspectiveMatrix) { |
michael@0 | 224 | SkString code; |
michael@0 | 225 | if (!inversePerspectiveMatrix.hasPerspective()) { |
michael@0 | 226 | return code; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | // Perspective matrix should be: |
michael@0 | 230 | // 1 0 0 |
michael@0 | 231 | // 0 1 0 |
michael@0 | 232 | // p0 p1 p2 |
michael@0 | 233 | |
michael@0 | 234 | const SkScalar p0 = inversePerspectiveMatrix[SkMatrix::kMPersp0]; |
michael@0 | 235 | const SkScalar p1 = inversePerspectiveMatrix[SkMatrix::kMPersp1]; |
michael@0 | 236 | const SkScalar p2 = inversePerspectiveMatrix[SkMatrix::kMPersp2]; |
michael@0 | 237 | |
michael@0 | 238 | // y = y / (p2 + p0 x + p1 y) |
michael@0 | 239 | // x = x / (p2 + p0 x + p1 y) |
michael@0 | 240 | |
michael@0 | 241 | // Input on stack: x y |
michael@0 | 242 | code.append(" dup "); // x y y |
michael@0 | 243 | code.appendScalar(p1); // x y y p1 |
michael@0 | 244 | code.append(" mul " // x y y*p1 |
michael@0 | 245 | " 2 index "); // x y y*p1 x |
michael@0 | 246 | code.appendScalar(p0); // x y y p1 x p0 |
michael@0 | 247 | code.append(" mul "); // x y y*p1 x*p0 |
michael@0 | 248 | code.appendScalar(p2); // x y y p1 x*p0 p2 |
michael@0 | 249 | code.append(" add " // x y y*p1 x*p0+p2 |
michael@0 | 250 | "add " // x y y*p1+x*p0+p2 |
michael@0 | 251 | "3 1 roll " // y*p1+x*p0+p2 x y |
michael@0 | 252 | "2 index " // z x y y*p1+x*p0+p2 |
michael@0 | 253 | "div " // y*p1+x*p0+p2 x y/(y*p1+x*p0+p2) |
michael@0 | 254 | "3 1 roll " // y/(y*p1+x*p0+p2) y*p1+x*p0+p2 x |
michael@0 | 255 | "exch " // y/(y*p1+x*p0+p2) x y*p1+x*p0+p2 |
michael@0 | 256 | "div " // y/(y*p1+x*p0+p2) x/(y*p1+x*p0+p2) |
michael@0 | 257 | "exch\n"); // x/(y*p1+x*p0+p2) y/(y*p1+x*p0+p2) |
michael@0 | 258 | return code; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | static SkString linearCode(const SkShader::GradientInfo& info, |
michael@0 | 262 | const SkMatrix& perspectiveRemover) { |
michael@0 | 263 | SkString function("{"); |
michael@0 | 264 | |
michael@0 | 265 | function.append(apply_perspective_to_coordinates(perspectiveRemover)); |
michael@0 | 266 | |
michael@0 | 267 | function.append("pop\n"); // Just ditch the y value. |
michael@0 | 268 | tileModeCode(info.fTileMode, &function); |
michael@0 | 269 | gradientFunctionCode(info, &function); |
michael@0 | 270 | function.append("}"); |
michael@0 | 271 | return function; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | static SkString radialCode(const SkShader::GradientInfo& info, |
michael@0 | 275 | const SkMatrix& perspectiveRemover) { |
michael@0 | 276 | SkString function("{"); |
michael@0 | 277 | |
michael@0 | 278 | function.append(apply_perspective_to_coordinates(perspectiveRemover)); |
michael@0 | 279 | |
michael@0 | 280 | // Find the distance from the origin. |
michael@0 | 281 | function.append("dup " // x y y |
michael@0 | 282 | "mul " // x y^2 |
michael@0 | 283 | "exch " // y^2 x |
michael@0 | 284 | "dup " // y^2 x x |
michael@0 | 285 | "mul " // y^2 x^2 |
michael@0 | 286 | "add " // y^2+x^2 |
michael@0 | 287 | "sqrt\n"); // sqrt(y^2+x^2) |
michael@0 | 288 | |
michael@0 | 289 | tileModeCode(info.fTileMode, &function); |
michael@0 | 290 | gradientFunctionCode(info, &function); |
michael@0 | 291 | function.append("}"); |
michael@0 | 292 | return function; |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | /* The math here is all based on the description in Two_Point_Radial_Gradient, |
michael@0 | 296 | with one simplification, the coordinate space has been scaled so that |
michael@0 | 297 | Dr = 1. This means we don't need to scale the entire equation by 1/Dr^2. |
michael@0 | 298 | */ |
michael@0 | 299 | static SkString twoPointRadialCode(const SkShader::GradientInfo& info, |
michael@0 | 300 | const SkMatrix& perspectiveRemover) { |
michael@0 | 301 | SkScalar dx = info.fPoint[0].fX - info.fPoint[1].fX; |
michael@0 | 302 | SkScalar dy = info.fPoint[0].fY - info.fPoint[1].fY; |
michael@0 | 303 | SkScalar sr = info.fRadius[0]; |
michael@0 | 304 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - SK_Scalar1; |
michael@0 | 305 | bool posRoot = info.fRadius[1] > info.fRadius[0]; |
michael@0 | 306 | |
michael@0 | 307 | // We start with a stack of (x y), copy it and then consume one copy in |
michael@0 | 308 | // order to calculate b and the other to calculate c. |
michael@0 | 309 | SkString function("{"); |
michael@0 | 310 | |
michael@0 | 311 | function.append(apply_perspective_to_coordinates(perspectiveRemover)); |
michael@0 | 312 | |
michael@0 | 313 | function.append("2 copy "); |
michael@0 | 314 | |
michael@0 | 315 | // Calculate -b and b^2. |
michael@0 | 316 | function.appendScalar(dy); |
michael@0 | 317 | function.append(" mul exch "); |
michael@0 | 318 | function.appendScalar(dx); |
michael@0 | 319 | function.append(" mul add "); |
michael@0 | 320 | function.appendScalar(sr); |
michael@0 | 321 | function.append(" sub 2 mul neg dup dup mul\n"); |
michael@0 | 322 | |
michael@0 | 323 | // Calculate c |
michael@0 | 324 | function.append("4 2 roll dup mul exch dup mul add "); |
michael@0 | 325 | function.appendScalar(SkScalarMul(sr, sr)); |
michael@0 | 326 | function.append(" sub\n"); |
michael@0 | 327 | |
michael@0 | 328 | // Calculate the determinate |
michael@0 | 329 | function.appendScalar(SkScalarMul(SkIntToScalar(4), a)); |
michael@0 | 330 | function.append(" mul sub abs sqrt\n"); |
michael@0 | 331 | |
michael@0 | 332 | // And then the final value of t. |
michael@0 | 333 | if (posRoot) { |
michael@0 | 334 | function.append("sub "); |
michael@0 | 335 | } else { |
michael@0 | 336 | function.append("add "); |
michael@0 | 337 | } |
michael@0 | 338 | function.appendScalar(SkScalarMul(SkIntToScalar(2), a)); |
michael@0 | 339 | function.append(" div\n"); |
michael@0 | 340 | |
michael@0 | 341 | tileModeCode(info.fTileMode, &function); |
michael@0 | 342 | gradientFunctionCode(info, &function); |
michael@0 | 343 | function.append("}"); |
michael@0 | 344 | return function; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | /* Conical gradient shader, based on the Canvas spec for radial gradients |
michael@0 | 348 | See: http://www.w3.org/TR/2dcontext/#dom-context-2d-createradialgradient |
michael@0 | 349 | */ |
michael@0 | 350 | static SkString twoPointConicalCode(const SkShader::GradientInfo& info, |
michael@0 | 351 | const SkMatrix& perspectiveRemover) { |
michael@0 | 352 | SkScalar dx = info.fPoint[1].fX - info.fPoint[0].fX; |
michael@0 | 353 | SkScalar dy = info.fPoint[1].fY - info.fPoint[0].fY; |
michael@0 | 354 | SkScalar r0 = info.fRadius[0]; |
michael@0 | 355 | SkScalar dr = info.fRadius[1] - info.fRadius[0]; |
michael@0 | 356 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - |
michael@0 | 357 | SkScalarMul(dr, dr); |
michael@0 | 358 | |
michael@0 | 359 | // First compute t, if the pixel falls outside the cone, then we'll end |
michael@0 | 360 | // with 'false' on the stack, otherwise we'll push 'true' with t below it |
michael@0 | 361 | |
michael@0 | 362 | // We start with a stack of (x y), copy it and then consume one copy in |
michael@0 | 363 | // order to calculate b and the other to calculate c. |
michael@0 | 364 | SkString function("{"); |
michael@0 | 365 | |
michael@0 | 366 | function.append(apply_perspective_to_coordinates(perspectiveRemover)); |
michael@0 | 367 | |
michael@0 | 368 | function.append("2 copy "); |
michael@0 | 369 | |
michael@0 | 370 | // Calculate b and b^2; b = -2 * (y * dy + x * dx + r0 * dr). |
michael@0 | 371 | function.appendScalar(dy); |
michael@0 | 372 | function.append(" mul exch "); |
michael@0 | 373 | function.appendScalar(dx); |
michael@0 | 374 | function.append(" mul add "); |
michael@0 | 375 | function.appendScalar(SkScalarMul(r0, dr)); |
michael@0 | 376 | function.append(" add -2 mul dup dup mul\n"); |
michael@0 | 377 | |
michael@0 | 378 | // c = x^2 + y^2 + radius0^2 |
michael@0 | 379 | function.append("4 2 roll dup mul exch dup mul add "); |
michael@0 | 380 | function.appendScalar(SkScalarMul(r0, r0)); |
michael@0 | 381 | function.append(" sub dup 4 1 roll\n"); |
michael@0 | 382 | |
michael@0 | 383 | // Contents of the stack at this point: c, b, b^2, c |
michael@0 | 384 | |
michael@0 | 385 | // if a = 0, then we collapse to a simpler linear case |
michael@0 | 386 | if (a == 0) { |
michael@0 | 387 | |
michael@0 | 388 | // t = -c/b |
michael@0 | 389 | function.append("pop pop div neg dup "); |
michael@0 | 390 | |
michael@0 | 391 | // compute radius(t) |
michael@0 | 392 | function.appendScalar(dr); |
michael@0 | 393 | function.append(" mul "); |
michael@0 | 394 | function.appendScalar(r0); |
michael@0 | 395 | function.append(" add\n"); |
michael@0 | 396 | |
michael@0 | 397 | // if r(t) < 0, then it's outside the cone |
michael@0 | 398 | function.append("0 lt {pop false} {true} ifelse\n"); |
michael@0 | 399 | |
michael@0 | 400 | } else { |
michael@0 | 401 | |
michael@0 | 402 | // quadratic case: the Canvas spec wants the largest |
michael@0 | 403 | // root t for which radius(t) > 0 |
michael@0 | 404 | |
michael@0 | 405 | // compute the discriminant (b^2 - 4ac) |
michael@0 | 406 | function.appendScalar(SkScalarMul(SkIntToScalar(4), a)); |
michael@0 | 407 | function.append(" mul sub dup\n"); |
michael@0 | 408 | |
michael@0 | 409 | // if d >= 0, proceed |
michael@0 | 410 | function.append("0 ge {\n"); |
michael@0 | 411 | |
michael@0 | 412 | // an intermediate value we'll use to compute the roots: |
michael@0 | 413 | // q = -0.5 * (b +/- sqrt(d)) |
michael@0 | 414 | function.append("sqrt exch dup 0 lt {exch -1 mul} if"); |
michael@0 | 415 | function.append(" add -0.5 mul dup\n"); |
michael@0 | 416 | |
michael@0 | 417 | // first root = q / a |
michael@0 | 418 | function.appendScalar(a); |
michael@0 | 419 | function.append(" div\n"); |
michael@0 | 420 | |
michael@0 | 421 | // second root = c / q |
michael@0 | 422 | function.append("3 1 roll div\n"); |
michael@0 | 423 | |
michael@0 | 424 | // put the larger root on top of the stack |
michael@0 | 425 | function.append("2 copy gt {exch} if\n"); |
michael@0 | 426 | |
michael@0 | 427 | // compute radius(t) for larger root |
michael@0 | 428 | function.append("dup "); |
michael@0 | 429 | function.appendScalar(dr); |
michael@0 | 430 | function.append(" mul "); |
michael@0 | 431 | function.appendScalar(r0); |
michael@0 | 432 | function.append(" add\n"); |
michael@0 | 433 | |
michael@0 | 434 | // if r(t) > 0, we have our t, pop off the smaller root and we're done |
michael@0 | 435 | function.append(" 0 gt {exch pop true}\n"); |
michael@0 | 436 | |
michael@0 | 437 | // otherwise, throw out the larger one and try the smaller root |
michael@0 | 438 | function.append("{pop dup\n"); |
michael@0 | 439 | function.appendScalar(dr); |
michael@0 | 440 | function.append(" mul "); |
michael@0 | 441 | function.appendScalar(r0); |
michael@0 | 442 | function.append(" add\n"); |
michael@0 | 443 | |
michael@0 | 444 | // if r(t) < 0, push false, otherwise the smaller root is our t |
michael@0 | 445 | function.append("0 le {pop false} {true} ifelse\n"); |
michael@0 | 446 | function.append("} ifelse\n"); |
michael@0 | 447 | |
michael@0 | 448 | // d < 0, clear the stack and push false |
michael@0 | 449 | function.append("} {pop pop pop false} ifelse\n"); |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | // if the pixel is in the cone, proceed to compute a color |
michael@0 | 453 | function.append("{"); |
michael@0 | 454 | tileModeCode(info.fTileMode, &function); |
michael@0 | 455 | gradientFunctionCode(info, &function); |
michael@0 | 456 | |
michael@0 | 457 | // otherwise, just write black |
michael@0 | 458 | function.append("} {0 0 0} ifelse }"); |
michael@0 | 459 | |
michael@0 | 460 | return function; |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | static SkString sweepCode(const SkShader::GradientInfo& info, |
michael@0 | 464 | const SkMatrix& perspectiveRemover) { |
michael@0 | 465 | SkString function("{exch atan 360 div\n"); |
michael@0 | 466 | tileModeCode(info.fTileMode, &function); |
michael@0 | 467 | gradientFunctionCode(info, &function); |
michael@0 | 468 | function.append("}"); |
michael@0 | 469 | return function; |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | class SkPDFShader::State { |
michael@0 | 473 | public: |
michael@0 | 474 | SkShader::GradientType fType; |
michael@0 | 475 | SkShader::GradientInfo fInfo; |
michael@0 | 476 | SkAutoFree fColorData; // This provides storage for arrays in fInfo. |
michael@0 | 477 | SkMatrix fCanvasTransform; |
michael@0 | 478 | SkMatrix fShaderTransform; |
michael@0 | 479 | SkIRect fBBox; |
michael@0 | 480 | |
michael@0 | 481 | SkBitmap fImage; |
michael@0 | 482 | uint32_t fPixelGeneration; |
michael@0 | 483 | SkShader::TileMode fImageTileModes[2]; |
michael@0 | 484 | |
michael@0 | 485 | State(const SkShader& shader, const SkMatrix& canvasTransform, |
michael@0 | 486 | const SkIRect& bbox); |
michael@0 | 487 | |
michael@0 | 488 | bool operator==(const State& b) const; |
michael@0 | 489 | |
michael@0 | 490 | SkPDFShader::State* CreateAlphaToLuminosityState() const; |
michael@0 | 491 | SkPDFShader::State* CreateOpaqueState() const; |
michael@0 | 492 | |
michael@0 | 493 | bool GradientHasAlpha() const; |
michael@0 | 494 | |
michael@0 | 495 | private: |
michael@0 | 496 | State(const State& other); |
michael@0 | 497 | State operator=(const State& rhs); |
michael@0 | 498 | void AllocateGradientInfoStorage(); |
michael@0 | 499 | }; |
michael@0 | 500 | |
michael@0 | 501 | class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader { |
michael@0 | 502 | SK_DECLARE_INST_COUNT(SkPDFFunctionShader) |
michael@0 | 503 | public: |
michael@0 | 504 | explicit SkPDFFunctionShader(SkPDFShader::State* state); |
michael@0 | 505 | virtual ~SkPDFFunctionShader() { |
michael@0 | 506 | if (isValid()) { |
michael@0 | 507 | RemoveShader(this); |
michael@0 | 508 | } |
michael@0 | 509 | fResources.unrefAll(); |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | virtual bool isValid() { return fResources.count() > 0; } |
michael@0 | 513 | |
michael@0 | 514 | void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
michael@0 | 515 | SkTSet<SkPDFObject*>* newResourceObjects) { |
michael@0 | 516 | GetResourcesHelper(&fResources, |
michael@0 | 517 | knownResourceObjects, |
michael@0 | 518 | newResourceObjects); |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | private: |
michael@0 | 522 | static SkPDFObject* RangeObject(); |
michael@0 | 523 | |
michael@0 | 524 | SkTDArray<SkPDFObject*> fResources; |
michael@0 | 525 | SkAutoTDelete<const SkPDFShader::State> fState; |
michael@0 | 526 | |
michael@0 | 527 | SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain); |
michael@0 | 528 | typedef SkPDFDict INHERITED; |
michael@0 | 529 | }; |
michael@0 | 530 | |
michael@0 | 531 | /** |
michael@0 | 532 | * A shader for PDF gradients. This encapsulates the function shader |
michael@0 | 533 | * inside a tiling pattern while providing a common pattern interface. |
michael@0 | 534 | * The encapsulation allows the use of a SMask for transparency gradients. |
michael@0 | 535 | */ |
michael@0 | 536 | class SkPDFAlphaFunctionShader : public SkPDFStream, public SkPDFShader { |
michael@0 | 537 | public: |
michael@0 | 538 | explicit SkPDFAlphaFunctionShader(SkPDFShader::State* state); |
michael@0 | 539 | virtual ~SkPDFAlphaFunctionShader() { |
michael@0 | 540 | if (isValid()) { |
michael@0 | 541 | RemoveShader(this); |
michael@0 | 542 | } |
michael@0 | 543 | } |
michael@0 | 544 | |
michael@0 | 545 | virtual bool isValid() { |
michael@0 | 546 | return fColorShader.get() != NULL; |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | private: |
michael@0 | 550 | SkAutoTDelete<const SkPDFShader::State> fState; |
michael@0 | 551 | |
michael@0 | 552 | SkPDFGraphicState* CreateSMaskGraphicState(); |
michael@0 | 553 | |
michael@0 | 554 | void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
michael@0 | 555 | SkTSet<SkPDFObject*>* newResourceObjects) { |
michael@0 | 556 | fResourceDict->getReferencedResources(knownResourceObjects, |
michael@0 | 557 | newResourceObjects, |
michael@0 | 558 | true); |
michael@0 | 559 | } |
michael@0 | 560 | |
michael@0 | 561 | SkAutoTUnref<SkPDFObject> fColorShader; |
michael@0 | 562 | SkAutoTUnref<SkPDFResourceDict> fResourceDict; |
michael@0 | 563 | }; |
michael@0 | 564 | |
michael@0 | 565 | class SkPDFImageShader : public SkPDFStream, public SkPDFShader { |
michael@0 | 566 | public: |
michael@0 | 567 | explicit SkPDFImageShader(SkPDFShader::State* state); |
michael@0 | 568 | virtual ~SkPDFImageShader() { |
michael@0 | 569 | if (isValid()) { |
michael@0 | 570 | RemoveShader(this); |
michael@0 | 571 | } |
michael@0 | 572 | fResources.unrefAll(); |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | virtual bool isValid() { return size() > 0; } |
michael@0 | 576 | |
michael@0 | 577 | void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
michael@0 | 578 | SkTSet<SkPDFObject*>* newResourceObjects) { |
michael@0 | 579 | GetResourcesHelper(&fResources.toArray(), |
michael@0 | 580 | knownResourceObjects, |
michael@0 | 581 | newResourceObjects); |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | private: |
michael@0 | 585 | SkTSet<SkPDFObject*> fResources; |
michael@0 | 586 | SkAutoTDelete<const SkPDFShader::State> fState; |
michael@0 | 587 | }; |
michael@0 | 588 | |
michael@0 | 589 | SkPDFShader::SkPDFShader() {} |
michael@0 | 590 | |
michael@0 | 591 | // static |
michael@0 | 592 | SkPDFObject* SkPDFShader::GetPDFShaderByState(State* inState) { |
michael@0 | 593 | SkPDFObject* result; |
michael@0 | 594 | |
michael@0 | 595 | SkAutoTDelete<State> shaderState(inState); |
michael@0 | 596 | if (shaderState.get()->fType == SkShader::kNone_GradientType && |
michael@0 | 597 | shaderState.get()->fImage.isNull()) { |
michael@0 | 598 | // TODO(vandebo) This drops SKComposeShader on the floor. We could |
michael@0 | 599 | // handle compose shader by pulling things up to a layer, drawing with |
michael@0 | 600 | // the first shader, applying the xfer mode and drawing again with the |
michael@0 | 601 | // second shader, then applying the layer to the original drawing. |
michael@0 | 602 | return NULL; |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | ShaderCanonicalEntry entry(NULL, shaderState.get()); |
michael@0 | 606 | int index = CanonicalShaders().find(entry); |
michael@0 | 607 | if (index >= 0) { |
michael@0 | 608 | result = CanonicalShaders()[index].fPDFShader; |
michael@0 | 609 | result->ref(); |
michael@0 | 610 | return result; |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | bool valid = false; |
michael@0 | 614 | // The PDFShader takes ownership of the shaderSate. |
michael@0 | 615 | if (shaderState.get()->fType == SkShader::kNone_GradientType) { |
michael@0 | 616 | SkPDFImageShader* imageShader = |
michael@0 | 617 | new SkPDFImageShader(shaderState.detach()); |
michael@0 | 618 | valid = imageShader->isValid(); |
michael@0 | 619 | result = imageShader; |
michael@0 | 620 | } else { |
michael@0 | 621 | if (shaderState.get()->GradientHasAlpha()) { |
michael@0 | 622 | SkPDFAlphaFunctionShader* gradientShader = |
michael@0 | 623 | SkNEW_ARGS(SkPDFAlphaFunctionShader, (shaderState.detach())); |
michael@0 | 624 | valid = gradientShader->isValid(); |
michael@0 | 625 | result = gradientShader; |
michael@0 | 626 | } else { |
michael@0 | 627 | SkPDFFunctionShader* functionShader = |
michael@0 | 628 | SkNEW_ARGS(SkPDFFunctionShader, (shaderState.detach())); |
michael@0 | 629 | valid = functionShader->isValid(); |
michael@0 | 630 | result = functionShader; |
michael@0 | 631 | } |
michael@0 | 632 | } |
michael@0 | 633 | if (!valid) { |
michael@0 | 634 | delete result; |
michael@0 | 635 | return NULL; |
michael@0 | 636 | } |
michael@0 | 637 | entry.fPDFShader = result; |
michael@0 | 638 | CanonicalShaders().push(entry); |
michael@0 | 639 | return result; // return the reference that came from new. |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | // static |
michael@0 | 643 | void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
michael@0 | 644 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
michael@0 | 645 | ShaderCanonicalEntry entry(shader, NULL); |
michael@0 | 646 | int index = CanonicalShaders().find(entry); |
michael@0 | 647 | SkASSERT(index >= 0); |
michael@0 | 648 | CanonicalShaders().removeShuffle(index); |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | // static |
michael@0 | 652 | SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
michael@0 | 653 | const SkMatrix& matrix, |
michael@0 | 654 | const SkIRect& surfaceBBox) { |
michael@0 | 655 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
michael@0 | 656 | return GetPDFShaderByState( |
michael@0 | 657 | SkNEW_ARGS(State, (shader, matrix, surfaceBBox))); |
michael@0 | 658 | } |
michael@0 | 659 | |
michael@0 | 660 | // static |
michael@0 | 661 | SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() { |
michael@0 | 662 | // This initialization is only thread safe with gcc. |
michael@0 | 663 | static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders; |
michael@0 | 664 | return gCanonicalShaders; |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | // static |
michael@0 | 668 | SkBaseMutex& SkPDFShader::CanonicalShadersMutex() { |
michael@0 | 669 | // This initialization is only thread safe with gcc or when |
michael@0 | 670 | // POD-style mutex initialization is used. |
michael@0 | 671 | SK_DECLARE_STATIC_MUTEX(gCanonicalShadersMutex); |
michael@0 | 672 | return gCanonicalShadersMutex; |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | // static |
michael@0 | 676 | SkPDFObject* SkPDFFunctionShader::RangeObject() { |
michael@0 | 677 | // This initialization is only thread safe with gcc. |
michael@0 | 678 | static SkPDFArray* range = NULL; |
michael@0 | 679 | // This method is only used with CanonicalShadersMutex, so it's safe to |
michael@0 | 680 | // populate domain. |
michael@0 | 681 | if (range == NULL) { |
michael@0 | 682 | range = new SkPDFArray; |
michael@0 | 683 | range->reserve(6); |
michael@0 | 684 | range->appendInt(0); |
michael@0 | 685 | range->appendInt(1); |
michael@0 | 686 | range->appendInt(0); |
michael@0 | 687 | range->appendInt(1); |
michael@0 | 688 | range->appendInt(0); |
michael@0 | 689 | range->appendInt(1); |
michael@0 | 690 | } |
michael@0 | 691 | return range; |
michael@0 | 692 | } |
michael@0 | 693 | |
michael@0 | 694 | static SkPDFResourceDict* get_gradient_resource_dict( |
michael@0 | 695 | SkPDFObject* functionShader, |
michael@0 | 696 | SkPDFObject* gState) { |
michael@0 | 697 | SkPDFResourceDict* dict = new SkPDFResourceDict(); |
michael@0 | 698 | |
michael@0 | 699 | if (functionShader != NULL) { |
michael@0 | 700 | dict->insertResourceAsReference( |
michael@0 | 701 | SkPDFResourceDict::kPattern_ResourceType, 0, functionShader); |
michael@0 | 702 | } |
michael@0 | 703 | if (gState != NULL) { |
michael@0 | 704 | dict->insertResourceAsReference( |
michael@0 | 705 | SkPDFResourceDict::kExtGState_ResourceType, 0, gState); |
michael@0 | 706 | } |
michael@0 | 707 | |
michael@0 | 708 | return dict; |
michael@0 | 709 | } |
michael@0 | 710 | |
michael@0 | 711 | static void populate_tiling_pattern_dict(SkPDFDict* pattern, |
michael@0 | 712 | SkRect& bbox, SkPDFDict* resources, |
michael@0 | 713 | const SkMatrix& matrix) { |
michael@0 | 714 | const int kTiling_PatternType = 1; |
michael@0 | 715 | const int kColoredTilingPattern_PaintType = 1; |
michael@0 | 716 | const int kConstantSpacing_TilingType = 1; |
michael@0 | 717 | |
michael@0 | 718 | pattern->insertName("Type", "Pattern"); |
michael@0 | 719 | pattern->insertInt("PatternType", kTiling_PatternType); |
michael@0 | 720 | pattern->insertInt("PaintType", kColoredTilingPattern_PaintType); |
michael@0 | 721 | pattern->insertInt("TilingType", kConstantSpacing_TilingType); |
michael@0 | 722 | pattern->insert("BBox", SkPDFUtils::RectToArray(bbox))->unref(); |
michael@0 | 723 | pattern->insertScalar("XStep", bbox.width()); |
michael@0 | 724 | pattern->insertScalar("YStep", bbox.height()); |
michael@0 | 725 | pattern->insert("Resources", resources); |
michael@0 | 726 | if (!matrix.isIdentity()) { |
michael@0 | 727 | pattern->insert("Matrix", SkPDFUtils::MatrixToArray(matrix))->unref(); |
michael@0 | 728 | } |
michael@0 | 729 | } |
michael@0 | 730 | |
michael@0 | 731 | /** |
michael@0 | 732 | * Creates a content stream which fills the pattern P0 across bounds. |
michael@0 | 733 | * @param gsIndex A graphics state resource index to apply, or <0 if no |
michael@0 | 734 | * graphics state to apply. |
michael@0 | 735 | */ |
michael@0 | 736 | static SkStream* create_pattern_fill_content(int gsIndex, SkRect& bounds) { |
michael@0 | 737 | SkDynamicMemoryWStream content; |
michael@0 | 738 | if (gsIndex >= 0) { |
michael@0 | 739 | SkPDFUtils::ApplyGraphicState(gsIndex, &content); |
michael@0 | 740 | } |
michael@0 | 741 | SkPDFUtils::ApplyPattern(0, &content); |
michael@0 | 742 | SkPDFUtils::AppendRectangle(bounds, &content); |
michael@0 | 743 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kEvenOdd_FillType, |
michael@0 | 744 | &content); |
michael@0 | 745 | |
michael@0 | 746 | return content.detachAsStream(); |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | /** |
michael@0 | 750 | * Creates a ExtGState with the SMask set to the luminosityShader in |
michael@0 | 751 | * luminosity mode. The shader pattern extends to the bbox. |
michael@0 | 752 | */ |
michael@0 | 753 | SkPDFGraphicState* SkPDFAlphaFunctionShader::CreateSMaskGraphicState() { |
michael@0 | 754 | SkRect bbox; |
michael@0 | 755 | bbox.set(fState.get()->fBBox); |
michael@0 | 756 | |
michael@0 | 757 | SkAutoTUnref<SkPDFObject> luminosityShader( |
michael@0 | 758 | SkPDFShader::GetPDFShaderByState( |
michael@0 | 759 | fState->CreateAlphaToLuminosityState())); |
michael@0 | 760 | |
michael@0 | 761 | SkAutoTUnref<SkStream> alphaStream(create_pattern_fill_content(-1, bbox)); |
michael@0 | 762 | |
michael@0 | 763 | SkAutoTUnref<SkPDFResourceDict> |
michael@0 | 764 | resources(get_gradient_resource_dict(luminosityShader, NULL)); |
michael@0 | 765 | |
michael@0 | 766 | SkAutoTUnref<SkPDFFormXObject> alphaMask( |
michael@0 | 767 | new SkPDFFormXObject(alphaStream.get(), bbox, resources.get())); |
michael@0 | 768 | |
michael@0 | 769 | return SkPDFGraphicState::GetSMaskGraphicState( |
michael@0 | 770 | alphaMask.get(), false, |
michael@0 | 771 | SkPDFGraphicState::kLuminosity_SMaskMode); |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | SkPDFAlphaFunctionShader::SkPDFAlphaFunctionShader(SkPDFShader::State* state) |
michael@0 | 775 | : fState(state) { |
michael@0 | 776 | SkRect bbox; |
michael@0 | 777 | bbox.set(fState.get()->fBBox); |
michael@0 | 778 | |
michael@0 | 779 | fColorShader.reset( |
michael@0 | 780 | SkPDFShader::GetPDFShaderByState(state->CreateOpaqueState())); |
michael@0 | 781 | |
michael@0 | 782 | // Create resource dict with alpha graphics state as G0 and |
michael@0 | 783 | // pattern shader as P0, then write content stream. |
michael@0 | 784 | SkAutoTUnref<SkPDFGraphicState> alphaGs(CreateSMaskGraphicState()); |
michael@0 | 785 | fResourceDict.reset( |
michael@0 | 786 | get_gradient_resource_dict(fColorShader.get(), alphaGs.get())); |
michael@0 | 787 | |
michael@0 | 788 | SkAutoTUnref<SkStream> colorStream( |
michael@0 | 789 | create_pattern_fill_content(0, bbox)); |
michael@0 | 790 | setData(colorStream.get()); |
michael@0 | 791 | |
michael@0 | 792 | populate_tiling_pattern_dict(this, bbox, fResourceDict.get(), |
michael@0 | 793 | SkMatrix::I()); |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | // Finds affine and persp such that in = affine * persp. |
michael@0 | 797 | // but it returns the inverse of perspective matrix. |
michael@0 | 798 | static bool split_perspective(const SkMatrix in, SkMatrix* affine, |
michael@0 | 799 | SkMatrix* perspectiveInverse) { |
michael@0 | 800 | const SkScalar p2 = in[SkMatrix::kMPersp2]; |
michael@0 | 801 | |
michael@0 | 802 | if (SkScalarNearlyZero(p2)) { |
michael@0 | 803 | return false; |
michael@0 | 804 | } |
michael@0 | 805 | |
michael@0 | 806 | const SkScalar zero = SkIntToScalar(0); |
michael@0 | 807 | const SkScalar one = SkIntToScalar(1); |
michael@0 | 808 | |
michael@0 | 809 | const SkScalar sx = in[SkMatrix::kMScaleX]; |
michael@0 | 810 | const SkScalar kx = in[SkMatrix::kMSkewX]; |
michael@0 | 811 | const SkScalar tx = in[SkMatrix::kMTransX]; |
michael@0 | 812 | const SkScalar ky = in[SkMatrix::kMSkewY]; |
michael@0 | 813 | const SkScalar sy = in[SkMatrix::kMScaleY]; |
michael@0 | 814 | const SkScalar ty = in[SkMatrix::kMTransY]; |
michael@0 | 815 | const SkScalar p0 = in[SkMatrix::kMPersp0]; |
michael@0 | 816 | const SkScalar p1 = in[SkMatrix::kMPersp1]; |
michael@0 | 817 | |
michael@0 | 818 | // Perspective matrix would be: |
michael@0 | 819 | // 1 0 0 |
michael@0 | 820 | // 0 1 0 |
michael@0 | 821 | // p0 p1 p2 |
michael@0 | 822 | // But we need the inverse of persp. |
michael@0 | 823 | perspectiveInverse->setAll(one, zero, zero, |
michael@0 | 824 | zero, one, zero, |
michael@0 | 825 | -p0/p2, -p1/p2, 1/p2); |
michael@0 | 826 | |
michael@0 | 827 | affine->setAll(sx - p0 * tx / p2, kx - p1 * tx / p2, tx / p2, |
michael@0 | 828 | ky - p0 * ty / p2, sy - p1 * ty / p2, ty / p2, |
michael@0 | 829 | zero, zero, one); |
michael@0 | 830 | |
michael@0 | 831 | return true; |
michael@0 | 832 | } |
michael@0 | 833 | |
michael@0 | 834 | SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) |
michael@0 | 835 | : SkPDFDict("Pattern"), |
michael@0 | 836 | fState(state) { |
michael@0 | 837 | SkString (*codeFunction)(const SkShader::GradientInfo& info, |
michael@0 | 838 | const SkMatrix& perspectiveRemover) = NULL; |
michael@0 | 839 | SkPoint transformPoints[2]; |
michael@0 | 840 | |
michael@0 | 841 | // Depending on the type of the gradient, we want to transform the |
michael@0 | 842 | // coordinate space in different ways. |
michael@0 | 843 | const SkShader::GradientInfo* info = &fState.get()->fInfo; |
michael@0 | 844 | transformPoints[0] = info->fPoint[0]; |
michael@0 | 845 | transformPoints[1] = info->fPoint[1]; |
michael@0 | 846 | switch (fState.get()->fType) { |
michael@0 | 847 | case SkShader::kLinear_GradientType: |
michael@0 | 848 | codeFunction = &linearCode; |
michael@0 | 849 | break; |
michael@0 | 850 | case SkShader::kRadial_GradientType: |
michael@0 | 851 | transformPoints[1] = transformPoints[0]; |
michael@0 | 852 | transformPoints[1].fX += info->fRadius[0]; |
michael@0 | 853 | codeFunction = &radialCode; |
michael@0 | 854 | break; |
michael@0 | 855 | case SkShader::kRadial2_GradientType: { |
michael@0 | 856 | // Bail out if the radii are the same. Empty fResources signals |
michael@0 | 857 | // an error and isValid will return false. |
michael@0 | 858 | if (info->fRadius[0] == info->fRadius[1]) { |
michael@0 | 859 | return; |
michael@0 | 860 | } |
michael@0 | 861 | transformPoints[1] = transformPoints[0]; |
michael@0 | 862 | SkScalar dr = info->fRadius[1] - info->fRadius[0]; |
michael@0 | 863 | transformPoints[1].fX += dr; |
michael@0 | 864 | codeFunction = &twoPointRadialCode; |
michael@0 | 865 | break; |
michael@0 | 866 | } |
michael@0 | 867 | case SkShader::kConical_GradientType: { |
michael@0 | 868 | transformPoints[1] = transformPoints[0]; |
michael@0 | 869 | transformPoints[1].fX += SK_Scalar1; |
michael@0 | 870 | codeFunction = &twoPointConicalCode; |
michael@0 | 871 | break; |
michael@0 | 872 | } |
michael@0 | 873 | case SkShader::kSweep_GradientType: |
michael@0 | 874 | transformPoints[1] = transformPoints[0]; |
michael@0 | 875 | transformPoints[1].fX += SK_Scalar1; |
michael@0 | 876 | codeFunction = &sweepCode; |
michael@0 | 877 | break; |
michael@0 | 878 | case SkShader::kColor_GradientType: |
michael@0 | 879 | case SkShader::kNone_GradientType: |
michael@0 | 880 | default: |
michael@0 | 881 | return; |
michael@0 | 882 | } |
michael@0 | 883 | |
michael@0 | 884 | // Move any scaling (assuming a unit gradient) or translation |
michael@0 | 885 | // (and rotation for linear gradient), of the final gradient from |
michael@0 | 886 | // info->fPoints to the matrix (updating bbox appropriately). Now |
michael@0 | 887 | // the gradient can be drawn on on the unit segment. |
michael@0 | 888 | SkMatrix mapperMatrix; |
michael@0 | 889 | unitToPointsMatrix(transformPoints, &mapperMatrix); |
michael@0 | 890 | |
michael@0 | 891 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
michael@0 | 892 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
michael@0 | 893 | finalMatrix.preConcat(mapperMatrix); |
michael@0 | 894 | |
michael@0 | 895 | // Preserves as much as posible in the final matrix, and only removes |
michael@0 | 896 | // the perspective. The inverse of the perspective is stored in |
michael@0 | 897 | // perspectiveInverseOnly matrix and has 3 useful numbers |
michael@0 | 898 | // (p0, p1, p2), while everything else is either 0 or 1. |
michael@0 | 899 | // In this way the shader will handle it eficiently, with minimal code. |
michael@0 | 900 | SkMatrix perspectiveInverseOnly = SkMatrix::I(); |
michael@0 | 901 | if (finalMatrix.hasPerspective()) { |
michael@0 | 902 | if (!split_perspective(finalMatrix, |
michael@0 | 903 | &finalMatrix, &perspectiveInverseOnly)) { |
michael@0 | 904 | return; |
michael@0 | 905 | } |
michael@0 | 906 | } |
michael@0 | 907 | |
michael@0 | 908 | SkRect bbox; |
michael@0 | 909 | bbox.set(fState.get()->fBBox); |
michael@0 | 910 | if (!inverseTransformBBox(finalMatrix, &bbox)) { |
michael@0 | 911 | return; |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | SkAutoTUnref<SkPDFArray> domain(new SkPDFArray); |
michael@0 | 915 | domain->reserve(4); |
michael@0 | 916 | domain->appendScalar(bbox.fLeft); |
michael@0 | 917 | domain->appendScalar(bbox.fRight); |
michael@0 | 918 | domain->appendScalar(bbox.fTop); |
michael@0 | 919 | domain->appendScalar(bbox.fBottom); |
michael@0 | 920 | |
michael@0 | 921 | SkString functionCode; |
michael@0 | 922 | // The two point radial gradient further references fState.get()->fInfo |
michael@0 | 923 | // in translating from x, y coordinates to the t parameter. So, we have |
michael@0 | 924 | // to transform the points and radii according to the calculated matrix. |
michael@0 | 925 | if (fState.get()->fType == SkShader::kRadial2_GradientType) { |
michael@0 | 926 | SkShader::GradientInfo twoPointRadialInfo = *info; |
michael@0 | 927 | SkMatrix inverseMapperMatrix; |
michael@0 | 928 | if (!mapperMatrix.invert(&inverseMapperMatrix)) { |
michael@0 | 929 | return; |
michael@0 | 930 | } |
michael@0 | 931 | inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2); |
michael@0 | 932 | twoPointRadialInfo.fRadius[0] = |
michael@0 | 933 | inverseMapperMatrix.mapRadius(info->fRadius[0]); |
michael@0 | 934 | twoPointRadialInfo.fRadius[1] = |
michael@0 | 935 | inverseMapperMatrix.mapRadius(info->fRadius[1]); |
michael@0 | 936 | functionCode = codeFunction(twoPointRadialInfo, perspectiveInverseOnly); |
michael@0 | 937 | } else { |
michael@0 | 938 | functionCode = codeFunction(*info, perspectiveInverseOnly); |
michael@0 | 939 | } |
michael@0 | 940 | |
michael@0 | 941 | SkAutoTUnref<SkPDFDict> pdfShader(new SkPDFDict); |
michael@0 | 942 | pdfShader->insertInt("ShadingType", 1); |
michael@0 | 943 | pdfShader->insertName("ColorSpace", "DeviceRGB"); |
michael@0 | 944 | pdfShader->insert("Domain", domain.get()); |
michael@0 | 945 | |
michael@0 | 946 | SkPDFStream* function = makePSFunction(functionCode, domain.get()); |
michael@0 | 947 | pdfShader->insert("Function", new SkPDFObjRef(function))->unref(); |
michael@0 | 948 | fResources.push(function); // Pass ownership to resource list. |
michael@0 | 949 | |
michael@0 | 950 | insertInt("PatternType", 2); |
michael@0 | 951 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
michael@0 | 952 | insert("Shading", pdfShader.get()); |
michael@0 | 953 | } |
michael@0 | 954 | |
michael@0 | 955 | SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { |
michael@0 | 956 | fState.get()->fImage.lockPixels(); |
michael@0 | 957 | |
michael@0 | 958 | // The image shader pattern cell will be drawn into a separate device |
michael@0 | 959 | // in pattern cell space (no scaling on the bitmap, though there may be |
michael@0 | 960 | // translations so that all content is in the device, coordinates > 0). |
michael@0 | 961 | |
michael@0 | 962 | // Map clip bounds to shader space to ensure the device is large enough |
michael@0 | 963 | // to handle fake clamping. |
michael@0 | 964 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
michael@0 | 965 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
michael@0 | 966 | SkRect deviceBounds; |
michael@0 | 967 | deviceBounds.set(fState.get()->fBBox); |
michael@0 | 968 | if (!inverseTransformBBox(finalMatrix, &deviceBounds)) { |
michael@0 | 969 | return; |
michael@0 | 970 | } |
michael@0 | 971 | |
michael@0 | 972 | const SkBitmap* image = &fState.get()->fImage; |
michael@0 | 973 | SkRect bitmapBounds; |
michael@0 | 974 | image->getBounds(&bitmapBounds); |
michael@0 | 975 | |
michael@0 | 976 | // For tiling modes, the bounds should be extended to include the bitmap, |
michael@0 | 977 | // otherwise the bitmap gets clipped out and the shader is empty and awful. |
michael@0 | 978 | // For clamp modes, we're only interested in the clip region, whether |
michael@0 | 979 | // or not the main bitmap is in it. |
michael@0 | 980 | SkShader::TileMode tileModes[2]; |
michael@0 | 981 | tileModes[0] = fState.get()->fImageTileModes[0]; |
michael@0 | 982 | tileModes[1] = fState.get()->fImageTileModes[1]; |
michael@0 | 983 | if (tileModes[0] != SkShader::kClamp_TileMode || |
michael@0 | 984 | tileModes[1] != SkShader::kClamp_TileMode) { |
michael@0 | 985 | deviceBounds.join(bitmapBounds); |
michael@0 | 986 | } |
michael@0 | 987 | |
michael@0 | 988 | SkMatrix unflip; |
michael@0 | 989 | unflip.setTranslate(0, SkScalarRoundToScalar(deviceBounds.height())); |
michael@0 | 990 | unflip.preScale(SK_Scalar1, -SK_Scalar1); |
michael@0 | 991 | SkISize size = SkISize::Make(SkScalarRoundToInt(deviceBounds.width()), |
michael@0 | 992 | SkScalarRoundToInt(deviceBounds.height())); |
michael@0 | 993 | // TODO(edisonn): should we pass here the DCT encoder of the destination device? |
michael@0 | 994 | // TODO(edisonn): NYI Perspective, use SkPDFDeviceFlattener. |
michael@0 | 995 | SkPDFDevice pattern(size, size, unflip); |
michael@0 | 996 | SkCanvas canvas(&pattern); |
michael@0 | 997 | |
michael@0 | 998 | SkRect patternBBox; |
michael@0 | 999 | image->getBounds(&patternBBox); |
michael@0 | 1000 | |
michael@0 | 1001 | // Translate the canvas so that the bitmap origin is at (0, 0). |
michael@0 | 1002 | canvas.translate(-deviceBounds.left(), -deviceBounds.top()); |
michael@0 | 1003 | patternBBox.offset(-deviceBounds.left(), -deviceBounds.top()); |
michael@0 | 1004 | // Undo the translation in the final matrix |
michael@0 | 1005 | finalMatrix.preTranslate(deviceBounds.left(), deviceBounds.top()); |
michael@0 | 1006 | |
michael@0 | 1007 | // If the bitmap is out of bounds (i.e. clamp mode where we only see the |
michael@0 | 1008 | // stretched sides), canvas will clip this out and the extraneous data |
michael@0 | 1009 | // won't be saved to the PDF. |
michael@0 | 1010 | canvas.drawBitmap(*image, 0, 0); |
michael@0 | 1011 | |
michael@0 | 1012 | SkScalar width = SkIntToScalar(image->width()); |
michael@0 | 1013 | SkScalar height = SkIntToScalar(image->height()); |
michael@0 | 1014 | |
michael@0 | 1015 | // Tiling is implied. First we handle mirroring. |
michael@0 | 1016 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
michael@0 | 1017 | SkMatrix xMirror; |
michael@0 | 1018 | xMirror.setScale(-1, 1); |
michael@0 | 1019 | xMirror.postTranslate(2 * width, 0); |
michael@0 | 1020 | canvas.drawBitmapMatrix(*image, xMirror); |
michael@0 | 1021 | patternBBox.fRight += width; |
michael@0 | 1022 | } |
michael@0 | 1023 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
michael@0 | 1024 | SkMatrix yMirror; |
michael@0 | 1025 | yMirror.setScale(SK_Scalar1, -SK_Scalar1); |
michael@0 | 1026 | yMirror.postTranslate(0, 2 * height); |
michael@0 | 1027 | canvas.drawBitmapMatrix(*image, yMirror); |
michael@0 | 1028 | patternBBox.fBottom += height; |
michael@0 | 1029 | } |
michael@0 | 1030 | if (tileModes[0] == SkShader::kMirror_TileMode && |
michael@0 | 1031 | tileModes[1] == SkShader::kMirror_TileMode) { |
michael@0 | 1032 | SkMatrix mirror; |
michael@0 | 1033 | mirror.setScale(-1, -1); |
michael@0 | 1034 | mirror.postTranslate(2 * width, 2 * height); |
michael@0 | 1035 | canvas.drawBitmapMatrix(*image, mirror); |
michael@0 | 1036 | } |
michael@0 | 1037 | |
michael@0 | 1038 | // Then handle Clamping, which requires expanding the pattern canvas to |
michael@0 | 1039 | // cover the entire surfaceBBox. |
michael@0 | 1040 | |
michael@0 | 1041 | // If both x and y are in clamp mode, we start by filling in the corners. |
michael@0 | 1042 | // (Which are just a rectangles of the corner colors.) |
michael@0 | 1043 | if (tileModes[0] == SkShader::kClamp_TileMode && |
michael@0 | 1044 | tileModes[1] == SkShader::kClamp_TileMode) { |
michael@0 | 1045 | SkPaint paint; |
michael@0 | 1046 | SkRect rect; |
michael@0 | 1047 | rect = SkRect::MakeLTRB(deviceBounds.left(), deviceBounds.top(), 0, 0); |
michael@0 | 1048 | if (!rect.isEmpty()) { |
michael@0 | 1049 | paint.setColor(image->getColor(0, 0)); |
michael@0 | 1050 | canvas.drawRect(rect, paint); |
michael@0 | 1051 | } |
michael@0 | 1052 | |
michael@0 | 1053 | rect = SkRect::MakeLTRB(width, deviceBounds.top(), |
michael@0 | 1054 | deviceBounds.right(), 0); |
michael@0 | 1055 | if (!rect.isEmpty()) { |
michael@0 | 1056 | paint.setColor(image->getColor(image->width() - 1, 0)); |
michael@0 | 1057 | canvas.drawRect(rect, paint); |
michael@0 | 1058 | } |
michael@0 | 1059 | |
michael@0 | 1060 | rect = SkRect::MakeLTRB(width, height, |
michael@0 | 1061 | deviceBounds.right(), deviceBounds.bottom()); |
michael@0 | 1062 | if (!rect.isEmpty()) { |
michael@0 | 1063 | paint.setColor(image->getColor(image->width() - 1, |
michael@0 | 1064 | image->height() - 1)); |
michael@0 | 1065 | canvas.drawRect(rect, paint); |
michael@0 | 1066 | } |
michael@0 | 1067 | |
michael@0 | 1068 | rect = SkRect::MakeLTRB(deviceBounds.left(), height, |
michael@0 | 1069 | 0, deviceBounds.bottom()); |
michael@0 | 1070 | if (!rect.isEmpty()) { |
michael@0 | 1071 | paint.setColor(image->getColor(0, image->height() - 1)); |
michael@0 | 1072 | canvas.drawRect(rect, paint); |
michael@0 | 1073 | } |
michael@0 | 1074 | } |
michael@0 | 1075 | |
michael@0 | 1076 | // Then expand the left, right, top, then bottom. |
michael@0 | 1077 | if (tileModes[0] == SkShader::kClamp_TileMode) { |
michael@0 | 1078 | SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, image->height()); |
michael@0 | 1079 | if (deviceBounds.left() < 0) { |
michael@0 | 1080 | SkBitmap left; |
michael@0 | 1081 | SkAssertResult(image->extractSubset(&left, subset)); |
michael@0 | 1082 | |
michael@0 | 1083 | SkMatrix leftMatrix; |
michael@0 | 1084 | leftMatrix.setScale(-deviceBounds.left(), 1); |
michael@0 | 1085 | leftMatrix.postTranslate(deviceBounds.left(), 0); |
michael@0 | 1086 | canvas.drawBitmapMatrix(left, leftMatrix); |
michael@0 | 1087 | |
michael@0 | 1088 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
michael@0 | 1089 | leftMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
michael@0 | 1090 | leftMatrix.postTranslate(0, 2 * height); |
michael@0 | 1091 | canvas.drawBitmapMatrix(left, leftMatrix); |
michael@0 | 1092 | } |
michael@0 | 1093 | patternBBox.fLeft = 0; |
michael@0 | 1094 | } |
michael@0 | 1095 | |
michael@0 | 1096 | if (deviceBounds.right() > width) { |
michael@0 | 1097 | SkBitmap right; |
michael@0 | 1098 | subset.offset(image->width() - 1, 0); |
michael@0 | 1099 | SkAssertResult(image->extractSubset(&right, subset)); |
michael@0 | 1100 | |
michael@0 | 1101 | SkMatrix rightMatrix; |
michael@0 | 1102 | rightMatrix.setScale(deviceBounds.right() - width, 1); |
michael@0 | 1103 | rightMatrix.postTranslate(width, 0); |
michael@0 | 1104 | canvas.drawBitmapMatrix(right, rightMatrix); |
michael@0 | 1105 | |
michael@0 | 1106 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
michael@0 | 1107 | rightMatrix.postScale(SK_Scalar1, -SK_Scalar1); |
michael@0 | 1108 | rightMatrix.postTranslate(0, 2 * height); |
michael@0 | 1109 | canvas.drawBitmapMatrix(right, rightMatrix); |
michael@0 | 1110 | } |
michael@0 | 1111 | patternBBox.fRight = deviceBounds.width(); |
michael@0 | 1112 | } |
michael@0 | 1113 | } |
michael@0 | 1114 | |
michael@0 | 1115 | if (tileModes[1] == SkShader::kClamp_TileMode) { |
michael@0 | 1116 | SkIRect subset = SkIRect::MakeXYWH(0, 0, image->width(), 1); |
michael@0 | 1117 | if (deviceBounds.top() < 0) { |
michael@0 | 1118 | SkBitmap top; |
michael@0 | 1119 | SkAssertResult(image->extractSubset(&top, subset)); |
michael@0 | 1120 | |
michael@0 | 1121 | SkMatrix topMatrix; |
michael@0 | 1122 | topMatrix.setScale(SK_Scalar1, -deviceBounds.top()); |
michael@0 | 1123 | topMatrix.postTranslate(0, deviceBounds.top()); |
michael@0 | 1124 | canvas.drawBitmapMatrix(top, topMatrix); |
michael@0 | 1125 | |
michael@0 | 1126 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
michael@0 | 1127 | topMatrix.postScale(-1, 1); |
michael@0 | 1128 | topMatrix.postTranslate(2 * width, 0); |
michael@0 | 1129 | canvas.drawBitmapMatrix(top, topMatrix); |
michael@0 | 1130 | } |
michael@0 | 1131 | patternBBox.fTop = 0; |
michael@0 | 1132 | } |
michael@0 | 1133 | |
michael@0 | 1134 | if (deviceBounds.bottom() > height) { |
michael@0 | 1135 | SkBitmap bottom; |
michael@0 | 1136 | subset.offset(0, image->height() - 1); |
michael@0 | 1137 | SkAssertResult(image->extractSubset(&bottom, subset)); |
michael@0 | 1138 | |
michael@0 | 1139 | SkMatrix bottomMatrix; |
michael@0 | 1140 | bottomMatrix.setScale(SK_Scalar1, deviceBounds.bottom() - height); |
michael@0 | 1141 | bottomMatrix.postTranslate(0, height); |
michael@0 | 1142 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
michael@0 | 1143 | |
michael@0 | 1144 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
michael@0 | 1145 | bottomMatrix.postScale(-1, 1); |
michael@0 | 1146 | bottomMatrix.postTranslate(2 * width, 0); |
michael@0 | 1147 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
michael@0 | 1148 | } |
michael@0 | 1149 | patternBBox.fBottom = deviceBounds.height(); |
michael@0 | 1150 | } |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | // Put the canvas into the pattern stream (fContent). |
michael@0 | 1154 | SkAutoTUnref<SkStream> content(pattern.content()); |
michael@0 | 1155 | setData(content.get()); |
michael@0 | 1156 | SkPDFResourceDict* resourceDict = pattern.getResourceDict(); |
michael@0 | 1157 | resourceDict->getReferencedResources(fResources, &fResources, false); |
michael@0 | 1158 | |
michael@0 | 1159 | populate_tiling_pattern_dict(this, patternBBox, |
michael@0 | 1160 | pattern.getResourceDict(), finalMatrix); |
michael@0 | 1161 | |
michael@0 | 1162 | fState.get()->fImage.unlockPixels(); |
michael@0 | 1163 | } |
michael@0 | 1164 | |
michael@0 | 1165 | SkPDFStream* SkPDFFunctionShader::makePSFunction(const SkString& psCode, |
michael@0 | 1166 | SkPDFArray* domain) { |
michael@0 | 1167 | SkAutoDataUnref funcData(SkData::NewWithCopy(psCode.c_str(), |
michael@0 | 1168 | psCode.size())); |
michael@0 | 1169 | SkPDFStream* result = new SkPDFStream(funcData.get()); |
michael@0 | 1170 | result->insertInt("FunctionType", 4); |
michael@0 | 1171 | result->insert("Domain", domain); |
michael@0 | 1172 | result->insert("Range", RangeObject()); |
michael@0 | 1173 | return result; |
michael@0 | 1174 | } |
michael@0 | 1175 | |
michael@0 | 1176 | SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader, |
michael@0 | 1177 | const State* state) |
michael@0 | 1178 | : fPDFShader(pdfShader), |
michael@0 | 1179 | fState(state) { |
michael@0 | 1180 | } |
michael@0 | 1181 | |
michael@0 | 1182 | bool SkPDFShader::ShaderCanonicalEntry::operator==( |
michael@0 | 1183 | const ShaderCanonicalEntry& b) const { |
michael@0 | 1184 | return fPDFShader == b.fPDFShader || |
michael@0 | 1185 | (fState != NULL && b.fState != NULL && *fState == *b.fState); |
michael@0 | 1186 | } |
michael@0 | 1187 | |
michael@0 | 1188 | bool SkPDFShader::State::operator==(const SkPDFShader::State& b) const { |
michael@0 | 1189 | if (fType != b.fType || |
michael@0 | 1190 | fCanvasTransform != b.fCanvasTransform || |
michael@0 | 1191 | fShaderTransform != b.fShaderTransform || |
michael@0 | 1192 | fBBox != b.fBBox) { |
michael@0 | 1193 | return false; |
michael@0 | 1194 | } |
michael@0 | 1195 | |
michael@0 | 1196 | if (fType == SkShader::kNone_GradientType) { |
michael@0 | 1197 | if (fPixelGeneration != b.fPixelGeneration || |
michael@0 | 1198 | fPixelGeneration == 0 || |
michael@0 | 1199 | fImageTileModes[0] != b.fImageTileModes[0] || |
michael@0 | 1200 | fImageTileModes[1] != b.fImageTileModes[1]) { |
michael@0 | 1201 | return false; |
michael@0 | 1202 | } |
michael@0 | 1203 | } else { |
michael@0 | 1204 | if (fInfo.fColorCount != b.fInfo.fColorCount || |
michael@0 | 1205 | memcmp(fInfo.fColors, b.fInfo.fColors, |
michael@0 | 1206 | sizeof(SkColor) * fInfo.fColorCount) != 0 || |
michael@0 | 1207 | memcmp(fInfo.fColorOffsets, b.fInfo.fColorOffsets, |
michael@0 | 1208 | sizeof(SkScalar) * fInfo.fColorCount) != 0 || |
michael@0 | 1209 | fInfo.fPoint[0] != b.fInfo.fPoint[0] || |
michael@0 | 1210 | fInfo.fTileMode != b.fInfo.fTileMode) { |
michael@0 | 1211 | return false; |
michael@0 | 1212 | } |
michael@0 | 1213 | |
michael@0 | 1214 | switch (fType) { |
michael@0 | 1215 | case SkShader::kLinear_GradientType: |
michael@0 | 1216 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1]) { |
michael@0 | 1217 | return false; |
michael@0 | 1218 | } |
michael@0 | 1219 | break; |
michael@0 | 1220 | case SkShader::kRadial_GradientType: |
michael@0 | 1221 | if (fInfo.fRadius[0] != b.fInfo.fRadius[0]) { |
michael@0 | 1222 | return false; |
michael@0 | 1223 | } |
michael@0 | 1224 | break; |
michael@0 | 1225 | case SkShader::kRadial2_GradientType: |
michael@0 | 1226 | case SkShader::kConical_GradientType: |
michael@0 | 1227 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1] || |
michael@0 | 1228 | fInfo.fRadius[0] != b.fInfo.fRadius[0] || |
michael@0 | 1229 | fInfo.fRadius[1] != b.fInfo.fRadius[1]) { |
michael@0 | 1230 | return false; |
michael@0 | 1231 | } |
michael@0 | 1232 | break; |
michael@0 | 1233 | case SkShader::kSweep_GradientType: |
michael@0 | 1234 | case SkShader::kNone_GradientType: |
michael@0 | 1235 | case SkShader::kColor_GradientType: |
michael@0 | 1236 | break; |
michael@0 | 1237 | } |
michael@0 | 1238 | } |
michael@0 | 1239 | return true; |
michael@0 | 1240 | } |
michael@0 | 1241 | |
michael@0 | 1242 | SkPDFShader::State::State(const SkShader& shader, |
michael@0 | 1243 | const SkMatrix& canvasTransform, const SkIRect& bbox) |
michael@0 | 1244 | : fCanvasTransform(canvasTransform), |
michael@0 | 1245 | fBBox(bbox), |
michael@0 | 1246 | fPixelGeneration(0) { |
michael@0 | 1247 | fInfo.fColorCount = 0; |
michael@0 | 1248 | fInfo.fColors = NULL; |
michael@0 | 1249 | fInfo.fColorOffsets = NULL; |
michael@0 | 1250 | fShaderTransform = shader.getLocalMatrix(); |
michael@0 | 1251 | fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; |
michael@0 | 1252 | |
michael@0 | 1253 | fType = shader.asAGradient(&fInfo); |
michael@0 | 1254 | |
michael@0 | 1255 | if (fType == SkShader::kNone_GradientType) { |
michael@0 | 1256 | SkShader::BitmapType bitmapType; |
michael@0 | 1257 | SkMatrix matrix; |
michael@0 | 1258 | bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes); |
michael@0 | 1259 | if (bitmapType != SkShader::kDefault_BitmapType) { |
michael@0 | 1260 | fImage.reset(); |
michael@0 | 1261 | return; |
michael@0 | 1262 | } |
michael@0 | 1263 | SkASSERT(matrix.isIdentity()); |
michael@0 | 1264 | fPixelGeneration = fImage.getGenerationID(); |
michael@0 | 1265 | } else { |
michael@0 | 1266 | AllocateGradientInfoStorage(); |
michael@0 | 1267 | shader.asAGradient(&fInfo); |
michael@0 | 1268 | } |
michael@0 | 1269 | } |
michael@0 | 1270 | |
michael@0 | 1271 | SkPDFShader::State::State(const SkPDFShader::State& other) |
michael@0 | 1272 | : fType(other.fType), |
michael@0 | 1273 | fCanvasTransform(other.fCanvasTransform), |
michael@0 | 1274 | fShaderTransform(other.fShaderTransform), |
michael@0 | 1275 | fBBox(other.fBBox) |
michael@0 | 1276 | { |
michael@0 | 1277 | // Only gradients supported for now, since that is all that is used. |
michael@0 | 1278 | // If needed, image state copy constructor can be added here later. |
michael@0 | 1279 | SkASSERT(fType != SkShader::kNone_GradientType); |
michael@0 | 1280 | |
michael@0 | 1281 | if (fType != SkShader::kNone_GradientType) { |
michael@0 | 1282 | fInfo = other.fInfo; |
michael@0 | 1283 | |
michael@0 | 1284 | AllocateGradientInfoStorage(); |
michael@0 | 1285 | for (int i = 0; i < fInfo.fColorCount; i++) { |
michael@0 | 1286 | fInfo.fColors[i] = other.fInfo.fColors[i]; |
michael@0 | 1287 | fInfo.fColorOffsets[i] = other.fInfo.fColorOffsets[i]; |
michael@0 | 1288 | } |
michael@0 | 1289 | } |
michael@0 | 1290 | } |
michael@0 | 1291 | |
michael@0 | 1292 | /** |
michael@0 | 1293 | * Create a copy of this gradient state with alpha assigned to RGB luminousity. |
michael@0 | 1294 | * Only valid for gradient states. |
michael@0 | 1295 | */ |
michael@0 | 1296 | SkPDFShader::State* SkPDFShader::State::CreateAlphaToLuminosityState() const { |
michael@0 | 1297 | SkASSERT(fType != SkShader::kNone_GradientType); |
michael@0 | 1298 | |
michael@0 | 1299 | SkPDFShader::State* newState = new SkPDFShader::State(*this); |
michael@0 | 1300 | |
michael@0 | 1301 | for (int i = 0; i < fInfo.fColorCount; i++) { |
michael@0 | 1302 | SkAlpha alpha = SkColorGetA(fInfo.fColors[i]); |
michael@0 | 1303 | newState->fInfo.fColors[i] = SkColorSetARGB(255, alpha, alpha, alpha); |
michael@0 | 1304 | } |
michael@0 | 1305 | |
michael@0 | 1306 | return newState; |
michael@0 | 1307 | } |
michael@0 | 1308 | |
michael@0 | 1309 | /** |
michael@0 | 1310 | * Create a copy of this gradient state with alpha set to fully opaque |
michael@0 | 1311 | * Only valid for gradient states. |
michael@0 | 1312 | */ |
michael@0 | 1313 | SkPDFShader::State* SkPDFShader::State::CreateOpaqueState() const { |
michael@0 | 1314 | SkASSERT(fType != SkShader::kNone_GradientType); |
michael@0 | 1315 | |
michael@0 | 1316 | SkPDFShader::State* newState = new SkPDFShader::State(*this); |
michael@0 | 1317 | for (int i = 0; i < fInfo.fColorCount; i++) { |
michael@0 | 1318 | newState->fInfo.fColors[i] = SkColorSetA(fInfo.fColors[i], |
michael@0 | 1319 | SK_AlphaOPAQUE); |
michael@0 | 1320 | } |
michael@0 | 1321 | |
michael@0 | 1322 | return newState; |
michael@0 | 1323 | } |
michael@0 | 1324 | |
michael@0 | 1325 | /** |
michael@0 | 1326 | * Returns true if state is a gradient and the gradient has alpha. |
michael@0 | 1327 | */ |
michael@0 | 1328 | bool SkPDFShader::State::GradientHasAlpha() const { |
michael@0 | 1329 | if (fType == SkShader::kNone_GradientType) { |
michael@0 | 1330 | return false; |
michael@0 | 1331 | } |
michael@0 | 1332 | |
michael@0 | 1333 | for (int i = 0; i < fInfo.fColorCount; i++) { |
michael@0 | 1334 | SkAlpha alpha = SkColorGetA(fInfo.fColors[i]); |
michael@0 | 1335 | if (alpha != SK_AlphaOPAQUE) { |
michael@0 | 1336 | return true; |
michael@0 | 1337 | } |
michael@0 | 1338 | } |
michael@0 | 1339 | return false; |
michael@0 | 1340 | } |
michael@0 | 1341 | |
michael@0 | 1342 | void SkPDFShader::State::AllocateGradientInfoStorage() { |
michael@0 | 1343 | fColorData.set(sk_malloc_throw( |
michael@0 | 1344 | fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
michael@0 | 1345 | fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
michael@0 | 1346 | fInfo.fColorOffsets = |
michael@0 | 1347 | reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
michael@0 | 1348 | } |