rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkGradientShaderPriv.h" |
| 9 | #include "SkLinearGradient.h" |
| 10 | #include "SkRadialGradient.h" |
| 11 | #include "SkTwoPointRadialGradient.h" |
| 12 | #include "SkTwoPointConicalGradient.h" |
| 13 | #include "SkSweepGradient.h" |
| 14 | |
| 15 | SkGradientShaderBase::SkGradientShaderBase(const SkColor colors[], const SkScalar pos[], |
| 16 | int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper) { |
| 17 | SkASSERT(colorCount > 1); |
| 18 | |
| 19 | fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return |
| 20 | |
| 21 | fMapper = mapper; |
| 22 | SkSafeRef(mapper); |
| 23 | |
| 24 | SkASSERT((unsigned)mode < SkShader::kTileModeCount); |
| 25 | SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); |
| 26 | fTileMode = mode; |
| 27 | fTileProc = gTileProcs[mode]; |
| 28 | |
| 29 | fCache16 = fCache16Storage = NULL; |
| 30 | fCache32 = NULL; |
| 31 | fCache32PixelRef = NULL; |
| 32 | |
| 33 | /* Note: we let the caller skip the first and/or last position. |
| 34 | i.e. pos[0] = 0.3, pos[1] = 0.7 |
| 35 | In these cases, we insert dummy entries to ensure that the final data |
| 36 | will be bracketed by [0, 1]. |
| 37 | i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1 |
| 38 | |
| 39 | Thus colorCount (the caller's value, and fColorCount (our value) may |
| 40 | differ by up to 2. In the above example: |
| 41 | colorCount = 2 |
| 42 | fColorCount = 4 |
| 43 | */ |
| 44 | fColorCount = colorCount; |
| 45 | // check if we need to add in dummy start and/or end position/colors |
| 46 | bool dummyFirst = false; |
| 47 | bool dummyLast = false; |
| 48 | if (pos) { |
| 49 | dummyFirst = pos[0] != 0; |
| 50 | dummyLast = pos[colorCount - 1] != SK_Scalar1; |
| 51 | fColorCount += dummyFirst + dummyLast; |
| 52 | } |
| 53 | |
| 54 | if (fColorCount > kColorStorageCount) { |
| 55 | size_t size = sizeof(SkColor) + sizeof(Rec); |
| 56 | fOrigColors = reinterpret_cast<SkColor*>( |
| 57 | sk_malloc_throw(size * fColorCount)); |
| 58 | } |
| 59 | else { |
| 60 | fOrigColors = fStorage; |
| 61 | } |
| 62 | |
| 63 | // Now copy over the colors, adding the dummies as needed |
| 64 | { |
| 65 | SkColor* origColors = fOrigColors; |
| 66 | if (dummyFirst) { |
| 67 | *origColors++ = colors[0]; |
| 68 | } |
| 69 | memcpy(origColors, colors, colorCount * sizeof(SkColor)); |
| 70 | if (dummyLast) { |
| 71 | origColors += colorCount; |
| 72 | *origColors = colors[colorCount - 1]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fRecs = (Rec*)(fOrigColors + fColorCount); |
| 77 | if (fColorCount > 2) { |
| 78 | Rec* recs = fRecs; |
| 79 | recs->fPos = 0; |
| 80 | // recs->fScale = 0; // unused; |
| 81 | recs += 1; |
| 82 | if (pos) { |
| 83 | /* We need to convert the user's array of relative positions into |
| 84 | fixed-point positions and scale factors. We need these results |
| 85 | to be strictly monotonic (no two values equal or out of order). |
| 86 | Hence this complex loop that just jams a zero for the scale |
| 87 | value if it sees a segment out of order, and it assures that |
| 88 | we start at 0 and end at 1.0 |
| 89 | */ |
| 90 | SkFixed prev = 0; |
| 91 | int startIndex = dummyFirst ? 0 : 1; |
| 92 | int count = colorCount + dummyLast; |
| 93 | for (int i = startIndex; i < count; i++) { |
| 94 | // force the last value to be 1.0 |
| 95 | SkFixed curr; |
| 96 | if (i == colorCount) { // we're really at the dummyLast |
| 97 | curr = SK_Fixed1; |
| 98 | } else { |
| 99 | curr = SkScalarToFixed(pos[i]); |
| 100 | } |
| 101 | // pin curr withing range |
| 102 | if (curr < 0) { |
| 103 | curr = 0; |
| 104 | } else if (curr > SK_Fixed1) { |
| 105 | curr = SK_Fixed1; |
| 106 | } |
| 107 | recs->fPos = curr; |
| 108 | if (curr > prev) { |
| 109 | recs->fScale = (1 << 24) / (curr - prev); |
| 110 | } else { |
| 111 | recs->fScale = 0; // ignore this segment |
| 112 | } |
| 113 | // get ready for the next value |
| 114 | prev = curr; |
| 115 | recs += 1; |
| 116 | } |
| 117 | } else { // assume even distribution |
| 118 | SkFixed dp = SK_Fixed1 / (colorCount - 1); |
| 119 | SkFixed p = dp; |
| 120 | SkFixed scale = (colorCount - 1) << 8; // (1 << 24) / dp |
| 121 | for (int i = 1; i < colorCount; i++) { |
| 122 | recs->fPos = p; |
| 123 | recs->fScale = scale; |
| 124 | recs += 1; |
| 125 | p += dp; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | this->initCommon(); |
| 130 | } |
| 131 | |
| 132 | SkGradientShaderBase::SkGradientShaderBase(SkFlattenableReadBuffer& buffer) : |
| 133 | INHERITED(buffer) { |
| 134 | fCacheAlpha = 256; |
| 135 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 136 | fMapper = buffer.readFlattenableT<SkUnitMapper>(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 137 | |
| 138 | fCache16 = fCache16Storage = NULL; |
| 139 | fCache32 = NULL; |
| 140 | fCache32PixelRef = NULL; |
| 141 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 142 | int colorCount = fColorCount = buffer.getArrayCount(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 143 | if (colorCount > kColorStorageCount) { |
| 144 | size_t size = sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec); |
| 145 | fOrigColors = (SkColor*)sk_malloc_throw(size * colorCount); |
| 146 | } else { |
| 147 | fOrigColors = fStorage; |
| 148 | } |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 149 | buffer.readColorArray(fOrigColors); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 150 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 151 | fTileMode = (TileMode)buffer.readUInt(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 152 | fTileProc = gTileProcs[fTileMode]; |
| 153 | fRecs = (Rec*)(fOrigColors + colorCount); |
| 154 | if (colorCount > 2) { |
| 155 | Rec* recs = fRecs; |
| 156 | recs[0].fPos = 0; |
| 157 | for (int i = 1; i < colorCount; i++) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 158 | recs[i].fPos = buffer.readInt(); |
| 159 | recs[i].fScale = buffer.readUInt(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | buffer.readMatrix(&fPtsToUnit); |
| 163 | this->initCommon(); |
| 164 | } |
| 165 | |
| 166 | SkGradientShaderBase::~SkGradientShaderBase() { |
| 167 | if (fCache16Storage) { |
| 168 | sk_free(fCache16Storage); |
| 169 | } |
| 170 | SkSafeUnref(fCache32PixelRef); |
| 171 | if (fOrigColors != fStorage) { |
| 172 | sk_free(fOrigColors); |
| 173 | } |
| 174 | SkSafeUnref(fMapper); |
| 175 | } |
| 176 | |
| 177 | void SkGradientShaderBase::initCommon() { |
| 178 | fFlags = 0; |
| 179 | unsigned colorAlpha = 0xFF; |
| 180 | for (int i = 0; i < fColorCount; i++) { |
| 181 | colorAlpha &= SkColorGetA(fOrigColors[i]); |
| 182 | } |
| 183 | fColorsAreOpaque = colorAlpha == 0xFF; |
| 184 | } |
| 185 | |
| 186 | void SkGradientShaderBase::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 187 | this->INHERITED::flatten(buffer); |
| 188 | buffer.writeFlattenable(fMapper); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 189 | buffer.writeColorArray(fOrigColors, fColorCount); |
| 190 | buffer.writeUInt(fTileMode); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 191 | if (fColorCount > 2) { |
| 192 | Rec* recs = fRecs; |
| 193 | for (int i = 1; i < fColorCount; i++) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 194 | buffer.writeInt(recs[i].fPos); |
| 195 | buffer.writeUInt(recs[i].fScale); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | buffer.writeMatrix(fPtsToUnit); |
| 199 | } |
| 200 | |
| 201 | bool SkGradientShaderBase::isOpaque() const { |
| 202 | return fColorsAreOpaque; |
| 203 | } |
| 204 | |
| 205 | bool SkGradientShaderBase::setContext(const SkBitmap& device, |
| 206 | const SkPaint& paint, |
| 207 | const SkMatrix& matrix) { |
| 208 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | const SkMatrix& inverse = this->getTotalInverse(); |
| 213 | |
| 214 | if (!fDstToIndex.setConcat(fPtsToUnit, inverse)) { |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 215 | // need to keep our set/end context calls balanced. |
| 216 | this->INHERITED::endContext(); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | fDstToIndexProc = fDstToIndex.getMapXYProc(); |
| 221 | fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex); |
| 222 | |
| 223 | // now convert our colors in to PMColors |
| 224 | unsigned paintAlpha = this->getPaintAlpha(); |
| 225 | |
| 226 | fFlags = this->INHERITED::getFlags(); |
| 227 | if (fColorsAreOpaque && paintAlpha == 0xFF) { |
| 228 | fFlags |= kOpaqueAlpha_Flag; |
| 229 | } |
| 230 | // we can do span16 as long as our individual colors are opaque, |
| 231 | // regardless of the paint's alpha |
| 232 | if (fColorsAreOpaque) { |
| 233 | fFlags |= kHasSpan16_Flag; |
| 234 | } |
| 235 | |
| 236 | this->setCacheAlpha(paintAlpha); |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | void SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const { |
| 241 | // if the new alpha differs from the previous time we were called, inval our cache |
| 242 | // this will trigger the cache to be rebuilt. |
| 243 | // we don't care about the first time, since the cache ptrs will already be NULL |
| 244 | if (fCacheAlpha != alpha) { |
| 245 | fCache16 = NULL; // inval the cache |
| 246 | fCache32 = NULL; // inval the cache |
| 247 | fCacheAlpha = alpha; // record the new alpha |
| 248 | // inform our subclasses |
| 249 | if (fCache32PixelRef) { |
| 250 | fCache32PixelRef->notifyPixelsChanged(); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) |
| 256 | |
| 257 | /** We take the original colors, not our premultiplied PMColors, since we can |
| 258 | build a 16bit table as long as the original colors are opaque, even if the |
| 259 | paint specifies a non-opaque alpha. |
| 260 | */ |
| 261 | void SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1, |
| 262 | int count) { |
| 263 | SkASSERT(count > 1); |
| 264 | SkASSERT(SkColorGetA(c0) == 0xFF); |
| 265 | SkASSERT(SkColorGetA(c1) == 0xFF); |
| 266 | |
| 267 | SkFixed r = SkColorGetR(c0); |
| 268 | SkFixed g = SkColorGetG(c0); |
| 269 | SkFixed b = SkColorGetB(c0); |
| 270 | |
| 271 | SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1); |
| 272 | SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1); |
| 273 | SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1); |
| 274 | |
| 275 | r = SkIntToFixed(r) + 0x8000; |
| 276 | g = SkIntToFixed(g) + 0x8000; |
| 277 | b = SkIntToFixed(b) + 0x8000; |
| 278 | |
| 279 | do { |
| 280 | unsigned rr = r >> 16; |
| 281 | unsigned gg = g >> 16; |
| 282 | unsigned bb = b >> 16; |
| 283 | cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb)); |
| 284 | cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb); |
| 285 | cache += 1; |
| 286 | r += dr; |
| 287 | g += dg; |
| 288 | b += db; |
| 289 | } while (--count != 0); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * 2x2 dither a fixed-point color component (8.16) down to 8, matching the |
| 294 | * semantics of how we 2x2 dither 32->16 |
| 295 | */ |
| 296 | static inline U8CPU dither_fixed_to_8(SkFixed n) { |
| 297 | n >>= 8; |
| 298 | return ((n << 1) - ((n >> 8 << 8) | (n >> 8))) >> 8; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * For dithering with premultiply, we want to ceiling the alpha component, |
| 303 | * to ensure that it is always >= any color component. |
| 304 | */ |
| 305 | static inline U8CPU dither_ceil_fixed_to_8(SkFixed n) { |
| 306 | n >>= 8; |
| 307 | return ((n << 1) - (n | (n >> 8))) >> 8; |
| 308 | } |
| 309 | |
| 310 | void SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1, |
| 311 | int count, U8CPU paintAlpha) { |
| 312 | SkASSERT(count > 1); |
| 313 | |
| 314 | // need to apply paintAlpha to our two endpoints |
| 315 | SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); |
| 316 | SkFixed da; |
| 317 | { |
| 318 | int tmp = SkMulDiv255Round(SkColorGetA(c1), paintAlpha); |
| 319 | da = SkIntToFixed(tmp - a) / (count - 1); |
| 320 | } |
| 321 | |
| 322 | SkFixed r = SkColorGetR(c0); |
| 323 | SkFixed g = SkColorGetG(c0); |
| 324 | SkFixed b = SkColorGetB(c0); |
| 325 | SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1); |
| 326 | SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1); |
| 327 | SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1); |
| 328 | |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 329 | #ifdef SK_IGNORE_GRADIENT_DITHER_FIX |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 330 | a = SkIntToFixed(a) + 0x8000; |
| 331 | r = SkIntToFixed(r) + 0x8000; |
| 332 | g = SkIntToFixed(g) + 0x8000; |
| 333 | b = SkIntToFixed(b) + 0x8000; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 334 | #else |
| 335 | a = SkIntToFixed(a); |
| 336 | r = SkIntToFixed(r); |
| 337 | g = SkIntToFixed(g); |
| 338 | b = SkIntToFixed(b); |
| 339 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 340 | |
| 341 | do { |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 342 | #ifdef SK_IGNORE_GRADIENT_DITHER_FIX |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 343 | cache[0] = SkPremultiplyARGBInline(a >> 16, r >> 16, g >> 16, b >> 16); |
| 344 | cache[kCache32Count] = |
| 345 | SkPremultiplyARGBInline(dither_ceil_fixed_to_8(a), |
| 346 | dither_fixed_to_8(r), |
| 347 | dither_fixed_to_8(g), |
| 348 | dither_fixed_to_8(b)); |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 349 | #else |
| 350 | /* |
| 351 | * Our dither-cell (spatially) is |
| 352 | * 0 2 |
| 353 | * 3 1 |
| 354 | * Where |
| 355 | * [0] -> [-1/8 ... 1/8 ) values near 0 |
| 356 | * [1] -> [ 1/8 ... 3/8 ) values near 1/4 |
| 357 | * [2] -> [ 3/8 ... 5/8 ) values near 1/2 |
| 358 | * [3] -> [ 5/8 ... 7/8 ) values near 3/4 |
| 359 | */ |
| 360 | cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0x2000) >> 16, |
| 361 | (r + 0x2000) >> 16, |
| 362 | (g + 0x2000) >> 16, |
| 363 | (b + 0x2000) >> 16); |
| 364 | cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x6000) >> 16, |
| 365 | (r + 0x6000) >> 16, |
| 366 | (g + 0x6000) >> 16, |
| 367 | (b + 0x6000) >> 16); |
| 368 | cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0xA000) >> 16, |
| 369 | (r + 0xA000) >> 16, |
| 370 | (g + 0xA000) >> 16, |
| 371 | (b + 0xA000) >> 16); |
| 372 | cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xE000) >> 16, |
| 373 | (r + 0xE000) >> 16, |
| 374 | (g + 0xE000) >> 16, |
| 375 | (b + 0xE000) >> 16); |
| 376 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 377 | cache += 1; |
| 378 | a += da; |
| 379 | r += dr; |
| 380 | g += dg; |
| 381 | b += db; |
| 382 | } while (--count != 0); |
| 383 | } |
| 384 | |
| 385 | static inline int SkFixedToFFFF(SkFixed x) { |
| 386 | SkASSERT((unsigned)x <= SK_Fixed1); |
| 387 | return x - (x >> 16); |
| 388 | } |
| 389 | |
| 390 | static inline U16CPU bitsTo16(unsigned x, const unsigned bits) { |
| 391 | SkASSERT(x < (1U << bits)); |
| 392 | if (6 == bits) { |
| 393 | return (x << 10) | (x << 4) | (x >> 2); |
| 394 | } |
| 395 | if (8 == bits) { |
| 396 | return (x << 8) | x; |
| 397 | } |
| 398 | sk_throw(); |
| 399 | return 0; |
| 400 | } |
| 401 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 402 | const uint16_t* SkGradientShaderBase::getCache16() const { |
| 403 | if (fCache16 == NULL) { |
| 404 | // double the count for dither entries |
| 405 | const int entryCount = kCache16Count * 2; |
| 406 | const size_t allocSize = sizeof(uint16_t) * entryCount; |
| 407 | |
| 408 | if (fCache16Storage == NULL) { // set the storage and our working ptr |
| 409 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 410 | } |
| 411 | fCache16 = fCache16Storage; |
| 412 | if (fColorCount == 2) { |
| 413 | Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1], |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 414 | kCache16Count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 415 | } else { |
| 416 | Rec* rec = fRecs; |
| 417 | int prevIndex = 0; |
| 418 | for (int i = 1; i < fColorCount; i++) { |
| 419 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; |
| 420 | SkASSERT(nextIndex < kCache16Count); |
| 421 | |
| 422 | if (nextIndex > prevIndex) |
| 423 | Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1); |
| 424 | prevIndex = nextIndex; |
| 425 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (fMapper) { |
| 429 | fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
| 430 | uint16_t* linear = fCache16; // just computed linear data |
| 431 | uint16_t* mapped = fCache16Storage; // storage for mapped data |
| 432 | SkUnitMapper* map = fMapper; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 433 | for (int i = 0; i < kCache16Count; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 434 | int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift; |
| 435 | mapped[i] = linear[index]; |
| 436 | mapped[i + kCache16Count] = linear[index + kCache16Count]; |
| 437 | } |
| 438 | sk_free(fCache16); |
| 439 | fCache16 = fCache16Storage; |
| 440 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 441 | } |
| 442 | return fCache16; |
| 443 | } |
| 444 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 445 | const SkPMColor* SkGradientShaderBase::getCache32() const { |
| 446 | if (fCache32 == NULL) { |
| 447 | // double the count for dither entries |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 448 | const int entryCount = kCache32Count * 4; |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 449 | const size_t allocSize = sizeof(SkPMColor) * entryCount; |
| 450 | |
| 451 | if (NULL == fCache32PixelRef) { |
| 452 | fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef, |
| 453 | (NULL, allocSize, NULL)); |
| 454 | } |
| 455 | fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); |
| 456 | if (fColorCount == 2) { |
| 457 | Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1], |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 458 | kCache32Count, fCacheAlpha); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 459 | } else { |
| 460 | Rec* rec = fRecs; |
| 461 | int prevIndex = 0; |
| 462 | for (int i = 1; i < fColorCount; i++) { |
| 463 | int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 464 | SkASSERT(nextIndex < kCache32Count); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 465 | |
| 466 | if (nextIndex > prevIndex) |
| 467 | Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1], |
| 468 | fOrigColors[i], |
| 469 | nextIndex - prevIndex + 1, fCacheAlpha); |
| 470 | prevIndex = nextIndex; |
| 471 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (fMapper) { |
| 475 | SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef, |
| 476 | (NULL, allocSize, NULL)); |
| 477 | SkPMColor* linear = fCache32; // just computed linear data |
| 478 | SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data |
| 479 | SkUnitMapper* map = fMapper; |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 480 | for (int i = 0; i < kCache32Count; i++) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 481 | int index = map->mapUnit16((i << 8) | i) >> 8; |
| 482 | mapped[i] = linear[index]; |
| 483 | mapped[i + kCache32Count] = linear[index + kCache32Count]; |
reed@google.com | 6004029 | 2013-02-04 18:21:23 +0000 | [diff] [blame] | 484 | #ifndef SK_IGNORE_GRADIENT_DITHER_FIX |
| 485 | mapped[i + kCache32Count*2] = linear[index + kCache32Count*2]; |
| 486 | mapped[i + kCache32Count*3] = linear[index + kCache32Count*3]; |
| 487 | #endif |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 488 | } |
| 489 | fCache32PixelRef->unref(); |
| 490 | fCache32PixelRef = newPR; |
| 491 | fCache32 = (SkPMColor*)newPR->getAddr(); |
| 492 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 493 | } |
| 494 | return fCache32; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Because our caller might rebuild the same (logically the same) gradient |
| 499 | * over and over, we'd like to return exactly the same "bitmap" if possible, |
| 500 | * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
| 501 | * To do that, we maintain a private cache of built-bitmaps, based on our |
| 502 | * colors and positions. Note: we don't try to flatten the fMapper, so if one |
| 503 | * is present, we skip the cache for now. |
| 504 | */ |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 505 | void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 506 | // our caller assumes no external alpha, so we ensure that our cache is |
| 507 | // built with 0xFF |
| 508 | this->setCacheAlpha(0xFF); |
| 509 | |
| 510 | // don't have a way to put the mapper into our cache-key yet |
| 511 | if (fMapper) { |
| 512 | // force our cahce32pixelref to be built |
| 513 | (void)this->getCache32(); |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 514 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, kCache32Count, 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 515 | bitmap->setPixelRef(fCache32PixelRef); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | // build our key: [numColors + colors[] + {positions[]} ] |
| 520 | int count = 1 + fColorCount; |
| 521 | if (fColorCount > 2) { |
| 522 | count += fColorCount - 1; // fRecs[].fPos |
| 523 | } |
| 524 | |
| 525 | SkAutoSTMalloc<16, int32_t> storage(count); |
| 526 | int32_t* buffer = storage.get(); |
| 527 | |
| 528 | *buffer++ = fColorCount; |
| 529 | memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor)); |
| 530 | buffer += fColorCount; |
| 531 | if (fColorCount > 2) { |
| 532 | for (int i = 1; i < fColorCount; i++) { |
| 533 | *buffer++ = fRecs[i].fPos; |
| 534 | } |
| 535 | } |
| 536 | SkASSERT(buffer - storage.get() == count); |
| 537 | |
| 538 | /////////////////////////////////// |
| 539 | |
| 540 | SK_DECLARE_STATIC_MUTEX(gMutex); |
| 541 | static SkBitmapCache* gCache; |
| 542 | // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp |
| 543 | static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
| 544 | SkAutoMutexAcquire ama(gMutex); |
| 545 | |
| 546 | if (NULL == gCache) { |
| 547 | gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); |
| 548 | } |
| 549 | size_t size = count * sizeof(int32_t); |
| 550 | |
| 551 | if (!gCache->find(storage.get(), size, bitmap)) { |
| 552 | // force our cahce32pixelref to be built |
| 553 | (void)this->getCache32(); |
reed@google.com | 3c2102c | 2013-02-01 12:59:40 +0000 | [diff] [blame] | 554 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, kCache32Count, 1); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 555 | bitmap->setPixelRef(fCache32PixelRef); |
| 556 | |
| 557 | gCache->add(storage.get(), size, *bitmap); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const { |
| 562 | if (info) { |
| 563 | if (info->fColorCount >= fColorCount) { |
| 564 | if (info->fColors) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 565 | memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor)); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 566 | } |
| 567 | if (info->fColorOffsets) { |
| 568 | if (fColorCount == 2) { |
| 569 | info->fColorOffsets[0] = 0; |
| 570 | info->fColorOffsets[1] = SK_Scalar1; |
| 571 | } else if (fColorCount > 2) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 572 | for (int i = 0; i < fColorCount; ++i) { |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 573 | info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos); |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 574 | } |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | } |
| 578 | info->fColorCount = fColorCount; |
| 579 | info->fTileMode = fTileMode; |
| 580 | } |
| 581 | } |
| 582 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 583 | #ifdef SK_DEVELOPER |
| 584 | void SkGradientShaderBase::toString(SkString* str) const { |
| 585 | |
| 586 | str->appendf("%d colors: ", fColorCount); |
| 587 | |
| 588 | for (int i = 0; i < fColorCount; ++i) { |
| 589 | str->appendHex(fOrigColors[i]); |
| 590 | if (i < fColorCount-1) { |
| 591 | str->append(", "); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (fColorCount > 2) { |
| 596 | str->append(" points: ("); |
| 597 | for (int i = 0; i < fColorCount; ++i) { |
| 598 | str->appendScalar(SkFixedToScalar(fRecs[i].fPos)); |
| 599 | if (i < fColorCount-1) { |
| 600 | str->append(", "); |
| 601 | } |
| 602 | } |
| 603 | str->append(")"); |
| 604 | } |
| 605 | |
| 606 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 607 | "clamp", "repeat", "mirror" |
| 608 | }; |
| 609 | |
| 610 | str->append(" "); |
| 611 | str->append(gTileModeName[fTileMode]); |
| 612 | |
| 613 | // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added |
| 614 | |
| 615 | this->INHERITED::toString(str); |
| 616 | } |
| 617 | #endif |
| 618 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 619 | /////////////////////////////////////////////////////////////////////////////// |
| 620 | /////////////////////////////////////////////////////////////////////////////// |
| 621 | |
| 622 | #include "SkEmptyShader.h" |
| 623 | |
| 624 | // assumes colors is SkColor* and pos is SkScalar* |
| 625 | #define EXPAND_1_COLOR(count) \ |
| 626 | SkColor tmp[2]; \ |
| 627 | do { \ |
| 628 | if (1 == count) { \ |
| 629 | tmp[0] = tmp[1] = colors[0]; \ |
| 630 | colors = tmp; \ |
| 631 | pos = NULL; \ |
| 632 | count = 2; \ |
| 633 | } \ |
| 634 | } while (0) |
| 635 | |
| 636 | SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], |
| 637 | const SkColor colors[], |
| 638 | const SkScalar pos[], int colorCount, |
| 639 | SkShader::TileMode mode, |
| 640 | SkUnitMapper* mapper) { |
| 641 | if (NULL == pts || NULL == colors || colorCount < 1) { |
| 642 | return NULL; |
| 643 | } |
| 644 | EXPAND_1_COLOR(colorCount); |
| 645 | |
| 646 | return SkNEW_ARGS(SkLinearGradient, |
| 647 | (pts, colors, pos, colorCount, mode, mapper)); |
| 648 | } |
| 649 | |
| 650 | SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, |
| 651 | const SkColor colors[], |
| 652 | const SkScalar pos[], int colorCount, |
| 653 | SkShader::TileMode mode, |
| 654 | SkUnitMapper* mapper) { |
| 655 | if (radius <= 0 || NULL == colors || colorCount < 1) { |
| 656 | return NULL; |
| 657 | } |
| 658 | EXPAND_1_COLOR(colorCount); |
| 659 | |
| 660 | return SkNEW_ARGS(SkRadialGradient, |
| 661 | (center, radius, colors, pos, colorCount, mode, mapper)); |
| 662 | } |
| 663 | |
| 664 | SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, |
| 665 | SkScalar startRadius, |
| 666 | const SkPoint& end, |
| 667 | SkScalar endRadius, |
| 668 | const SkColor colors[], |
| 669 | const SkScalar pos[], |
| 670 | int colorCount, |
| 671 | SkShader::TileMode mode, |
| 672 | SkUnitMapper* mapper) { |
| 673 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 674 | return NULL; |
| 675 | } |
| 676 | EXPAND_1_COLOR(colorCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 677 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 678 | return SkNEW_ARGS(SkTwoPointRadialGradient, |
| 679 | (start, startRadius, end, endRadius, colors, pos, |
| 680 | colorCount, mode, mapper)); |
| 681 | } |
| 682 | |
| 683 | SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, |
| 684 | SkScalar startRadius, |
| 685 | const SkPoint& end, |
| 686 | SkScalar endRadius, |
| 687 | const SkColor colors[], |
| 688 | const SkScalar pos[], |
| 689 | int colorCount, |
| 690 | SkShader::TileMode mode, |
| 691 | SkUnitMapper* mapper) { |
| 692 | if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 693 | return NULL; |
| 694 | } |
| 695 | if (start == end && startRadius == endRadius) { |
| 696 | return SkNEW(SkEmptyShader); |
| 697 | } |
rileya@google.com | 1ee7c6a | 2012-07-31 16:00:13 +0000 | [diff] [blame] | 698 | EXPAND_1_COLOR(colorCount); |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 699 | |
| 700 | return SkNEW_ARGS(SkTwoPointConicalGradient, |
| 701 | (start, startRadius, end, endRadius, colors, pos, |
| 702 | colorCount, mode, mapper)); |
| 703 | } |
| 704 | |
| 705 | SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
| 706 | const SkColor colors[], |
| 707 | const SkScalar pos[], |
| 708 | int count, SkUnitMapper* mapper) { |
| 709 | if (NULL == colors || count < 1) { |
| 710 | return NULL; |
| 711 | } |
| 712 | EXPAND_1_COLOR(count); |
| 713 | |
| 714 | return SkNEW_ARGS(SkSweepGradient, (cx, cy, colors, pos, count, mapper)); |
| 715 | } |
| 716 | |
| 717 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
| 718 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
| 719 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
| 720 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
| 721 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) |
| 722 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) |
| 723 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 724 | |
| 725 | /////////////////////////////////////////////////////////////////////////////// |
| 726 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 727 | #if SK_SUPPORT_GPU |
| 728 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 729 | #include "effects/GrTextureStripAtlas.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 730 | #include "SkGr.h" |
| 731 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 732 | GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 733 | : INHERITED(factory) |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 734 | , fCachedYCoord(SK_ScalarMax) |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 735 | , fFSYUni(GrGLUniformManager::kInvalidUniformHandle) { |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 736 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 737 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 738 | GrGLGradientEffect::~GrGLGradientEffect() { } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 739 | |
bsalomon@google.com | f78df33 | 2012-10-29 12:43:38 +0000 | [diff] [blame] | 740 | void GrGLGradientEffect::emitYCoordUniform(GrGLShaderBuilder* builder) { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 741 | fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 742 | kFloat_GrSLType, "GradientYCoordFS"); |
| 743 | } |
| 744 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 745 | void GrGLGradientEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 746 | const GrGradientEffect& e = GetEffectFromStage<GrGradientEffect>(stage); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 747 | const GrTexture* texture = e.texture(0); |
| 748 | fEffectMatrix.setData(uman, e.getMatrix(), stage.getCoordChangeMatrix(), texture); |
| 749 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 750 | SkScalar yCoord = e.getYCoord(); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 751 | if (yCoord != fCachedYCoord) { |
| 752 | uman.set1f(fFSYUni, yCoord); |
| 753 | fCachedYCoord = yCoord; |
| 754 | } |
| 755 | } |
| 756 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 757 | GrGLEffect::EffectKey GrGLGradientEffect::GenMatrixKey(const GrEffectStage& s) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 758 | const GrGradientEffect& e = GetEffectFromStage<GrGradientEffect>(s); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 759 | const GrTexture* texture = e.texture(0); |
skia.committer@gmail.com | 760f2d9 | 2012-11-02 02:01:24 +0000 | [diff] [blame] | 760 | return GrGLEffectMatrix::GenKey(e.getMatrix(), s.getCoordChangeMatrix(), texture); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, |
| 764 | EffectKey key, |
| 765 | const char* vertexCoords, |
| 766 | const char** fsCoordName, |
| 767 | const char** vsVaryingName, |
| 768 | GrSLType* vsVaryingType) { |
| 769 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, |
| 770 | key & kMatrixKeyMask, |
| 771 | vertexCoords, |
| 772 | fsCoordName, |
| 773 | vsVaryingName, |
| 774 | vsVaryingType); |
| 775 | } |
| 776 | |
bsalomon@google.com | 0707c29 | 2012-10-25 21:45:42 +0000 | [diff] [blame] | 777 | void GrGLGradientEffect::emitColorLookup(GrGLShaderBuilder* builder, |
| 778 | const char* gradientTValue, |
| 779 | const char* outputColor, |
| 780 | const char* inputColor, |
| 781 | const GrGLShaderBuilder::TextureSampler& sampler) { |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 782 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 783 | SkString* code = &builder->fFSCode; |
| 784 | code->appendf("\tvec2 coord = vec2(%s, %s);\n", |
| 785 | gradientTValue, |
| 786 | builder->getUniformVariable(fFSYUni).c_str()); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 787 | code->appendf("\t%s = ", outputColor); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 788 | builder->appendTextureLookupAndModulate(code, inputColor, sampler, "coord"); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 789 | code->append(";\n"); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | ///////////////////////////////////////////////////////////////////// |
| 793 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 794 | GrGradientEffect::GrGradientEffect(GrContext* ctx, |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 795 | const SkGradientShaderBase& shader, |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 796 | const SkMatrix& matrix, |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 797 | SkShader::TileMode tileMode) { |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 798 | // TODO: check for simple cases where we don't need a texture: |
| 799 | //GradientInfo info; |
| 800 | //shader.asAGradient(&info); |
| 801 | //if (info.fColorCount == 2) { ... |
| 802 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 803 | fMatrix = matrix; |
| 804 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 805 | SkBitmap bitmap; |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame] | 806 | shader.getGradientTableBitmap(&bitmap); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 807 | |
bsalomon@google.com | 371e105 | 2013-01-11 21:08:55 +0000 | [diff] [blame] | 808 | fIsOpaque = shader.isOpaque(); |
| 809 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 810 | GrTextureStripAtlas::Desc desc; |
| 811 | desc.fWidth = bitmap.width(); |
| 812 | desc.fHeight = 32; |
| 813 | desc.fRowHeight = bitmap.height(); |
| 814 | desc.fContext = ctx; |
| 815 | desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); |
| 816 | fAtlas = GrTextureStripAtlas::GetAtlas(desc); |
| 817 | GrAssert(NULL != fAtlas); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 818 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 819 | // We always filter the gradient table. Each table is one row of a texture, so always y-clamp. |
| 820 | GrTextureParams params; |
| 821 | params.setBilerp(true); |
| 822 | params.setTileModeX(tileMode); |
| 823 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 824 | fRow = fAtlas->lockRow(bitmap); |
| 825 | if (-1 != fRow) { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 826 | fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 827 | fAtlas->getVerticalScaleFactor(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 828 | fTextureAccess.reset(fAtlas->getTexture(), params); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 829 | } else { |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 830 | GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 831 | fTextureAccess.reset(texture, params); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 832 | fYCoord = SK_ScalarHalf; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 833 | |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 834 | // Unlock immediately, this is not great, but we don't have a way of |
| 835 | // knowing when else to unlock it currently, so it may get purged from |
| 836 | // the cache, but it'll still be ref'd until it's no longer being used. |
bsalomon@google.com | 95ed55a | 2013-01-24 14:46:47 +0000 | [diff] [blame] | 837 | GrUnlockAndUnrefCachedBitmapTexture(texture); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 838 | } |
bsalomon@google.com | 50db75c | 2013-01-11 13:54:30 +0000 | [diff] [blame] | 839 | this->addTextureAccess(&fTextureAccess); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | GrGradientEffect::~GrGradientEffect() { |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 843 | if (this->useAtlas()) { |
| 844 | fAtlas->unlockRow(fRow); |
rileya@google.com | b3e50f2 | 2012-08-20 17:43:08 +0000 | [diff] [blame] | 845 | } |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 846 | } |
| 847 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 848 | bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 849 | const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 850 | return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && |
| 851 | fTextureAccess.getParams().getTileModeX() == |
| 852 | s.fTextureAccess.getParams().getTileModeX() && |
| 853 | this->useAtlas() == s.useAtlas() && |
| 854 | fYCoord == s.getYCoord() && |
| 855 | fMatrix.cheapEqualTo(s.getMatrix()); |
| 856 | } |
| 857 | |
| 858 | void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 859 | if (fIsOpaque && (kA_ValidComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) { |
| 860 | *validFlags = kA_ValidComponentFlag; |
| 861 | } else { |
| 862 | *validFlags = 0; |
| 863 | } |
| 864 | } |
| 865 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 866 | int GrGradientEffect::RandomGradientParams(SkRandom* random, |
| 867 | SkColor colors[], |
| 868 | SkScalar** stops, |
| 869 | SkShader::TileMode* tm) { |
| 870 | int outColors = random->nextRangeU(1, kMaxRandomGradientColors); |
| 871 | |
| 872 | // if one color, omit stops, otherwise randomly decide whether or not to |
| 873 | if (outColors == 1 || (outColors >= 2 && random->nextBool())) { |
| 874 | *stops = NULL; |
| 875 | } |
| 876 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 877 | SkScalar stop = 0.f; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 878 | for (int i = 0; i < outColors; ++i) { |
| 879 | colors[i] = random->nextU(); |
| 880 | if (NULL != *stops) { |
| 881 | (*stops)[i] = stop; |
| 882 | stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f; |
| 883 | } |
| 884 | } |
| 885 | *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount)); |
| 886 | |
| 887 | return outColors; |
| 888 | } |
| 889 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 890 | #endif |