vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SkPDFShader.h" |
| 18 | |
| 19 | #include "SkCanvas.h" |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 20 | #include "SkData.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 21 | #include "SkPDFCatalog.h" |
| 22 | #include "SkPDFDevice.h" |
| 23 | #include "SkPDFTypes.h" |
| 24 | #include "SkPDFUtils.h" |
| 25 | #include "SkScalar.h" |
| 26 | #include "SkStream.h" |
twiz@google.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 27 | #include "SkTemplates.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 28 | #include "SkThread.h" |
| 29 | #include "SkTypes.h" |
| 30 | |
| 31 | static void transformBBox(const SkMatrix& matrix, SkRect* bbox) { |
| 32 | SkMatrix inverse; |
| 33 | inverse.reset(); |
| 34 | matrix.invert(&inverse); |
| 35 | inverse.mapRect(bbox); |
| 36 | } |
| 37 | |
| 38 | static void unitToPointsMatrix(const SkPoint pts[2], SkMatrix* matrix) { |
| 39 | SkVector vec = pts[1] - pts[0]; |
| 40 | SkScalar mag = vec.length(); |
| 41 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 42 | |
| 43 | vec.scale(inv); |
| 44 | matrix->setSinCos(vec.fY, vec.fX); |
| 45 | matrix->preTranslate(pts[0].fX, pts[0].fY); |
| 46 | matrix->preScale(mag, mag); |
| 47 | } |
| 48 | |
| 49 | /* Assumes t + startOffset is on the stack and does a linear interpolation on t |
| 50 | between startOffset and endOffset from prevColor to curColor (for each color |
| 51 | component), leaving the result in component order on the stack. |
| 52 | @param range endOffset - startOffset |
| 53 | @param curColor[components] The current color components. |
| 54 | @param prevColor[components] The previous color components. |
| 55 | @param result The result ps function. |
| 56 | */ |
| 57 | static void interpolateColorCode(SkScalar range, SkScalar* curColor, |
| 58 | SkScalar* prevColor, int components, |
| 59 | SkString* result) { |
| 60 | // Figure out how to scale each color component. |
twiz@google.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 61 | SkAutoSTMalloc<4, SkScalar> multiplierAlloc(components); |
| 62 | SkScalar *multiplier = multiplierAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 63 | for (int i = 0; i < components; i++) { |
| 64 | multiplier[i] = SkScalarDiv(curColor[i] - prevColor[i], range); |
| 65 | } |
| 66 | |
| 67 | // Calculate when we no longer need to keep a copy of the input parameter t. |
| 68 | // If the last component to use t is i, then dupInput[0..i - 1] = true |
| 69 | // and dupInput[i .. components] = false. |
twiz@google.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 70 | SkAutoSTMalloc<4, bool> dupInputAlloc(components); |
| 71 | bool *dupInput = dupInputAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 72 | dupInput[components - 1] = false; |
| 73 | for (int i = components - 2; i >= 0; i--) { |
| 74 | dupInput[i] = dupInput[i + 1] || multiplier[i + 1] != 0; |
| 75 | } |
| 76 | |
| 77 | if (!dupInput[0] && multiplier[0] == 0) { |
| 78 | result->append("pop "); |
| 79 | } |
| 80 | |
| 81 | for (int i = 0; i < components; i++) { |
| 82 | // If the next components needs t, make a copy. |
| 83 | if (dupInput[i]) { |
| 84 | result->append("dup "); |
| 85 | } |
| 86 | |
| 87 | if (multiplier[i] == 0) { |
| 88 | result->appendScalar(prevColor[i]); |
| 89 | result->append(" "); |
| 90 | } else { |
| 91 | if (multiplier[i] != 1) { |
| 92 | result->appendScalar(multiplier[i]); |
| 93 | result->append(" mul "); |
| 94 | } |
| 95 | if (prevColor[i] != 0) { |
| 96 | result->appendScalar(prevColor[i]); |
| 97 | result->append(" add "); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (dupInput[i]) { |
| 102 | result->append("exch\n"); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* Generate Type 4 function code to map t=[0,1) to the passed gradient, |
| 108 | clamping at the edges of the range. The generated code will be of the form: |
| 109 | if (t < 0) { |
| 110 | return colorData[0][r,g,b]; |
| 111 | } else { |
| 112 | if (t < info.fColorOffsets[1]) { |
| 113 | return linearinterpolation(colorData[0][r,g,b], |
| 114 | colorData[1][r,g,b]); |
| 115 | } else { |
| 116 | if (t < info.fColorOffsets[2]) { |
| 117 | return linearinterpolation(colorData[1][r,g,b], |
| 118 | colorData[2][r,g,b]); |
| 119 | } else { |
| 120 | |
| 121 | ... } else { |
| 122 | return colorData[info.fColorCount - 1][r,g,b]; |
| 123 | } |
| 124 | ... |
| 125 | } |
| 126 | } |
| 127 | */ |
| 128 | static void gradientFunctionCode(const SkShader::GradientInfo& info, |
| 129 | SkString* result) { |
| 130 | /* We want to linearly interpolate from the previous color to the next. |
| 131 | Scale the colors from 0..255 to 0..1 and determine the multipliers |
| 132 | for interpolation. |
| 133 | C{r,g,b}(t, section) = t - offset_(section-1) + t * Multiplier{r,g,b}. |
| 134 | */ |
| 135 | static const int kColorComponents = 3; |
twiz@google.com | 316338a | 2011-03-09 23:14:04 +0000 | [diff] [blame] | 136 | typedef SkScalar ColorTuple[kColorComponents]; |
| 137 | SkAutoSTMalloc<4, ColorTuple> colorDataAlloc(info.fColorCount); |
| 138 | ColorTuple *colorData = colorDataAlloc.get(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 139 | const SkScalar scale = SkScalarInvert(SkIntToScalar(255)); |
| 140 | for (int i = 0; i < info.fColorCount; i++) { |
| 141 | colorData[i][0] = SkScalarMul(SkColorGetR(info.fColors[i]), scale); |
| 142 | colorData[i][1] = SkScalarMul(SkColorGetG(info.fColors[i]), scale); |
| 143 | colorData[i][2] = SkScalarMul(SkColorGetB(info.fColors[i]), scale); |
| 144 | } |
| 145 | |
| 146 | // Clamp the initial color. |
| 147 | result->append("dup 0 le {pop "); |
| 148 | result->appendScalar(colorData[0][0]); |
| 149 | result->append(" "); |
| 150 | result->appendScalar(colorData[0][1]); |
| 151 | result->append(" "); |
| 152 | result->appendScalar(colorData[0][2]); |
| 153 | result->append(" }\n"); |
| 154 | |
| 155 | // The gradient colors. |
| 156 | for (int i = 1 ; i < info.fColorCount; i++) { |
| 157 | result->append("{dup "); |
| 158 | result->appendScalar(info.fColorOffsets[i]); |
| 159 | result->append(" le {"); |
| 160 | if (info.fColorOffsets[i - 1] != 0) { |
| 161 | result->appendScalar(info.fColorOffsets[i - 1]); |
| 162 | result->append(" sub\n"); |
| 163 | } |
| 164 | |
| 165 | interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1], |
| 166 | colorData[i], colorData[i - 1], kColorComponents, |
| 167 | result); |
| 168 | result->append("}\n"); |
| 169 | } |
| 170 | |
| 171 | // Clamp the final color. |
| 172 | result->append("{pop "); |
| 173 | result->appendScalar(colorData[info.fColorCount - 1][0]); |
| 174 | result->append(" "); |
| 175 | result->appendScalar(colorData[info.fColorCount - 1][1]); |
| 176 | result->append(" "); |
| 177 | result->appendScalar(colorData[info.fColorCount - 1][2]); |
| 178 | |
| 179 | for (int i = 0 ; i < info.fColorCount; i++) { |
| 180 | result->append("} ifelse\n"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */ |
| 185 | static void tileModeCode(SkShader::TileMode mode, SkString* result) { |
| 186 | if (mode == SkShader::kRepeat_TileMode) { |
| 187 | result->append("dup truncate sub\n"); // Get the fractional part. |
| 188 | result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1) |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (mode == SkShader::kMirror_TileMode) { |
| 193 | // Map t mod 2 into [0, 1, 1, 0]. |
| 194 | // Code Stack |
| 195 | result->append("abs " // Map negative to positive. |
| 196 | "dup " // t.s t.s |
| 197 | "truncate " // t.s t |
| 198 | "dup " // t.s t t |
| 199 | "cvi " // t.s t T |
| 200 | "2 mod " // t.s t (i mod 2) |
| 201 | "1 eq " // t.s t true|false |
| 202 | "3 1 roll " // true|false t.s t |
| 203 | "sub " // true|false 0.s |
| 204 | "exch " // 0.s true|false |
| 205 | "{1 exch sub} if\n"); // 1 - 0.s|0.s |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static SkString linearCode(const SkShader::GradientInfo& info) { |
| 210 | SkString function("{pop\n"); // Just ditch the y value. |
| 211 | tileModeCode(info.fTileMode, &function); |
| 212 | gradientFunctionCode(info, &function); |
| 213 | function.append("}"); |
| 214 | return function; |
| 215 | } |
| 216 | |
| 217 | static SkString radialCode(const SkShader::GradientInfo& info) { |
| 218 | SkString function("{"); |
| 219 | // Find the distance from the origin. |
| 220 | function.append("dup " // x y y |
| 221 | "mul " // x y^2 |
| 222 | "exch " // y^2 x |
| 223 | "dup " // y^2 x x |
| 224 | "mul " // y^2 x^2 |
| 225 | "add " // y^2+x^2 |
| 226 | "sqrt\n"); // sqrt(y^2+x^2) |
| 227 | |
| 228 | tileModeCode(info.fTileMode, &function); |
| 229 | gradientFunctionCode(info, &function); |
| 230 | function.append("}"); |
| 231 | return function; |
| 232 | } |
| 233 | |
| 234 | /* The math here is all based on the description in Two_Point_Radial_Gradient, |
| 235 | with one simplification, the coordinate space has been scaled so that |
| 236 | Dr = 1. This means we don't need to scale the entire equation by 1/Dr^2. |
| 237 | */ |
| 238 | static SkString twoPointRadialCode(const SkShader::GradientInfo& info) { |
| 239 | SkScalar dx = info.fPoint[0].fX - info.fPoint[1].fX; |
| 240 | SkScalar dy = info.fPoint[0].fY - info.fPoint[1].fY; |
| 241 | SkScalar sr = info.fRadius[0]; |
| 242 | SkScalar a = SkScalarMul(dx, dx) + SkScalarMul(dy, dy) - SK_Scalar1; |
| 243 | bool posRoot = info.fRadius[1] > info.fRadius[0]; |
| 244 | |
| 245 | // We start with a stack of (x y), copy it and then consume one copy in |
| 246 | // order to calculate b and the other to calculate c. |
| 247 | SkString function("{"); |
| 248 | function.append("2 copy "); |
| 249 | |
| 250 | // Calculate -b and b^2. |
| 251 | function.appendScalar(dy); |
| 252 | function.append(" mul exch "); |
| 253 | function.appendScalar(dx); |
| 254 | function.append(" mul add "); |
| 255 | function.appendScalar(sr); |
| 256 | function.append(" sub 2 mul neg dup dup mul\n"); |
| 257 | |
| 258 | // Calculate c |
| 259 | function.append("4 2 roll dup mul exch dup mul add "); |
| 260 | function.appendScalar(SkScalarMul(sr, sr)); |
| 261 | function.append(" sub\n"); |
| 262 | |
| 263 | // Calculate the determinate |
| 264 | function.appendScalar(SkScalarMul(SkIntToScalar(4), a)); |
| 265 | function.append(" mul sub abs sqrt\n"); |
| 266 | |
| 267 | // And then the final value of t. |
| 268 | if (posRoot) { |
| 269 | function.append("sub "); |
| 270 | } else { |
| 271 | function.append("add "); |
| 272 | } |
| 273 | function.appendScalar(SkScalarMul(SkIntToScalar(2), a)); |
| 274 | function.append(" div\n"); |
| 275 | |
| 276 | tileModeCode(info.fTileMode, &function); |
| 277 | gradientFunctionCode(info, &function); |
| 278 | function.append("}"); |
| 279 | return function; |
| 280 | } |
| 281 | |
| 282 | static SkString sweepCode(const SkShader::GradientInfo& info) { |
| 283 | SkString function("{exch atan 360 div\n"); |
| 284 | tileModeCode(info.fTileMode, &function); |
| 285 | gradientFunctionCode(info, &function); |
| 286 | function.append("}"); |
| 287 | return function; |
| 288 | } |
| 289 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 290 | class SkPDFShader::State { |
| 291 | public: |
| 292 | SkShader::GradientType fType; |
| 293 | SkShader::GradientInfo fInfo; |
| 294 | SkAutoFree fColorData; |
| 295 | SkMatrix fCanvasTransform; |
| 296 | SkMatrix fShaderTransform; |
| 297 | SkIRect fBBox; |
| 298 | |
| 299 | SkBitmap fImage; |
| 300 | uint32_t fPixelGeneration; |
| 301 | SkShader::TileMode fImageTileModes[2]; |
| 302 | |
| 303 | explicit State(const SkShader& shader, const SkMatrix& canvasTransform, |
| 304 | const SkIRect& bbox); |
| 305 | bool operator==(const State& b) const; |
| 306 | }; |
| 307 | |
| 308 | class SkPDFFunctionShader : public SkPDFDict, public SkPDFShader { |
| 309 | public: |
| 310 | SkPDFFunctionShader(SkPDFShader::State* state); |
| 311 | ~SkPDFFunctionShader() { |
| 312 | if (isValid()) { |
| 313 | RemoveShader(this); |
| 314 | } |
| 315 | fResources.unrefAll(); |
| 316 | } |
| 317 | |
| 318 | bool isValid() { return fResources.count() > 0; } |
| 319 | |
| 320 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 321 | GetResourcesHelper(&fResources, resourceList); |
| 322 | } |
| 323 | |
| 324 | private: |
| 325 | static SkPDFObject* RangeObject(); |
| 326 | |
| 327 | SkTDArray<SkPDFObject*> fResources; |
| 328 | SkAutoTDelete<const SkPDFShader::State> fState; |
| 329 | |
| 330 | SkPDFStream* makePSFunction(const SkString& psCode, SkPDFArray* domain); |
| 331 | }; |
| 332 | |
| 333 | class SkPDFImageShader : public SkPDFStream, public SkPDFShader { |
| 334 | public: |
| 335 | SkPDFImageShader(SkPDFShader::State* state); |
| 336 | ~SkPDFImageShader() { |
| 337 | RemoveShader(this); |
| 338 | fResources.unrefAll(); |
| 339 | } |
| 340 | |
| 341 | void getResources(SkTDArray<SkPDFObject*>* resourceList) { |
| 342 | GetResourcesHelper(&fResources, resourceList); |
| 343 | } |
| 344 | |
| 345 | private: |
| 346 | SkTDArray<SkPDFObject*> fResources; |
| 347 | SkAutoTDelete<const SkPDFShader::State> fState; |
| 348 | }; |
| 349 | |
| 350 | SkPDFShader::SkPDFShader() {} |
| 351 | |
| 352 | // static |
| 353 | void SkPDFShader::RemoveShader(SkPDFObject* shader) { |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 354 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 355 | ShaderCanonicalEntry entry(shader, NULL); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 356 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 357 | SkASSERT(index >= 0); |
| 358 | CanonicalShaders().removeShuffle(index); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 362 | SkPDFObject* SkPDFShader::GetPDFShader(const SkShader& shader, |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 363 | const SkMatrix& matrix, |
| 364 | const SkIRect& surfaceBBox) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 365 | SkPDFObject* result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 366 | SkAutoMutexAcquire lock(CanonicalShadersMutex()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 367 | SkAutoTDelete<State> shaderState(new State(shader, matrix, surfaceBBox)); |
| 368 | |
| 369 | ShaderCanonicalEntry entry(NULL, shaderState.get()); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 370 | int index = CanonicalShaders().find(entry); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 371 | if (index >= 0) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 372 | result = CanonicalShaders()[index].fPDFShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 373 | result->ref(); |
| 374 | return result; |
| 375 | } |
| 376 | // The PDFShader takes ownership of the shaderSate. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 377 | if (shaderState.get()->fType == SkShader::kNone_GradientType) { |
| 378 | result = new SkPDFImageShader(shaderState.detach()); |
| 379 | } else { |
| 380 | SkPDFFunctionShader* functionShader = |
| 381 | new SkPDFFunctionShader(shaderState.detach()); |
| 382 | if (!functionShader->isValid()) { |
| 383 | delete functionShader; |
| 384 | return NULL; |
| 385 | } |
| 386 | result = functionShader; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 387 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 388 | entry.fPDFShader = result; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 389 | CanonicalShaders().push(entry); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 390 | return result; // return the reference that came from new. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // static |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 394 | SkTDArray<SkPDFShader::ShaderCanonicalEntry>& SkPDFShader::CanonicalShaders() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 395 | // This initialization is only thread safe with gcc. |
| 396 | static SkTDArray<ShaderCanonicalEntry> gCanonicalShaders; |
| 397 | return gCanonicalShaders; |
| 398 | } |
| 399 | |
| 400 | // static |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 401 | SkMutex& SkPDFShader::CanonicalShadersMutex() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 402 | // This initialization is only thread safe with gcc. |
| 403 | static SkMutex gCanonicalShadersMutex; |
| 404 | return gCanonicalShadersMutex; |
| 405 | } |
| 406 | |
| 407 | // static |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 408 | SkPDFObject* SkPDFFunctionShader::RangeObject() { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 409 | // This initialization is only thread safe with gcc. |
| 410 | static SkPDFArray* range = NULL; |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 411 | // 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] | 412 | // populate domain. |
| 413 | if (range == NULL) { |
| 414 | range = new SkPDFArray; |
| 415 | range->reserve(6); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 416 | range->appendInt(0); |
| 417 | range->appendInt(1); |
| 418 | range->appendInt(0); |
| 419 | range->appendInt(1); |
| 420 | range->appendInt(0); |
| 421 | range->appendInt(1); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 422 | } |
| 423 | return range; |
| 424 | } |
| 425 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 426 | SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) |
| 427 | : SkPDFDict("Pattern"), |
| 428 | fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 429 | SkString (*codeFunction)(const SkShader::GradientInfo& info) = NULL; |
| 430 | SkPoint transformPoints[2]; |
| 431 | |
| 432 | // Depending on the type of the gradient, we want to transform the |
| 433 | // coordinate space in different ways. |
| 434 | const SkShader::GradientInfo* info = &fState.get()->fInfo; |
| 435 | transformPoints[0] = info->fPoint[0]; |
| 436 | transformPoints[1] = info->fPoint[1]; |
| 437 | switch (fState.get()->fType) { |
| 438 | case SkShader::kLinear_GradientType: |
| 439 | codeFunction = &linearCode; |
| 440 | break; |
| 441 | case SkShader::kRadial_GradientType: |
| 442 | transformPoints[1] = transformPoints[0]; |
| 443 | transformPoints[1].fX += info->fRadius[0]; |
| 444 | codeFunction = &radialCode; |
| 445 | break; |
| 446 | case SkShader::kRadial2_GradientType: { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 447 | // Bail out if the radii are the same. Empty fResources signals |
| 448 | // an error and isValid will return false. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 449 | if (info->fRadius[0] == info->fRadius[1]) { |
| 450 | return; |
| 451 | } |
| 452 | transformPoints[1] = transformPoints[0]; |
| 453 | SkScalar dr = info->fRadius[1] - info->fRadius[0]; |
| 454 | transformPoints[1].fX += dr; |
| 455 | codeFunction = &twoPointRadialCode; |
| 456 | break; |
| 457 | } |
| 458 | case SkShader::kSweep_GradientType: |
| 459 | transformPoints[1] = transformPoints[0]; |
| 460 | transformPoints[1].fX += 1; |
| 461 | codeFunction = &sweepCode; |
| 462 | break; |
| 463 | case SkShader::kColor_GradientType: |
| 464 | case SkShader::kNone_GradientType: |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | // Move any scaling (assuming a unit gradient) or translation |
| 469 | // (and rotation for linear gradient), of the final gradient from |
| 470 | // info->fPoints to the matrix (updating bbox appropriately). Now |
| 471 | // the gradient can be drawn on on the unit segment. |
| 472 | SkMatrix mapperMatrix; |
| 473 | unitToPointsMatrix(transformPoints, &mapperMatrix); |
| 474 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
| 475 | finalMatrix.preConcat(mapperMatrix); |
| 476 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
| 477 | SkRect bbox; |
| 478 | bbox.set(fState.get()->fBBox); |
| 479 | transformBBox(finalMatrix, &bbox); |
| 480 | |
| 481 | SkRefPtr<SkPDFArray> domain = new SkPDFArray; |
| 482 | domain->unref(); // SkRefPtr and new both took a reference. |
| 483 | domain->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 484 | domain->appendScalar(bbox.fLeft); |
| 485 | domain->appendScalar(bbox.fRight); |
| 486 | domain->appendScalar(bbox.fTop); |
| 487 | domain->appendScalar(bbox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 488 | |
| 489 | SkString functionCode; |
| 490 | // The two point radial gradient further references fState.get()->fInfo |
| 491 | // in translating from x, y coordinates to the t parameter. So, we have |
| 492 | // to transform the points and radii according to the calculated matrix. |
| 493 | if (fState.get()->fType == SkShader::kRadial2_GradientType) { |
| 494 | SkShader::GradientInfo twoPointRadialInfo = *info; |
| 495 | SkMatrix inverseMapperMatrix; |
| 496 | mapperMatrix.invert(&inverseMapperMatrix); |
| 497 | inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2); |
| 498 | twoPointRadialInfo.fRadius[0] = |
| 499 | inverseMapperMatrix.mapRadius(info->fRadius[0]); |
| 500 | twoPointRadialInfo.fRadius[1] = |
| 501 | inverseMapperMatrix.mapRadius(info->fRadius[1]); |
| 502 | functionCode = codeFunction(twoPointRadialInfo); |
| 503 | } else { |
| 504 | functionCode = codeFunction(*info); |
| 505 | } |
| 506 | |
| 507 | SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get()); |
| 508 | // Pass one reference to fResources, SkRefPtr and new both took a reference. |
| 509 | fResources.push(function.get()); |
| 510 | |
| 511 | SkRefPtr<SkPDFDict> pdfShader = new SkPDFDict; |
| 512 | pdfShader->unref(); // SkRefPtr and new both took a reference. |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 513 | pdfShader->insertInt("ShadingType", 1); |
| 514 | pdfShader->insertName("ColorSpace", "DeviceRGB"); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 515 | pdfShader->insert("Domain", domain.get()); |
| 516 | pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref(); |
| 517 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 518 | insertInt("PatternType", 2); |
| 519 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
| 520 | insert("Shading", pdfShader.get()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 521 | } |
| 522 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 523 | SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 524 | fState.get()->fImage.lockPixels(); |
| 525 | |
| 526 | SkMatrix finalMatrix = fState.get()->fCanvasTransform; |
| 527 | finalMatrix.preConcat(fState.get()->fShaderTransform); |
| 528 | SkRect surfaceBBox; |
| 529 | surfaceBBox.set(fState.get()->fBBox); |
| 530 | transformBBox(finalMatrix, &surfaceBBox); |
| 531 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 532 | SkMatrix unflip; |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 533 | unflip.setTranslate(0, SkScalarRound(surfaceBBox.height())); |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 534 | unflip.preScale(1, -1); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 535 | SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()), |
| 536 | SkScalarRound(surfaceBBox.height())); |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 537 | SkPDFDevice pattern(size, size, unflip); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 538 | SkCanvas canvas(&pattern); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 539 | canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop); |
| 540 | finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 541 | |
| 542 | const SkBitmap* image = &fState.get()->fImage; |
| 543 | int width = image->width(); |
| 544 | int height = image->height(); |
| 545 | SkShader::TileMode tileModes[2]; |
| 546 | tileModes[0] = fState.get()->fImageTileModes[0]; |
| 547 | tileModes[1] = fState.get()->fImageTileModes[1]; |
| 548 | |
| 549 | canvas.drawBitmap(*image, 0, 0); |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 550 | SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop, |
| 551 | width, height); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 552 | |
| 553 | // Tiling is implied. First we handle mirroring. |
| 554 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 555 | SkMatrix xMirror; |
| 556 | xMirror.setScale(-1, 1); |
| 557 | xMirror.postTranslate(2 * width, 0); |
| 558 | canvas.drawBitmapMatrix(*image, xMirror); |
| 559 | patternBBox.fRight += width; |
| 560 | } |
| 561 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
| 562 | SkMatrix yMirror; |
| 563 | yMirror.setScale(1, -1); |
| 564 | yMirror.postTranslate(0, 2 * height); |
| 565 | canvas.drawBitmapMatrix(*image, yMirror); |
| 566 | patternBBox.fBottom += height; |
| 567 | } |
| 568 | if (tileModes[0] == SkShader::kMirror_TileMode && |
| 569 | tileModes[1] == SkShader::kMirror_TileMode) { |
| 570 | SkMatrix mirror; |
| 571 | mirror.setScale(-1, -1); |
| 572 | mirror.postTranslate(2 * width, 2 * height); |
| 573 | canvas.drawBitmapMatrix(*image, mirror); |
| 574 | } |
| 575 | |
| 576 | // Then handle Clamping, which requires expanding the pattern canvas to |
| 577 | // cover the entire surfaceBBox. |
| 578 | |
| 579 | // If both x and y are in clamp mode, we start by filling in the corners. |
| 580 | // (Which are just a rectangles of the corner colors.) |
| 581 | if (tileModes[0] == SkShader::kClamp_TileMode && |
| 582 | tileModes[1] == SkShader::kClamp_TileMode) { |
| 583 | SkPaint paint; |
| 584 | SkRect rect; |
| 585 | rect = SkRect::MakeLTRB(surfaceBBox.fLeft, surfaceBBox.fTop, 0, 0); |
| 586 | if (!rect.isEmpty()) { |
| 587 | paint.setColor(image->getColor(0, 0)); |
| 588 | canvas.drawRect(rect, paint); |
| 589 | } |
| 590 | |
| 591 | rect = SkRect::MakeLTRB(width, surfaceBBox.fTop, surfaceBBox.fRight, 0); |
| 592 | if (!rect.isEmpty()) { |
| 593 | paint.setColor(image->getColor(width - 1, 0)); |
| 594 | canvas.drawRect(rect, paint); |
| 595 | } |
| 596 | |
| 597 | rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight, |
| 598 | surfaceBBox.fBottom); |
| 599 | if (!rect.isEmpty()) { |
| 600 | paint.setColor(image->getColor(width - 1, height - 1)); |
| 601 | canvas.drawRect(rect, paint); |
| 602 | } |
| 603 | |
| 604 | rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0, |
| 605 | surfaceBBox.fBottom); |
| 606 | if (!rect.isEmpty()) { |
| 607 | paint.setColor(image->getColor(0, height - 1)); |
| 608 | canvas.drawRect(rect, paint); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // Then expand the left, right, top, then bottom. |
| 613 | if (tileModes[0] == SkShader::kClamp_TileMode) { |
| 614 | SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, height); |
| 615 | if (surfaceBBox.fLeft < 0) { |
| 616 | SkBitmap left; |
| 617 | SkAssertResult(image->extractSubset(&left, subset)); |
| 618 | |
| 619 | SkMatrix leftMatrix; |
| 620 | leftMatrix.setScale(-surfaceBBox.fLeft, 1); |
| 621 | leftMatrix.postTranslate(surfaceBBox.fLeft, 0); |
| 622 | canvas.drawBitmapMatrix(left, leftMatrix); |
| 623 | |
| 624 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
| 625 | leftMatrix.postScale(1, -1); |
| 626 | leftMatrix.postTranslate(0, 2 * height); |
| 627 | canvas.drawBitmapMatrix(left, leftMatrix); |
| 628 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 629 | patternBBox.fLeft = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | if (surfaceBBox.fRight > width) { |
| 633 | SkBitmap right; |
| 634 | subset.offset(width - 1, 0); |
| 635 | SkAssertResult(image->extractSubset(&right, subset)); |
| 636 | |
| 637 | SkMatrix rightMatrix; |
| 638 | rightMatrix.setScale(surfaceBBox.fRight - width, 1); |
| 639 | rightMatrix.postTranslate(width, 0); |
| 640 | canvas.drawBitmapMatrix(right, rightMatrix); |
| 641 | |
| 642 | if (tileModes[1] == SkShader::kMirror_TileMode) { |
| 643 | rightMatrix.postScale(1, -1); |
| 644 | rightMatrix.postTranslate(0, 2 * height); |
| 645 | canvas.drawBitmapMatrix(right, rightMatrix); |
| 646 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 647 | patternBBox.fRight = surfaceBBox.width(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | if (tileModes[1] == SkShader::kClamp_TileMode) { |
| 652 | SkIRect subset = SkIRect::MakeXYWH(0, 0, width, 1); |
| 653 | if (surfaceBBox.fTop < 0) { |
| 654 | SkBitmap top; |
| 655 | SkAssertResult(image->extractSubset(&top, subset)); |
| 656 | |
| 657 | SkMatrix topMatrix; |
| 658 | topMatrix.setScale(1, -surfaceBBox.fTop); |
| 659 | topMatrix.postTranslate(0, surfaceBBox.fTop); |
| 660 | canvas.drawBitmapMatrix(top, topMatrix); |
| 661 | |
| 662 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 663 | topMatrix.postScale(-1, 1); |
| 664 | topMatrix.postTranslate(2 * width, 0); |
| 665 | canvas.drawBitmapMatrix(top, topMatrix); |
| 666 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 667 | patternBBox.fTop = 0; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | if (surfaceBBox.fBottom > height) { |
| 671 | SkBitmap bottom; |
| 672 | subset.offset(0, height - 1); |
| 673 | SkAssertResult(image->extractSubset(&bottom, subset)); |
| 674 | |
| 675 | SkMatrix bottomMatrix; |
| 676 | bottomMatrix.setScale(1, surfaceBBox.fBottom - height); |
| 677 | bottomMatrix.postTranslate(0, height); |
| 678 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
| 679 | |
| 680 | if (tileModes[0] == SkShader::kMirror_TileMode) { |
| 681 | bottomMatrix.postScale(-1, 1); |
| 682 | bottomMatrix.postTranslate(2 * width, 0); |
| 683 | canvas.drawBitmapMatrix(bottom, bottomMatrix); |
| 684 | } |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 685 | patternBBox.fBottom = surfaceBBox.height(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray; |
| 690 | patternBBoxArray->unref(); // SkRefPtr and new both took a reference. |
| 691 | patternBBoxArray->reserve(4); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 692 | patternBBoxArray->appendScalar(patternBBox.fLeft); |
| 693 | patternBBoxArray->appendScalar(patternBBox.fTop); |
| 694 | patternBBoxArray->appendScalar(patternBBox.fRight); |
| 695 | patternBBoxArray->appendScalar(patternBBox.fBottom); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 696 | |
| 697 | // Put the canvas into the pattern stream (fContent). |
| 698 | SkRefPtr<SkStream> content = pattern.content(); |
| 699 | content->unref(); // SkRefPtr and content() both took a reference. |
| 700 | pattern.getResources(&fResources); |
| 701 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 702 | setData(content.get()); |
| 703 | insertName("Type", "Pattern"); |
| 704 | insertInt("PatternType", 1); |
| 705 | insertInt("PaintType", 1); |
| 706 | insertInt("TilingType", 1); |
| 707 | insert("BBox", patternBBoxArray.get()); |
| 708 | insertScalar("XStep", patternBBox.width()); |
| 709 | insertScalar("YStep", patternBBox.height()); |
| 710 | insert("Resources", pattern.getResourceDict().get()); |
| 711 | insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 712 | |
| 713 | fState.get()->fImage.unlockPixels(); |
| 714 | } |
| 715 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 716 | SkPDFStream* SkPDFFunctionShader::makePSFunction(const SkString& psCode, |
| 717 | SkPDFArray* domain) { |
| 718 | SkAutoDataUnref funcData(SkData::NewWithCopy(psCode.c_str(), |
| 719 | psCode.size())); |
| 720 | SkPDFStream* result = new SkPDFStream(funcData.get()); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 721 | result->insertInt("FunctionType", 4); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 722 | result->insert("Domain", domain); |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 723 | result->insert("Range", RangeObject()); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 724 | return result; |
| 725 | } |
| 726 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame^] | 727 | SkPDFShader::ShaderCanonicalEntry::ShaderCanonicalEntry(SkPDFObject* pdfShader, |
| 728 | const State* state) |
| 729 | : fPDFShader(pdfShader), |
| 730 | fState(state) { |
| 731 | } |
| 732 | |
| 733 | bool SkPDFShader::ShaderCanonicalEntry::operator==( |
| 734 | const ShaderCanonicalEntry& b) const { |
| 735 | return fPDFShader == b.fPDFShader || |
| 736 | (fState != NULL && b.fState != NULL && *fState == *b.fState); |
| 737 | } |
| 738 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 739 | bool SkPDFShader::State::operator==(const SkPDFShader::State& b) const { |
| 740 | if (fType != b.fType || |
| 741 | fCanvasTransform != b.fCanvasTransform || |
| 742 | fShaderTransform != b.fShaderTransform || |
| 743 | fBBox != b.fBBox) { |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | if (fType == SkShader::kNone_GradientType) { |
| 748 | if (fPixelGeneration != b.fPixelGeneration || |
| 749 | fPixelGeneration == 0 || |
| 750 | fImageTileModes[0] != b.fImageTileModes[0] || |
| 751 | fImageTileModes[1] != b.fImageTileModes[1]) { |
| 752 | return false; |
| 753 | } |
| 754 | } else { |
| 755 | if (fInfo.fColorCount != b.fInfo.fColorCount || |
| 756 | memcmp(fInfo.fColors, b.fInfo.fColors, |
| 757 | sizeof(SkColor) * fInfo.fColorCount) != 0 || |
| 758 | memcmp(fInfo.fColorOffsets, b.fInfo.fColorOffsets, |
| 759 | sizeof(SkScalar) * fInfo.fColorCount) != 0 || |
| 760 | fInfo.fPoint[0] != b.fInfo.fPoint[0] || |
| 761 | fInfo.fTileMode != b.fInfo.fTileMode) { |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | switch (fType) { |
| 766 | case SkShader::kLinear_GradientType: |
| 767 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1]) { |
| 768 | return false; |
| 769 | } |
| 770 | break; |
| 771 | case SkShader::kRadial_GradientType: |
| 772 | if (fInfo.fRadius[0] != b.fInfo.fRadius[0]) { |
| 773 | return false; |
| 774 | } |
| 775 | break; |
| 776 | case SkShader::kRadial2_GradientType: |
| 777 | if (fInfo.fPoint[1] != b.fInfo.fPoint[1] || |
| 778 | fInfo.fRadius[0] != b.fInfo.fRadius[0] || |
| 779 | fInfo.fRadius[1] != b.fInfo.fRadius[1]) { |
| 780 | return false; |
| 781 | } |
| 782 | break; |
| 783 | case SkShader::kSweep_GradientType: |
| 784 | case SkShader::kNone_GradientType: |
| 785 | case SkShader::kColor_GradientType: |
| 786 | break; |
| 787 | } |
| 788 | } |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | SkPDFShader::State::State(const SkShader& shader, |
| 793 | const SkMatrix& canvasTransform, const SkIRect& bbox) |
| 794 | : fCanvasTransform(canvasTransform), |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 795 | fBBox(bbox), |
| 796 | fPixelGeneration(0) { |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 797 | fInfo.fColorCount = 0; |
| 798 | fInfo.fColors = NULL; |
| 799 | fInfo.fColorOffsets = NULL; |
| 800 | shader.getLocalMatrix(&fShaderTransform); |
vandebo@chromium.org | e1bc274 | 2011-06-21 22:26:39 +0000 | [diff] [blame] | 801 | fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 802 | |
| 803 | fType = shader.asAGradient(&fInfo); |
| 804 | |
| 805 | if (fType == SkShader::kNone_GradientType) { |
| 806 | SkShader::BitmapType bitmapType; |
| 807 | SkMatrix matrix; |
| 808 | bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes, NULL); |
| 809 | if (bitmapType != SkShader::kDefault_BitmapType) { |
| 810 | fImage.reset(); |
| 811 | return; |
| 812 | } |
| 813 | SkASSERT(matrix.isIdentity()); |
| 814 | fPixelGeneration = fImage.getGenerationID(); |
| 815 | } else { |
| 816 | fColorData.set(sk_malloc_throw( |
| 817 | fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
| 818 | fInfo.fColors = (SkColor*)fColorData.get(); |
| 819 | fInfo.fColorOffsets = (SkScalar*)(fInfo.fColors + fInfo.fColorCount); |
| 820 | shader.asAGradient(&fInfo); |
| 821 | } |
| 822 | } |