Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: src/effects/gradients/SkGradientShader.cpp

Issue 2257993002: Revert of Return color shader instead of 2-color gradient when color count is 1 (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
745 static void desc_init(SkGradientShaderBase::Descriptor* desc, 757 static void desc_init(SkGradientShaderBase::Descriptor* desc,
746 const SkColor colors[], const SkScalar pos[], int colorCou nt, 758 const SkColor colors[], const SkScalar pos[], int colorCou nt,
747 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l ocalMatrix) { 759 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l ocalMatrix) {
748 SkASSERT(colorCount > 1); 760 SkASSERT(colorCount > 1);
749 761
750 desc->fColors = colors; 762 desc->fColors = colors;
751 desc->fPos = pos; 763 desc->fPos = pos;
752 desc->fCount = colorCount; 764 desc->fCount = colorCount;
753 desc->fTileMode = mode; 765 desc->fTileMode = mode;
754 desc->fGradFlags = flags; 766 desc->fGradFlags = flags;
755 desc->fLocalMatrix = localMatrix; 767 desc->fLocalMatrix = localMatrix;
756 } 768 }
757 769
758 sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2], 770 sk_sp<SkShader> SkGradientShader::MakeLinear(const SkPoint pts[2],
759 const SkColor colors[], 771 const SkColor colors[],
760 const SkScalar pos[], int colorCount, 772 const SkScalar pos[], int colorCount,
761 SkShader::TileMode mode, 773 SkShader::TileMode mode,
762 uint32_t flags, 774 uint32_t flags,
763 const SkMatrix* localMatrix) { 775 const SkMatrix* localMatrix) {
764 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) { 776 if (!pts || !SkScalarIsFinite((pts[1] - pts[0]).length())) {
765 return nullptr; 777 return nullptr;
766 } 778 }
767 if (!valid_grad(colors, pos, colorCount, mode)) { 779 if (!valid_grad(colors, pos, colorCount, mode)) {
768 return nullptr; 780 return nullptr;
769 } 781 }
770 if (1 == colorCount) { 782 EXPAND_1_COLOR(colorCount);
771 return SkShader::MakeColorShader(colors[0]);
772 }
773 783
774 SkGradientShaderBase::Descriptor desc; 784 SkGradientShaderBase::Descriptor desc;
775 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 785 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
776 return sk_make_sp<SkLinearGradient>(pts, desc); 786 return sk_make_sp<SkLinearGradient>(pts, desc);
777 } 787 }
778 788
779 sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar rad ius, 789 sk_sp<SkShader> SkGradientShader::MakeRadial(const SkPoint& center, SkScalar rad ius,
780 const SkColor colors[], 790 const SkColor colors[],
781 const SkScalar pos[], int colorCount, 791 const SkScalar pos[], int colorCount,
782 SkShader::TileMode mode, 792 SkShader::TileMode mode,
783 uint32_t flags, 793 uint32_t flags,
784 const SkMatrix* localMatrix) { 794 const SkMatrix* localMatrix) {
785 if (radius <= 0) { 795 if (radius <= 0) {
786 return nullptr; 796 return nullptr;
787 } 797 }
788 if (!valid_grad(colors, pos, colorCount, mode)) { 798 if (!valid_grad(colors, pos, colorCount, mode)) {
789 return nullptr; 799 return nullptr;
790 } 800 }
791 if (1 == colorCount) { 801 EXPAND_1_COLOR(colorCount);
792 return SkShader::MakeColorShader(colors[0]);
793 }
794 802
795 SkGradientShaderBase::Descriptor desc; 803 SkGradientShaderBase::Descriptor desc;
796 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 804 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
797 return sk_make_sp<SkRadialGradient>(center, radius, desc); 805 return sk_make_sp<SkRadialGradient>(center, radius, desc);
798 } 806 }
799 807
800 sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start, 808 sk_sp<SkShader> SkGradientShader::MakeTwoPointConical(const SkPoint& start,
801 SkScalar startRadius, 809 SkScalar startRadius,
802 const SkPoint& end, 810 const SkPoint& end,
803 SkScalar endRadius, 811 SkScalar endRadius,
804 const SkColor colors[], 812 const SkColor colors[],
805 const SkScalar pos[], 813 const SkScalar pos[],
806 int colorCount, 814 int colorCount,
807 SkShader::TileMode mode, 815 SkShader::TileMode mode,
808 uint32_t flags, 816 uint32_t flags,
809 const SkMatrix* localMatrix) { 817 const SkMatrix* localMatrix) {
810 if (startRadius < 0 || endRadius < 0) { 818 if (startRadius < 0 || endRadius < 0) {
811 return nullptr; 819 return nullptr;
812 } 820 }
813 if (!valid_grad(colors, pos, colorCount, mode)) { 821 if (!valid_grad(colors, pos, colorCount, mode)) {
814 return nullptr; 822 return nullptr;
815 } 823 }
816 if (startRadius == endRadius) { 824 if (startRadius == endRadius) {
817 if (start == end || startRadius == 0) { 825 if (start == end || startRadius == 0) {
818 return SkShader::MakeEmptyShader(); 826 return SkShader::MakeEmptyShader();
819 } 827 }
820 } 828 }
821 if (1 == colorCount) { 829
822 return SkShader::MakeColorShader(colors[0]); 830 EXPAND_1_COLOR(colorCount);
823 }
824 831
825 bool flipGradient = startRadius > endRadius; 832 bool flipGradient = startRadius > endRadius;
826 833
827 SkGradientShaderBase::Descriptor desc; 834 SkGradientShaderBase::Descriptor desc;
828 835
829 if (!flipGradient) { 836 if (!flipGradient) {
830 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 837 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
831 return sk_make_sp<SkTwoPointConicalGradient>(start, startRadius, end, en dRadius, 838 return sk_make_sp<SkTwoPointConicalGradient>(start, startRadius, end, en dRadius,
832 flipGradient, desc); 839 flipGradient, desc);
833 } else { 840 } else {
(...skipping 19 matching lines...) Expand all
853 860
854 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy, 861 sk_sp<SkShader> SkGradientShader::MakeSweep(SkScalar cx, SkScalar cy,
855 const SkColor colors[], 862 const SkColor colors[],
856 const SkScalar pos[], 863 const SkScalar pos[],
857 int colorCount, 864 int colorCount,
858 uint32_t flags, 865 uint32_t flags,
859 const SkMatrix* localMatrix) { 866 const SkMatrix* localMatrix) {
860 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) { 867 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) {
861 return nullptr; 868 return nullptr;
862 } 869 }
863 if (1 == colorCount) { 870 EXPAND_1_COLOR(colorCount);
864 return SkShader::MakeColorShader(colors[0]);
865 }
866 871
867 SkGradientShaderBase::Descriptor desc; 872 SkGradientShaderBase::Descriptor desc;
868 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags, localMatrix); 873 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags, localMatrix);
869 return sk_make_sp<SkSweepGradient>(cx, cy, desc); 874 return sk_make_sp<SkSweepGradient>(cx, cy, desc);
870 } 875 }
871 876
872 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) 877 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
873 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) 878 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient)
874 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) 879 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient)
875 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) 880 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient)
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after
1422 (*stops)[i] = stop; 1427 (*stops)[i] = stop;
1423 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; 1428 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f;
1424 } 1429 }
1425 } 1430 }
1426 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); 1431 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount));
1427 1432
1428 return outColors; 1433 return outColors;
1429 } 1434 }
1430 1435
1431 #endif 1436 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698