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" |
11 #include "SkTwoPointRadialGradient.h" | 11 #include "SkTwoPointRadialGradient.h" |
12 #include "SkTwoPointConicalGradient.h" | 12 #include "SkTwoPointConicalGradient.h" |
13 #include "SkSweepGradient.h" | 13 #include "SkSweepGradient.h" |
14 | 14 |
15 SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatri
x* localMatrix) | 15 SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc, const SkMatri
x* localMatrix) |
16 : INHERITED(localMatrix) | 16 : INHERITED(localMatrix) |
17 { | 17 { |
18 SkASSERT(desc.fCount > 1); | 18 SkASSERT(desc.fCount > 1); |
19 | 19 |
20 fMapper = desc.fMapper; | |
21 SkSafeRef(fMapper); | |
22 fGradFlags = SkToU8(desc.fGradFlags); | 20 fGradFlags = SkToU8(desc.fGradFlags); |
23 | 21 |
24 SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount); | 22 SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount); |
25 SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); | 23 SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); |
26 fTileMode = desc.fTileMode; | 24 fTileMode = desc.fTileMode; |
27 fTileProc = gTileProcs[desc.fTileMode]; | 25 fTileProc = gTileProcs[desc.fTileMode]; |
28 | 26 |
29 /* Note: we let the caller skip the first and/or last position. | 27 /* Note: we let the caller skip the first and/or last position. |
30 i.e. pos[0] = 0.3, pos[1] = 0.7 | 28 i.e. pos[0] = 0.3, pos[1] = 0.7 |
31 In these cases, we insert dummy entries to ensure that the final data | 29 In these cases, we insert dummy entries to ensure that the final data |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 133 |
136 static SkShader::TileMode unpack_mode(uint32_t packed) { | 134 static SkShader::TileMode unpack_mode(uint32_t packed) { |
137 return (SkShader::TileMode)(packed & 0xF); | 135 return (SkShader::TileMode)(packed & 0xF); |
138 } | 136 } |
139 | 137 |
140 static uint32_t unpack_flags(uint32_t packed) { | 138 static uint32_t unpack_flags(uint32_t packed) { |
141 return packed >> 4; | 139 return packed >> 4; |
142 } | 140 } |
143 | 141 |
144 SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buf
fer) { | 142 SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buf
fer) { |
145 fMapper = buffer.readUnitMapper(); | 143 if (buffer.isVersionLT(SkReadBuffer::kNoUnitMappers_Version)) { |
| 144 // skip the old SkUnitMapper slot |
| 145 buffer.skipFlattenable(); |
| 146 } |
146 | 147 |
147 int colorCount = fColorCount = buffer.getArrayCount(); | 148 int colorCount = fColorCount = buffer.getArrayCount(); |
148 if (colorCount > kColorStorageCount) { | 149 if (colorCount > kColorStorageCount) { |
149 size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) *
colorCount; | 150 size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) *
colorCount; |
150 if (buffer.validateAvailable(allocSize)) { | 151 if (buffer.validateAvailable(allocSize)) { |
151 fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize))
; | 152 fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize))
; |
152 } else { | 153 } else { |
153 fOrigColors = NULL; | 154 fOrigColors = NULL; |
154 colorCount = fColorCount = 0; | 155 colorCount = fColorCount = 0; |
155 } | 156 } |
(...skipping 18 matching lines...) Expand all Loading... |
174 } | 175 } |
175 } | 176 } |
176 buffer.readMatrix(&fPtsToUnit); | 177 buffer.readMatrix(&fPtsToUnit); |
177 this->initCommon(); | 178 this->initCommon(); |
178 } | 179 } |
179 | 180 |
180 SkGradientShaderBase::~SkGradientShaderBase() { | 181 SkGradientShaderBase::~SkGradientShaderBase() { |
181 if (fOrigColors != fStorage) { | 182 if (fOrigColors != fStorage) { |
182 sk_free(fOrigColors); | 183 sk_free(fOrigColors); |
183 } | 184 } |
184 SkSafeUnref(fMapper); | |
185 } | 185 } |
186 | 186 |
187 void SkGradientShaderBase::initCommon() { | 187 void SkGradientShaderBase::initCommon() { |
188 unsigned colorAlpha = 0xFF; | 188 unsigned colorAlpha = 0xFF; |
189 for (int i = 0; i < fColorCount; i++) { | 189 for (int i = 0; i < fColorCount; i++) { |
190 colorAlpha &= SkColorGetA(fOrigColors[i]); | 190 colorAlpha &= SkColorGetA(fOrigColors[i]); |
191 } | 191 } |
192 fColorsAreOpaque = colorAlpha == 0xFF; | 192 fColorsAreOpaque = colorAlpha == 0xFF; |
193 } | 193 } |
194 | 194 |
195 void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { | 195 void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { |
196 this->INHERITED::flatten(buffer); | 196 this->INHERITED::flatten(buffer); |
197 buffer.writeFlattenable(fMapper); | |
198 buffer.writeColorArray(fOrigColors, fColorCount); | 197 buffer.writeColorArray(fOrigColors, fColorCount); |
199 buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags)); | 198 buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags)); |
200 if (fColorCount > 2) { | 199 if (fColorCount > 2) { |
201 Rec* recs = fRecs; | 200 Rec* recs = fRecs; |
202 for (int i = 1; i < fColorCount; i++) { | 201 for (int i = 1; i < fColorCount; i++) { |
203 buffer.writeInt(recs[i].fPos); | 202 buffer.writeInt(recs[i].fPos); |
204 buffer.writeUInt(recs[i].fScale); | 203 buffer.writeUInt(recs[i].fScale); |
205 } | 204 } |
206 } | 205 } |
207 buffer.writeMatrix(fPtsToUnit); | 206 buffer.writeMatrix(fPtsToUnit); |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 for (int i = 1; i < cache->fShader.fColorCount; i++) { | 520 for (int i = 1; i < cache->fShader.fColorCount; i++) { |
522 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; | 521 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; |
523 SkASSERT(nextIndex < kCache16Count); | 522 SkASSERT(nextIndex < kCache16Count); |
524 | 523 |
525 if (nextIndex > prevIndex) | 524 if (nextIndex > prevIndex) |
526 Build16bitCache(cache->fCache16 + prevIndex, cache->fShader.fOri
gColors[i-1], | 525 Build16bitCache(cache->fCache16 + prevIndex, cache->fShader.fOri
gColors[i-1], |
527 cache->fShader.fOrigColors[i], nextIndex - prevI
ndex + 1); | 526 cache->fShader.fOrigColors[i], nextIndex - prevI
ndex + 1); |
528 prevIndex = nextIndex; | 527 prevIndex = nextIndex; |
529 } | 528 } |
530 } | 529 } |
531 | |
532 if (cache->fShader.fMapper) { | |
533 cache->fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); | |
534 uint16_t* linear = cache->fCache16; // just computed linear data | |
535 uint16_t* mapped = cache->fCache16Storage; // storage for mapped data | |
536 SkUnitMapper* map = cache->fShader.fMapper; | |
537 for (int i = 0; i < kCache16Count; i++) { | |
538 int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shi
ft; | |
539 mapped[i] = linear[index]; | |
540 mapped[i + kCache16Count] = linear[index + kCache16Count]; | |
541 } | |
542 sk_free(cache->fCache16); | |
543 cache->fCache16 = cache->fCache16Storage; | |
544 } | |
545 } | 530 } |
546 | 531 |
547 const SkPMColor* SkGradientShaderBase::GradientShaderCache::getCache32() { | 532 const SkPMColor* SkGradientShaderBase::GradientShaderCache::getCache32() { |
548 SkOnce(&fCache32Inited, &fCache32Mutex, SkGradientShaderBase::GradientShader
Cache::initCache32, | 533 SkOnce(&fCache32Inited, &fCache32Mutex, SkGradientShaderBase::GradientShader
Cache::initCache32, |
549 this); | 534 this); |
550 SkASSERT(fCache32); | 535 SkASSERT(fCache32); |
551 return fCache32; | 536 return fCache32; |
552 } | 537 } |
553 | 538 |
554 void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache*
cache) { | 539 void SkGradientShaderBase::GradientShaderCache::initCache32(GradientShaderCache*
cache) { |
(...skipping 17 matching lines...) Expand all Loading... |
572 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; | 557 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; |
573 SkASSERT(nextIndex < kCache32Count); | 558 SkASSERT(nextIndex < kCache32Count); |
574 | 559 |
575 if (nextIndex > prevIndex) | 560 if (nextIndex > prevIndex) |
576 Build32bitCache(cache->fCache32 + prevIndex, cache->fShader.fOri
gColors[i-1], | 561 Build32bitCache(cache->fCache32 + prevIndex, cache->fShader.fOri
gColors[i-1], |
577 cache->fShader.fOrigColors[i], nextIndex - prevI
ndex + 1, | 562 cache->fShader.fOrigColors[i], nextIndex - prevI
ndex + 1, |
578 cache->fCacheAlpha, cache->fShader.fGradFlags); | 563 cache->fCacheAlpha, cache->fShader.fGradFlags); |
579 prevIndex = nextIndex; | 564 prevIndex = nextIndex; |
580 } | 565 } |
581 } | 566 } |
582 | |
583 if (cache->fShader.fMapper) { | |
584 SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL); | |
585 SkPMColor* linear = cache->fCache32; // just computed linear d
ata | |
586 SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapp
ed data | |
587 SkUnitMapper* map = cache->fShader.fMapper; | |
588 for (int i = 0; i < kCache32Count; i++) { | |
589 int index = map->mapUnit16((i << 8) | i) >> 8; | |
590 mapped[i + kCache32Count*0] = linear[index + kCache32Count*0]; | |
591 mapped[i + kCache32Count*1] = linear[index + kCache32Count*1]; | |
592 mapped[i + kCache32Count*2] = linear[index + kCache32Count*2]; | |
593 mapped[i + kCache32Count*3] = linear[index + kCache32Count*3]; | |
594 } | |
595 cache->fCache32PixelRef->unref(); | |
596 cache->fCache32PixelRef = newPR; | |
597 cache->fCache32 = (SkPMColor*)newPR->getAddr(); | |
598 } | |
599 } | 567 } |
600 | 568 |
601 /* | 569 /* |
602 * The gradient holds a cache for the most recent value of alpha. Successive | 570 * The gradient holds a cache for the most recent value of alpha. Successive |
603 * callers with the same alpha value will share the same cache. | 571 * callers with the same alpha value will share the same cache. |
604 */ | 572 */ |
605 SkGradientShaderBase::GradientShaderCache* SkGradientShaderBase::refCache(U8CPU
alpha) const { | 573 SkGradientShaderBase::GradientShaderCache* SkGradientShaderBase::refCache(U8CPU
alpha) const { |
606 SkAutoMutexAcquire ama(fCacheMutex); | 574 SkAutoMutexAcquire ama(fCacheMutex); |
607 if (!fCache || fCache->getAlpha() != alpha) { | 575 if (!fCache || fCache->getAlpha() != alpha) { |
608 fCache.reset(SkNEW_ARGS(GradientShaderCache, (alpha, *this))); | 576 fCache.reset(SkNEW_ARGS(GradientShaderCache, (alpha, *this))); |
(...skipping 11 matching lines...) Expand all Loading... |
620 * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). | 588 * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). |
621 * To do that, we maintain a private cache of built-bitmaps, based on our | 589 * To do that, we maintain a private cache of built-bitmaps, based on our |
622 * colors and positions. Note: we don't try to flatten the fMapper, so if one | 590 * colors and positions. Note: we don't try to flatten the fMapper, so if one |
623 * is present, we skip the cache for now. | 591 * is present, we skip the cache for now. |
624 */ | 592 */ |
625 void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { | 593 void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { |
626 // our caller assumes no external alpha, so we ensure that our cache is | 594 // our caller assumes no external alpha, so we ensure that our cache is |
627 // built with 0xFF | 595 // built with 0xFF |
628 SkAutoTUnref<GradientShaderCache> cache(this->refCache(0xFF)); | 596 SkAutoTUnref<GradientShaderCache> cache(this->refCache(0xFF)); |
629 | 597 |
630 // don't have a way to put the mapper into our cache-key yet | |
631 if (fMapper) { | |
632 // force our cache32pixelref to be built | |
633 (void)cache->getCache32(); | |
634 bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); | |
635 bitmap->setPixelRef(cache->getCache32PixelRef()); | |
636 return; | |
637 } | |
638 | |
639 // build our key: [numColors + colors[] + {positions[]} + flags ] | 598 // build our key: [numColors + colors[] + {positions[]} + flags ] |
640 int count = 1 + fColorCount + 1; | 599 int count = 1 + fColorCount + 1; |
641 if (fColorCount > 2) { | 600 if (fColorCount > 2) { |
642 count += fColorCount - 1; // fRecs[].fPos | 601 count += fColorCount - 1; // fRecs[].fPos |
643 } | 602 } |
644 | 603 |
645 SkAutoSTMalloc<16, int32_t> storage(count); | 604 SkAutoSTMalloc<16, int32_t> storage(count); |
646 int32_t* buffer = storage.get(); | 605 int32_t* buffer = storage.get(); |
647 | 606 |
648 *buffer++ = fColorCount; | 607 *buffer++ = fColorCount; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 str->append(")"); | 696 str->append(")"); |
738 } | 697 } |
739 | 698 |
740 static const char* gTileModeName[SkShader::kTileModeCount] = { | 699 static const char* gTileModeName[SkShader::kTileModeCount] = { |
741 "clamp", "repeat", "mirror" | 700 "clamp", "repeat", "mirror" |
742 }; | 701 }; |
743 | 702 |
744 str->append(" "); | 703 str->append(" "); |
745 str->append(gTileModeName[fTileMode]); | 704 str->append(gTileModeName[fTileMode]); |
746 | 705 |
747 // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added | |
748 | |
749 this->INHERITED::toString(str); | 706 this->INHERITED::toString(str); |
750 } | 707 } |
751 #endif | 708 #endif |
752 | 709 |
753 /////////////////////////////////////////////////////////////////////////////// | 710 /////////////////////////////////////////////////////////////////////////////// |
754 /////////////////////////////////////////////////////////////////////////////// | 711 /////////////////////////////////////////////////////////////////////////////// |
755 | 712 |
756 #include "SkEmptyShader.h" | 713 #include "SkEmptyShader.h" |
757 | 714 |
758 // assumes colors is SkColor* and pos is SkScalar* | 715 // assumes colors is SkColor* and pos is SkScalar* |
759 #define EXPAND_1_COLOR(count) \ | 716 #define EXPAND_1_COLOR(count) \ |
760 SkColor tmp[2]; \ | 717 SkColor tmp[2]; \ |
761 do { \ | 718 do { \ |
762 if (1 == count) { \ | 719 if (1 == count) { \ |
763 tmp[0] = tmp[1] = colors[0]; \ | 720 tmp[0] = tmp[1] = colors[0]; \ |
764 colors = tmp; \ | 721 colors = tmp; \ |
765 pos = NULL; \ | 722 pos = NULL; \ |
766 count = 2; \ | 723 count = 2; \ |
767 } \ | 724 } \ |
768 } while (0) | 725 } while (0) |
769 | 726 |
770 static void desc_init(SkGradientShaderBase::Descriptor* desc, | 727 static void desc_init(SkGradientShaderBase::Descriptor* desc, |
771 const SkColor colors[], | 728 const SkColor colors[], |
772 const SkScalar pos[], int colorCount, | 729 const SkScalar pos[], int colorCount, |
773 SkShader::TileMode mode, | 730 SkShader::TileMode mode, uint32_t flags) { |
774 SkUnitMapper* mapper, uint32_t flags) { | |
775 desc->fColors = colors; | 731 desc->fColors = colors; |
776 desc->fPos = pos; | 732 desc->fPos = pos; |
777 desc->fCount = colorCount; | 733 desc->fCount = colorCount; |
778 desc->fTileMode = mode; | 734 desc->fTileMode = mode; |
779 desc->fMapper = mapper; | |
780 desc->fGradFlags = flags; | 735 desc->fGradFlags = flags; |
781 } | 736 } |
782 | 737 |
783 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], | 738 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], |
784 const SkColor colors[], | 739 const SkColor colors[], |
785 const SkScalar pos[], int colorCount, | 740 const SkScalar pos[], int colorCount, |
786 SkShader::TileMode mode, | 741 SkShader::TileMode mode, |
787 SkUnitMapper* mapper, | |
788 uint32_t flags, | 742 uint32_t flags, |
789 const SkMatrix* localMatrix) { | 743 const SkMatrix* localMatrix) { |
790 if (NULL == pts || NULL == colors || colorCount < 1) { | 744 if (NULL == pts || NULL == colors || colorCount < 1) { |
791 return NULL; | 745 return NULL; |
792 } | 746 } |
793 EXPAND_1_COLOR(colorCount); | 747 EXPAND_1_COLOR(colorCount); |
794 | 748 |
795 SkGradientShaderBase::Descriptor desc; | 749 SkGradientShaderBase::Descriptor desc; |
796 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); | 750 desc_init(&desc, colors, pos, colorCount, mode, flags); |
797 return SkNEW_ARGS(SkLinearGradient, (pts, desc, localMatrix)); | 751 return SkNEW_ARGS(SkLinearGradient, (pts, desc, localMatrix)); |
798 } | 752 } |
799 | 753 |
800 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, | 754 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, |
801 const SkColor colors[], | 755 const SkColor colors[], |
802 const SkScalar pos[], int colorCount, | 756 const SkScalar pos[], int colorCount, |
803 SkShader::TileMode mode, | 757 SkShader::TileMode mode, |
804 SkUnitMapper* mapper, | |
805 uint32_t flags, | 758 uint32_t flags, |
806 const SkMatrix* localMatrix) { | 759 const SkMatrix* localMatrix) { |
807 if (radius <= 0 || NULL == colors || colorCount < 1) { | 760 if (radius <= 0 || NULL == colors || colorCount < 1) { |
808 return NULL; | 761 return NULL; |
809 } | 762 } |
810 EXPAND_1_COLOR(colorCount); | 763 EXPAND_1_COLOR(colorCount); |
811 | 764 |
812 SkGradientShaderBase::Descriptor desc; | 765 SkGradientShaderBase::Descriptor desc; |
813 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); | 766 desc_init(&desc, colors, pos, colorCount, mode, flags); |
814 return SkNEW_ARGS(SkRadialGradient, (center, radius, desc, localMatrix)); | 767 return SkNEW_ARGS(SkRadialGradient, (center, radius, desc, localMatrix)); |
815 } | 768 } |
816 | 769 |
817 SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, | 770 SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, |
818 SkScalar startRadius, | 771 SkScalar startRadius, |
819 const SkPoint& end, | 772 const SkPoint& end, |
820 SkScalar endRadius, | 773 SkScalar endRadius, |
821 const SkColor colors[], | 774 const SkColor colors[], |
822 const SkScalar pos[], | 775 const SkScalar pos[], |
823 int colorCount, | 776 int colorCount, |
824 SkShader::TileMode mode, | 777 SkShader::TileMode mode, |
825 SkUnitMapper* mapper, | |
826 uint32_t flags, | 778 uint32_t flags, |
827 const SkMatrix* localMatrix) { | 779 const SkMatrix* localMatrix) { |
828 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { | 780 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
829 return NULL; | 781 return NULL; |
830 } | 782 } |
831 EXPAND_1_COLOR(colorCount); | 783 EXPAND_1_COLOR(colorCount); |
832 | 784 |
833 SkGradientShaderBase::Descriptor desc; | 785 SkGradientShaderBase::Descriptor desc; |
834 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); | 786 desc_init(&desc, colors, pos, colorCount, mode, flags); |
835 return SkNEW_ARGS(SkTwoPointRadialGradient, | 787 return SkNEW_ARGS(SkTwoPointRadialGradient, |
836 (start, startRadius, end, endRadius, desc, localMatrix)); | 788 (start, startRadius, end, endRadius, desc, localMatrix)); |
837 } | 789 } |
838 | 790 |
839 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, | 791 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, |
840 SkScalar startRadius, | 792 SkScalar startRadius, |
841 const SkPoint& end, | 793 const SkPoint& end, |
842 SkScalar endRadius, | 794 SkScalar endRadius, |
843 const SkColor colors[], | 795 const SkColor colors[], |
844 const SkScalar pos[], | 796 const SkScalar pos[], |
845 int colorCount, | 797 int colorCount, |
846 SkShader::TileMode mode, | 798 SkShader::TileMode mode, |
847 SkUnitMapper* mapper, | |
848 uint32_t flags, | 799 uint32_t flags, |
849 const SkMatrix* localMatrix) { | 800 const SkMatrix* localMatrix) { |
850 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { | 801 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
851 return NULL; | 802 return NULL; |
852 } | 803 } |
853 if (start == end && startRadius == endRadius) { | 804 if (start == end && startRadius == endRadius) { |
854 return SkNEW(SkEmptyShader); | 805 return SkNEW(SkEmptyShader); |
855 } | 806 } |
856 | 807 |
857 EXPAND_1_COLOR(colorCount); | 808 EXPAND_1_COLOR(colorCount); |
858 | 809 |
859 bool flipGradient = startRadius > endRadius; | 810 bool flipGradient = startRadius > endRadius; |
860 | 811 |
861 SkGradientShaderBase::Descriptor desc; | 812 SkGradientShaderBase::Descriptor desc; |
862 | 813 |
863 if (!flipGradient) { | 814 if (!flipGradient) { |
864 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); | 815 desc_init(&desc, colors, pos, colorCount, mode, flags); |
865 return SkNEW_ARGS(SkTwoPointConicalGradient, | 816 return SkNEW_ARGS(SkTwoPointConicalGradient, |
866 (start, startRadius, end, endRadius, flipGradient, des
c, localMatrix)); | 817 (start, startRadius, end, endRadius, flipGradient, des
c, localMatrix)); |
867 } else { | 818 } else { |
868 SkAutoSTArray<8, SkColor> colorsNew(colorCount); | 819 SkAutoSTArray<8, SkColor> colorsNew(colorCount); |
869 SkAutoSTArray<8, SkScalar> posNew(colorCount); | 820 SkAutoSTArray<8, SkScalar> posNew(colorCount); |
870 for (int i = 0; i < colorCount; ++i) { | 821 for (int i = 0; i < colorCount; ++i) { |
871 colorsNew[i] = colors[colorCount - i - 1]; | 822 colorsNew[i] = colors[colorCount - i - 1]; |
872 } | 823 } |
873 | 824 |
874 if (pos) { | 825 if (pos) { |
875 for (int i = 0; i < colorCount; ++i) { | 826 for (int i = 0; i < colorCount; ++i) { |
876 posNew[i] = 1 - pos[colorCount - i - 1]; | 827 posNew[i] = 1 - pos[colorCount - i - 1]; |
877 } | 828 } |
878 desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, ma
pper, flags); | 829 desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, fl
ags); |
879 } else { | 830 } else { |
880 desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, mapper, fl
ags); | 831 desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, flags); |
881 } | 832 } |
882 | 833 |
883 return SkNEW_ARGS(SkTwoPointConicalGradient, | 834 return SkNEW_ARGS(SkTwoPointConicalGradient, |
884 (end, endRadius, start, startRadius, flipGradient, des
c, localMatrix)); | 835 (end, endRadius, start, startRadius, flipGradient, des
c, localMatrix)); |
885 } | 836 } |
886 } | 837 } |
887 | 838 |
888 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, | 839 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
889 const SkColor colors[], | 840 const SkColor colors[], |
890 const SkScalar pos[], | 841 const SkScalar pos[], |
891 int colorCount, SkUnitMapper* mapper, | 842 int colorCount, |
892 uint32_t flags, | 843 uint32_t flags, |
893 const SkMatrix* localMatrix) { | 844 const SkMatrix* localMatrix) { |
894 if (NULL == colors || colorCount < 1) { | 845 if (NULL == colors || colorCount < 1) { |
895 return NULL; | 846 return NULL; |
896 } | 847 } |
897 EXPAND_1_COLOR(colorCount); | 848 EXPAND_1_COLOR(colorCount); |
898 | 849 |
899 SkGradientShaderBase::Descriptor desc; | 850 SkGradientShaderBase::Descriptor desc; |
900 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper,
flags); | 851 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags); |
901 return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc, localMatrix)); | 852 return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc, localMatrix)); |
902 } | 853 } |
903 | 854 |
904 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) | 855 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
905 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) | 856 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
906 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) | 857 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
907 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) | 858 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
908 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) | 859 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) |
909 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) | 860 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) |
910 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 861 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 (*stops)[i] = stop; | 1161 (*stops)[i] = stop; |
1211 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; | 1162 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; |
1212 } | 1163 } |
1213 } | 1164 } |
1214 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); | 1165 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); |
1215 | 1166 |
1216 return outColors; | 1167 return outColors; |
1217 } | 1168 } |
1218 | 1169 |
1219 #endif | 1170 #endif |
OLD | NEW |