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