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

Side by Side Diff: gm/gradients.cpp

Issue 2370063002: Gradients are serialized (and can be constructed) as SkColor4f + SkColorSpace (Closed)
Patch Set: Widen storage for flags. Shift used bits to be contiguous. Document layout. Created 4 years, 2 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 | include/core/SkPicture.h » ('j') | 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 2011 Google Inc. 2 * Copyright 2011 Google Inc.
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 "gm.h" 8 #include "gm.h"
9 #include "SkGradientShader.h" 9 #include "SkGradientShader.h"
10 #include "SkLinearGradient.h" 10 #include "SkLinearGradient.h"
11 11
12 namespace skiagm { 12 namespace skiagm {
13 13
14 struct GradData { 14 struct GradData {
15 int fCount; 15 int fCount;
16 const SkColor* fColors; 16 const SkColor* fColors;
17 const SkScalar* fPos; 17 const SkColor4f* fColors4f;
18 const SkScalar* fPos;
18 }; 19 };
19 20
20 constexpr SkColor gColors[] = { 21 constexpr SkColor gColors[] = {
21 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK 22 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
22 }; 23 };
24 constexpr SkColor4f gColors4f[] ={
25 { 1.0f, 0.0f, 0.0f, 1.0f }, // Red
26 { 0.0f, 1.0f, 0.0f, 1.0f }, // Green
27 { 0.0f, 0.0f, 1.0f, 1.0f }, // Blue
28 { 1.0f, 1.0f, 1.0f, 1.0f }, // White
29 { 0.0f, 0.0f, 0.0f, 1.0f } // Black
30 };
23 constexpr SkScalar gPos0[] = { 0, SK_Scalar1 }; 31 constexpr SkScalar gPos0[] = { 0, SK_Scalar1 };
24 constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; 32 constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
25 constexpr SkScalar gPos2[] = { 33 constexpr SkScalar gPos2[] = {
26 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 34 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
27 }; 35 };
28 36
29 constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; 37 constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f};
30 constexpr SkColor gColorClamp[] = { 38 constexpr SkColor gColorClamp[] = {
31 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE 39 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE
32 }; 40 };
33 41 constexpr SkColor4f gColor4fClamp[] ={
42 { 1.0f, 0.0f, 0.0f, 1.0f }, // Red
43 { 0.0f, 1.0f, 0.0f, 1.0f }, // Green
44 { 0.0f, 1.0f, 0.0f, 1.0f }, // Green
45 { 0.0f, 0.0f, 1.0f, 1.0f } // Blue
46 };
34 constexpr GradData gGradData[] = { 47 constexpr GradData gGradData[] = {
35 { 2, gColors, nullptr }, 48 { 2, gColors, gColors4f, nullptr },
36 { 2, gColors, gPos0 }, 49 { 2, gColors, gColors4f, gPos0 },
37 { 2, gColors, gPos1 }, 50 { 2, gColors, gColors4f, gPos1 },
38 { 5, gColors, nullptr }, 51 { 5, gColors, gColors4f, nullptr },
39 { 5, gColors, gPos2 }, 52 { 5, gColors, gColors4f, gPos2 },
40 { 4, gColorClamp, gPosClamp } 53 { 4, gColorClamp, gColor4fClamp, gPosClamp }
41 }; 54 };
42 55
43 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, 56 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
44 SkShader::TileMode tm, const SkMatrix& localMa trix) { 57 SkShader::TileMode tm, const SkMatrix& localMa trix) {
45 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun t, tm, 0, 58 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun t, tm, 0,
46 &localMatrix); 59 &localMatrix);
47 } 60 }
48 61
62 static sk_sp<SkShader> MakeLinear4f(const SkPoint pts[2], const GradData& data,
63 SkShader::TileMode tm, const SkMatrix& local Matrix) {
64 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGam ma();
65 return SkGradientShader::MakeLinear(pts, data.fColors4f, srgb, data.fPos, da ta.fCount, tm, 0,
66 &localMatrix);
67 }
68
49 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, 69 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
50 SkShader::TileMode tm, const SkMatrix& localMa trix) { 70 SkShader::TileMode tm, const SkMatrix& localMa trix) {
51 SkPoint center; 71 SkPoint center;
52 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 72 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
53 SkScalarAve(pts[0].fY, pts[1].fY)); 73 SkScalarAve(pts[0].fY, pts[1].fY));
54 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fP os, data.fCount, 74 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fP os, data.fCount,
55 tm, 0, &localMatrix); 75 tm, 0, &localMatrix);
56 } 76 }
57 77
78 static sk_sp<SkShader> MakeRadial4f(const SkPoint pts[2], const GradData& data,
79 SkShader::TileMode tm, const SkMatrix& local Matrix) {
80 SkPoint center;
81 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
82 SkScalarAve(pts[0].fY, pts[1].fY));
83 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGam ma();
84 return SkGradientShader::MakeRadial(center, center.fX, data.fColors4f, srgb, data.fPos,
85 data.fCount, tm, 0, &localMatrix);
86 }
87
58 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, 88 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
59 SkShader::TileMode, const SkMatrix& localMatrix ) { 89 SkShader::TileMode, const SkMatrix& localMatrix ) {
60 SkPoint center; 90 SkPoint center;
61 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 91 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
62 SkScalarAve(pts[0].fY, pts[1].fY)); 92 SkScalarAve(pts[0].fY, pts[1].fY));
63 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data. fPos, data.fCount, 93 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data. fPos, data.fCount,
64 0, &localMatrix); 94 0, &localMatrix);
65 } 95 }
66 96
97 static sk_sp<SkShader> MakeSweep4f(const SkPoint pts[2], const GradData& data,
98 SkShader::TileMode, const SkMatrix& localMatr ix) {
99 SkPoint center;
100 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
101 SkScalarAve(pts[0].fY, pts[1].fY));
102 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGam ma();
103 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors4f, srg b, data.fPos,
104 data.fCount, 0, &localMatrix);
105 }
106
67 static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data, 107 static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data,
68 SkShader::TileMode tm, const SkMatrix& localM atrix) { 108 SkShader::TileMode tm, const SkMatrix& localM atrix) {
69 SkPoint center0, center1; 109 SkPoint center0, center1;
70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), 110 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY)); 111 SkScalarAve(pts[0].fY, pts[1].fY));
72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), 112 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); 113 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
74 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX ) / 7, 114 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX ) / 7,
75 center0, (pts[1].fX - pts[0].fX ) / 2, 115 center0, (pts[1].fX - pts[0].fX ) / 2,
76 data.fColors, data.fPos, data.f Count, tm, 116 data.fColors, data.fPos, data.f Count, tm,
77 0, &localMatrix); 117 0, &localMatrix);
78 } 118 }
79 119
120 static sk_sp<SkShader> Make2Radial4f(const SkPoint pts[2], const GradData& data,
121 SkShader::TileMode tm, const SkMatrix& loca lMatrix) {
122 SkPoint center0, center1;
123 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
124 SkScalarAve(pts[0].fY, pts[1].fY));
125 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3) / 5),
126 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1) / 4));
127 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGam ma();
128 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX ) / 7,
129 center0, (pts[1].fX - pts[0].fX ) / 2,
130 data.fColors4f, srgb, data.fPos , data.fCount, tm,
131 0, &localMatrix);
132 }
133
80 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, 134 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
81 SkShader::TileMode tm, const SkMatrix& localMatrix) { 135 SkShader::TileMode tm, const SkMatrix& local Matrix) {
82 SkPoint center0, center1; 136 SkPoint center0, center1;
83 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; 137 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
84 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; 138 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
85 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); 139 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
86 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); 140 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
87 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radi us0, 141 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radi us0,
88 data.fColors, data.fPos, 142 data.fColors, data.fPos,
89 data.fCount, tm, 0, &localMatri x); 143 data.fCount, tm, 0, &localMatri x);
90 } 144 }
91 145
146 static sk_sp<SkShader> Make2Conical4f(const SkPoint pts[2], const GradData& data ,
147 SkShader::TileMode tm, const SkMatrix& loc alMatrix) {
148 SkPoint center0, center1;
149 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
150 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
151 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
152 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
153 auto srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)->makeLinearGam ma();
154 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radi us0,
155 data.fColors4f, srgb, data.fPos ,
156 data.fCount, tm, 0, &localMatri x);
157 }
158
92 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, 159 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
93 SkShader::TileMode tm, const SkMatrix& loca lMatrix); 160 SkShader::TileMode tm, const SkMatrix& loca lMatrix);
94 constexpr GradMaker gGradMakers[] = { 161 constexpr GradMaker gGradMakers[] = {
95 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical 162 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
96 }; 163 };
164 constexpr GradMaker gGradMakers4f[] ={
165 MakeLinear4f, MakeRadial4f, MakeSweep4f, Make2Radial4f, Make2Conical4f
166 };
97 167
98 /////////////////////////////////////////////////////////////////////////////// 168 ///////////////////////////////////////////////////////////////////////////////
99 169
100 class GradientsGM : public GM { 170 class GradientsGM : public GM {
101 public: 171 public:
102 GradientsGM(bool dither) : fDither(dither) { 172 GradientsGM(bool dither) : fDither(dither) {
103 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); 173 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
104 } 174 }
105 175
106 protected: 176 protected:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 215
146 protected: 216 protected:
147 bool fDither; 217 bool fDither;
148 218
149 private: 219 private:
150 typedef GM INHERITED; 220 typedef GM INHERITED;
151 }; 221 };
152 DEF_GM( return new GradientsGM(true); ) 222 DEF_GM( return new GradientsGM(true); )
153 DEF_GM( return new GradientsGM(false); ) 223 DEF_GM( return new GradientsGM(false); )
154 224
225 // Like the original gradients GM, but using the SkColor4f shader factories. Sho uld be identical.
226 class Gradients4fGM : public GM {
227 public:
228 Gradients4fGM(bool dither) : fDither(dither) {
229 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
230 }
231
232 protected:
233
234 SkString onShortName() {
235 return SkString(fDither ? "gradients4f" : "gradients4f_nodither");
236 }
237
238 virtual SkISize onISize() { return SkISize::Make(840, 815); }
239
240 virtual void onDraw(SkCanvas* canvas) {
241
242 SkPoint pts[2] ={
243 { 0, 0 },
244 { SkIntToScalar(100), SkIntToScalar(100) }
245 };
246 SkShader::TileMode tm = SkShader::kClamp_TileMode;
247 SkRect r ={ 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
248 SkPaint paint;
249 paint.setAntiAlias(true);
250 paint.setDither(fDither);
251
252 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
253 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
254 canvas->save();
255 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers4f); j++) {
256 SkMatrix scale = SkMatrix::I();
257
258 if (i == 5) { // if the clamp case
259 scale.setScale(0.5f, 0.5f);
260 scale.postTranslate(25.f, 25.f);
261 }
262
263 paint.setShader(gGradMakers4f[j](pts, gGradData[i], tm, scale));
264 canvas->drawRect(r, paint);
265 canvas->translate(0, SkIntToScalar(120));
266 }
267 canvas->restore();
268 canvas->translate(SkIntToScalar(120), 0);
269 }
270 }
271
272 protected:
273 bool fDither;
274
275 private:
276 typedef GM INHERITED;
277 };
278 DEF_GM(return new Gradients4fGM(true); )
279 DEF_GM(return new Gradients4fGM(false); )
280
155 // Based on the original gradient slide, but with perspective applied to the 281 // Based on the original gradient slide, but with perspective applied to the
156 // gradient shaders' local matrices 282 // gradient shaders' local matrices
157 class GradientsLocalPerspectiveGM : public GM { 283 class GradientsLocalPerspectiveGM : public GM {
158 public: 284 public:
159 GradientsLocalPerspectiveGM(bool dither) : fDither(dither) { 285 GradientsLocalPerspectiveGM(bool dither) : fDither(dither) {
160 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); 286 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
161 } 287 }
162 288
163 protected: 289 protected:
164 290
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p); 912 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
787 } 913 }
788 914
789 DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) { 915 DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) {
790 draw_many_stops(canvas, 0); 916 draw_many_stops(canvas, 0);
791 } 917 }
792 918
793 DEF_SIMPLE_GM(gradient_many_stops_4f, canvas, 500, 500) { 919 DEF_SIMPLE_GM(gradient_many_stops_4f, canvas, 500, 500) {
794 draw_many_stops(canvas, SkLinearGradient::kForce4fContext_PrivateFlag); 920 draw_many_stops(canvas, SkLinearGradient::kForce4fContext_PrivateFlag);
795 } 921 }
OLDNEW
« no previous file with comments | « no previous file | include/core/SkPicture.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698