gfx/skia/trunk/src/effects/gradients/SkGradientShader.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkGradientShaderPriv.h"
michael@0 9 #include "SkLinearGradient.h"
michael@0 10 #include "SkRadialGradient.h"
michael@0 11 #include "SkTwoPointRadialGradient.h"
michael@0 12 #include "SkTwoPointConicalGradient.h"
michael@0 13 #include "SkSweepGradient.h"
michael@0 14
michael@0 15 SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc) {
michael@0 16 SkASSERT(desc.fCount > 1);
michael@0 17
michael@0 18 fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return
michael@0 19
michael@0 20 fMapper = desc.fMapper;
michael@0 21 SkSafeRef(fMapper);
michael@0 22 fGradFlags = SkToU8(desc.fFlags);
michael@0 23
michael@0 24 SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount);
michael@0 25 SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs));
michael@0 26 fTileMode = desc.fTileMode;
michael@0 27 fTileProc = gTileProcs[desc.fTileMode];
michael@0 28
michael@0 29 fCache16 = fCache16Storage = NULL;
michael@0 30 fCache32 = NULL;
michael@0 31 fCache32PixelRef = NULL;
michael@0 32
michael@0 33 /* Note: we let the caller skip the first and/or last position.
michael@0 34 i.e. pos[0] = 0.3, pos[1] = 0.7
michael@0 35 In these cases, we insert dummy entries to ensure that the final data
michael@0 36 will be bracketed by [0, 1].
michael@0 37 i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1
michael@0 38
michael@0 39 Thus colorCount (the caller's value, and fColorCount (our value) may
michael@0 40 differ by up to 2. In the above example:
michael@0 41 colorCount = 2
michael@0 42 fColorCount = 4
michael@0 43 */
michael@0 44 fColorCount = desc.fCount;
michael@0 45 // check if we need to add in dummy start and/or end position/colors
michael@0 46 bool dummyFirst = false;
michael@0 47 bool dummyLast = false;
michael@0 48 if (desc.fPos) {
michael@0 49 dummyFirst = desc.fPos[0] != 0;
michael@0 50 dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
michael@0 51 fColorCount += dummyFirst + dummyLast;
michael@0 52 }
michael@0 53
michael@0 54 if (fColorCount > kColorStorageCount) {
michael@0 55 size_t size = sizeof(SkColor) + sizeof(Rec);
michael@0 56 fOrigColors = reinterpret_cast<SkColor*>(
michael@0 57 sk_malloc_throw(size * fColorCount));
michael@0 58 }
michael@0 59 else {
michael@0 60 fOrigColors = fStorage;
michael@0 61 }
michael@0 62
michael@0 63 // Now copy over the colors, adding the dummies as needed
michael@0 64 {
michael@0 65 SkColor* origColors = fOrigColors;
michael@0 66 if (dummyFirst) {
michael@0 67 *origColors++ = desc.fColors[0];
michael@0 68 }
michael@0 69 memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor));
michael@0 70 if (dummyLast) {
michael@0 71 origColors += desc.fCount;
michael@0 72 *origColors = desc.fColors[desc.fCount - 1];
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 fRecs = (Rec*)(fOrigColors + fColorCount);
michael@0 77 if (fColorCount > 2) {
michael@0 78 Rec* recs = fRecs;
michael@0 79 recs->fPos = 0;
michael@0 80 // recs->fScale = 0; // unused;
michael@0 81 recs += 1;
michael@0 82 if (desc.fPos) {
michael@0 83 /* We need to convert the user's array of relative positions into
michael@0 84 fixed-point positions and scale factors. We need these results
michael@0 85 to be strictly monotonic (no two values equal or out of order).
michael@0 86 Hence this complex loop that just jams a zero for the scale
michael@0 87 value if it sees a segment out of order, and it assures that
michael@0 88 we start at 0 and end at 1.0
michael@0 89 */
michael@0 90 SkFixed prev = 0;
michael@0 91 int startIndex = dummyFirst ? 0 : 1;
michael@0 92 int count = desc.fCount + dummyLast;
michael@0 93 for (int i = startIndex; i < count; i++) {
michael@0 94 // force the last value to be 1.0
michael@0 95 SkFixed curr;
michael@0 96 if (i == desc.fCount) { // we're really at the dummyLast
michael@0 97 curr = SK_Fixed1;
michael@0 98 } else {
michael@0 99 curr = SkScalarToFixed(desc.fPos[i]);
michael@0 100 }
michael@0 101 // pin curr withing range
michael@0 102 if (curr < 0) {
michael@0 103 curr = 0;
michael@0 104 } else if (curr > SK_Fixed1) {
michael@0 105 curr = SK_Fixed1;
michael@0 106 }
michael@0 107 recs->fPos = curr;
michael@0 108 if (curr > prev) {
michael@0 109 recs->fScale = (1 << 24) / (curr - prev);
michael@0 110 } else {
michael@0 111 recs->fScale = 0; // ignore this segment
michael@0 112 }
michael@0 113 // get ready for the next value
michael@0 114 prev = curr;
michael@0 115 recs += 1;
michael@0 116 }
michael@0 117 } else { // assume even distribution
michael@0 118 SkFixed dp = SK_Fixed1 / (desc.fCount - 1);
michael@0 119 SkFixed p = dp;
michael@0 120 SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp
michael@0 121 for (int i = 1; i < desc.fCount; i++) {
michael@0 122 recs->fPos = p;
michael@0 123 recs->fScale = scale;
michael@0 124 recs += 1;
michael@0 125 p += dp;
michael@0 126 }
michael@0 127 }
michael@0 128 }
michael@0 129 this->initCommon();
michael@0 130 }
michael@0 131
michael@0 132 static uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) {
michael@0 133 SkASSERT(0 == (flags >> 28));
michael@0 134 SkASSERT(0 == ((uint32_t)mode >> 4));
michael@0 135 return (flags << 4) | mode;
michael@0 136 }
michael@0 137
michael@0 138 static SkShader::TileMode unpack_mode(uint32_t packed) {
michael@0 139 return (SkShader::TileMode)(packed & 0xF);
michael@0 140 }
michael@0 141
michael@0 142 static uint32_t unpack_flags(uint32_t packed) {
michael@0 143 return packed >> 4;
michael@0 144 }
michael@0 145
michael@0 146 SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buffer) {
michael@0 147 fCacheAlpha = 256;
michael@0 148
michael@0 149 fMapper = buffer.readUnitMapper();
michael@0 150
michael@0 151 fCache16 = fCache16Storage = NULL;
michael@0 152 fCache32 = NULL;
michael@0 153 fCache32PixelRef = NULL;
michael@0 154
michael@0 155 int colorCount = fColorCount = buffer.getArrayCount();
michael@0 156 if (colorCount > kColorStorageCount) {
michael@0 157 size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) * colorCount;
michael@0 158 if (buffer.validateAvailable(allocSize)) {
michael@0 159 fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize));
michael@0 160 } else {
michael@0 161 fOrigColors = NULL;
michael@0 162 colorCount = fColorCount = 0;
michael@0 163 }
michael@0 164 } else {
michael@0 165 fOrigColors = fStorage;
michael@0 166 }
michael@0 167 buffer.readColorArray(fOrigColors, colorCount);
michael@0 168
michael@0 169 {
michael@0 170 uint32_t packed = buffer.readUInt();
michael@0 171 fGradFlags = SkToU8(unpack_flags(packed));
michael@0 172 fTileMode = unpack_mode(packed);
michael@0 173 }
michael@0 174 fTileProc = gTileProcs[fTileMode];
michael@0 175 fRecs = (Rec*)(fOrigColors + colorCount);
michael@0 176 if (colorCount > 2) {
michael@0 177 Rec* recs = fRecs;
michael@0 178 recs[0].fPos = 0;
michael@0 179 for (int i = 1; i < colorCount; i++) {
michael@0 180 recs[i].fPos = buffer.readInt();
michael@0 181 recs[i].fScale = buffer.readUInt();
michael@0 182 }
michael@0 183 }
michael@0 184 buffer.readMatrix(&fPtsToUnit);
michael@0 185 this->initCommon();
michael@0 186 }
michael@0 187
michael@0 188 SkGradientShaderBase::~SkGradientShaderBase() {
michael@0 189 if (fCache16Storage) {
michael@0 190 sk_free(fCache16Storage);
michael@0 191 }
michael@0 192 SkSafeUnref(fCache32PixelRef);
michael@0 193 if (fOrigColors != fStorage) {
michael@0 194 sk_free(fOrigColors);
michael@0 195 }
michael@0 196 SkSafeUnref(fMapper);
michael@0 197 }
michael@0 198
michael@0 199 void SkGradientShaderBase::initCommon() {
michael@0 200 fFlags = 0;
michael@0 201 unsigned colorAlpha = 0xFF;
michael@0 202 for (int i = 0; i < fColorCount; i++) {
michael@0 203 colorAlpha &= SkColorGetA(fOrigColors[i]);
michael@0 204 }
michael@0 205 fColorsAreOpaque = colorAlpha == 0xFF;
michael@0 206 }
michael@0 207
michael@0 208 void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const {
michael@0 209 this->INHERITED::flatten(buffer);
michael@0 210 buffer.writeFlattenable(fMapper);
michael@0 211 buffer.writeColorArray(fOrigColors, fColorCount);
michael@0 212 buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags));
michael@0 213 if (fColorCount > 2) {
michael@0 214 Rec* recs = fRecs;
michael@0 215 for (int i = 1; i < fColorCount; i++) {
michael@0 216 buffer.writeInt(recs[i].fPos);
michael@0 217 buffer.writeUInt(recs[i].fScale);
michael@0 218 }
michael@0 219 }
michael@0 220 buffer.writeMatrix(fPtsToUnit);
michael@0 221 }
michael@0 222
michael@0 223 bool SkGradientShaderBase::isOpaque() const {
michael@0 224 return fColorsAreOpaque;
michael@0 225 }
michael@0 226
michael@0 227 bool SkGradientShaderBase::setContext(const SkBitmap& device,
michael@0 228 const SkPaint& paint,
michael@0 229 const SkMatrix& matrix) {
michael@0 230 if (!this->INHERITED::setContext(device, paint, matrix)) {
michael@0 231 return false;
michael@0 232 }
michael@0 233
michael@0 234 const SkMatrix& inverse = this->getTotalInverse();
michael@0 235
michael@0 236 if (!fDstToIndex.setConcat(fPtsToUnit, inverse)) {
michael@0 237 // need to keep our set/end context calls balanced.
michael@0 238 this->INHERITED::endContext();
michael@0 239 return false;
michael@0 240 }
michael@0 241
michael@0 242 fDstToIndexProc = fDstToIndex.getMapXYProc();
michael@0 243 fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex);
michael@0 244
michael@0 245 // now convert our colors in to PMColors
michael@0 246 unsigned paintAlpha = this->getPaintAlpha();
michael@0 247
michael@0 248 fFlags = this->INHERITED::getFlags();
michael@0 249 if (fColorsAreOpaque && paintAlpha == 0xFF) {
michael@0 250 fFlags |= kOpaqueAlpha_Flag;
michael@0 251 }
michael@0 252 // we can do span16 as long as our individual colors are opaque,
michael@0 253 // regardless of the paint's alpha
michael@0 254 if (fColorsAreOpaque) {
michael@0 255 fFlags |= kHasSpan16_Flag;
michael@0 256 }
michael@0 257
michael@0 258 this->setCacheAlpha(paintAlpha);
michael@0 259 return true;
michael@0 260 }
michael@0 261
michael@0 262 void SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const {
michael@0 263 // if the new alpha differs from the previous time we were called, inval our cache
michael@0 264 // this will trigger the cache to be rebuilt.
michael@0 265 // we don't care about the first time, since the cache ptrs will already be NULL
michael@0 266 if (fCacheAlpha != alpha) {
michael@0 267 fCache16 = NULL; // inval the cache
michael@0 268 fCache32 = NULL; // inval the cache
michael@0 269 fCacheAlpha = alpha; // record the new alpha
michael@0 270 // inform our subclasses
michael@0 271 if (fCache32PixelRef) {
michael@0 272 fCache32PixelRef->notifyPixelsChanged();
michael@0 273 }
michael@0 274 }
michael@0 275 }
michael@0 276
michael@0 277 #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8)
michael@0 278
michael@0 279 /** We take the original colors, not our premultiplied PMColors, since we can
michael@0 280 build a 16bit table as long as the original colors are opaque, even if the
michael@0 281 paint specifies a non-opaque alpha.
michael@0 282 */
michael@0 283 void SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1,
michael@0 284 int count) {
michael@0 285 SkASSERT(count > 1);
michael@0 286 SkASSERT(SkColorGetA(c0) == 0xFF);
michael@0 287 SkASSERT(SkColorGetA(c1) == 0xFF);
michael@0 288
michael@0 289 SkFixed r = SkColorGetR(c0);
michael@0 290 SkFixed g = SkColorGetG(c0);
michael@0 291 SkFixed b = SkColorGetB(c0);
michael@0 292
michael@0 293 SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1);
michael@0 294 SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1);
michael@0 295 SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1);
michael@0 296
michael@0 297 r = SkIntToFixed(r) + 0x8000;
michael@0 298 g = SkIntToFixed(g) + 0x8000;
michael@0 299 b = SkIntToFixed(b) + 0x8000;
michael@0 300
michael@0 301 do {
michael@0 302 unsigned rr = r >> 16;
michael@0 303 unsigned gg = g >> 16;
michael@0 304 unsigned bb = b >> 16;
michael@0 305 cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb));
michael@0 306 cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb);
michael@0 307 cache += 1;
michael@0 308 r += dr;
michael@0 309 g += dg;
michael@0 310 b += db;
michael@0 311 } while (--count != 0);
michael@0 312 }
michael@0 313
michael@0 314 /*
michael@0 315 * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in
michael@0 316 * release builds, we saw a compiler error where the 0xFF parameter in
michael@0 317 * SkPackARGB32() was being totally ignored whenever it was called with
michael@0 318 * a non-zero add (e.g. 0x8000).
michael@0 319 *
michael@0 320 * We found two work-arounds:
michael@0 321 * 1. change r,g,b to unsigned (or just one of them)
michael@0 322 * 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead
michael@0 323 * of using |
michael@0 324 *
michael@0 325 * We chose #1 just because it was more localized.
michael@0 326 * See http://code.google.com/p/skia/issues/detail?id=1113
michael@0 327 *
michael@0 328 * The type SkUFixed encapsulate this need for unsigned, but logically Fixed.
michael@0 329 */
michael@0 330 typedef uint32_t SkUFixed;
michael@0 331
michael@0 332 void SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1,
michael@0 333 int count, U8CPU paintAlpha, uint32_t gradFlags) {
michael@0 334 SkASSERT(count > 1);
michael@0 335
michael@0 336 // need to apply paintAlpha to our two endpoints
michael@0 337 uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
michael@0 338 uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha);
michael@0 339
michael@0 340
michael@0 341 const bool interpInPremul = SkToBool(gradFlags &
michael@0 342 SkGradientShader::kInterpolateColorsInPremul_Flag);
michael@0 343
michael@0 344 uint32_t r0 = SkColorGetR(c0);
michael@0 345 uint32_t g0 = SkColorGetG(c0);
michael@0 346 uint32_t b0 = SkColorGetB(c0);
michael@0 347
michael@0 348 uint32_t r1 = SkColorGetR(c1);
michael@0 349 uint32_t g1 = SkColorGetG(c1);
michael@0 350 uint32_t b1 = SkColorGetB(c1);
michael@0 351
michael@0 352 if (interpInPremul) {
michael@0 353 r0 = SkMulDiv255Round(r0, a0);
michael@0 354 g0 = SkMulDiv255Round(g0, a0);
michael@0 355 b0 = SkMulDiv255Round(b0, a0);
michael@0 356
michael@0 357 r1 = SkMulDiv255Round(r1, a1);
michael@0 358 g1 = SkMulDiv255Round(g1, a1);
michael@0 359 b1 = SkMulDiv255Round(b1, a1);
michael@0 360 }
michael@0 361
michael@0 362 SkFixed da = SkIntToFixed(a1 - a0) / (count - 1);
michael@0 363 SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1);
michael@0 364 SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1);
michael@0 365 SkFixed db = SkIntToFixed(b1 - b0) / (count - 1);
michael@0 366
michael@0 367 /* We pre-add 1/8 to avoid having to add this to our [0] value each time
michael@0 368 in the loop. Without this, the bias for each would be
michael@0 369 0x2000 0xA000 0xE000 0x6000
michael@0 370 With this trick, we can add 0 for the first (no-op) and just adjust the
michael@0 371 others.
michael@0 372 */
michael@0 373 SkUFixed a = SkIntToFixed(a0) + 0x2000;
michael@0 374 SkUFixed r = SkIntToFixed(r0) + 0x2000;
michael@0 375 SkUFixed g = SkIntToFixed(g0) + 0x2000;
michael@0 376 SkUFixed b = SkIntToFixed(b0) + 0x2000;
michael@0 377
michael@0 378 /*
michael@0 379 * Our dither-cell (spatially) is
michael@0 380 * 0 2
michael@0 381 * 3 1
michael@0 382 * Where
michael@0 383 * [0] -> [-1/8 ... 1/8 ) values near 0
michael@0 384 * [1] -> [ 1/8 ... 3/8 ) values near 1/4
michael@0 385 * [2] -> [ 3/8 ... 5/8 ) values near 1/2
michael@0 386 * [3] -> [ 5/8 ... 7/8 ) values near 3/4
michael@0 387 */
michael@0 388
michael@0 389 if (0xFF == a0 && 0 == da) {
michael@0 390 do {
michael@0 391 cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16,
michael@0 392 (g + 0 ) >> 16,
michael@0 393 (b + 0 ) >> 16);
michael@0 394 cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16,
michael@0 395 (g + 0x8000) >> 16,
michael@0 396 (b + 0x8000) >> 16);
michael@0 397 cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16,
michael@0 398 (g + 0xC000) >> 16,
michael@0 399 (b + 0xC000) >> 16);
michael@0 400 cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16,
michael@0 401 (g + 0x4000) >> 16,
michael@0 402 (b + 0x4000) >> 16);
michael@0 403 cache += 1;
michael@0 404 r += dr;
michael@0 405 g += dg;
michael@0 406 b += db;
michael@0 407 } while (--count != 0);
michael@0 408 } else if (interpInPremul) {
michael@0 409 do {
michael@0 410 cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16,
michael@0 411 (r + 0 ) >> 16,
michael@0 412 (g + 0 ) >> 16,
michael@0 413 (b + 0 ) >> 16);
michael@0 414 cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16,
michael@0 415 (r + 0x8000) >> 16,
michael@0 416 (g + 0x8000) >> 16,
michael@0 417 (b + 0x8000) >> 16);
michael@0 418 cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16,
michael@0 419 (r + 0xC000) >> 16,
michael@0 420 (g + 0xC000) >> 16,
michael@0 421 (b + 0xC000) >> 16);
michael@0 422 cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16,
michael@0 423 (r + 0x4000) >> 16,
michael@0 424 (g + 0x4000) >> 16,
michael@0 425 (b + 0x4000) >> 16);
michael@0 426 cache += 1;
michael@0 427 a += da;
michael@0 428 r += dr;
michael@0 429 g += dg;
michael@0 430 b += db;
michael@0 431 } while (--count != 0);
michael@0 432 } else { // interpolate in unpreml space
michael@0 433 do {
michael@0 434 cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16,
michael@0 435 (r + 0 ) >> 16,
michael@0 436 (g + 0 ) >> 16,
michael@0 437 (b + 0 ) >> 16);
michael@0 438 cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16,
michael@0 439 (r + 0x8000) >> 16,
michael@0 440 (g + 0x8000) >> 16,
michael@0 441 (b + 0x8000) >> 16);
michael@0 442 cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16,
michael@0 443 (r + 0xC000) >> 16,
michael@0 444 (g + 0xC000) >> 16,
michael@0 445 (b + 0xC000) >> 16);
michael@0 446 cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16,
michael@0 447 (r + 0x4000) >> 16,
michael@0 448 (g + 0x4000) >> 16,
michael@0 449 (b + 0x4000) >> 16);
michael@0 450 cache += 1;
michael@0 451 a += da;
michael@0 452 r += dr;
michael@0 453 g += dg;
michael@0 454 b += db;
michael@0 455 } while (--count != 0);
michael@0 456 }
michael@0 457 }
michael@0 458
michael@0 459 static inline int SkFixedToFFFF(SkFixed x) {
michael@0 460 SkASSERT((unsigned)x <= SK_Fixed1);
michael@0 461 return x - (x >> 16);
michael@0 462 }
michael@0 463
michael@0 464 static inline U16CPU bitsTo16(unsigned x, const unsigned bits) {
michael@0 465 SkASSERT(x < (1U << bits));
michael@0 466 if (6 == bits) {
michael@0 467 return (x << 10) | (x << 4) | (x >> 2);
michael@0 468 }
michael@0 469 if (8 == bits) {
michael@0 470 return (x << 8) | x;
michael@0 471 }
michael@0 472 sk_throw();
michael@0 473 return 0;
michael@0 474 }
michael@0 475
michael@0 476 const uint16_t* SkGradientShaderBase::getCache16() const {
michael@0 477 if (fCache16 == NULL) {
michael@0 478 // double the count for dither entries
michael@0 479 const int entryCount = kCache16Count * 2;
michael@0 480 const size_t allocSize = sizeof(uint16_t) * entryCount;
michael@0 481
michael@0 482 if (fCache16Storage == NULL) { // set the storage and our working ptr
michael@0 483 fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
michael@0 484 }
michael@0 485 fCache16 = fCache16Storage;
michael@0 486 if (fColorCount == 2) {
michael@0 487 Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1],
michael@0 488 kCache16Count);
michael@0 489 } else {
michael@0 490 Rec* rec = fRecs;
michael@0 491 int prevIndex = 0;
michael@0 492 for (int i = 1; i < fColorCount; i++) {
michael@0 493 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift;
michael@0 494 SkASSERT(nextIndex < kCache16Count);
michael@0 495
michael@0 496 if (nextIndex > prevIndex)
michael@0 497 Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1);
michael@0 498 prevIndex = nextIndex;
michael@0 499 }
michael@0 500 }
michael@0 501
michael@0 502 if (fMapper) {
michael@0 503 fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
michael@0 504 uint16_t* linear = fCache16; // just computed linear data
michael@0 505 uint16_t* mapped = fCache16Storage; // storage for mapped data
michael@0 506 SkUnitMapper* map = fMapper;
michael@0 507 for (int i = 0; i < kCache16Count; i++) {
michael@0 508 int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift;
michael@0 509 mapped[i] = linear[index];
michael@0 510 mapped[i + kCache16Count] = linear[index + kCache16Count];
michael@0 511 }
michael@0 512 sk_free(fCache16);
michael@0 513 fCache16 = fCache16Storage;
michael@0 514 }
michael@0 515 }
michael@0 516 return fCache16;
michael@0 517 }
michael@0 518
michael@0 519 const SkPMColor* SkGradientShaderBase::getCache32() const {
michael@0 520 if (fCache32 == NULL) {
michael@0 521 SkImageInfo info;
michael@0 522 info.fWidth = kCache32Count;
michael@0 523 info.fHeight = 4; // for our 4 dither rows
michael@0 524 info.fAlphaType = kPremul_SkAlphaType;
michael@0 525 info.fColorType = kPMColor_SkColorType;
michael@0 526
michael@0 527 if (NULL == fCache32PixelRef) {
michael@0 528 fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL);
michael@0 529 }
michael@0 530 fCache32 = (SkPMColor*)fCache32PixelRef->getAddr();
michael@0 531 if (fColorCount == 2) {
michael@0 532 Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1],
michael@0 533 kCache32Count, fCacheAlpha, fGradFlags);
michael@0 534 } else {
michael@0 535 Rec* rec = fRecs;
michael@0 536 int prevIndex = 0;
michael@0 537 for (int i = 1; i < fColorCount; i++) {
michael@0 538 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift;
michael@0 539 SkASSERT(nextIndex < kCache32Count);
michael@0 540
michael@0 541 if (nextIndex > prevIndex)
michael@0 542 Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1],
michael@0 543 fOrigColors[i], nextIndex - prevIndex + 1,
michael@0 544 fCacheAlpha, fGradFlags);
michael@0 545 prevIndex = nextIndex;
michael@0 546 }
michael@0 547 }
michael@0 548
michael@0 549 if (fMapper) {
michael@0 550 SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL);
michael@0 551 SkPMColor* linear = fCache32; // just computed linear data
michael@0 552 SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data
michael@0 553 SkUnitMapper* map = fMapper;
michael@0 554 for (int i = 0; i < kCache32Count; i++) {
michael@0 555 int index = map->mapUnit16((i << 8) | i) >> 8;
michael@0 556 mapped[i + kCache32Count*0] = linear[index + kCache32Count*0];
michael@0 557 mapped[i + kCache32Count*1] = linear[index + kCache32Count*1];
michael@0 558 mapped[i + kCache32Count*2] = linear[index + kCache32Count*2];
michael@0 559 mapped[i + kCache32Count*3] = linear[index + kCache32Count*3];
michael@0 560 }
michael@0 561 fCache32PixelRef->unref();
michael@0 562 fCache32PixelRef = newPR;
michael@0 563 fCache32 = (SkPMColor*)newPR->getAddr();
michael@0 564 }
michael@0 565 }
michael@0 566 return fCache32;
michael@0 567 }
michael@0 568
michael@0 569 /*
michael@0 570 * Because our caller might rebuild the same (logically the same) gradient
michael@0 571 * over and over, we'd like to return exactly the same "bitmap" if possible,
michael@0 572 * allowing the client to utilize a cache of our bitmap (e.g. with a GPU).
michael@0 573 * To do that, we maintain a private cache of built-bitmaps, based on our
michael@0 574 * colors and positions. Note: we don't try to flatten the fMapper, so if one
michael@0 575 * is present, we skip the cache for now.
michael@0 576 */
michael@0 577 void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const {
michael@0 578 // our caller assumes no external alpha, so we ensure that our cache is
michael@0 579 // built with 0xFF
michael@0 580 this->setCacheAlpha(0xFF);
michael@0 581
michael@0 582 // don't have a way to put the mapper into our cache-key yet
michael@0 583 if (fMapper) {
michael@0 584 // force our cahce32pixelref to be built
michael@0 585 (void)this->getCache32();
michael@0 586 bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1));
michael@0 587 bitmap->setPixelRef(fCache32PixelRef);
michael@0 588 return;
michael@0 589 }
michael@0 590
michael@0 591 // build our key: [numColors + colors[] + {positions[]} + flags ]
michael@0 592 int count = 1 + fColorCount + 1;
michael@0 593 if (fColorCount > 2) {
michael@0 594 count += fColorCount - 1; // fRecs[].fPos
michael@0 595 }
michael@0 596
michael@0 597 SkAutoSTMalloc<16, int32_t> storage(count);
michael@0 598 int32_t* buffer = storage.get();
michael@0 599
michael@0 600 *buffer++ = fColorCount;
michael@0 601 memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor));
michael@0 602 buffer += fColorCount;
michael@0 603 if (fColorCount > 2) {
michael@0 604 for (int i = 1; i < fColorCount; i++) {
michael@0 605 *buffer++ = fRecs[i].fPos;
michael@0 606 }
michael@0 607 }
michael@0 608 *buffer++ = fGradFlags;
michael@0 609 SkASSERT(buffer - storage.get() == count);
michael@0 610
michael@0 611 ///////////////////////////////////
michael@0 612
michael@0 613 SK_DECLARE_STATIC_MUTEX(gMutex);
michael@0 614 static SkBitmapCache* gCache;
michael@0 615 // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp
michael@0 616 static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32;
michael@0 617 SkAutoMutexAcquire ama(gMutex);
michael@0 618
michael@0 619 if (NULL == gCache) {
michael@0 620 gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS));
michael@0 621 }
michael@0 622 size_t size = count * sizeof(int32_t);
michael@0 623
michael@0 624 if (!gCache->find(storage.get(), size, bitmap)) {
michael@0 625 // force our cahce32pixelref to be built
michael@0 626 (void)this->getCache32();
michael@0 627 bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1));
michael@0 628 bitmap->setPixelRef(fCache32PixelRef);
michael@0 629
michael@0 630 gCache->add(storage.get(), size, *bitmap);
michael@0 631 }
michael@0 632 }
michael@0 633
michael@0 634 void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const {
michael@0 635 if (info) {
michael@0 636 if (info->fColorCount >= fColorCount) {
michael@0 637 if (info->fColors) {
michael@0 638 memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor));
michael@0 639 }
michael@0 640 if (info->fColorOffsets) {
michael@0 641 if (fColorCount == 2) {
michael@0 642 info->fColorOffsets[0] = 0;
michael@0 643 info->fColorOffsets[1] = SK_Scalar1;
michael@0 644 } else if (fColorCount > 2) {
michael@0 645 for (int i = 0; i < fColorCount; ++i) {
michael@0 646 info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos);
michael@0 647 }
michael@0 648 }
michael@0 649 }
michael@0 650 }
michael@0 651 info->fColorCount = fColorCount;
michael@0 652 info->fTileMode = fTileMode;
michael@0 653 info->fGradientFlags = fGradFlags;
michael@0 654 }
michael@0 655 }
michael@0 656
michael@0 657 #ifndef SK_IGNORE_TO_STRING
michael@0 658 void SkGradientShaderBase::toString(SkString* str) const {
michael@0 659
michael@0 660 str->appendf("%d colors: ", fColorCount);
michael@0 661
michael@0 662 for (int i = 0; i < fColorCount; ++i) {
michael@0 663 str->appendHex(fOrigColors[i]);
michael@0 664 if (i < fColorCount-1) {
michael@0 665 str->append(", ");
michael@0 666 }
michael@0 667 }
michael@0 668
michael@0 669 if (fColorCount > 2) {
michael@0 670 str->append(" points: (");
michael@0 671 for (int i = 0; i < fColorCount; ++i) {
michael@0 672 str->appendScalar(SkFixedToScalar(fRecs[i].fPos));
michael@0 673 if (i < fColorCount-1) {
michael@0 674 str->append(", ");
michael@0 675 }
michael@0 676 }
michael@0 677 str->append(")");
michael@0 678 }
michael@0 679
michael@0 680 static const char* gTileModeName[SkShader::kTileModeCount] = {
michael@0 681 "clamp", "repeat", "mirror"
michael@0 682 };
michael@0 683
michael@0 684 str->append(" ");
michael@0 685 str->append(gTileModeName[fTileMode]);
michael@0 686
michael@0 687 // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added
michael@0 688
michael@0 689 this->INHERITED::toString(str);
michael@0 690 }
michael@0 691 #endif
michael@0 692
michael@0 693 ///////////////////////////////////////////////////////////////////////////////
michael@0 694 ///////////////////////////////////////////////////////////////////////////////
michael@0 695
michael@0 696 #include "SkEmptyShader.h"
michael@0 697
michael@0 698 // assumes colors is SkColor* and pos is SkScalar*
michael@0 699 #define EXPAND_1_COLOR(count) \
michael@0 700 SkColor tmp[2]; \
michael@0 701 do { \
michael@0 702 if (1 == count) { \
michael@0 703 tmp[0] = tmp[1] = colors[0]; \
michael@0 704 colors = tmp; \
michael@0 705 pos = NULL; \
michael@0 706 count = 2; \
michael@0 707 } \
michael@0 708 } while (0)
michael@0 709
michael@0 710 static void desc_init(SkGradientShaderBase::Descriptor* desc,
michael@0 711 const SkColor colors[],
michael@0 712 const SkScalar pos[], int colorCount,
michael@0 713 SkShader::TileMode mode,
michael@0 714 SkUnitMapper* mapper, uint32_t flags) {
michael@0 715 desc->fColors = colors;
michael@0 716 desc->fPos = pos;
michael@0 717 desc->fCount = colorCount;
michael@0 718 desc->fTileMode = mode;
michael@0 719 desc->fMapper = mapper;
michael@0 720 desc->fFlags = flags;
michael@0 721 }
michael@0 722
michael@0 723 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
michael@0 724 const SkColor colors[],
michael@0 725 const SkScalar pos[], int colorCount,
michael@0 726 SkShader::TileMode mode,
michael@0 727 SkUnitMapper* mapper,
michael@0 728 uint32_t flags) {
michael@0 729 if (NULL == pts || NULL == colors || colorCount < 1) {
michael@0 730 return NULL;
michael@0 731 }
michael@0 732 EXPAND_1_COLOR(colorCount);
michael@0 733
michael@0 734 SkGradientShaderBase::Descriptor desc;
michael@0 735 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
michael@0 736 return SkNEW_ARGS(SkLinearGradient, (pts, desc));
michael@0 737 }
michael@0 738
michael@0 739 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
michael@0 740 const SkColor colors[],
michael@0 741 const SkScalar pos[], int colorCount,
michael@0 742 SkShader::TileMode mode,
michael@0 743 SkUnitMapper* mapper,
michael@0 744 uint32_t flags) {
michael@0 745 if (radius <= 0 || NULL == colors || colorCount < 1) {
michael@0 746 return NULL;
michael@0 747 }
michael@0 748 EXPAND_1_COLOR(colorCount);
michael@0 749
michael@0 750 SkGradientShaderBase::Descriptor desc;
michael@0 751 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
michael@0 752 return SkNEW_ARGS(SkRadialGradient, (center, radius, desc));
michael@0 753 }
michael@0 754
michael@0 755 SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
michael@0 756 SkScalar startRadius,
michael@0 757 const SkPoint& end,
michael@0 758 SkScalar endRadius,
michael@0 759 const SkColor colors[],
michael@0 760 const SkScalar pos[],
michael@0 761 int colorCount,
michael@0 762 SkShader::TileMode mode,
michael@0 763 SkUnitMapper* mapper,
michael@0 764 uint32_t flags) {
michael@0 765 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
michael@0 766 return NULL;
michael@0 767 }
michael@0 768 EXPAND_1_COLOR(colorCount);
michael@0 769
michael@0 770 SkGradientShaderBase::Descriptor desc;
michael@0 771 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
michael@0 772 return SkNEW_ARGS(SkTwoPointRadialGradient,
michael@0 773 (start, startRadius, end, endRadius, desc));
michael@0 774 }
michael@0 775
michael@0 776 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
michael@0 777 SkScalar startRadius,
michael@0 778 const SkPoint& end,
michael@0 779 SkScalar endRadius,
michael@0 780 const SkColor colors[],
michael@0 781 const SkScalar pos[],
michael@0 782 int colorCount,
michael@0 783 SkShader::TileMode mode,
michael@0 784 SkUnitMapper* mapper,
michael@0 785 uint32_t flags) {
michael@0 786 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
michael@0 787 return NULL;
michael@0 788 }
michael@0 789 if (start == end && startRadius == endRadius) {
michael@0 790 return SkNEW(SkEmptyShader);
michael@0 791 }
michael@0 792 EXPAND_1_COLOR(colorCount);
michael@0 793
michael@0 794 SkGradientShaderBase::Descriptor desc;
michael@0 795 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
michael@0 796 return SkNEW_ARGS(SkTwoPointConicalGradient,
michael@0 797 (start, startRadius, end, endRadius, desc));
michael@0 798 }
michael@0 799
michael@0 800 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
michael@0 801 const SkColor colors[],
michael@0 802 const SkScalar pos[],
michael@0 803 int colorCount, SkUnitMapper* mapper,
michael@0 804 uint32_t flags) {
michael@0 805 if (NULL == colors || colorCount < 1) {
michael@0 806 return NULL;
michael@0 807 }
michael@0 808 EXPAND_1_COLOR(colorCount);
michael@0 809
michael@0 810 SkGradientShaderBase::Descriptor desc;
michael@0 811 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper, flags);
michael@0 812 return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc));
michael@0 813 }
michael@0 814
michael@0 815 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
michael@0 816 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient)
michael@0 817 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient)
michael@0 818 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient)
michael@0 819 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient)
michael@0 820 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient)
michael@0 821 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
michael@0 822
michael@0 823 ///////////////////////////////////////////////////////////////////////////////
michael@0 824
michael@0 825 #if SK_SUPPORT_GPU
michael@0 826
michael@0 827 #include "effects/GrTextureStripAtlas.h"
michael@0 828 #include "GrTBackendEffectFactory.h"
michael@0 829 #include "SkGr.h"
michael@0 830
michael@0 831 GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory)
michael@0 832 : INHERITED(factory)
michael@0 833 , fCachedYCoord(SK_ScalarMax) {
michael@0 834 }
michael@0 835
michael@0 836 GrGLGradientEffect::~GrGLGradientEffect() { }
michael@0 837
michael@0 838 void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) {
michael@0 839
michael@0 840 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)) { // 2 Color case
michael@0 841 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 842 kVec4f_GrSLType, "GradientStartColor");
michael@0 843 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 844 kVec4f_GrSLType, "GradientEndColor");
michael@0 845
michael@0 846 } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){ // 3 Color Case
michael@0 847 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 848 kVec4f_GrSLType, "GradientStartColor");
michael@0 849 fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 850 kVec4f_GrSLType, "GradientMidColor");
michael@0 851 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 852 kVec4f_GrSLType, "GradientEndColor");
michael@0 853
michael@0 854 } else { // if not a fast case
michael@0 855 fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
michael@0 856 kFloat_GrSLType, "GradientYCoordFS");
michael@0 857 }
michael@0 858 }
michael@0 859
michael@0 860 static inline void set_color_uni(const GrGLUniformManager& uman,
michael@0 861 const GrGLUniformManager::UniformHandle uni,
michael@0 862 const SkColor* color) {
michael@0 863 uman.set4f(uni,
michael@0 864 SkColorGetR(*color) / 255.f,
michael@0 865 SkColorGetG(*color) / 255.f,
michael@0 866 SkColorGetB(*color) / 255.f,
michael@0 867 SkColorGetA(*color) / 255.f);
michael@0 868 }
michael@0 869
michael@0 870 static inline void set_mul_color_uni(const GrGLUniformManager& uman,
michael@0 871 const GrGLUniformManager::UniformHandle uni,
michael@0 872 const SkColor* color){
michael@0 873 float a = SkColorGetA(*color) / 255.f;
michael@0 874 float aDiv255 = a / 255.f;
michael@0 875 uman.set4f(uni,
michael@0 876 SkColorGetR(*color) * aDiv255,
michael@0 877 SkColorGetG(*color) * aDiv255,
michael@0 878 SkColorGetB(*color) * aDiv255,
michael@0 879 a);
michael@0 880 }
michael@0 881
michael@0 882 void GrGLGradientEffect::setData(const GrGLUniformManager& uman,
michael@0 883 const GrDrawEffect& drawEffect) {
michael@0 884
michael@0 885 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
michael@0 886
michael@0 887
michael@0 888 if (GrGradientEffect::kTwo_ColorType == e.getColorType()){
michael@0 889
michael@0 890 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
michael@0 891 set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
michael@0 892 set_mul_color_uni(uman, fColorEndUni, e.getColors(1));
michael@0 893 } else {
michael@0 894 set_color_uni(uman, fColorStartUni, e.getColors(0));
michael@0 895 set_color_uni(uman, fColorEndUni, e.getColors(1));
michael@0 896 }
michael@0 897
michael@0 898 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){
michael@0 899
michael@0 900 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
michael@0 901 set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
michael@0 902 set_mul_color_uni(uman, fColorMidUni, e.getColors(1));
michael@0 903 set_mul_color_uni(uman, fColorEndUni, e.getColors(2));
michael@0 904 } else {
michael@0 905 set_color_uni(uman, fColorStartUni, e.getColors(0));
michael@0 906 set_color_uni(uman, fColorMidUni, e.getColors(1));
michael@0 907 set_color_uni(uman, fColorEndUni, e.getColors(2));
michael@0 908 }
michael@0 909 } else {
michael@0 910
michael@0 911 SkScalar yCoord = e.getYCoord();
michael@0 912 if (yCoord != fCachedYCoord) {
michael@0 913 uman.set1f(fFSYUni, yCoord);
michael@0 914 fCachedYCoord = yCoord;
michael@0 915 }
michael@0 916 }
michael@0 917 }
michael@0 918
michael@0 919
michael@0 920 GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) {
michael@0 921 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
michael@0 922
michael@0 923 EffectKey key = 0;
michael@0 924
michael@0 925 if (GrGradientEffect::kTwo_ColorType == e.getColorType()) {
michael@0 926 key |= kTwoColorKey;
michael@0 927 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){
michael@0 928 key |= kThreeColorKey;
michael@0 929 }
michael@0 930
michael@0 931 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
michael@0 932 key |= kPremulBeforeInterpKey;
michael@0 933 }
michael@0 934
michael@0 935 return key;
michael@0 936 }
michael@0 937
michael@0 938 void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder,
michael@0 939 const char* gradientTValue,
michael@0 940 EffectKey key,
michael@0 941 const char* outputColor,
michael@0 942 const char* inputColor,
michael@0 943 const TextureSamplerArray& samplers) {
michael@0 944 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)){
michael@0 945 builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n",
michael@0 946 builder->getUniformVariable(fColorStartUni).c_str(),
michael@0 947 builder->getUniformVariable(fColorEndUni).c_str(),
michael@0 948 gradientTValue);
michael@0 949 // Note that we could skip this step if both colors are known to be opaque. Two
michael@0 950 // considerations:
michael@0 951 // The gradient SkShader reporting opaque is more restrictive than necessary in the two pt
michael@0 952 // case. Make sure the key reflects this optimization (and note that it can use the same
michael@0 953 // shader as thekBeforeIterp case). This same optimization applies to the 3 color case below.
michael@0 954 if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) {
michael@0 955 builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
michael@0 956 }
michael@0 957
michael@0 958 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 959 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
michael@0 960 } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){
michael@0 961 builder->fsCodeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n",
michael@0 962 gradientTValue);
michael@0 963 builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n",
michael@0 964 builder->getUniformVariable(fColorStartUni).c_str());
michael@0 965 if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) {
michael@0 966 // The Tegra3 compiler will sometimes never return if we have
michael@0 967 // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression.
michael@0 968 builder->fsCodeAppend("\tfloat minAbs = abs(oneMinus2t);\n");
michael@0 969 builder->fsCodeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n");
michael@0 970 builder->fsCodeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n",
michael@0 971 builder->getUniformVariable(fColorMidUni).c_str());
michael@0 972 } else {
michael@0 973 builder->fsCodeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n",
michael@0 974 builder->getUniformVariable(fColorMidUni).c_str());
michael@0 975 }
michael@0 976 builder->fsCodeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n",
michael@0 977 builder->getUniformVariable(fColorEndUni).c_str());
michael@0 978 if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) {
michael@0 979 builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
michael@0 980 }
michael@0 981
michael@0 982 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
michael@0 983 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
michael@0 984 } else {
michael@0 985 builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n",
michael@0 986 gradientTValue,
michael@0 987 builder->getUniformVariable(fFSYUni).c_str());
michael@0 988 builder->fsCodeAppendf("\t%s = ", outputColor);
michael@0 989 builder->fsAppendTextureLookupAndModulate(inputColor,
michael@0 990 samplers[0],
michael@0 991 "coord");
michael@0 992 builder->fsCodeAppend(";\n");
michael@0 993 }
michael@0 994 }
michael@0 995
michael@0 996 /////////////////////////////////////////////////////////////////////
michael@0 997
michael@0 998 GrGradientEffect::GrGradientEffect(GrContext* ctx,
michael@0 999 const SkGradientShaderBase& shader,
michael@0 1000 const SkMatrix& matrix,
michael@0 1001 SkShader::TileMode tileMode) {
michael@0 1002
michael@0 1003 fIsOpaque = shader.isOpaque();
michael@0 1004
michael@0 1005 SkShader::GradientInfo info;
michael@0 1006 SkScalar pos[3] = {0};
michael@0 1007
michael@0 1008 info.fColorCount = 3;
michael@0 1009 info.fColors = &fColors[0];
michael@0 1010 info.fColorOffsets = &pos[0];
michael@0 1011 shader.asAGradient(&info);
michael@0 1012
michael@0 1013 // The two and three color specializations do not currently support tiling.
michael@0 1014 bool foundSpecialCase = false;
michael@0 1015 if (SkShader::kClamp_TileMode == info.fTileMode) {
michael@0 1016 if (2 == info.fColorCount) {
michael@0 1017 fRow = -1; // flag for no atlas
michael@0 1018 fColorType = kTwo_ColorType;
michael@0 1019 foundSpecialCase = true;
michael@0 1020 } else if (3 == info.fColorCount &&
michael@0 1021 (SkScalarAbs(pos[1] - SK_ScalarHalf) < SK_Scalar1 / 1000)) { // 3 color symmetric
michael@0 1022 fRow = -1; // flag for no atlas
michael@0 1023 fColorType = kThree_ColorType;
michael@0 1024 foundSpecialCase = true;
michael@0 1025 }
michael@0 1026 }
michael@0 1027 if (foundSpecialCase) {
michael@0 1028 if (SkGradientShader::kInterpolateColorsInPremul_Flag & info.fGradientFlags) {
michael@0 1029 fPremulType = kBeforeInterp_PremulType;
michael@0 1030 } else {
michael@0 1031 fPremulType = kAfterInterp_PremulType;
michael@0 1032 }
michael@0 1033 fCoordTransform.reset(kCoordSet, matrix);
michael@0 1034 } else {
michael@0 1035 // doesn't matter how this is set, just be consistent because it is part of the effect key.
michael@0 1036 fPremulType = kBeforeInterp_PremulType;
michael@0 1037 SkBitmap bitmap;
michael@0 1038 shader.getGradientTableBitmap(&bitmap);
michael@0 1039 fColorType = kTexture_ColorType;
michael@0 1040
michael@0 1041 GrTextureStripAtlas::Desc desc;
michael@0 1042 desc.fWidth = bitmap.width();
michael@0 1043 desc.fHeight = 32;
michael@0 1044 desc.fRowHeight = bitmap.height();
michael@0 1045 desc.fContext = ctx;
michael@0 1046 desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.colorType(), bitmap.alphaType());
michael@0 1047 fAtlas = GrTextureStripAtlas::GetAtlas(desc);
michael@0 1048 SkASSERT(NULL != fAtlas);
michael@0 1049
michael@0 1050 // We always filter the gradient table. Each table is one row of a texture, always y-clamp.
michael@0 1051 GrTextureParams params;
michael@0 1052 params.setFilterMode(GrTextureParams::kBilerp_FilterMode);
michael@0 1053 params.setTileModeX(tileMode);
michael@0 1054
michael@0 1055 fRow = fAtlas->lockRow(bitmap);
michael@0 1056 if (-1 != fRow) {
michael@0 1057 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf *
michael@0 1058 fAtlas->getVerticalScaleFactor();
michael@0 1059 fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture());
michael@0 1060 fTextureAccess.reset(fAtlas->getTexture(), params);
michael@0 1061 } else {
michael@0 1062 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &params);
michael@0 1063 fCoordTransform.reset(kCoordSet, matrix, texture);
michael@0 1064 fTextureAccess.reset(texture, params);
michael@0 1065 fYCoord = SK_ScalarHalf;
michael@0 1066
michael@0 1067 // Unlock immediately, this is not great, but we don't have a way of
michael@0 1068 // knowing when else to unlock it currently, so it may get purged from
michael@0 1069 // the cache, but it'll still be ref'd until it's no longer being used.
michael@0 1070 GrUnlockAndUnrefCachedBitmapTexture(texture);
michael@0 1071 }
michael@0 1072 this->addTextureAccess(&fTextureAccess);
michael@0 1073 }
michael@0 1074 this->addCoordTransform(&fCoordTransform);
michael@0 1075 }
michael@0 1076
michael@0 1077 GrGradientEffect::~GrGradientEffect() {
michael@0 1078 if (this->useAtlas()) {
michael@0 1079 fAtlas->unlockRow(fRow);
michael@0 1080 }
michael@0 1081 }
michael@0 1082
michael@0 1083 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const {
michael@0 1084 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect);
michael@0 1085
michael@0 1086 if (this->fColorType == s.getColorType()){
michael@0 1087
michael@0 1088 if (kTwo_ColorType == fColorType) {
michael@0 1089 if (*this->getColors(0) != *s.getColors(0) ||
michael@0 1090 *this->getColors(1) != *s.getColors(1)) {
michael@0 1091 return false;
michael@0 1092 }
michael@0 1093 } else if (kThree_ColorType == fColorType) {
michael@0 1094 if (*this->getColors(0) != *s.getColors(0) ||
michael@0 1095 *this->getColors(1) != *s.getColors(1) ||
michael@0 1096 *this->getColors(2) != *s.getColors(2)) {
michael@0 1097 return false;
michael@0 1098 }
michael@0 1099 } else {
michael@0 1100 if (fYCoord != s.getYCoord()) {
michael@0 1101 return false;
michael@0 1102 }
michael@0 1103 }
michael@0 1104
michael@0 1105 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() &&
michael@0 1106 fTextureAccess.getParams().getTileModeX() ==
michael@0 1107 s.fTextureAccess.getParams().getTileModeX() &&
michael@0 1108 this->useAtlas() == s.useAtlas() &&
michael@0 1109 fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix());
michael@0 1110 }
michael@0 1111
michael@0 1112 return false;
michael@0 1113 }
michael@0 1114
michael@0 1115 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
michael@0 1116 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) {
michael@0 1117 *validFlags = kA_GrColorComponentFlag;
michael@0 1118 } else {
michael@0 1119 *validFlags = 0;
michael@0 1120 }
michael@0 1121 }
michael@0 1122
michael@0 1123 int GrGradientEffect::RandomGradientParams(SkRandom* random,
michael@0 1124 SkColor colors[],
michael@0 1125 SkScalar** stops,
michael@0 1126 SkShader::TileMode* tm) {
michael@0 1127 int outColors = random->nextRangeU(1, kMaxRandomGradientColors);
michael@0 1128
michael@0 1129 // if one color, omit stops, otherwise randomly decide whether or not to
michael@0 1130 if (outColors == 1 || (outColors >= 2 && random->nextBool())) {
michael@0 1131 *stops = NULL;
michael@0 1132 }
michael@0 1133
michael@0 1134 SkScalar stop = 0.f;
michael@0 1135 for (int i = 0; i < outColors; ++i) {
michael@0 1136 colors[i] = random->nextU();
michael@0 1137 if (NULL != *stops) {
michael@0 1138 (*stops)[i] = stop;
michael@0 1139 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
michael@0 1140 }
michael@0 1141 }
michael@0 1142 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount));
michael@0 1143
michael@0 1144 return outColors;
michael@0 1145 }
michael@0 1146
michael@0 1147 #endif

mercurial