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