| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 recs->fScale = 0; // ignore this segment | 111 recs->fScale = 0; // ignore this segment |
| 112 } | 112 } |
| 113 // get ready for the next value | 113 // get ready for the next value |
| 114 prev = curr; | 114 prev = curr; |
| 115 recs += 1; | 115 recs += 1; |
| 116 } | 116 } |
| 117 } else { // assume even distribution | 117 } else { // assume even distribution |
| 118 SkFixed dp = SK_Fixed1 / (desc.fCount - 1); | 118 SkFixed dp = SK_Fixed1 / (desc.fCount - 1); |
| 119 SkFixed p = dp; | 119 SkFixed p = dp; |
| 120 SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp | 120 SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp |
| 121 for (int i = 1; i < desc.fCount; i++) { | 121 for (int i = 1; i < desc.fCount - 1; i++) { |
| 122 recs->fPos = p; | 122 recs->fPos = p; |
| 123 recs->fScale = scale; | 123 recs->fScale = scale; |
| 124 recs += 1; | 124 recs += 1; |
| 125 p += dp; | 125 p += dp; |
| 126 } | 126 } |
| 127 recs->fPos = SK_Fixed1; |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 this->initCommon(); | 130 this->initCommon(); |
| 130 } | 131 } |
| 131 | 132 |
| 132 static uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) { | 133 static uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) { |
| 133 SkASSERT(0 == (flags >> 28)); | 134 SkASSERT(0 == (flags >> 28)); |
| 134 SkASSERT(0 == ((uint32_t)mode >> 4)); | 135 SkASSERT(0 == ((uint32_t)mode >> 4)); |
| 135 return (flags << 4) | mode; | 136 return (flags << 4) | mode; |
| 136 } | 137 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 return kTwo_GpuColorType; | 231 return kTwo_GpuColorType; |
| 231 } else if (3 == fColorCount && | 232 } else if (3 == fColorCount && |
| 232 (SkScalarAbs( | 233 (SkScalarAbs( |
| 233 SkFixedToScalar(fRecs[1].fPos) - SK_ScalarHalf) < SK_Scalar1
/ 1000)) { | 234 SkFixedToScalar(fRecs[1].fPos) - SK_ScalarHalf) < SK_Scalar1
/ 1000)) { |
| 234 return kThree_GpuColorType; | 235 return kThree_GpuColorType; |
| 235 } | 236 } |
| 236 } | 237 } |
| 237 return kTexture_GpuColorType; | 238 return kTexture_GpuColorType; |
| 238 } | 239 } |
| 239 | 240 |
| 241 void SkGradientShaderBase::FlipGradientColors(SkColor* colorDst, Rec* recDst, |
| 242 SkColor* colorSrc, Rec* recSrc, |
| 243 int count) { |
| 244 SkAutoSTArray<8, SkColor> colorsTemp(count); |
| 245 for (int i = 0; i < count; ++i) { |
| 246 int offset = count - i - 1; |
| 247 colorsTemp[i] = colorSrc[offset]; |
| 248 } |
| 249 if (count > 2) { |
| 250 SkAutoSTArray<8, Rec> recsTemp(count); |
| 251 for (int i = 0; i < count; ++i) { |
| 252 int offset = count - i - 1; |
| 253 recsTemp[i].fPos = SK_Fixed1 - recSrc[offset].fPos; |
| 254 recsTemp[i].fScale = recSrc[offset].fScale; |
| 255 } |
| 256 memcpy(recDst, recsTemp.get(), count * sizeof(Rec)); |
| 257 } |
| 258 memcpy(colorDst, colorsTemp.get(), count * sizeof(SkColor)); |
| 259 } |
| 260 |
| 261 void SkGradientShaderBase::flipGradientColors() { |
| 262 FlipGradientColors(fOrigColors, fRecs, fOrigColors, fRecs, fColorCount); |
| 263 } |
| 264 |
| 240 bool SkGradientShaderBase::isOpaque() const { | 265 bool SkGradientShaderBase::isOpaque() const { |
| 241 return fColorsAreOpaque; | 266 return fColorsAreOpaque; |
| 242 } | 267 } |
| 243 | 268 |
| 244 bool SkGradientShaderBase::setContext(const SkBitmap& device, | 269 bool SkGradientShaderBase::setContext(const SkBitmap& device, |
| 245 const SkPaint& paint, | 270 const SkPaint& paint, |
| 246 const SkMatrix& matrix) { | 271 const SkMatrix& matrix) { |
| 247 if (!this->INHERITED::setContext(device, paint, matrix)) { | 272 if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 248 return false; | 273 return false; |
| 249 } | 274 } |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 if (!gCache->find(storage.get(), size, bitmap)) { | 661 if (!gCache->find(storage.get(), size, bitmap)) { |
| 637 // force our cahce32pixelref to be built | 662 // force our cahce32pixelref to be built |
| 638 (void)this->getCache32(); | 663 (void)this->getCache32(); |
| 639 bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); | 664 bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); |
| 640 bitmap->setPixelRef(fCache32PixelRef); | 665 bitmap->setPixelRef(fCache32PixelRef); |
| 641 | 666 |
| 642 gCache->add(storage.get(), size, *bitmap); | 667 gCache->add(storage.get(), size, *bitmap); |
| 643 } | 668 } |
| 644 } | 669 } |
| 645 | 670 |
| 646 void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const { | 671 void SkGradientShaderBase::commonAsAGradient(GradientInfo* info, bool flipGrad)
const { |
| 647 if (info) { | 672 if (info) { |
| 648 if (info->fColorCount >= fColorCount) { | 673 if (info->fColorCount >= fColorCount) { |
| 674 SkColor* colorLoc; |
| 675 Rec* recLoc; |
| 676 if (flipGrad && (info->fColors || info->fColorOffsets)) { |
| 677 SkAutoSTArray<8, SkColor> colorStorage(fColorCount); |
| 678 SkAutoSTArray<8, Rec> recStorage(fColorCount); |
| 679 colorLoc = colorStorage.get(); |
| 680 recLoc = recStorage.get(); |
| 681 FlipGradientColors(colorLoc, recLoc, fOrigColors, fRecs, fColorC
ount); |
| 682 } else { |
| 683 colorLoc = fOrigColors; |
| 684 recLoc = fRecs; |
| 685 } |
| 649 if (info->fColors) { | 686 if (info->fColors) { |
| 650 memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor)
); | 687 memcpy(info->fColors, colorLoc, fColorCount * sizeof(SkColor)); |
| 651 } | 688 } |
| 652 if (info->fColorOffsets) { | 689 if (info->fColorOffsets) { |
| 653 if (fColorCount == 2) { | 690 if (fColorCount == 2) { |
| 654 info->fColorOffsets[0] = 0; | 691 info->fColorOffsets[0] = 0; |
| 655 info->fColorOffsets[1] = SK_Scalar1; | 692 info->fColorOffsets[1] = SK_Scalar1; |
| 656 } else if (fColorCount > 2) { | 693 } else if (fColorCount > 2) { |
| 657 for (int i = 0; i < fColorCount; ++i) { | 694 for (int i = 0; i < fColorCount; ++i) { |
| 658 info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos); | 695 info->fColorOffsets[i] = SkFixedToScalar(recLoc[i].fPos)
; |
| 659 } | 696 } |
| 660 } | 697 } |
| 661 } | 698 } |
| 662 } | 699 } |
| 663 info->fColorCount = fColorCount; | 700 info->fColorCount = fColorCount; |
| 664 info->fTileMode = fTileMode; | 701 info->fTileMode = fTileMode; |
| 665 info->fGradientFlags = fGradFlags; | 702 info->fGradientFlags = fGradFlags; |
| 666 } | 703 } |
| 667 } | 704 } |
| 668 | 705 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 int colorCount, | 831 int colorCount, |
| 795 SkShader::TileMode mode, | 832 SkShader::TileMode mode, |
| 796 SkUnitMapper* mapper, | 833 SkUnitMapper* mapper, |
| 797 uint32_t flags) { | 834 uint32_t flags) { |
| 798 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { | 835 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { |
| 799 return NULL; | 836 return NULL; |
| 800 } | 837 } |
| 801 if (start == end && startRadius == endRadius) { | 838 if (start == end && startRadius == endRadius) { |
| 802 return SkNEW(SkEmptyShader); | 839 return SkNEW(SkEmptyShader); |
| 803 } | 840 } |
| 841 |
| 804 EXPAND_1_COLOR(colorCount); | 842 EXPAND_1_COLOR(colorCount); |
| 805 | 843 |
| 844 bool flipGradient = startRadius > endRadius; |
| 845 |
| 806 SkGradientShaderBase::Descriptor desc; | 846 SkGradientShaderBase::Descriptor desc; |
| 807 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); | 847 |
| 808 return SkNEW_ARGS(SkTwoPointConicalGradient, | 848 if (!flipGradient) { |
| 809 (start, startRadius, end, endRadius, desc)); | 849 desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); |
| 850 return SkNEW_ARGS(SkTwoPointConicalGradient, |
| 851 (start, startRadius, end, endRadius, flipGradient, des
c)); |
| 852 } else { |
| 853 SkAutoSTArray<8, SkColor> colorsNew(colorCount); |
| 854 SkAutoSTArray<8, SkScalar> posNew(colorCount); |
| 855 for (int i = 0; i < colorCount; ++i) { |
| 856 colorsNew[i] = colors[colorCount - i - 1]; |
| 857 } |
| 858 |
| 859 if (pos) { |
| 860 for (int i = 0; i < colorCount; ++i) { |
| 861 posNew[i] = 1 - pos[colorCount - i - 1]; |
| 862 } |
| 863 desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, ma
pper, flags); |
| 864 } else { |
| 865 desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, mapper, fl
ags); |
| 866 } |
| 867 |
| 868 return SkNEW_ARGS(SkTwoPointConicalGradient, |
| 869 (end, endRadius, start, startRadius, flipGradient, des
c)); |
| 870 } |
| 810 } | 871 } |
| 811 | 872 |
| 812 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, | 873 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, |
| 813 const SkColor colors[], | 874 const SkColor colors[], |
| 814 const SkScalar pos[], | 875 const SkScalar pos[], |
| 815 int colorCount, SkUnitMapper* mapper, | 876 int colorCount, SkUnitMapper* mapper, |
| 816 uint32_t flags) { | 877 uint32_t flags) { |
| 817 if (NULL == colors || colorCount < 1) { | 878 if (NULL == colors || colorCount < 1) { |
| 818 return NULL; | 879 return NULL; |
| 819 } | 880 } |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1133 (*stops)[i] = stop; | 1194 (*stops)[i] = stop; |
| 1134 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; | 1195 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; |
| 1135 } | 1196 } |
| 1136 } | 1197 } |
| 1137 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); | 1198 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); |
| 1138 | 1199 |
| 1139 return outColors; | 1200 return outColors; |
| 1140 } | 1201 } |
| 1141 | 1202 |
| 1142 #endif | 1203 #endif |
| OLD | NEW |