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

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

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