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

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

Issue 837013002: improve sanity checks on gradient constructor parameters (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 11 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 "SkGradientShaderPriv.h" 8 #include "SkGradientShaderPriv.h"
9 #include "SkLinearGradient.h" 9 #include "SkLinearGradient.h"
10 #include "SkRadialGradient.h" 10 #include "SkRadialGradient.h"
(...skipping 717 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 str->append(" "); 728 str->append(" ");
729 str->append(gTileModeName[fTileMode]); 729 str->append(gTileModeName[fTileMode]);
730 730
731 this->INHERITED::toString(str); 731 this->INHERITED::toString(str);
732 } 732 }
733 #endif 733 #endif
734 734
735 /////////////////////////////////////////////////////////////////////////////// 735 ///////////////////////////////////////////////////////////////////////////////
736 /////////////////////////////////////////////////////////////////////////////// 736 ///////////////////////////////////////////////////////////////////////////////
737 737
738 // Return true if these parameters are valid/legal/safe to construct a gradient
739 //
740 static bool valid_grad(const SkColor colors[], const SkScalar pos[], int count, unsigned tileMode) {
Stephen White 2015/01/06 14:53:30 Nit: pos is unused here (presumably because NULL i
741 return NULL != colors && count >= 1 && tileMode < (unsigned)SkShader::kTileM odeCount;
742 }
743
738 // assumes colors is SkColor* and pos is SkScalar* 744 // assumes colors is SkColor* and pos is SkScalar*
739 #define EXPAND_1_COLOR(count) \ 745 #define EXPAND_1_COLOR(count) \
740 SkColor tmp[2]; \ 746 SkColor tmp[2]; \
741 do { \ 747 do { \
742 if (1 == count) { \ 748 if (1 == count) { \
743 tmp[0] = tmp[1] = colors[0]; \ 749 tmp[0] = tmp[1] = colors[0]; \
744 colors = tmp; \ 750 colors = tmp; \
745 pos = NULL; \ 751 pos = NULL; \
746 count = 2; \ 752 count = 2; \
747 } \ 753 } \
748 } while (0) 754 } while (0)
749 755
750 static void desc_init(SkGradientShaderBase::Descriptor* desc, 756 static void desc_init(SkGradientShaderBase::Descriptor* desc,
751 const SkColor colors[], const SkScalar pos[], int colorCou nt, 757 const SkColor colors[], const SkScalar pos[], int colorCou nt,
752 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l ocalMatrix) { 758 SkShader::TileMode mode, uint32_t flags, const SkMatrix* l ocalMatrix) {
753 desc->fColors = colors; 759 desc->fColors = colors;
754 desc->fPos = pos; 760 desc->fPos = pos;
755 desc->fCount = colorCount; 761 desc->fCount = colorCount;
756 desc->fTileMode = mode; 762 desc->fTileMode = mode;
757 desc->fGradFlags = flags; 763 desc->fGradFlags = flags;
758 desc->fLocalMatrix = localMatrix; 764 desc->fLocalMatrix = localMatrix;
759 } 765 }
760 766
761 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], 767 SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
762 const SkColor colors[], 768 const SkColor colors[],
763 const SkScalar pos[], int colorCount, 769 const SkScalar pos[], int colorCount,
764 SkShader::TileMode mode, 770 SkShader::TileMode mode,
765 uint32_t flags, 771 uint32_t flags,
766 const SkMatrix* localMatrix) { 772 const SkMatrix* localMatrix) {
767 if (NULL == pts || NULL == colors || colorCount < 1) { 773 if (!pts) {
774 return NULL;
775 }
776 if (!valid_grad(colors, pos, colorCount, mode)) {
768 return NULL; 777 return NULL;
769 } 778 }
770 EXPAND_1_COLOR(colorCount); 779 EXPAND_1_COLOR(colorCount);
771 780
772 SkGradientShaderBase::Descriptor desc; 781 SkGradientShaderBase::Descriptor desc;
773 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 782 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
774 return SkNEW_ARGS(SkLinearGradient, (pts, desc)); 783 return SkNEW_ARGS(SkLinearGradient, (pts, desc));
775 } 784 }
776 785
777 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, 786 SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
778 const SkColor colors[], 787 const SkColor colors[],
779 const SkScalar pos[], int colorCount, 788 const SkScalar pos[], int colorCount,
780 SkShader::TileMode mode, 789 SkShader::TileMode mode,
781 uint32_t flags, 790 uint32_t flags,
782 const SkMatrix* localMatrix) { 791 const SkMatrix* localMatrix) {
783 if (radius <= 0 || NULL == colors || colorCount < 1) { 792 if (radius <= 0) {
793 return NULL;
794 }
795 if (!valid_grad(colors, pos, colorCount, mode)) {
784 return NULL; 796 return NULL;
785 } 797 }
786 EXPAND_1_COLOR(colorCount); 798 EXPAND_1_COLOR(colorCount);
787 799
788 SkGradientShaderBase::Descriptor desc; 800 SkGradientShaderBase::Descriptor desc;
789 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 801 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
790 return SkNEW_ARGS(SkRadialGradient, (center, radius, desc)); 802 return SkNEW_ARGS(SkRadialGradient, (center, radius, desc));
791 } 803 }
792 804
793 SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, 805 SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
794 SkScalar startRadius, 806 SkScalar startRadius,
795 const SkPoint& end, 807 const SkPoint& end,
796 SkScalar endRadius, 808 SkScalar endRadius,
797 const SkColor colors[], 809 const SkColor colors[],
798 const SkScalar pos[], 810 const SkScalar pos[],
799 int colorCount, 811 int colorCount,
800 SkShader::TileMode mode, 812 SkShader::TileMode mode,
801 uint32_t flags, 813 uint32_t flags,
802 const SkMatrix* localMatrix) { 814 const SkMatrix* localMatrix) {
803 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { 815 if (startRadius < 0 || endRadius < 0) {
816 return NULL;
817 }
818 if (!valid_grad(colors, pos, colorCount, mode)) {
804 return NULL; 819 return NULL;
805 } 820 }
806 EXPAND_1_COLOR(colorCount); 821 EXPAND_1_COLOR(colorCount);
807 822
808 SkGradientShaderBase::Descriptor desc; 823 SkGradientShaderBase::Descriptor desc;
809 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix); 824 desc_init(&desc, colors, pos, colorCount, mode, flags, localMatrix);
810 return SkNEW_ARGS(SkTwoPointRadialGradient, 825 return SkNEW_ARGS(SkTwoPointRadialGradient,
811 (start, startRadius, end, endRadius, desc)); 826 (start, startRadius, end, endRadius, desc));
812 } 827 }
813 828
814 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, 829 SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
815 SkScalar startRadius, 830 SkScalar startRadius,
816 const SkPoint& end, 831 const SkPoint& end,
817 SkScalar endRadius, 832 SkScalar endRadius,
818 const SkColor colors[], 833 const SkColor colors[],
819 const SkScalar pos[], 834 const SkScalar pos[],
820 int colorCount, 835 int colorCount,
821 SkShader::TileMode mode, 836 SkShader::TileMode mode,
822 uint32_t flags, 837 uint32_t flags,
823 const SkMatrix* localMatrix) { 838 const SkMatrix* localMatrix) {
824 if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { 839 if (startRadius < 0 || endRadius < 0) {
840 return NULL;
841 }
842 if (!valid_grad(colors, pos, colorCount, mode)) {
825 return NULL; 843 return NULL;
826 } 844 }
827 if (start == end && startRadius == endRadius) { 845 if (start == end && startRadius == endRadius) {
828 return SkShader::CreateEmptyShader(); 846 return SkShader::CreateEmptyShader();
829 } 847 }
830 848
831 EXPAND_1_COLOR(colorCount); 849 EXPAND_1_COLOR(colorCount);
832 850
833 bool flipGradient = startRadius > endRadius; 851 bool flipGradient = startRadius > endRadius;
834 852
(...skipping 23 matching lines...) Expand all
858 (end, endRadius, start, startRadius, flipGradient, des c)); 876 (end, endRadius, start, startRadius, flipGradient, des c));
859 } 877 }
860 } 878 }
861 879
862 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, 880 SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
863 const SkColor colors[], 881 const SkColor colors[],
864 const SkScalar pos[], 882 const SkScalar pos[],
865 int colorCount, 883 int colorCount,
866 uint32_t flags, 884 uint32_t flags,
867 const SkMatrix* localMatrix) { 885 const SkMatrix* localMatrix) {
868 if (NULL == colors || colorCount < 1) { 886 if (!valid_grad(colors, pos, colorCount, SkShader::kClamp_TileMode)) {
869 return NULL; 887 return NULL;
870 } 888 }
871 EXPAND_1_COLOR(colorCount); 889 EXPAND_1_COLOR(colorCount);
872 890
873 SkGradientShaderBase::Descriptor desc; 891 SkGradientShaderBase::Descriptor desc;
874 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags, localMatrix); 892 desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, flags, localMatrix);
875 return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc)); 893 return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc));
876 } 894 }
877 895
878 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) 896 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1182 (*stops)[i] = stop; 1200 (*stops)[i] = stop;
1183 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; 1201 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f;
1184 } 1202 }
1185 } 1203 }
1186 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); 1204 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount));
1187 1205
1188 return outColors; 1206 return outColors;
1189 } 1207 }
1190 1208
1191 #endif 1209 #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