blob: fd02ceeae90b3e4a8e77da4db43e428b8af54d9b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com42309d42009-06-22 02:06:35 +00008#include "gm.h"
9#include "SkGradientShader.h"
10
11namespace skiagm {
12
13struct GradData {
14 int fCount;
15 const SkColor* fColors;
16 const SkScalar* fPos;
17};
18
19static const SkColor gColors[] = {
20 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
21};
22static const SkScalar gPos0[] = { 0, SK_Scalar1 };
23static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
24static const SkScalar gPos2[] = {
25 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
26};
27
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +000028static const SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f};
29static const SkColor gColorClamp[] = {
30 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE
31};
32
reed@android.com42309d42009-06-22 02:06:35 +000033static const GradData gGradData[] = {
34 { 2, gColors, NULL },
35 { 2, gColors, gPos0 },
36 { 2, gColors, gPos1 },
37 { 5, gColors, NULL },
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +000038 { 5, gColors, gPos2 },
39 { 4, gColorClamp, gPosClamp }
reed@android.com42309d42009-06-22 02:06:35 +000040};
41
42static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000043 SkShader::TileMode tm, const SkMatrix& localMatrix) {
reed@android.com42309d42009-06-22 02:06:35 +000044 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000045 data.fCount, tm, 0, &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000046}
reed@google.comf3c1cc92010-12-23 16:45:33 +000047
reed@android.com42309d42009-06-22 02:06:35 +000048static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000049 SkShader::TileMode tm, const SkMatrix& localMatrix) {
reed@android.com42309d42009-06-22 02:06:35 +000050 SkPoint center;
51 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
52 SkScalarAve(pts[0].fY, pts[1].fY));
53 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000054 data.fPos, data.fCount, tm, 0, &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000055}
56
57static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000058 SkShader::TileMode, const SkMatrix& localMatrix) {
reed@android.com42309d42009-06-22 02:06:35 +000059 SkPoint center;
60 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
61 SkScalarAve(pts[0].fY, pts[1].fY));
62 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000063 data.fPos, data.fCount, 0, &localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000064}
65
reed@google.comf3c1cc92010-12-23 16:45:33 +000066static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000067 SkShader::TileMode tm, const SkMatrix& localMatrix) {
reed@google.comf3c1cc92010-12-23 16:45:33 +000068 SkPoint center0, center1;
69 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
70 SkScalarAve(pts[0].fY, pts[1].fY));
71 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
72 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
73 return SkGradientShader::CreateTwoPointRadial(
74 center1, (pts[1].fX - pts[0].fX) / 7,
75 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000076 data.fColors, data.fPos, data.fCount, tm,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000077 0, &localMatrix);
reed@google.comf3c1cc92010-12-23 16:45:33 +000078}
79
rileya@google.com5cf2c282012-07-09 14:42:16 +000080static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000081 SkShader::TileMode tm, const SkMatrix& localMatrix) {
rileya@google.com5cf2c282012-07-09 14:42:16 +000082 SkPoint center0, center1;
83 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10);
84 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3);
85 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
86 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 return SkGradientShader::CreateTwoPointConical(center1, radius1,
88 center0, radius0,
89 data.fColors, data.fPos,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000090 data.fCount, tm, 0, &localMatrix);
rileya@google.com5cf2c282012-07-09 14:42:16 +000091}
92
reed@android.com42309d42009-06-22 02:06:35 +000093typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000094 SkShader::TileMode tm, const SkMatrix& localMatrix);
reed@android.com42309d42009-06-22 02:06:35 +000095static const GradMaker gGradMakers[] = {
rileya@google.com5cf2c282012-07-09 14:42:16 +000096 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
reed@android.com42309d42009-06-22 02:06:35 +000097};
98
99///////////////////////////////////////////////////////////////////////////////
100
101class GradientsGM : public GM {
102public:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 GradientsGM() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000104 this->setBGColor(0xFFDDDDDD);
105 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106
reed@android.com42309d42009-06-22 02:06:35 +0000107protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000108 virtual uint32_t onGetFlags() const SK_OVERRIDE {
109 return kSkipTiled_Flag;
110 }
111
reed@android.com42309d42009-06-22 02:06:35 +0000112 SkString onShortName() {
113 return SkString("gradients");
114 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
edisonn@google.com1da3a802013-09-19 17:55:49 +0000116 virtual SkISize onISize() { return SkISize::Make(840, 815); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
reed@android.com42309d42009-06-22 02:06:35 +0000118 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
reed@android.com42309d42009-06-22 02:06:35 +0000120 SkPoint pts[2] = {
121 { 0, 0 },
122 { SkIntToScalar(100), SkIntToScalar(100) }
123 };
124 SkShader::TileMode tm = SkShader::kClamp_TileMode;
125 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
126 SkPaint paint;
127 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
reed@android.com42309d42009-06-22 02:06:35 +0000129 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
130 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
131 canvas->save();
132 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000133 SkMatrix scale = SkMatrix::I();
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000134
135 if (i == 5) { // if the clamp case
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000136 scale.setScale(0.5f, 0.5f);
137 scale.postTranslate(25.f, 25.f);
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000138 }
skia.committer@gmail.comd55e3572013-08-08 07:01:20 +0000139
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000140 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000141
reed@android.com42309d42009-06-22 02:06:35 +0000142 paint.setShader(shader);
143 canvas->drawRect(r, paint);
144 shader->unref();
145 canvas->translate(0, SkIntToScalar(120));
146 }
147 canvas->restore();
148 canvas->translate(SkIntToScalar(120), 0);
149 }
150 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151
reed@android.com42309d42009-06-22 02:06:35 +0000152private:
153 typedef GM INHERITED;
154};
155
rileya@google.com5cf2c282012-07-09 14:42:16 +0000156// Based on the original gradient slide, but with perspective applied to the
157// gradient shaders' local matrices
158class GradientsLocalPerspectiveGM : public GM {
159public:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160 GradientsLocalPerspectiveGM() {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000161 this->setBGColor(0xFFDDDDDD);
162 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000163
rileya@google.com5cf2c282012-07-09 14:42:16 +0000164protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000165 virtual uint32_t onGetFlags() const SK_OVERRIDE {
166 return kSkipTiled_Flag;
167 }
168
rileya@google.com5cf2c282012-07-09 14:42:16 +0000169 SkString onShortName() {
170 return SkString("gradients_local_perspective");
171 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000172
edisonn@google.com1da3a802013-09-19 17:55:49 +0000173 virtual SkISize onISize() { return SkISize::Make(840, 815); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174
rileya@google.com5cf2c282012-07-09 14:42:16 +0000175 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000176
rileya@google.com5cf2c282012-07-09 14:42:16 +0000177 SkPoint pts[2] = {
178 { 0, 0 },
179 { SkIntToScalar(100), SkIntToScalar(100) }
180 };
181 SkShader::TileMode tm = SkShader::kClamp_TileMode;
182 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
183 SkPaint paint;
184 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000185
rileya@google.com5cf2c282012-07-09 14:42:16 +0000186 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
187 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
188 canvas->save();
189 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000190 // apply an increasing y perspective as we move to the right
191 SkMatrix perspective;
192 perspective.setIdentity();
borenet@google.comb7961192012-08-20 18:58:26 +0000193 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1),
rileya@google.com5cf2c282012-07-09 14:42:16 +0000194 SkIntToScalar(500)));
borenet@google.comb7961192012-08-20 18:58:26 +0000195 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1),
rileya@google.com5cf2c282012-07-09 14:42:16 +0000196 SkIntToScalar(10)));
commit-bot@chromium.org8ba1ad32013-08-07 15:22:13 +0000197
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000198 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspective);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000199
200 paint.setShader(shader);
201 canvas->drawRect(r, paint);
202 shader->unref();
203 canvas->translate(0, SkIntToScalar(120));
204 }
205 canvas->restore();
206 canvas->translate(SkIntToScalar(120), 0);
207 }
208 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000209
rileya@google.com5cf2c282012-07-09 14:42:16 +0000210private:
211 typedef GM INHERITED;
212};
213
214// Based on the original gradient slide, but with perspective applied to
215// the view matrix
216class GradientsViewPerspectiveGM : public GradientsGM {
217protected:
218 SkString onShortName() {
219 return SkString("gradients_view_perspective");
220 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
edisonn@google.com1da3a802013-09-19 17:55:49 +0000222 virtual SkISize onISize() { return SkISize::Make(840, 500); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000223
rileya@google.com5cf2c282012-07-09 14:42:16 +0000224 virtual void onDraw(SkCanvas* canvas) {
225 SkMatrix perspective;
226 perspective.setIdentity();
227 perspective.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
228 perspective.setSkewX(SkScalarDiv(SkIntToScalar(8), SkIntToScalar(25)));
scroggo@google.com837d31a2012-08-15 18:42:45 +0000229 canvas->concat(perspective);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000230 INHERITED::onDraw(canvas);
231 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232
rileya@google.com5cf2c282012-07-09 14:42:16 +0000233private:
234 typedef GradientsGM INHERITED;
235};
236
reed@google.comac864a92011-06-27 18:11:17 +0000237/*
238 Inspired by this <canvas> javascript, where we need to detect that we are not
239 solving a quadratic equation, but must instead solve a linear (since our X^2
240 coefficient is 0)
241
242 ctx.fillStyle = '#f00';
243 ctx.fillRect(0, 0, 100, 50);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000244
reed@google.comac864a92011-06-27 18:11:17 +0000245 var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150);
246 g.addColorStop(0, '#f00');
247 g.addColorStop(0.01, '#0f0');
248 g.addColorStop(0.99, '#0f0');
249 g.addColorStop(1, '#f00');
250 ctx.fillStyle = g;
251 ctx.fillRect(0, 0, 100, 50);
252 */
253class GradientsDegenrate2PointGM : public GM {
254public:
255 GradientsDegenrate2PointGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000256
reed@google.comac864a92011-06-27 18:11:17 +0000257protected:
258 SkString onShortName() {
259 return SkString("gradients_degenerate_2pt");
260 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000261
edisonn@google.com1da3a802013-09-19 17:55:49 +0000262 virtual SkISize onISize() { return SkISize::Make(320, 320); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
reed@google.comac864a92011-06-27 18:11:17 +0000264 void drawBG(SkCanvas* canvas) {
265 canvas->drawColor(SK_ColorBLUE);
266 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000267
reed@google.comac864a92011-06-27 18:11:17 +0000268 virtual void onDraw(SkCanvas* canvas) {
269 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000270
reed@google.comac864a92011-06-27 18:11:17 +0000271 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000272 SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 };
epoger@google.com59f3abf2011-07-21 15:50:33 +0000273 SkPoint c0;
274 c0.iset(-80, 25);
275 SkScalar r0 = SkIntToScalar(70);
276 SkPoint c1;
277 c1.iset(0, 25);
278 SkScalar r1 = SkIntToScalar(150);
reed@google.comac864a92011-06-27 18:11:17 +0000279 SkShader* s = SkGradientShader::CreateTwoPointRadial(c0, r0, c1, r1, colors,
280 pos, SK_ARRAY_COUNT(pos),
281 SkShader::kClamp_TileMode);
282 SkPaint paint;
283 paint.setShader(s)->unref();
284 canvas->drawPaint(paint);
285 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000286
reed@google.comac864a92011-06-27 18:11:17 +0000287private:
288 typedef GM INHERITED;
289};
290
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000291/// Tests correctness of *optimized* codepaths in gradients.
292
293class ClampedGradientsGM : public GM {
294public:
295 ClampedGradientsGM() {}
296
297protected:
298 SkString onShortName() { return SkString("clamped_gradients"); }
299
edisonn@google.com1da3a802013-09-19 17:55:49 +0000300 virtual SkISize onISize() { return SkISize::Make(640, 510); }
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000301
302 void drawBG(SkCanvas* canvas) {
303 canvas->drawColor(0xFFDDDDDD);
304 }
305
306 virtual void onDraw(SkCanvas* canvas) {
307 this->drawBG(canvas);
308
309 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) };
310 SkPaint paint;
311 paint.setAntiAlias(true);
312
313 SkPoint center;
314 center.iset(0, 300);
315 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
316 SkShader* shader = SkGradientShader::CreateRadial(
317 SkPoint(center),
bungeman@google.com01744a42011-10-06 19:37:39 +0000318 SkIntToScalar(200), gColors, NULL, 5,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000319 SkShader::kClamp_TileMode);
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000320 paint.setShader(shader);
321 canvas->drawRect(r, paint);
322 shader->unref();
323 }
324
325private:
326 typedef GM INHERITED;
327};
328
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000329/// Checks quality of large radial gradients, which may display
330/// some banding.
331
332class RadialGradientGM : public GM {
333public:
334 RadialGradientGM() {}
335
336protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000337 virtual uint32_t onGetFlags() const SK_OVERRIDE {
338 return kSkipTiled_Flag;
339 }
340
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000341 SkString onShortName() { return SkString("radial_gradient"); }
edisonn@google.com1da3a802013-09-19 17:55:49 +0000342 virtual SkISize onISize() { return SkISize::Make(1280, 1280); }
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000343 void drawBG(SkCanvas* canvas) {
344 canvas->drawColor(0xFF000000);
345 }
346 virtual void onDraw(SkCanvas* canvas) {
reed@google.combb0948f2012-01-31 14:44:13 +0000347 const SkISize dim = this->getISize();
348
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000349 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000351 SkPaint paint;
352 paint.setDither(true);
353 SkPoint center;
reed@google.combb0948f2012-01-31 14:44:13 +0000354 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2);
355 SkScalar radius = SkIntToScalar(dim.width())/2;
356 const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 };
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000357 const SkScalar pos[] = { 0.0f,
358 0.35f,
359 1.0f };
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000360 SkShader* shader =
361 SkGradientShader::CreateRadial(center, radius, colors,
reed@google.combb0948f2012-01-31 14:44:13 +0000362 pos, SK_ARRAY_COUNT(pos),
363 SkShader::kClamp_TileMode);
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000364 paint.setShader(shader)->unref();
reed@google.combb0948f2012-01-31 14:44:13 +0000365 SkRect r = {
366 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height())
367 };
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000368 canvas->drawRect(r, paint);
369 }
370private:
371 typedef GM INHERITED;
372};
373
374
mtklein@google.com361a72f2013-08-19 18:43:34 +0000375class RadialGradient2GM : public GM {
376public:
377 RadialGradient2GM() {}
378
379protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000380 virtual uint32_t onGetFlags() const SK_OVERRIDE {
381 return kSkipTiled_Flag;
382 }
383
mtklein@google.com361a72f2013-08-19 18:43:34 +0000384 SkString onShortName() { return SkString("radial_gradient2"); }
edisonn@google.com1da3a802013-09-19 17:55:49 +0000385 virtual SkISize onISize() { return SkISize::Make(800, 400); }
mtklein@google.com361a72f2013-08-19 18:43:34 +0000386 void drawBG(SkCanvas* canvas) {
387 canvas->drawColor(0xFF000000);
388 }
389
390 // Reproduces the example given in bug 7671058.
391 virtual void onDraw(SkCanvas* canvas) {
392 SkPaint paint1, paint2, paint3;
393 paint1.setStyle(SkPaint::kFill_Style);
394 paint2.setStyle(SkPaint::kFill_Style);
395 paint3.setStyle(SkPaint::kFill_Style);
396
397 const SkColor sweep_colors[] =
398 { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 };
399 const SkColor colors1[] = { 0xFFFFFFFF, 0x00000000 };
400 const SkColor colors2[] = { 0xFF000000, 0x00000000 };
401
402 const SkScalar cx = 200, cy = 200, radius = 150;
403 SkPoint center;
404 center.set(cx, cy);
405
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000406 // We can either interpolate endpoints and premultiply each point (default, more precision),
407 // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap).
408 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPremul_Flag };
mtklein@google.com361a72f2013-08-19 18:43:34 +0000409
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000410 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) {
411 SkAutoTUnref<SkShader> sweep(
412 SkGradientShader::CreateSweep(cx, cy, sweep_colors,
413 NULL, SK_ARRAY_COUNT(sweep_colors),
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000414 flags[i], NULL));
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000415 SkAutoTUnref<SkShader> radial1(
416 SkGradientShader::CreateRadial(center, radius, colors1,
417 NULL, SK_ARRAY_COUNT(colors1),
418 SkShader::kClamp_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000419 flags[i], NULL));
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000420 SkAutoTUnref<SkShader> radial2(
421 SkGradientShader::CreateRadial(center, radius, colors2,
422 NULL, SK_ARRAY_COUNT(colors2),
423 SkShader::kClamp_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000424 flags[i], NULL));
mtklein@google.com3ef7eea2013-09-16 13:02:52 +0000425 paint1.setShader(sweep);
426 paint2.setShader(radial1);
427 paint3.setShader(radial2);
428
429 canvas->drawCircle(cx, cy, radius, paint1);
430 canvas->drawCircle(cx, cy, radius, paint3);
431 canvas->drawCircle(cx, cy, radius, paint2);
432
433 canvas->translate(400, 0);
434 }
mtklein@google.com361a72f2013-08-19 18:43:34 +0000435 }
436
437private:
438 typedef GM INHERITED;
439};
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000440
reed@android.com42309d42009-06-22 02:06:35 +0000441///////////////////////////////////////////////////////////////////////////////
442
443static GM* MyFactory(void*) { return new GradientsGM; }
444static GMRegistry reg(MyFactory);
445
reed@google.comac864a92011-06-27 18:11:17 +0000446static GM* MyFactory2(void*) { return new GradientsDegenrate2PointGM; }
447static GMRegistry reg2(MyFactory2);
448
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000449static GM* MyFactory3(void*) { return new ClampedGradientsGM; }
450static GMRegistry reg3(MyFactory3);
451
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000452static GM* MyFactory4(void*) { return new RadialGradientGM; }
453static GMRegistry reg4(MyFactory4);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000454
455static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; }
456static GMRegistry reg5(MyFactory5);
457
458static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; }
459static GMRegistry reg6(MyFactory6);
mtklein@google.com361a72f2013-08-19 18:43:34 +0000460
461static GM* MyFactory7(void*) { return new RadialGradient2GM; }
462static GMRegistry reg7(MyFactory7);
reed@android.com42309d42009-06-22 02:06:35 +0000463}