OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGradientShaderPriv.h" | 8 #include "SkGradientShaderPriv.h" |
9 #include "SkLinearGradient.h" | 9 #include "SkLinearGradient.h" |
10 #include "SkRadialGradient.h" | 10 #include "SkRadialGradient.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 } | 42 } |
43 | 43 |
44 if (!buffer.readColorArray(const_cast<SkColor*>(fColors), fCount)) { | 44 if (!buffer.readColorArray(const_cast<SkColor*>(fColors), fCount)) { |
45 return false; | 45 return false; |
46 } | 46 } |
47 if (buffer.readBool()) { | 47 if (buffer.readBool()) { |
48 if (!buffer.readScalarArray(const_cast<SkScalar*>(fPos), fCount)) { | 48 if (!buffer.readScalarArray(const_cast<SkScalar*>(fPos), fCount)) { |
49 return false; | 49 return false; |
50 } | 50 } |
51 } else { | 51 } else { |
52 fPos = NULL; | 52 fPos = nullptr; |
53 } | 53 } |
54 | 54 |
55 fTileMode = (SkShader::TileMode)buffer.read32(); | 55 fTileMode = (SkShader::TileMode)buffer.read32(); |
56 fGradFlags = buffer.read32(); | 56 fGradFlags = buffer.read32(); |
57 | 57 |
58 if (buffer.readBool()) { | 58 if (buffer.readBool()) { |
59 fLocalMatrix = &fLocalMatrixStorage; | 59 fLocalMatrix = &fLocalMatrixStorage; |
60 buffer.readMatrix(&fLocalMatrixStorage); | 60 buffer.readMatrix(&fLocalMatrixStorage); |
61 } else { | 61 } else { |
62 fLocalMatrix = NULL; | 62 fLocalMatrix = nullptr; |
63 } | 63 } |
64 return buffer.isValid(); | 64 return buffer.isValid(); |
65 } | 65 } |
66 | 66 |
67 ////////////////////////////////////////////////////////////////////////////////
//////////// | 67 ////////////////////////////////////////////////////////////////////////////////
//////////// |
68 | 68 |
69 SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatri
x& ptsToUnit) | 69 SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatri
x& ptsToUnit) |
70 : INHERITED(desc.fLocalMatrix) | 70 : INHERITED(desc.fLocalMatrix) |
71 , fPtsToUnit(ptsToUnit) | 71 , fPtsToUnit(ptsToUnit) |
72 { | 72 { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 if (dummyLast) { | 123 if (dummyLast) { |
124 origColors += desc.fCount; | 124 origColors += desc.fCount; |
125 *origColors = desc.fColors[desc.fCount - 1]; | 125 *origColors = desc.fColors[desc.fCount - 1]; |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 if (desc.fPos && fColorCount) { | 129 if (desc.fPos && fColorCount) { |
130 fOrigPos = (SkScalar*)(fOrigColors + fColorCount); | 130 fOrigPos = (SkScalar*)(fOrigColors + fColorCount); |
131 fRecs = (Rec*)(fOrigPos + fColorCount); | 131 fRecs = (Rec*)(fOrigPos + fColorCount); |
132 } else { | 132 } else { |
133 fOrigPos = NULL; | 133 fOrigPos = nullptr; |
134 fRecs = (Rec*)(fOrigColors + fColorCount); | 134 fRecs = (Rec*)(fOrigColors + fColorCount); |
135 } | 135 } |
136 | 136 |
137 if (fColorCount > 2) { | 137 if (fColorCount > 2) { |
138 Rec* recs = fRecs; | 138 Rec* recs = fRecs; |
139 recs->fPos = 0; | 139 recs->fPos = 0; |
140 // recs->fScale = 0; // unused; | 140 // recs->fScale = 0; // unused; |
141 recs += 1; | 141 recs += 1; |
142 if (desc.fPos) { | 142 if (desc.fPos) { |
143 SkScalar* origPosPtr = fOrigPos; | 143 SkScalar* origPosPtr = fOrigPos; |
(...skipping 24 matching lines...) Expand all Loading... |
168 if (diff > 0) { | 168 if (diff > 0) { |
169 recs->fScale = (1 << 24) / diff; | 169 recs->fScale = (1 << 24) / diff; |
170 } else { | 170 } else { |
171 recs->fScale = 0; // ignore this segment | 171 recs->fScale = 0; // ignore this segment |
172 } | 172 } |
173 // get ready for the next value | 173 // get ready for the next value |
174 prev = curr; | 174 prev = curr; |
175 recs += 1; | 175 recs += 1; |
176 } | 176 } |
177 } else { // assume even distribution | 177 } else { // assume even distribution |
178 fOrigPos = NULL; | 178 fOrigPos = nullptr; |
179 | 179 |
180 SkFixed dp = SK_Fixed1 / (desc.fCount - 1); | 180 SkFixed dp = SK_Fixed1 / (desc.fCount - 1); |
181 SkFixed p = dp; | 181 SkFixed p = dp; |
182 SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp | 182 SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp |
183 for (int i = 1; i < desc.fCount - 1; i++) { | 183 for (int i = 1; i < desc.fCount - 1; i++) { |
184 recs->fPos = p; | 184 recs->fPos = p; |
185 recs->fScale = scale; | 185 recs->fScale = scale; |
186 recs += 1; | 186 recs += 1; |
187 p += dp; | 187 p += dp; |
188 } | 188 } |
189 recs->fPos = SK_Fixed1; | 189 recs->fPos = SK_Fixed1; |
190 recs->fScale = scale; | 190 recs->fScale = scale; |
191 } | 191 } |
192 } else if (desc.fPos) { | 192 } else if (desc.fPos) { |
193 SkASSERT(2 == fColorCount); | 193 SkASSERT(2 == fColorCount); |
194 fOrigPos[0] = SkScalarPin(desc.fPos[0], 0, 1); | 194 fOrigPos[0] = SkScalarPin(desc.fPos[0], 0, 1); |
195 fOrigPos[1] = SkScalarPin(desc.fPos[1], fOrigPos[0], 1); | 195 fOrigPos[1] = SkScalarPin(desc.fPos[1], fOrigPos[0], 1); |
196 if (0 == fOrigPos[0] && 1 == fOrigPos[1]) { | 196 if (0 == fOrigPos[0] && 1 == fOrigPos[1]) { |
197 fOrigPos = NULL; | 197 fOrigPos = nullptr; |
198 } | 198 } |
199 } | 199 } |
200 this->initCommon(); | 200 this->initCommon(); |
201 } | 201 } |
202 | 202 |
203 SkGradientShaderBase::~SkGradientShaderBase() { | 203 SkGradientShaderBase::~SkGradientShaderBase() { |
204 if (fOrigColors != fStorage) { | 204 if (fOrigColors != fStorage) { |
205 sk_free(fOrigColors); | 205 sk_free(fOrigColors); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 void SkGradientShaderBase::initCommon() { | 209 void SkGradientShaderBase::initCommon() { |
210 unsigned colorAlpha = 0xFF; | 210 unsigned colorAlpha = 0xFF; |
211 for (int i = 0; i < fColorCount; i++) { | 211 for (int i = 0; i < fColorCount; i++) { |
212 colorAlpha &= SkColorGetA(fOrigColors[i]); | 212 colorAlpha &= SkColorGetA(fOrigColors[i]); |
213 } | 213 } |
214 fColorsAreOpaque = colorAlpha == 0xFF; | 214 fColorsAreOpaque = colorAlpha == 0xFF; |
215 } | 215 } |
216 | 216 |
217 void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { | 217 void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { |
218 Descriptor desc; | 218 Descriptor desc; |
219 desc.fColors = fOrigColors; | 219 desc.fColors = fOrigColors; |
220 desc.fPos = fOrigPos; | 220 desc.fPos = fOrigPos; |
221 desc.fCount = fColorCount; | 221 desc.fCount = fColorCount; |
222 desc.fTileMode = fTileMode; | 222 desc.fTileMode = fTileMode; |
223 desc.fGradFlags = fGradFlags; | 223 desc.fGradFlags = fGradFlags; |
224 | 224 |
225 const SkMatrix& m = this->getLocalMatrix(); | 225 const SkMatrix& m = this->getLocalMatrix(); |
226 desc.fLocalMatrix = m.isIdentity() ? NULL : &m; | 226 desc.fLocalMatrix = m.isIdentity() ? nullptr : &m; |
227 desc.flatten(buffer); | 227 desc.flatten(buffer); |
228 } | 228 } |
229 | 229 |
230 SkGradientShaderBase::GpuColorType SkGradientShaderBase::getGpuColorType(SkColor
colors[3]) const { | 230 SkGradientShaderBase::GpuColorType SkGradientShaderBase::getGpuColorType(SkColor
colors[3]) const { |
231 if (fColorCount <= 3) { | 231 if (fColorCount <= 3) { |
232 memcpy(colors, fOrigColors, fColorCount * sizeof(SkColor)); | 232 memcpy(colors, fOrigColors, fColorCount * sizeof(SkColor)); |
233 } | 233 } |
234 | 234 |
235 if (SkShader::kClamp_TileMode == fTileMode) { | 235 if (SkShader::kClamp_TileMode == fTileMode) { |
236 if (2 == fColorCount) { | 236 if (2 == fColorCount) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 } | 317 } |
318 | 318 |
319 SkGradientShaderBase::GradientShaderCache::GradientShaderCache( | 319 SkGradientShaderBase::GradientShaderCache::GradientShaderCache( |
320 U8CPU alpha, const SkGradientShaderBase& shader) | 320 U8CPU alpha, const SkGradientShaderBase& shader) |
321 : fCacheAlpha(alpha) | 321 : fCacheAlpha(alpha) |
322 , fShader(shader) | 322 , fShader(shader) |
323 , fCache16Inited(false) | 323 , fCache16Inited(false) |
324 , fCache32Inited(false) | 324 , fCache32Inited(false) |
325 { | 325 { |
326 // Only initialize the cache in getCache16/32. | 326 // Only initialize the cache in getCache16/32. |
327 fCache16 = NULL; | 327 fCache16 = nullptr; |
328 fCache32 = NULL; | 328 fCache32 = nullptr; |
329 fCache16Storage = NULL; | 329 fCache16Storage = nullptr; |
330 fCache32PixelRef = NULL; | 330 fCache32PixelRef = nullptr; |
331 } | 331 } |
332 | 332 |
333 SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() { | 333 SkGradientShaderBase::GradientShaderCache::~GradientShaderCache() { |
334 sk_free(fCache16Storage); | 334 sk_free(fCache16Storage); |
335 SkSafeUnref(fCache32PixelRef); | 335 SkSafeUnref(fCache32PixelRef); |
336 } | 336 } |
337 | 337 |
338 #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) | 338 #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) |
339 | 339 |
340 /** We take the original colors, not our premultiplied PMColors, since we can | 340 /** We take the original colors, not our premultiplied PMColors, since we can |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 this); | 528 this); |
529 SkASSERT(fCache16); | 529 SkASSERT(fCache16); |
530 return fCache16; | 530 return fCache16; |
531 } | 531 } |
532 | 532 |
533 void SkGradientShaderBase::GradientShaderCache::initCache16(GradientShaderCache*
cache) { | 533 void SkGradientShaderBase::GradientShaderCache::initCache16(GradientShaderCache*
cache) { |
534 // double the count for dither entries | 534 // double the count for dither entries |
535 const int entryCount = kCache16Count * 2; | 535 const int entryCount = kCache16Count * 2; |
536 const size_t allocSize = sizeof(uint16_t) * entryCount; | 536 const size_t allocSize = sizeof(uint16_t) * entryCount; |
537 | 537 |
538 SkASSERT(NULL == cache->fCache16Storage); | 538 SkASSERT(nullptr == cache->fCache16Storage); |
539 cache->fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); | 539 cache->fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); |
540 cache->fCache16 = cache->fCache16Storage; | 540 cache->fCache16 = cache->fCache16Storage; |
541 if (cache->fShader.fColorCount == 2) { | 541 if (cache->fShader.fColorCount == 2) { |
542 Build16bitCache(cache->fCache16, cache->fShader.fOrigColors[0], | 542 Build16bitCache(cache->fCache16, cache->fShader.fOrigColors[0], |
543 cache->fShader.fOrigColors[1], kCache16Count); | 543 cache->fShader.fOrigColors[1], kCache16Count); |
544 } else { | 544 } else { |
545 Rec* rec = cache->fShader.fRecs; | 545 Rec* rec = cache->fShader.fRecs; |
546 int prevIndex = 0; | 546 int prevIndex = 0; |
547 for (int i = 1; i < cache->fShader.fColorCount; i++) { | 547 for (int i = 1; i < cache->fShader.fColorCount; i++) { |
548 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; | 548 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; |
(...skipping 11 matching lines...) Expand all Loading... |
560 SkOnce(&fCache32Inited, &fCache32Mutex, SkGradientShaderBase::GradientShader
Cache::initCache32, | 560 SkOnce(&fCache32Inited, &fCache32Mutex, SkGradientShaderBase::GradientShader
Cache::initCache32, |
561 this); | 561 this); |
562 SkASSERT(fCache32); | 562 SkASSERT(fCache32); |
563 return fCache32; | 563 return fCache32; |
564 } | 564 } |
565 | 565 |
566 void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache*
cache) { | 566 void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache*
cache) { |
567 const int kNumberOfDitherRows = 4; | 567 const int kNumberOfDitherRows = 4; |
568 const SkImageInfo info = SkImageInfo::MakeN32Premul(kCache32Count, kNumberOf
DitherRows); | 568 const SkImageInfo info = SkImageInfo::MakeN32Premul(kCache32Count, kNumberOf
DitherRows); |
569 | 569 |
570 SkASSERT(NULL == cache->fCache32PixelRef); | 570 SkASSERT(nullptr == cache->fCache32PixelRef); |
571 cache->fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL); | 571 cache->fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, nullptr); |
572 cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->getAddr(); | 572 cache->fCache32 = (SkPMColor*)cache->fCache32PixelRef->getAddr(); |
573 if (cache->fShader.fColorCount == 2) { | 573 if (cache->fShader.fColorCount == 2) { |
574 Build32bitCache(cache->fCache32, cache->fShader.fOrigColors[0], | 574 Build32bitCache(cache->fCache32, cache->fShader.fOrigColors[0], |
575 cache->fShader.fOrigColors[1], kCache32Count, cache->fCa
cheAlpha, | 575 cache->fShader.fOrigColors[1], kCache32Count, cache->fCa
cheAlpha, |
576 cache->fShader.fGradFlags); | 576 cache->fShader.fGradFlags); |
577 } else { | 577 } else { |
578 Rec* rec = cache->fShader.fRecs; | 578 Rec* rec = cache->fShader.fRecs; |
579 int prevIndex = 0; | 579 int prevIndex = 0; |
580 for (int i = 1; i < cache->fShader.fColorCount; i++) { | 580 for (int i = 1; i < cache->fShader.fColorCount; i++) { |
581 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; | 581 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 *buffer++ = fGradFlags; | 640 *buffer++ = fGradFlags; |
641 SkASSERT(buffer - storage.get() == count); | 641 SkASSERT(buffer - storage.get() == count); |
642 | 642 |
643 /////////////////////////////////// | 643 /////////////////////////////////// |
644 | 644 |
645 static SkGradientBitmapCache* gCache; | 645 static SkGradientBitmapCache* gCache; |
646 // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp | 646 // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp |
647 static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; | 647 static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; |
648 SkAutoMutexAcquire ama(gGradientCacheMutex); | 648 SkAutoMutexAcquire ama(gGradientCacheMutex); |
649 | 649 |
650 if (NULL == gCache) { | 650 if (nullptr == gCache) { |
651 gCache = new SkGradientBitmapCache(MAX_NUM_CACHED_GRADIENT_BITMAPS); | 651 gCache = new SkGradientBitmapCache(MAX_NUM_CACHED_GRADIENT_BITMAPS); |
652 } | 652 } |
653 size_t size = count * sizeof(int32_t); | 653 size_t size = count * sizeof(int32_t); |
654 | 654 |
655 if (!gCache->find(storage.get(), size, bitmap)) { | 655 if (!gCache->find(storage.get(), size, bitmap)) { |
656 // force our cahce32pixelref to be built | 656 // force our cahce32pixelref to be built |
657 (void)cache->getCache32(); | 657 (void)cache->getCache32(); |
658 bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1)); | 658 bitmap->setInfo(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
659 bitmap->setPixelRef(cache->getCache32PixelRef()); | 659 bitmap->setPixelRef(cache->getCache32PixelRef()); |
660 | 660 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
730 this->INHERITED::toString(str); | 730 this->INHERITED::toString(str); |
731 } | 731 } |
732 #endif | 732 #endif |
733 | 733 |
734 /////////////////////////////////////////////////////////////////////////////// | 734 /////////////////////////////////////////////////////////////////////////////// |
735 /////////////////////////////////////////////////////////////////////////////// | 735 /////////////////////////////////////////////////////////////////////////////// |
736 | 736 |
737 // Return true if these parameters are valid/legal/safe to construct a gradient | 737 // Return true if these parameters are valid/legal/safe to construct a gradient |
738 // | 738 // |
739 static bool valid_grad(const SkColor colors[], const SkScalar pos[], int count,
unsigned tileMode) { | 739 static bool valid_grad(const SkColor colors[], const SkScalar pos[], int count,
unsigned tileMode) { |
740 return NULL != colors && count >= 1 && tileMode < (unsigned)SkShader::kTileM
odeCount; | 740 return nullptr != colors && count >= 1 && tileMode < (unsigned)SkShader::kTi
leModeCount; |
741 } | 741 } |
742 | 742 |
743 // assumes colors is SkColor* and pos is SkScalar* | 743 // assumes colors is SkColor* and pos is SkScalar* |
744 #define EXPAND_1_COLOR(count) \ | 744 #define EXPAND_1_COLOR(count) \ |
745 SkColor tmp[2]; \ | 745 SkColor tmp[2]; \ |
746 do { \ | 746 do { \ |
747 if (1 == count) { \ | 747 if (1 == count) { \ |
748 tmp[0] = tmp[1] = colors[0]; \ | 748 tmp[0] = tmp[1] = colors[0]; \ |
749 colors = tmp; \ | 749 colors = tmp; \ |
750 pos = NULL; \ | 750 pos = nullptr; \ |
751 count = 2; \ | 751 count = 2; \ |
752 } \ | 752 } \ |
753 } while (0) | 753 } while (0) |
754 | 754 |
755 static void desc_init(SkGradientShaderBase::Descriptor* desc, | 755 static void desc_init(SkGradientShaderBase::Descriptor* desc, |
756 const SkColor colors[], const SkScalar pos[], int colorCou
nt, | 756 const SkColor colors[], const SkScalar pos[], int colorCou
nt, |
757 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l
ocalMatrix) { | 757 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l
ocalMatrix) { |
758 desc->fColors = colors; | 758 desc->fColors = colors; |
759 desc->fPos = pos; | 759 desc->fPos = pos; |
760 desc->fCount = colorCount; | 760 desc->fCount = colorCount; |
761 desc->fTileMode = mode; | 761 desc->fTileMode = mode; |
762 desc->fGradFlags = flags; | 762 desc->fGradFlags = flags; |
763 desc->fLocalMatrix = localMatrix; | 763 desc->fLocalMatrix = localMatrix; |
764 } | 764 } |
765 | 765 |
766 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], | 766 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], |
767 const SkColor colors[], | 767 const SkColor colors[], |
768 const SkScalar pos[], int colorCount, | 768 const SkScalar pos[], int colorCount, |
769 SkShader::TileMode mode, | 769 SkShader::TileMode mode, |
770 uint32_t flags, | 770 uint32_t flags, |
771 const SkMatrix* localMatrix) { | 771 const SkMatrix* localMatrix) { |
772 if (!pts) { | 772 if (!pts) { |
773 return NULL; | 773 return nullptr; |
774 } | 774 } |
775 if (!valid_grad(colors, pos, colorCount, mode)) { | 775 if (!valid_grad(colors, pos, colorCount, mode)) { |
776 return NULL; | 776 return nullptr; |
777 } | 777 } |
778 EXPAND_1_COLOR(colorCount); | 778 EXPAND_1_COLOR(colorCount); |
779 | 779 |
780 SkGradientShaderBase::Descriptor desc; | 780 SkGradientShaderBase::Descriptor desc; |
781 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 781 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
782 return new SkLinearGradient(pts, desc); | 782 return new SkLinearGradient(pts, desc); |
783 } | 783 } |
784 | 784 |
785 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, | 785 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, |
786 const SkColor colors[], | 786 const SkColor colors[], |
787 const SkScalar pos[], int colorCount, | 787 const SkScalar pos[], int colorCount, |
788 SkShader::TileMode mode, | 788 SkShader::TileMode mode, |
789 uint32_t flags, | 789 uint32_t flags, |
790 const SkMatrix* localMatrix) { | 790 const SkMatrix* localMatrix) { |
791 if (radius <= 0) { | 791 if (radius <= 0) { |
792 return NULL; | 792 return nullptr; |
793 } | 793 } |
794 if (!valid_grad(colors, pos, colorCount, mode)) { | 794 if (!valid_grad(colors, pos, colorCount, mode)) { |
795 return NULL; | 795 return nullptr; |
796 } | 796 } |
797 EXPAND_1_COLOR(colorCount); | 797 EXPAND_1_COLOR(colorCount); |
798 | 798 |
799 SkGradientShaderBase::Descriptor desc; | 799 SkGradientShaderBase::Descriptor desc; |
800 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 800 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
801 return new SkRadialGradient(center, radius, desc); | 801 return new SkRadialGradient(center, radius, desc); |
802 } | 802 } |
803 | 803 |
804 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, | 804 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, |
805 SkScalar startRadius, | 805 SkScalar startRadius, |
806 const SkPoint& end, | 806 const SkPoint& end, |
807 SkScalar endRadius, | 807 SkScalar endRadius, |
808 const SkColor colors[], | 808 const SkColor colors[], |
809 const SkScalar pos[], | 809 const SkScalar pos[], |
810 int colorCount, | 810 int colorCount, |
811 SkShader::TileMode mode, | 811 SkShader::TileMode mode, |
812 uint32_t flags, | 812 uint32_t flags, |
813 const SkMatrix* localMatrix) { | 813 const SkMatrix* localMatrix) { |
814 if (startRadius < 0 || endRadius < 0) { | 814 if (startRadius < 0 || endRadius < 0) { |
815 return NULL; | 815 return nullptr; |
816 } | 816 } |
817 if (!valid_grad(colors, pos, colorCount, mode)) { | 817 if (!valid_grad(colors, pos, colorCount, mode)) { |
818 return NULL; | 818 return nullptr; |
819 } | 819 } |
820 if (start == end && startRadius == endRadius) { | 820 if (start == end && startRadius == endRadius) { |
821 return SkShader::CreateEmptyShader(); | 821 return SkShader::CreateEmptyShader(); |
822 } | 822 } |
823 | 823 |
824 EXPAND_1_COLOR(colorCount); | 824 EXPAND_1_COLOR(colorCount); |
825 | 825 |
826 bool flipGradient = startRadius > endRadius; | 826 bool flipGradient = startRadius > endRadius; |
827 | 827 |
828 SkGradientShaderBase::Descriptor desc; | 828 SkGradientShaderBase::Descriptor desc; |
829 | 829 |
830 if (!flipGradient) { | 830 if (!flipGradient) { |
831 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 831 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
832 return new SkTwoPointConicalGradient(start, startRadius, end, endRadius,
flipGradient, | 832 return new SkTwoPointConicalGradient(start, startRadius, end, endRadius,
flipGradient, |
833 desc); | 833 desc); |
834 } else { | 834 } else { |
835 SkAutoSTArray<8, SkColor> colorsNew(colorCount); | 835 SkAutoSTArray<8, SkColor> colorsNew(colorCount); |
836 SkAutoSTArray<8, SkScalar> posNew(colorCount); | 836 SkAutoSTArray<8, SkScalar> posNew(colorCount); |
837 for (int i = 0; i < colorCount; ++i) { | 837 for (int i = 0; i < colorCount; ++i) { |
838 colorsNew[i] = colors[colorCount - i - 1]; | 838 colorsNew[i] = colors[colorCount - i - 1]; |
839 } | 839 } |
840 | 840 |
841 if (pos) { | 841 if (pos) { |
842 for (int i = 0; i < colorCount; ++i) { | 842 for (int i = 0; i < colorCount; ++i) { |
843 posNew[i] = 1 - pos[colorCount - i - 1]; | 843 posNew[i] = 1 - pos[colorCount - i - 1]; |
844 } | 844 } |
845 desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, fl
ags, localMatrix); | 845 desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, fl
ags, localMatrix); |
846 } else { | 846 } else { |
847 desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, flags, loc
alMatrix); | 847 desc_init(&desc, colorsNew.get(), nullptr, colorCount, mode, flags,
localMatrix); |
848 } | 848 } |
849 | 849 |
850 return new SkTwoPointConicalGradient(end, endRadius, start, startRadius,
flipGradient, | 850 return new SkTwoPointConicalGradient(end, endRadius, start, startRadius,
flipGradient, |
851 desc); | 851 desc); |
852 } | 852 } |
853 } | 853 } |
854 | 854 |
855 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, | 855 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
856 const SkColor colors[], | 856 const SkColor colors[], |
857 const SkScalar pos[], | 857 const SkScalar pos[], |
858 int colorCount, | 858 int colorCount, |
859 uint32_t flags, | 859 uint32_t flags, |
860 const SkMatrix* localMatrix) { | 860 const SkMatrix* localMatrix) { |
861 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { | 861 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { |
862 return NULL; | 862 return nullptr; |
863 } | 863 } |
864 EXPAND_1_COLOR(colorCount); | 864 EXPAND_1_COLOR(colorCount); |
865 | 865 |
866 SkGradientShaderBase::Descriptor desc; | 866 SkGradientShaderBase::Descriptor desc; |
867 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags,
localMatrix); | 867 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags,
localMatrix); |
868 return new SkSweepGradient(cx, cy, desc); | 868 return new SkSweepGradient(cx, cy, desc); |
869 } | 869 } |
870 | 870 |
871 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) | 871 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
872 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) | 872 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1161 } | 1161 } |
1162 | 1162 |
1163 int GrGradientEffect::RandomGradientParams(SkRandom* random, | 1163 int GrGradientEffect::RandomGradientParams(SkRandom* random, |
1164 SkColor colors[], | 1164 SkColor colors[], |
1165 SkScalar** stops, | 1165 SkScalar** stops, |
1166 SkShader::TileMode* tm) { | 1166 SkShader::TileMode* tm) { |
1167 int outColors = random->nextRangeU(1, kMaxRandomGradientColors); | 1167 int outColors = random->nextRangeU(1, kMaxRandomGradientColors); |
1168 | 1168 |
1169 // if one color, omit stops, otherwise randomly decide whether or not to | 1169 // if one color, omit stops, otherwise randomly decide whether or not to |
1170 if (outColors == 1 || (outColors >= 2 && random->nextBool())) { | 1170 if (outColors == 1 || (outColors >= 2 && random->nextBool())) { |
1171 *stops = NULL; | 1171 *stops = nullptr; |
1172 } | 1172 } |
1173 | 1173 |
1174 SkScalar stop = 0.f; | 1174 SkScalar stop = 0.f; |
1175 for (int i = 0; i < outColors; ++i) { | 1175 for (int i = 0; i < outColors; ++i) { |
1176 colors[i] = random->nextU(); | 1176 colors[i] = random->nextU(); |
1177 if (*stops) { | 1177 if (*stops) { |
1178 (*stops)[i] = stop; | 1178 (*stops)[i] = stop; |
1179 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; | 1179 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; |
1180 } | 1180 } |
1181 } | 1181 } |
1182 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); | 1182 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); |
1183 | 1183 |
1184 return outColors; | 1184 return outColors; |
1185 } | 1185 } |
1186 | 1186 |
1187 #endif | 1187 #endif |
OLD | NEW |