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 "Sk4fLinearGradient.h" | 8 #include "Sk4fLinearGradient.h" |
9 #include "SkGradientShaderPriv.h" | 9 #include "SkGradientShaderPriv.h" |
10 #include "SkLinearGradient.h" | 10 #include "SkLinearGradient.h" |
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 | 735 |
736 /////////////////////////////////////////////////////////////////////////////// | 736 /////////////////////////////////////////////////////////////////////////////// |
737 /////////////////////////////////////////////////////////////////////////////// | 737 /////////////////////////////////////////////////////////////////////////////// |
738 | 738 |
739 // Return true if these parameters are valid/legal/safe to construct a gradient | 739 // Return true if these parameters are valid/legal/safe to construct a gradient |
740 // | 740 // |
741 static bool valid_grad(const SkColor colors[], const SkScalar pos[], int count,
unsigned tileMode) { | 741 static bool valid_grad(const SkColor colors[], const SkScalar pos[], int count,
unsigned tileMode) { |
742 return nullptr != colors && count >= 1 && tileMode < (unsigned)SkShader::kTi
leModeCount; | 742 return nullptr != colors && count >= 1 && tileMode < (unsigned)SkShader::kTi
leModeCount; |
743 } | 743 } |
744 | 744 |
745 // assumes colors is SkColor* and pos is SkScalar* | |
746 #define EXPAND_1_COLOR(count) \ | |
747 SkColor tmp[2]; \ | |
748 do { \ | |
749 if (1 == count) { \ | |
750 tmp[0] = tmp[1] = colors[0]; \ | |
751 colors = tmp; \ | |
752 pos = nullptr; \ | |
753 count = 2; \ | |
754 } \ | |
755 } while (0) | |
756 | |
757 static void desc_init(SkGradientShaderBase::Descriptor* desc, | 745 static void desc_init(SkGradientShaderBase::Descriptor* desc, |
758 const SkColor colors[], const SkScalar pos[], int colorCou
nt, | 746 const SkColor colors[], const SkScalar pos[], int colorCou
nt, |
759 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l
ocalMatrix) { | 747 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l
ocalMatrix) { |
760 SkASSERT(colorCount > 1); | 748 SkASSERT(colorCount > 1); |
761 | 749 |
762 desc->fColors = colors; | 750 desc->fColors = colors; |
763 desc->fPos = pos; | 751 desc->fPos = pos; |
764 desc->fCount = colorCount; | 752 desc->fCount = colorCount; |
765 desc->fTileMode = mode; | 753 desc->fTileMode = mode; |
766 desc->fGradFlags = flags; | 754 desc->fGradFlags = flags; |
767 desc->fLocalMatrix = localMatrix; | 755 desc->fLocalMatrix = localMatrix; |
768 } | 756 } |
769 | 757 |
| 758 // assumes colors is SkColor* and pos is SkScalar* |
| 759 #define EXPAND_1_COLOR(count) \ |
| 760 SkColor tmp[2]; \ |
| 761 do { \ |
| 762 if (1 == count) { \ |
| 763 tmp[0] = tmp[1] = colors[0]; \ |
| 764 colors = tmp; \ |
| 765 pos = nullptr; \ |
| 766 count = 2; \ |
| 767 } \ |
| 768 } while (0) |
| 769 |
770 sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2], | 770 sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2], |
771 const SkColor colors[], | 771 const SkColor colors[], |
772 const SkScalar pos[], int colorCount, | 772 const SkScalar pos[], int colorCount, |
773 SkShader::TileMode mode, | 773 SkShader::TileMode mode, |
774 uint32_t flags, | 774 uint32_t flags, |
775 const SkMatrix* localMatrix) { | 775 const SkMatrix* localMatrix) { |
776 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) { | 776 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) { |
777 return nullptr; | 777 return nullptr; |
778 } | 778 } |
779 if (!valid_grad(colors, pos, colorCount, mode)) { | 779 if (!valid_grad(colors, pos, colorCount, mode)) { |
780 return nullptr; | 780 return nullptr; |
781 } | 781 } |
782 EXPAND_1_COLOR(colorCount); | 782 if (1 == colorCount) { |
| 783 return SkShader::MakeColorShader(colors[0]); |
| 784 } |
783 | 785 |
784 SkGradientShaderBase::Descriptor desc; | 786 SkGradientShaderBase::Descriptor desc; |
785 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 787 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
786 return sk_make_sp<SkLinearGradient>(pts, desc); | 788 return sk_make_sp<SkLinearGradient>(pts, desc); |
787 } | 789 } |
788 | 790 |
789 sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar rad
ius, | 791 sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar rad
ius, |
790 const SkColor colors[], | 792 const SkColor colors[], |
791 const SkScalar pos[], int colorCount, | 793 const SkScalar pos[], int colorCount, |
792 SkShader::TileMode mode, | 794 SkShader::TileMode mode, |
793 uint32_t flags, | 795 uint32_t flags, |
794 const SkMatrix* localMatrix) { | 796 const SkMatrix* localMatrix) { |
795 if (radius <= 0) { | 797 if (radius <= 0) { |
796 return nullptr; | 798 return nullptr; |
797 } | 799 } |
798 if (!valid_grad(colors, pos, colorCount, mode)) { | 800 if (!valid_grad(colors, pos, colorCount, mode)) { |
799 return nullptr; | 801 return nullptr; |
800 } | 802 } |
801 EXPAND_1_COLOR(colorCount); | 803 if (1 == colorCount) { |
| 804 return SkShader::MakeColorShader(colors[0]); |
| 805 } |
802 | 806 |
803 SkGradientShaderBase::Descriptor desc; | 807 SkGradientShaderBase::Descriptor desc; |
804 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 808 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
805 return sk_make_sp<SkRadialGradient>(center, radius, desc); | 809 return sk_make_sp<SkRadialGradient>(center, radius, desc); |
806 } | 810 } |
807 | 811 |
808 sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start, | 812 sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start, |
809 SkScalar startRadius, | 813 SkScalar startRadius, |
810 const SkPoint& end, | 814 const SkPoint& end, |
811 SkScalar endRadius, | 815 SkScalar endRadius, |
812 const SkColor colors[], | 816 const SkColor colors[], |
813 const SkScalar pos[], | 817 const SkScalar pos[], |
814 int colorCount, | 818 int colorCount, |
815 SkShader::TileMode mode, | 819 SkShader::TileMode mode, |
816 uint32_t flags, | 820 uint32_t flags, |
817 const SkMatrix* localMatrix) { | 821 const SkMatrix* localMatrix) { |
818 if (startRadius < 0 || endRadius < 0) { | 822 if (startRadius < 0 || endRadius < 0) { |
819 return nullptr; | 823 return nullptr; |
820 } | 824 } |
821 if (!valid_grad(colors, pos, colorCount, mode)) { | 825 if (!valid_grad(colors, pos, colorCount, mode)) { |
822 return nullptr; | 826 return nullptr; |
823 } | 827 } |
824 if (startRadius == endRadius) { | 828 if (startRadius == endRadius) { |
825 if (start == end || startRadius == 0) { | 829 if (start == end || startRadius == 0) { |
826 return SkShader::MakeEmptyShader(); | 830 return SkShader::MakeEmptyShader(); |
827 } | 831 } |
828 } | 832 } |
829 | |
830 EXPAND_1_COLOR(colorCount); | 833 EXPAND_1_COLOR(colorCount); |
831 | 834 |
832 bool flipGradient = startRadius > endRadius; | 835 bool flipGradient = startRadius > endRadius; |
833 | 836 |
834 SkGradientShaderBase::Descriptor desc; | 837 SkGradientShaderBase::Descriptor desc; |
835 | 838 |
836 if (!flipGradient) { | 839 if (!flipGradient) { |
837 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); | 840 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); |
838 return sk_make_sp<SkTwoPointConicalGradient>(start, startRadius, end, en
dRadius, | 841 return sk_make_sp<SkTwoPointConicalGradient>(start, startRadius, end, en
dRadius, |
839 flipGradient, desc); | 842 flipGradient, desc); |
(...skipping 20 matching lines...) Expand all Loading... |
860 | 863 |
861 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy, | 864 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy, |
862 const SkColor colors[], | 865 const SkColor colors[], |
863 const SkScalar pos[], | 866 const SkScalar pos[], |
864 int colorCount, | 867 int colorCount, |
865 uint32_t flags, | 868 uint32_t flags, |
866 const SkMatrix* localMatrix) { | 869 const SkMatrix* localMatrix) { |
867 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { | 870 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { |
868 return nullptr; | 871 return nullptr; |
869 } | 872 } |
870 EXPAND_1_COLOR(colorCount); | 873 if (1 == colorCount) { |
| 874 return SkShader::MakeColorShader(colors[0]); |
| 875 } |
871 | 876 |
872 SkGradientShaderBase::Descriptor desc; | 877 SkGradientShaderBase::Descriptor desc; |
873 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags,
localMatrix); | 878 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags,
localMatrix); |
874 return sk_make_sp<SkSweepGradient>(cx, cy, desc); | 879 return sk_make_sp<SkSweepGradient>(cx, cy, desc); |
875 } | 880 } |
876 | 881 |
877 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) | 882 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) |
878 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) | 883 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) |
879 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) | 884 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) |
880 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) | 885 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1427 (*stops)[i] = stop; | 1432 (*stops)[i] = stop; |
1428 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; | 1433 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st
op) : 1.f; |
1429 } | 1434 } |
1430 } | 1435 } |
1431 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); | 1436 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM
odeCount)); |
1432 | 1437 |
1433 return outColors; | 1438 return outColors; |
1434 } | 1439 } |
1435 | 1440 |
1436 #endif | 1441 #endif |
OLD | NEW |