blob: 8ae1dd7901e45cd6044ca2968b7b8cb8cba324c9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed3d9005c2015-04-23 10:30:27 -07007
reed@android.com42309d42009-06-22 02:06:35 +00008#include "gm.h"
9#include "SkGradientShader.h"
fmalitabc590c02016-02-22 09:12:33 -080010#include "SkLinearGradient.h"
reed@android.com42309d42009-06-22 02:06:35 +000011
12namespace skiagm {
13
14struct GradData {
brianosmane25d71c2016-09-28 11:27:28 -070015 int fCount;
16 const SkColor* fColors;
17 const SkColor4f* fColors4f;
18 const SkScalar* fPos;
reed@android.com42309d42009-06-22 02:06:35 +000019};
20
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr SkColor gColors[] = {
reed@android.com42309d42009-06-22 02:06:35 +000022 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
23};
brianosmane25d71c2016-09-28 11:27:28 -070024constexpr 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};
mtkleindbfd7ab2016-09-01 11:24:54 -070031constexpr SkScalar gPos0[] = { 0, SK_Scalar1 };
32constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
33constexpr SkScalar gPos2[] = {
reed@android.com42309d42009-06-22 02:06:35 +000034 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
35};
36
mtkleindbfd7ab2016-09-01 11:24:54 -070037constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f};
38constexpr SkColor gColorClamp[] = {
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +000039 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE
40};
brianosmane25d71c2016-09-28 11:27:28 -070041constexpr 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};
mtkleindbfd7ab2016-09-01 11:24:54 -070047constexpr GradData gGradData[] = {
brianosmane25d71c2016-09-28 11:27:28 -070048 { 2, gColors, gColors4f, nullptr },
49 { 2, gColors, gColors4f, gPos0 },
50 { 2, gColors, gColors4f, gPos1 },
51 { 5, gColors, gColors4f, nullptr },
52 { 5, gColors, gColors4f, gPos2 },
53 { 4, gColorClamp, gColor4fClamp, gPosClamp }
reed@android.com42309d42009-06-22 02:06:35 +000054};
55
reed1a9b9642016-03-13 14:13:58 -070056static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
57 SkShader::TileMode tm, const SkMatrix& localMatrix) {
58 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm, 0,
59 &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000060}
reed@google.comf3c1cc92010-12-23 16:45:33 +000061
brianosmane25d71c2016-09-28 11:27:28 -070062static sk_sp<SkShader> MakeLinear4f(const SkPoint pts[2], const GradData& data,
63 SkShader::TileMode tm, const SkMatrix& localMatrix) {
Brian Osman526972e2016-10-24 09:24:02 -040064 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmane25d71c2016-09-28 11:27:28 -070065 return SkGradientShader::MakeLinear(pts, data.fColors4f, srgb, data.fPos, data.fCount, tm, 0,
66 &localMatrix);
67}
68
reed1a9b9642016-03-13 14:13:58 -070069static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
70 SkShader::TileMode tm, const SkMatrix& localMatrix) {
reed@android.com42309d42009-06-22 02:06:35 +000071 SkPoint center;
72 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
73 SkScalarAve(pts[0].fY, pts[1].fY));
reed1a9b9642016-03-13 14:13:58 -070074 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fPos, data.fCount,
75 tm, 0, &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000076}
77
brianosmane25d71c2016-09-28 11:27:28 -070078static sk_sp<SkShader> MakeRadial4f(const SkPoint pts[2], const GradData& data,
79 SkShader::TileMode tm, const SkMatrix& localMatrix) {
80 SkPoint center;
81 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
82 SkScalarAve(pts[0].fY, pts[1].fY));
Brian Osman526972e2016-10-24 09:24:02 -040083 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmane25d71c2016-09-28 11:27:28 -070084 return SkGradientShader::MakeRadial(center, center.fX, data.fColors4f, srgb, data.fPos,
85 data.fCount, tm, 0, &localMatrix);
86}
87
reed1a9b9642016-03-13 14:13:58 -070088static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
89 SkShader::TileMode, const SkMatrix& localMatrix) {
reed@android.com42309d42009-06-22 02:06:35 +000090 SkPoint center;
91 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
92 SkScalarAve(pts[0].fY, pts[1].fY));
reed1a9b9642016-03-13 14:13:58 -070093 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount,
94 0, &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000095}
96
brianosmane25d71c2016-09-28 11:27:28 -070097static sk_sp<SkShader> MakeSweep4f(const SkPoint pts[2], const GradData& data,
98 SkShader::TileMode, const SkMatrix& localMatrix) {
99 SkPoint center;
100 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
101 SkScalarAve(pts[0].fY, pts[1].fY));
Brian Osman526972e2016-10-24 09:24:02 -0400102 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmane25d71c2016-09-28 11:27:28 -0700103 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors4f, srgb, data.fPos,
104 data.fCount, 0, &localMatrix);
105}
106
reed1a9b9642016-03-13 14:13:58 -0700107static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data,
108 SkShader::TileMode tm, const SkMatrix& localMatrix) {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000109 SkPoint center0, center1;
110 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
111 SkScalarAve(pts[0].fY, pts[1].fY));
112 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
113 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
reed1a9b9642016-03-13 14:13:58 -0700114 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
115 center0, (pts[1].fX - pts[0].fX) / 2,
116 data.fColors, data.fPos, data.fCount, tm,
117 0, &localMatrix);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000118}
119
brianosmane25d71c2016-09-28 11:27:28 -0700120static sk_sp<SkShader> Make2Radial4f(const SkPoint pts[2], const GradData& data,
121 SkShader::TileMode tm, const SkMatrix& localMatrix) {
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));
Brian Osman526972e2016-10-24 09:24:02 -0400127 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmane25d71c2016-09-28 11:27:28 -0700128 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
reed1a9b9642016-03-13 14:13:58 -0700134static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
brianosmane25d71c2016-09-28 11:27:28 -0700135 SkShader::TileMode tm, const SkMatrix& localMatrix) {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000136 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700137 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
138 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
rileya@google.com5cf2c282012-07-09 14:42:16 +0000139 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
140 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reed1a9b9642016-03-13 14:13:58 -0700141 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0,
142 data.fColors, data.fPos,
143 data.fCount, tm, 0, &localMatrix);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000144}
145
brianosmane25d71c2016-09-28 11:27:28 -0700146static sk_sp<SkShader> Make2Conical4f(const SkPoint pts[2], const GradData& data,
147 SkShader::TileMode tm, const SkMatrix& localMatrix) {
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);
Brian Osman526972e2016-10-24 09:24:02 -0400153 auto srgb = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmane25d71c2016-09-28 11:27:28 -0700154 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0,
155 data.fColors4f, srgb, data.fPos,
156 data.fCount, tm, 0, &localMatrix);
157}
158
reed1a9b9642016-03-13 14:13:58 -0700159typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
160 SkShader::TileMode tm, const SkMatrix& localMatrix);
mtkleindbfd7ab2016-09-01 11:24:54 -0700161constexpr GradMaker gGradMakers[] = {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000162 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
reed@android.com42309d42009-06-22 02:06:35 +0000163};
brianosmane25d71c2016-09-28 11:27:28 -0700164constexpr GradMaker gGradMakers4f[] ={
165 MakeLinear4f, MakeRadial4f, MakeSweep4f, Make2Radial4f, Make2Conical4f
166};
reed@android.com42309d42009-06-22 02:06:35 +0000167
168///////////////////////////////////////////////////////////////////////////////
169
170class GradientsGM : public GM {
171public:
fmalita063675b2015-10-12 10:41:48 -0700172 GradientsGM(bool dither) : fDither(dither) {
caryclark65cdba62015-06-15 06:51:08 -0700173 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000174 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
reed@android.com42309d42009-06-22 02:06:35 +0000176protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000177
reed@android.com42309d42009-06-22 02:06:35 +0000178 SkString onShortName() {
fmalita063675b2015-10-12 10:41:48 -0700179 return SkString(fDither ? "gradients" : "gradients_nodither");
reed@android.com42309d42009-06-22 02:06:35 +0000180 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
edisonn@google.com1da3a802013-09-19 17:55:49 +0000182 virtual SkISize onISize() { return SkISize::Make(840, 815); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
reed@android.com42309d42009-06-22 02:06:35 +0000184 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
reed@android.com42309d42009-06-22 02:06:35 +0000186 SkPoint pts[2] = {
187 { 0, 0 },
188 { SkIntToScalar(100), SkIntToScalar(100) }
189 };
190 SkShader::TileMode tm = SkShader::kClamp_TileMode;
191 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
192 SkPaint paint;
193 paint.setAntiAlias(true);
fmalita063675b2015-10-12 10:41:48 -0700194 paint.setDither(fDither);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195
reed@android.com42309d42009-06-22 02:06:35 +0000196 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
197 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
198 canvas->save();
199 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000200 SkMatrix scale = SkMatrix::I();
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000201
202 if (i == 5) { // if the clamp case
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000203 scale.setScale(0.5f, 0.5f);
204 scale.postTranslate(25.f, 25.f);
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000205 }
skia.committer@gmail.comd55e3572013-08-08 07:01:20 +0000206
reed1a9b9642016-03-13 14:13:58 -0700207 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, scale));
reed@android.com42309d42009-06-22 02:06:35 +0000208 canvas->drawRect(r, paint);
reed@android.com42309d42009-06-22 02:06:35 +0000209 canvas->translate(0, SkIntToScalar(120));
210 }
211 canvas->restore();
212 canvas->translate(SkIntToScalar(120), 0);
213 }
214 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000215
fmalita063675b2015-10-12 10:41:48 -0700216protected:
217 bool fDither;
218
reed@android.com42309d42009-06-22 02:06:35 +0000219private:
220 typedef GM INHERITED;
221};
fmalita063675b2015-10-12 10:41:48 -0700222DEF_GM( return new GradientsGM(true); )
223DEF_GM( return new GradientsGM(false); )
reed@android.com42309d42009-06-22 02:06:35 +0000224
brianosmane25d71c2016-09-28 11:27:28 -0700225// Like the original gradients GM, but using the SkColor4f shader factories. Should be identical.
226class Gradients4fGM : public GM {
227public:
228 Gradients4fGM(bool dither) : fDither(dither) {
229 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
230 }
231
232protected:
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
272protected:
273 bool fDither;
274
275private:
276 typedef GM INHERITED;
277};
278DEF_GM(return new Gradients4fGM(true); )
279DEF_GM(return new Gradients4fGM(false); )
280
rileya@google.com5cf2c282012-07-09 14:42:16 +0000281// Based on the original gradient slide, but with perspective applied to the
282// gradient shaders' local matrices
283class GradientsLocalPerspectiveGM : public GM {
284public:
fmalita063675b2015-10-12 10:41:48 -0700285 GradientsLocalPerspectiveGM(bool dither) : fDither(dither) {
caryclark65cdba62015-06-15 06:51:08 -0700286 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
rileya@google.com5cf2c282012-07-09 14:42:16 +0000287 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000288
rileya@google.com5cf2c282012-07-09 14:42:16 +0000289protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000290
rileya@google.com5cf2c282012-07-09 14:42:16 +0000291 SkString onShortName() {
fmalita063675b2015-10-12 10:41:48 -0700292 return SkString(fDither ? "gradients_local_perspective" :
293 "gradients_local_perspective_nodither");
rileya@google.com5cf2c282012-07-09 14:42:16 +0000294 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000295
edisonn@google.com1da3a802013-09-19 17:55:49 +0000296 virtual SkISize onISize() { return SkISize::Make(840, 815); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000297
rileya@google.com5cf2c282012-07-09 14:42:16 +0000298 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000299
rileya@google.com5cf2c282012-07-09 14:42:16 +0000300 SkPoint pts[2] = {
301 { 0, 0 },
302 { SkIntToScalar(100), SkIntToScalar(100) }
303 };
304 SkShader::TileMode tm = SkShader::kClamp_TileMode;
305 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
306 SkPaint paint;
307 paint.setAntiAlias(true);
fmalita063675b2015-10-12 10:41:48 -0700308 paint.setDither(fDither);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000309
rileya@google.com5cf2c282012-07-09 14:42:16 +0000310 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
311 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
312 canvas->save();
313 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000314 // apply an increasing y perspective as we move to the right
315 SkMatrix perspective;
316 perspective.setIdentity();
reed80ea19c2015-05-12 10:37:34 -0700317 perspective.setPerspY(SkIntToScalar(i+1) / 500);
318 perspective.setSkewX(SkIntToScalar(i+1) / 10);
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000319
reed1a9b9642016-03-13 14:13:58 -0700320 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, perspective));
rileya@google.com5cf2c282012-07-09 14:42:16 +0000321 canvas->drawRect(r, paint);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000322 canvas->translate(0, SkIntToScalar(120));
323 }
324 canvas->restore();
325 canvas->translate(SkIntToScalar(120), 0);
326 }
327 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000328
rileya@google.com5cf2c282012-07-09 14:42:16 +0000329private:
fmalita063675b2015-10-12 10:41:48 -0700330 bool fDither;
331
rileya@google.com5cf2c282012-07-09 14:42:16 +0000332 typedef GM INHERITED;
333};
fmalita063675b2015-10-12 10:41:48 -0700334DEF_GM( return new GradientsLocalPerspectiveGM(true); )
335DEF_GM( return new GradientsLocalPerspectiveGM(false); )
rileya@google.com5cf2c282012-07-09 14:42:16 +0000336
337// Based on the original gradient slide, but with perspective applied to
338// the view matrix
339class GradientsViewPerspectiveGM : public GradientsGM {
fmalita063675b2015-10-12 10:41:48 -0700340public:
341 GradientsViewPerspectiveGM(bool dither) : INHERITED(dither) { }
342
rileya@google.com5cf2c282012-07-09 14:42:16 +0000343protected:
344 SkString onShortName() {
fmalita063675b2015-10-12 10:41:48 -0700345 return SkString(fDither ? "gradients_view_perspective" :
346 "gradients_view_perspective_nodither");
rileya@google.com5cf2c282012-07-09 14:42:16 +0000347 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348
edisonn@google.com1da3a802013-09-19 17:55:49 +0000349 virtual SkISize onISize() { return SkISize::Make(840, 500); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350
rileya@google.com5cf2c282012-07-09 14:42:16 +0000351 virtual void onDraw(SkCanvas* canvas) {
352 SkMatrix perspective;
353 perspective.setIdentity();
reed80ea19c2015-05-12 10:37:34 -0700354 perspective.setPerspY(0.001f);
355 perspective.setSkewX(SkIntToScalar(8) / 25);
scroggo@google.com837d31a2012-08-15 18:42:45 +0000356 canvas->concat(perspective);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000357 INHERITED::onDraw(canvas);
358 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000359
rileya@google.com5cf2c282012-07-09 14:42:16 +0000360private:
361 typedef GradientsGM INHERITED;
362};
fmalita063675b2015-10-12 10:41:48 -0700363DEF_GM( return new GradientsViewPerspectiveGM(true); )
364DEF_GM( return new GradientsViewPerspectiveGM(false); )
rileya@google.com5cf2c282012-07-09 14:42:16 +0000365
reed@google.comac864a92011-06-27 18:11:17 +0000366/*
367 Inspired by this <canvas> javascript, where we need to detect that we are not
368 solving a quadratic equation, but must instead solve a linear (since our X^2
369 coefficient is 0)
370
371 ctx.fillStyle = '#f00';
372 ctx.fillRect(0, 0, 100, 50);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373
reed@google.comac864a92011-06-27 18:11:17 +0000374 var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150);
375 g.addColorStop(0, '#f00');
376 g.addColorStop(0.01, '#0f0');
377 g.addColorStop(0.99, '#0f0');
378 g.addColorStop(1, '#f00');
379 ctx.fillStyle = g;
380 ctx.fillRect(0, 0, 100, 50);
381 */
382class GradientsDegenrate2PointGM : public GM {
383public:
fmalita063675b2015-10-12 10:41:48 -0700384 GradientsDegenrate2PointGM(bool dither) : fDither(dither) {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000385
reed@google.comac864a92011-06-27 18:11:17 +0000386protected:
387 SkString onShortName() {
fmalita063675b2015-10-12 10:41:48 -0700388 return SkString(fDither ? "gradients_degenerate_2pt" : "gradients_degenerate_2pt_nodither");
reed@google.comac864a92011-06-27 18:11:17 +0000389 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390
edisonn@google.com1da3a802013-09-19 17:55:49 +0000391 virtual SkISize onISize() { return SkISize::Make(320, 320); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392
reed@google.comac864a92011-06-27 18:11:17 +0000393 void drawBG(SkCanvas* canvas) {
394 canvas->drawColor(SK_ColorBLUE);
395 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000396
reed@google.comac864a92011-06-27 18:11:17 +0000397 virtual void onDraw(SkCanvas* canvas) {
398 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000399
reed@google.comac864a92011-06-27 18:11:17 +0000400 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000401 SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 };
epoger@google.com59f3abf2011-07-21 15:50:33 +0000402 SkPoint c0;
403 c0.iset(-80, 25);
404 SkScalar r0 = SkIntToScalar(70);
405 SkPoint c1;
406 c1.iset(0, 25);
407 SkScalar r1 = SkIntToScalar(150);
reed9283d202016-03-13 13:01:57 -0700408 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700409 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors,
410 pos, SK_ARRAY_COUNT(pos),
411 SkShader::kClamp_TileMode));
fmalita063675b2015-10-12 10:41:48 -0700412 paint.setDither(fDither);
reed@google.comac864a92011-06-27 18:11:17 +0000413 canvas->drawPaint(paint);
414 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000415
reed@google.comac864a92011-06-27 18:11:17 +0000416private:
fmalita063675b2015-10-12 10:41:48 -0700417 bool fDither;
418
reed@google.comac864a92011-06-27 18:11:17 +0000419 typedef GM INHERITED;
420};
fmalita063675b2015-10-12 10:41:48 -0700421DEF_GM( return new GradientsDegenrate2PointGM(true); )
422DEF_GM( return new GradientsDegenrate2PointGM(false); )
reed@google.comac864a92011-06-27 18:11:17 +0000423
caryclarkcb100712016-02-26 05:59:40 -0800424/* bug.skia.org/517
425<canvas id="canvas"></canvas>
426<script>
427var c = document.getElementById("canvas");
428var ctx = c.getContext("2d");
429ctx.fillStyle = '#ff0';
430ctx.fillRect(0, 0, 100, 50);
431
432var g = ctx.createRadialGradient(200, 25, 20, 200, 25, 10);
433g.addColorStop(0, '#0f0');
434g.addColorStop(0.003, '#f00'); // 0.004 makes this work
435g.addColorStop(1, '#ff0');
436ctx.fillStyle = g;
437ctx.fillRect(0, 0, 100, 50);
438</script>
439*/
440
441// should draw only green
442DEF_SIMPLE_GM(small_color_stop, canvas, 100, 150) {
443 SkColor colors[] = { SK_ColorGREEN, SK_ColorRED, SK_ColorYELLOW };
444 SkScalar pos[] = { 0, 0.003f, SK_Scalar1 }; // 0.004f makes this work
445 SkPoint c0 = { 200, 25 };
446 SkScalar r0 = 20;
447 SkPoint c1 = { 200, 25 };
448 SkScalar r1 = 10;
reed1a9b9642016-03-13 14:13:58 -0700449
caryclarkcb100712016-02-26 05:59:40 -0800450 SkPaint paint;
451 paint.setColor(SK_ColorYELLOW);
452 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
reed1a9b9642016-03-13 14:13:58 -0700453 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors, pos,
454 SK_ARRAY_COUNT(pos),
455 SkShader::kClamp_TileMode));
caryclarkcb100712016-02-26 05:59:40 -0800456 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
457}
458
459
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000460/// Tests correctness of *optimized* codepaths in gradients.
461
462class ClampedGradientsGM : public GM {
463public:
fmalita063675b2015-10-12 10:41:48 -0700464 ClampedGradientsGM(bool dither) : fDither(dither) {}
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000465
466protected:
fmalita063675b2015-10-12 10:41:48 -0700467 SkString onShortName() {
468 return SkString(fDither ? "clamped_gradients" : "clamped_gradients_nodither");
469 }
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000470
edisonn@google.com1da3a802013-09-19 17:55:49 +0000471 virtual SkISize onISize() { return SkISize::Make(640, 510); }
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000472
473 void drawBG(SkCanvas* canvas) {
caryclark12596012015-07-29 05:27:47 -0700474 canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000475 }
476
477 virtual void onDraw(SkCanvas* canvas) {
478 this->drawBG(canvas);
479
480 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) };
481 SkPaint paint;
fmalita063675b2015-10-12 10:41:48 -0700482 paint.setDither(fDither);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000483 paint.setAntiAlias(true);
484
485 SkPoint center;
486 center.iset(0, 300);
487 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
reed1a9b9642016-03-13 14:13:58 -0700488 paint.setShader(SkGradientShader::MakeRadial(
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000489 SkPoint(center),
halcanary96fcdcc2015-08-27 07:41:13 -0700490 SkIntToScalar(200), gColors, nullptr, 5,
reed1a9b9642016-03-13 14:13:58 -0700491 SkShader::kClamp_TileMode));
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000492 canvas->drawRect(r, paint);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000493 }
494
495private:
fmalita063675b2015-10-12 10:41:48 -0700496 bool fDither;
497
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000498 typedef GM INHERITED;
499};
fmalita063675b2015-10-12 10:41:48 -0700500DEF_GM( return new ClampedGradientsGM(true); )
501DEF_GM( return new ClampedGradientsGM(false); )
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000502
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000503/// Checks quality of large radial gradients, which may display
504/// some banding.
505
506class RadialGradientGM : public GM {
507public:
508 RadialGradientGM() {}
509
510protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000511
mtklein36352bf2015-03-25 18:17:31 -0700512 SkString onShortName() override { return SkString("radial_gradient"); }
513 SkISize onISize() override { return SkISize::Make(1280, 1280); }
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000514 void drawBG(SkCanvas* canvas) {
515 canvas->drawColor(0xFF000000);
516 }
mtklein36352bf2015-03-25 18:17:31 -0700517 void onDraw(SkCanvas* canvas) override {
reed@google.combb0948f2012-01-31 14:44:13 +0000518 const SkISize dim = this->getISize();
519
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000520 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000521
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000522 SkPaint paint;
523 paint.setDither(true);
524 SkPoint center;
reed@google.combb0948f2012-01-31 14:44:13 +0000525 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2);
526 SkScalar radius = SkIntToScalar(dim.width())/2;
527 const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000528 const SkScalar pos[] = { 0.0f,
529 0.35f,
530 1.0f };
reed1a9b9642016-03-13 14:13:58 -0700531 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos,
532 SK_ARRAY_COUNT(pos),
533 SkShader::kClamp_TileMode));
reed@google.combb0948f2012-01-31 14:44:13 +0000534 SkRect r = {
535 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height())
536 };
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000537 canvas->drawRect(r, paint);
538 }
539private:
540 typedef GM INHERITED;
541};
reed3d9005c2015-04-23 10:30:27 -0700542DEF_GM( return new RadialGradientGM; )
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000543
mtklein@google.com361a72f2013-08-19 18:43:34 +0000544class RadialGradient2GM : public GM {
545public:
fmalita063675b2015-10-12 10:41:48 -0700546 RadialGradient2GM(bool dither) : fDither(dither) {}
mtklein@google.com361a72f2013-08-19 18:43:34 +0000547
548protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000549
fmalita063675b2015-10-12 10:41:48 -0700550 SkString onShortName() override {
551 return SkString(fDither ? "radial_gradient2" : "radial_gradient2_nodither");
552 }
553
mtklein36352bf2015-03-25 18:17:31 -0700554 SkISize onISize() override { return SkISize::Make(800, 400); }
mtklein@google.com361a72f2013-08-19 18:43:34 +0000555 void drawBG(SkCanvas* canvas) {
556 canvas->drawColor(0xFF000000);
557 }
558
559 // Reproduces the example given in bug 7671058.
mtklein36352bf2015-03-25 18:17:31 -0700560 void onDraw(SkCanvas* canvas) override {
mtklein@google.com361a72f2013-08-19 18:43:34 +0000561 SkPaint paint1, paint2, paint3;
562 paint1.setStyle(SkPaint::kFill_Style);
563 paint2.setStyle(SkPaint::kFill_Style);
564 paint3.setStyle(SkPaint::kFill_Style);
565
566 const SkColor sweep_colors[] =
567 { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 };
568 const SkColor colors1[] = { 0xFFFFFFFF, 0x00000000 };
569 const SkColor colors2[] = { 0xFF000000, 0x00000000 };
570
571 const SkScalar cx = 200, cy = 200, radius = 150;
572 SkPoint center;
573 center.set(cx, cy);
574
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000575 // We can either interpolate endpoints and premultiply each point (default, more precision),
576 // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap).
577 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag };
mtklein@google.com361a72f2013-08-19 18:43:34 +0000578
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000579 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) {
reed1a9b9642016-03-13 14:13:58 -0700580 paint1.setShader(SkGradientShader::MakeSweep(cx, cy, sweep_colors,
581 nullptr, SK_ARRAY_COUNT(sweep_colors),
582 flags[i], nullptr));
583 paint2.setShader(SkGradientShader::MakeRadial(center, radius, colors1,
584 nullptr, SK_ARRAY_COUNT(colors1),
585 SkShader::kClamp_TileMode,
586 flags[i], nullptr));
587 paint3.setShader(SkGradientShader::MakeRadial(center, radius, colors2,
588 nullptr, SK_ARRAY_COUNT(colors2),
589 SkShader::kClamp_TileMode,
590 flags[i], nullptr));
fmalita063675b2015-10-12 10:41:48 -0700591 paint1.setDither(fDither);
fmalita063675b2015-10-12 10:41:48 -0700592 paint2.setDither(fDither);
fmalita063675b2015-10-12 10:41:48 -0700593 paint3.setDither(fDither);
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000594
595 canvas->drawCircle(cx, cy, radius, paint1);
596 canvas->drawCircle(cx, cy, radius, paint3);
597 canvas->drawCircle(cx, cy, radius, paint2);
598
599 canvas->translate(400, 0);
600 }
mtklein@google.com361a72f2013-08-19 18:43:34 +0000601 }
602
603private:
fmalita063675b2015-10-12 10:41:48 -0700604 bool fDither;
605
mtklein@google.com361a72f2013-08-19 18:43:34 +0000606 typedef GM INHERITED;
607};
fmalita063675b2015-10-12 10:41:48 -0700608DEF_GM( return new RadialGradient2GM(true); )
609DEF_GM( return new RadialGradient2GM(false); )
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000610
reed3d9005c2015-04-23 10:30:27 -0700611// Shallow radial (shows banding on raster)
612class RadialGradient3GM : public GM {
fmalita063675b2015-10-12 10:41:48 -0700613public:
614 RadialGradient3GM(bool dither) : fDither(dither) { }
reed@android.com42309d42009-06-22 02:06:35 +0000615
reed3d9005c2015-04-23 10:30:27 -0700616protected:
fmalita063675b2015-10-12 10:41:48 -0700617 SkString onShortName() override {
618 return SkString(fDither ? "radial_gradient3" : "radial_gradient3_nodither");
619 }
reed@android.com42309d42009-06-22 02:06:35 +0000620
reed3d9005c2015-04-23 10:30:27 -0700621 SkISize onISize() override { return SkISize::Make(500, 500); }
reed@google.comac864a92011-06-27 18:11:17 +0000622
reed3d9005c2015-04-23 10:30:27 -0700623 bool runAsBench() const override { return true; }
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000624
reed3d9005c2015-04-23 10:30:27 -0700625 void onOnceBeforeDraw() override {
626 const SkPoint center = { 0, 0 };
627 const SkScalar kRadius = 3000;
628 const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 };
reed1a9b9642016-03-13 14:13:58 -0700629 fShader = SkGradientShader::MakeRadial(center, kRadius, gColors, nullptr, 2,
630 SkShader::kClamp_TileMode);
reed3d9005c2015-04-23 10:30:27 -0700631 }
rileya@google.com5cf2c282012-07-09 14:42:16 +0000632
reed3d9005c2015-04-23 10:30:27 -0700633 void onDraw(SkCanvas* canvas) override {
634 SkPaint paint;
635 paint.setShader(fShader);
fmalita063675b2015-10-12 10:41:48 -0700636 paint.setDither(fDither);
reed3d9005c2015-04-23 10:30:27 -0700637 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
638 }
halcanary9d524f22016-03-29 09:03:52 -0700639
reed3d9005c2015-04-23 10:30:27 -0700640private:
reed1a9b9642016-03-13 14:13:58 -0700641 sk_sp<SkShader> fShader;
fmalita063675b2015-10-12 10:41:48 -0700642 bool fDither;
643
reed3d9005c2015-04-23 10:30:27 -0700644 typedef GM INHERITED;
645};
fmalita063675b2015-10-12 10:41:48 -0700646DEF_GM( return new RadialGradient3GM(true); )
647DEF_GM( return new RadialGradient3GM(false); )
rileya@google.com5cf2c282012-07-09 14:42:16 +0000648
caryclark1864bfa2015-07-30 06:41:39 -0700649class RadialGradient4GM : public GM {
fmalita063675b2015-10-12 10:41:48 -0700650public:
651 RadialGradient4GM(bool dither) : fDither(dither) { }
caryclark1864bfa2015-07-30 06:41:39 -0700652
653protected:
fmalita063675b2015-10-12 10:41:48 -0700654 SkString onShortName() override {
655 return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodither");
656 }
caryclark1864bfa2015-07-30 06:41:39 -0700657
658 SkISize onISize() override { return SkISize::Make(500, 500); }
659
660 void onOnceBeforeDraw() override {
661 const SkPoint center = { 250, 250 };
662 const SkScalar kRadius = 250;
663 const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_ColorWHITE,
664 SK_ColorRED };
665 const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 };
reed1a9b9642016-03-13 14:13:58 -0700666 fShader = SkGradientShader::MakeRadial(center, kRadius, colors, pos,
667 SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode);
caryclark1864bfa2015-07-30 06:41:39 -0700668 }
669
670 void onDraw(SkCanvas* canvas) override {
671 SkPaint paint;
672 paint.setAntiAlias(true);
fmalita063675b2015-10-12 10:41:48 -0700673 paint.setDither(fDither);
caryclark1864bfa2015-07-30 06:41:39 -0700674 paint.setShader(fShader);
675 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
676 }
halcanary9d524f22016-03-29 09:03:52 -0700677
caryclark1864bfa2015-07-30 06:41:39 -0700678private:
reed1a9b9642016-03-13 14:13:58 -0700679 sk_sp<SkShader> fShader;
fmalita063675b2015-10-12 10:41:48 -0700680 bool fDither;
681
caryclark1864bfa2015-07-30 06:41:39 -0700682 typedef GM INHERITED;
683};
fmalita063675b2015-10-12 10:41:48 -0700684DEF_GM( return new RadialGradient4GM(true); )
685DEF_GM( return new RadialGradient4GM(false); )
caryclark1864bfa2015-07-30 06:41:39 -0700686
caryclark159fa572015-07-30 12:35:48 -0700687class LinearGradientGM : public GM {
fmalita063675b2015-10-12 10:41:48 -0700688public:
689 LinearGradientGM(bool dither) : fDither(dither) { }
caryclark159fa572015-07-30 12:35:48 -0700690
691protected:
fmalita063675b2015-10-12 10:41:48 -0700692 SkString onShortName() override {
693 return SkString(fDither ? "linear_gradient" : "linear_gradient_nodither");
694 }
695
caryclark159fa572015-07-30 12:35:48 -0700696 const SkScalar kWidthBump = 30.f;
697 const SkScalar kHeight = 5.f;
698 const SkScalar kMinWidth = 540.f;
699
700 SkISize onISize() override { return SkISize::Make(500, 500); }
701
702 void onOnceBeforeDraw() override {
703 SkPoint pts[2] = { {0, 0}, {0, 0} };
704 const SkColor colors[] = { SK_ColorWHITE, SK_ColorWHITE, 0xFF008200, 0xFF008200,
705 SK_ColorWHITE, SK_ColorWHITE };
706 const SkScalar unitPos[] = { 0, 50, 70, 500, 540 };
707 SkScalar pos[6];
708 pos[5] = 1;
709 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) {
710 pts[1].fX = 500.f + index * kWidthBump;
711 for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) {
712 pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump);
713 }
reed1a9b9642016-03-13 14:13:58 -0700714 fShader[index] = SkGradientShader::MakeLinear(pts, colors, pos,
715 SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode);
caryclark159fa572015-07-30 12:35:48 -0700716 }
717 }
718
719 void onDraw(SkCanvas* canvas) override {
720 SkPaint paint;
721 paint.setAntiAlias(true);
fmalita063675b2015-10-12 10:41:48 -0700722 paint.setDither(fDither);
caryclark159fa572015-07-30 12:35:48 -0700723 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) {
724 paint.setShader(fShader[index]);
725 canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + index * kWidthBump,
726 (index + 1) * kHeight), paint);
727 }
728 }
halcanary9d524f22016-03-29 09:03:52 -0700729
caryclark159fa572015-07-30 12:35:48 -0700730private:
reed1a9b9642016-03-13 14:13:58 -0700731 sk_sp<SkShader> fShader[100];
fmalita063675b2015-10-12 10:41:48 -0700732 bool fDither;
733
caryclark159fa572015-07-30 12:35:48 -0700734 typedef GM INHERITED;
735};
fmalita063675b2015-10-12 10:41:48 -0700736DEF_GM( return new LinearGradientGM(true); )
737DEF_GM( return new LinearGradientGM(false); )
caryclark159fa572015-07-30 12:35:48 -0700738
fmalita8b78bd62015-11-20 13:58:24 -0800739class LinearGradientTinyGM : public GM {
fmalitabc590c02016-02-22 09:12:33 -0800740public:
741 LinearGradientTinyGM(uint32_t flags, const char* suffix = nullptr)
742 : fName("linear_gradient_tiny")
743 , fFlags(flags) {
744 fName.append(suffix);
745 }
746
fmalita8b78bd62015-11-20 13:58:24 -0800747protected:
748 SkString onShortName() override {
fmalitabc590c02016-02-22 09:12:33 -0800749 return fName;
fmalita8b78bd62015-11-20 13:58:24 -0800750 }
751
752 SkISize onISize() override {
753 return SkISize::Make(600, 500);
754 }
755
756 void onDraw(SkCanvas* canvas) override {
757 const SkScalar kRectSize = 100;
758 const unsigned kStopCount = 3;
759 const SkColor colors[kStopCount] = { SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN };
760 const struct {
761 SkPoint pts[2];
762 SkScalar pos[kStopCount];
763 } configs[] = {
reedde3aac82015-11-22 13:00:04 -0800764 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999f, 1 }},
765 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000001f, 1 }},
766 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999999f, 1 }},
767 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000000001f, 1 }},
fmalita8b78bd62015-11-20 13:58:24 -0800768
reedde3aac82015-11-22 13:00:04 -0800769 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999f, 1 }},
770 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000001f, 1 }},
771 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999999f, 1 }},
772 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000000001f, 1 }},
773
774 { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0.5f, 1 }},
775 { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0.5f, 1 }},
776 { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0.5f, 1 }},
777 { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0.5f, 1 }},
fmalita8b78bd62015-11-20 13:58:24 -0800778 };
779
780 SkPaint paint;
781 for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
782 SkAutoCanvasRestore acr(canvas, true);
reed1a9b9642016-03-13 14:13:58 -0700783 paint.setShader(SkGradientShader::MakeLinear(configs[i].pts, colors, configs[i].pos,
784 kStopCount, SkShader::kClamp_TileMode,
785 fFlags, nullptr));
fmalita8b78bd62015-11-20 13:58:24 -0800786 canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f),
787 kRectSize * ((i / 4) * 1.5f + 0.25f));
788
fmalita8b78bd62015-11-20 13:58:24 -0800789 canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint);
790 }
791 }
792
793private:
794 typedef GM INHERITED;
fmalita8b78bd62015-11-20 13:58:24 -0800795
fmalitabc590c02016-02-22 09:12:33 -0800796 SkString fName;
797 uint32_t fFlags;
798};
799DEF_GM( return new LinearGradientTinyGM(0); )
800DEF_GM( return new LinearGradientTinyGM(SkLinearGradient::kForce4fContext_PrivateFlag, "_4f"); )
reed@android.com42309d42009-06-22 02:06:35 +0000801}
reedd4eaa252016-01-22 10:35:26 -0800802
803///////////////////////////////////////////////////////////////////////////////////////////////////
804
805struct GradRun {
806 SkColor fColors[4];
807 SkScalar fPos[4];
808 int fCount;
809};
810
811#define SIZE 121
812
reed1a9b9642016-03-13 14:13:58 -0700813static sk_sp<SkShader> make_linear(const GradRun& run, SkShader::TileMode mode) {
reedd4eaa252016-01-22 10:35:26 -0800814 const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } };
reed1a9b9642016-03-13 14:13:58 -0700815 return SkGradientShader::MakeLinear(pts, run.fColors, run.fPos, run.fCount, mode);
reedd4eaa252016-01-22 10:35:26 -0800816}
817
reed1a9b9642016-03-13 14:13:58 -0700818static sk_sp<SkShader> make_radial(const GradRun& run, SkShader::TileMode mode) {
reedd4eaa252016-01-22 10:35:26 -0800819 const SkScalar half = SIZE * 0.5f;
reed1a9b9642016-03-13 14:13:58 -0700820 return SkGradientShader::MakeRadial({half,half}, half - 10, run.fColors, run.fPos,
821 run.fCount, mode);
reedd4eaa252016-01-22 10:35:26 -0800822}
823
reed1a9b9642016-03-13 14:13:58 -0700824static sk_sp<SkShader> make_conical(const GradRun& run, SkShader::TileMode mode) {
reedd4eaa252016-01-22 10:35:26 -0800825 const SkScalar half = SIZE * 0.5f;
826 const SkPoint center { half, half };
reed1a9b9642016-03-13 14:13:58 -0700827 return SkGradientShader::MakeTwoPointConical(center, 20, center, half - 10,
828 run.fColors, run.fPos, run.fCount, mode);
reedd4eaa252016-01-22 10:35:26 -0800829}
830
reed1a9b9642016-03-13 14:13:58 -0700831static sk_sp<SkShader> make_sweep(const GradRun& run, SkShader::TileMode) {
reedd4eaa252016-01-22 10:35:26 -0800832 const SkScalar half = SIZE * 0.5f;
reed1a9b9642016-03-13 14:13:58 -0700833 return SkGradientShader::MakeSweep(half, half, run.fColors, run.fPos, run.fCount);
reedd4eaa252016-01-22 10:35:26 -0800834}
835
836/*
837 * Exercise duplicate color-stops, at the ends, and in the middle
838 *
839 * At the time of this writing, only Linear correctly deals with duplicates at the ends,
840 * and then only correctly on CPU backend.
841 */
842DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) {
843 const SkColor preColor = 0xFFFF0000; // clamp color before start
844 const SkColor postColor = 0xFF0000FF; // clamp color after end
845 const SkColor color0 = 0xFF000000;
846 const SkColor color1 = 0xFF00FF00;
847 const SkColor badColor = 0xFF3388BB; // should never be seen, fills out fixed-size array
848
849 const GradRun runs[] = {
850 { { color0, color1, badColor, badColor },
851 { 0, 1, -1, -1 },
852 2,
853 },
854 { { preColor, color0, color1, badColor },
855 { 0, 0, 1, -1 },
856 3,
857 },
858 { { color0, color1, postColor, badColor },
859 { 0, 1, 1, -1 },
860 3,
861 },
862 { { preColor, color0, color1, postColor },
863 { 0, 0, 1, 1 },
864 4,
865 },
866 { { color0, color0, color1, color1 },
867 { 0, 0.5f, 0.5f, 1 },
868 4,
869 },
870 };
reed1a9b9642016-03-13 14:13:58 -0700871 sk_sp<SkShader> (*factories[])(const GradRun&, SkShader::TileMode) {
reedd4eaa252016-01-22 10:35:26 -0800872 make_linear, make_radial, make_conical, make_sweep
873 };
874
875 const SkRect rect = SkRect::MakeWH(SIZE, SIZE);
876 const SkScalar dx = SIZE + 20;
877 const SkScalar dy = SIZE + 20;
878 const SkShader::TileMode mode = SkShader::kClamp_TileMode;
879
880 SkPaint paint;
881 canvas->translate(10, 10 - dy);
882 for (auto factory : factories) {
883 canvas->translate(0, dy);
884 SkAutoCanvasRestore acr(canvas, true);
885 for (const auto& run : runs) {
reed1a9b9642016-03-13 14:13:58 -0700886 paint.setShader(factory(run, mode));
reedd4eaa252016-01-22 10:35:26 -0800887 canvas->drawRect(rect, paint);
888 canvas->translate(dx, 0);
889 }
890 }
891}
fmalitabc590c02016-02-22 09:12:33 -0800892
893static void draw_many_stops(SkCanvas* canvas, uint32_t flags) {
894 const unsigned kStopCount = 200;
895 const SkPoint pts[] = { {50, 50}, {450, 465}};
896
897 SkColor colors[kStopCount];
898 for (unsigned i = 0; i < kStopCount; i++) {
899 switch (i % 5) {
900 case 0: colors[i] = SK_ColorRED; break;
901 case 1: colors[i] = SK_ColorGREEN; break;
902 case 2: colors[i] = SK_ColorGREEN; break;
903 case 3: colors[i] = SK_ColorBLUE; break;
904 case 4: colors[i] = SK_ColorRED; break;
905 }
906 }
907
reed9283d202016-03-13 13:01:57 -0700908 SkPaint p;
reed1a9b9642016-03-13 14:13:58 -0700909 p.setShader(SkGradientShader::MakeLinear(
910 pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, flags, nullptr));
reed9283d202016-03-13 13:01:57 -0700911
fmalitabc590c02016-02-22 09:12:33 -0800912 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
913}
914
915DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) {
916 draw_many_stops(canvas, 0);
917}
918
919DEF_SIMPLE_GM(gradient_many_stops_4f, canvas, 500, 500) {
920 draw_many_stops(canvas, SkLinearGradient::kForce4fContext_PrivateFlag);
921}
fmalitaafac5812016-11-01 13:41:34 -0700922
923static void draw_subpixel_gradient(SkCanvas* canvas, uint32_t flags) {
924 const SkPoint pts[] = { {50, 50}, {50.1f, 50.1f}};
925 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
926 SkPaint p;
927 p.setShader(SkGradientShader::MakeLinear(
928 pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kRepeat_TileMode, flags, nullptr));
929 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
930}
931
932DEF_SIMPLE_GM(gradient_subpixel, canvas, 500, 500) {
933 draw_subpixel_gradient(canvas, 0);
934}
935
936DEF_SIMPLE_GM(gradient_subpixel_4f, canvas, 500, 500) {
937 draw_subpixel_gradient(canvas, SkLinearGradient::kForce4fContext_PrivateFlag);
938}