blob: 97f8fdaca8b5813b06ae010fddf4dda8f1113fa4 [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
28static const GradData gGradData[] = {
29 { 2, gColors, NULL },
30 { 2, gColors, gPos0 },
31 { 2, gColors, gPos1 },
32 { 5, gColors, NULL },
33 { 5, gColors, gPos2 }
34};
35
36static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data,
37 SkShader::TileMode tm, SkUnitMapper* mapper) {
38 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos,
39 data.fCount, tm, mapper);
40}
reed@google.comf3c1cc92010-12-23 16:45:33 +000041
reed@android.com42309d42009-06-22 02:06:35 +000042static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data,
43 SkShader::TileMode tm, SkUnitMapper* mapper) {
44 SkPoint center;
45 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
46 SkScalarAve(pts[0].fY, pts[1].fY));
47 return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
48 data.fPos, data.fCount, tm, mapper);
49}
50
51static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data,
sugoi@google.com0f0d9b72013-02-27 15:41:12 +000052 SkShader::TileMode, SkUnitMapper* mapper) {
reed@android.com42309d42009-06-22 02:06:35 +000053 SkPoint center;
54 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
55 SkScalarAve(pts[0].fY, pts[1].fY));
56 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors,
57 data.fPos, data.fCount, mapper);
58}
59
reed@google.comf3c1cc92010-12-23 16:45:33 +000060static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data,
61 SkShader::TileMode tm, SkUnitMapper* mapper) {
62 SkPoint center0, center1;
63 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
64 SkScalarAve(pts[0].fY, pts[1].fY));
65 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
66 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
67 return SkGradientShader::CreateTwoPointRadial(
68 center1, (pts[1].fX - pts[0].fX) / 7,
69 center0, (pts[1].fX - pts[0].fX) / 2,
70 data.fColors, data.fPos, data.fCount, tm, mapper);
71}
72
rileya@google.com5cf2c282012-07-09 14:42:16 +000073static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data,
74 SkShader::TileMode tm, SkUnitMapper* mapper) {
75 SkPoint center0, center1;
76 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10);
77 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3);
78 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
79 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
rmistry@google.comd6176b02012-08-23 18:14:13 +000080 return SkGradientShader::CreateTwoPointConical(center1, radius1,
81 center0, radius0,
82 data.fColors, data.fPos,
rileya@google.com5cf2c282012-07-09 14:42:16 +000083 data.fCount, tm, mapper);
84}
85
reed@android.com42309d42009-06-22 02:06:35 +000086typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data,
reed@google.comf3c1cc92010-12-23 16:45:33 +000087 SkShader::TileMode tm, SkUnitMapper* mapper);
reed@android.com42309d42009-06-22 02:06:35 +000088static const GradMaker gGradMakers[] = {
rileya@google.com5cf2c282012-07-09 14:42:16 +000089 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
reed@android.com42309d42009-06-22 02:06:35 +000090};
91
92///////////////////////////////////////////////////////////////////////////////
93
94class GradientsGM : public GM {
95public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000096 GradientsGM() {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000097 this->setBGColor(0xFFDDDDDD);
98 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
reed@android.com42309d42009-06-22 02:06:35 +0000100protected:
101 SkString onShortName() {
102 return SkString("gradients");
103 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
rileya@google.com5cf2c282012-07-09 14:42:16 +0000105 virtual SkISize onISize() { return make_isize(640, 615); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106
reed@android.com42309d42009-06-22 02:06:35 +0000107 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@android.com42309d42009-06-22 02:06:35 +0000109 SkPoint pts[2] = {
110 { 0, 0 },
111 { SkIntToScalar(100), SkIntToScalar(100) }
112 };
113 SkShader::TileMode tm = SkShader::kClamp_TileMode;
114 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
115 SkPaint paint;
116 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
reed@android.com42309d42009-06-22 02:06:35 +0000118 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
119 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
120 canvas->save();
121 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
122 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
123 paint.setShader(shader);
124 canvas->drawRect(r, paint);
125 shader->unref();
126 canvas->translate(0, SkIntToScalar(120));
127 }
128 canvas->restore();
129 canvas->translate(SkIntToScalar(120), 0);
130 }
131 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@android.com42309d42009-06-22 02:06:35 +0000133private:
134 typedef GM INHERITED;
135};
136
rileya@google.com5cf2c282012-07-09 14:42:16 +0000137// Based on the original gradient slide, but with perspective applied to the
138// gradient shaders' local matrices
139class GradientsLocalPerspectiveGM : public GM {
140public:
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141 GradientsLocalPerspectiveGM() {
rileya@google.com5cf2c282012-07-09 14:42:16 +0000142 this->setBGColor(0xFFDDDDDD);
143 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144
rileya@google.com5cf2c282012-07-09 14:42:16 +0000145protected:
146 SkString onShortName() {
147 return SkString("gradients_local_perspective");
148 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149
rileya@google.com5cf2c282012-07-09 14:42:16 +0000150 virtual SkISize onISize() { return make_isize(640, 615); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151
rileya@google.com5cf2c282012-07-09 14:42:16 +0000152 virtual void onDraw(SkCanvas* canvas) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000153
rileya@google.com5cf2c282012-07-09 14:42:16 +0000154 SkPoint pts[2] = {
155 { 0, 0 },
156 { SkIntToScalar(100), SkIntToScalar(100) }
157 };
158 SkShader::TileMode tm = SkShader::kClamp_TileMode;
159 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
160 SkPaint paint;
161 paint.setAntiAlias(true);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000162
rileya@google.com5cf2c282012-07-09 14:42:16 +0000163 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
164 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
165 canvas->save();
166 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
167 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000168
rileya@google.com5cf2c282012-07-09 14:42:16 +0000169 // apply an increasing y perspective as we move to the right
170 SkMatrix perspective;
171 perspective.setIdentity();
borenet@google.comb7961192012-08-20 18:58:26 +0000172 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1),
rileya@google.com5cf2c282012-07-09 14:42:16 +0000173 SkIntToScalar(500)));
borenet@google.comb7961192012-08-20 18:58:26 +0000174 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1),
rileya@google.com5cf2c282012-07-09 14:42:16 +0000175 SkIntToScalar(10)));
176 shader->setLocalMatrix(perspective);
177
178 paint.setShader(shader);
179 canvas->drawRect(r, paint);
180 shader->unref();
181 canvas->translate(0, SkIntToScalar(120));
182 }
183 canvas->restore();
184 canvas->translate(SkIntToScalar(120), 0);
185 }
186 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
rileya@google.com5cf2c282012-07-09 14:42:16 +0000188private:
189 typedef GM INHERITED;
190};
191
192// Based on the original gradient slide, but with perspective applied to
193// the view matrix
194class GradientsViewPerspectiveGM : public GradientsGM {
195protected:
196 SkString onShortName() {
197 return SkString("gradients_view_perspective");
198 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
rileya@google.com5cf2c282012-07-09 14:42:16 +0000200 virtual SkISize onISize() { return make_isize(640, 400); }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000201
rileya@google.com5cf2c282012-07-09 14:42:16 +0000202 virtual void onDraw(SkCanvas* canvas) {
203 SkMatrix perspective;
204 perspective.setIdentity();
205 perspective.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
206 perspective.setSkewX(SkScalarDiv(SkIntToScalar(8), SkIntToScalar(25)));
scroggo@google.com837d31a2012-08-15 18:42:45 +0000207 canvas->concat(perspective);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000208 INHERITED::onDraw(canvas);
209 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210
rileya@google.com5cf2c282012-07-09 14:42:16 +0000211private:
212 typedef GradientsGM INHERITED;
213};
214
reed@google.comac864a92011-06-27 18:11:17 +0000215/*
216 Inspired by this <canvas> javascript, where we need to detect that we are not
217 solving a quadratic equation, but must instead solve a linear (since our X^2
218 coefficient is 0)
219
220 ctx.fillStyle = '#f00';
221 ctx.fillRect(0, 0, 100, 50);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000222
reed@google.comac864a92011-06-27 18:11:17 +0000223 var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150);
224 g.addColorStop(0, '#f00');
225 g.addColorStop(0.01, '#0f0');
226 g.addColorStop(0.99, '#0f0');
227 g.addColorStop(1, '#f00');
228 ctx.fillStyle = g;
229 ctx.fillRect(0, 0, 100, 50);
230 */
231class GradientsDegenrate2PointGM : public GM {
232public:
233 GradientsDegenrate2PointGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000234
reed@google.comac864a92011-06-27 18:11:17 +0000235protected:
236 SkString onShortName() {
237 return SkString("gradients_degenerate_2pt");
238 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000239
240 virtual SkISize onISize() { return make_isize(320, 320); }
241
reed@google.comac864a92011-06-27 18:11:17 +0000242 void drawBG(SkCanvas* canvas) {
243 canvas->drawColor(SK_ColorBLUE);
244 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000245
reed@google.comac864a92011-06-27 18:11:17 +0000246 virtual void onDraw(SkCanvas* canvas) {
247 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
reed@google.comac864a92011-06-27 18:11:17 +0000249 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorRED };
250 SkScalar pos[] = { 0, SkFloatToScalar(0.01f), SkFloatToScalar(0.99f), SK_Scalar1 };
epoger@google.com59f3abf2011-07-21 15:50:33 +0000251 SkPoint c0;
252 c0.iset(-80, 25);
253 SkScalar r0 = SkIntToScalar(70);
254 SkPoint c1;
255 c1.iset(0, 25);
256 SkScalar r1 = SkIntToScalar(150);
reed@google.comac864a92011-06-27 18:11:17 +0000257 SkShader* s = SkGradientShader::CreateTwoPointRadial(c0, r0, c1, r1, colors,
258 pos, SK_ARRAY_COUNT(pos),
259 SkShader::kClamp_TileMode);
260 SkPaint paint;
261 paint.setShader(s)->unref();
262 canvas->drawPaint(paint);
263 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264
reed@google.comac864a92011-06-27 18:11:17 +0000265private:
266 typedef GM INHERITED;
267};
268
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000269/// Tests correctness of *optimized* codepaths in gradients.
270
271class ClampedGradientsGM : public GM {
272public:
273 ClampedGradientsGM() {}
274
275protected:
276 SkString onShortName() { return SkString("clamped_gradients"); }
277
278 virtual SkISize onISize() { return make_isize(640, 510); }
279
280 void drawBG(SkCanvas* canvas) {
281 canvas->drawColor(0xFFDDDDDD);
282 }
283
284 virtual void onDraw(SkCanvas* canvas) {
285 this->drawBG(canvas);
286
287 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) };
288 SkPaint paint;
289 paint.setAntiAlias(true);
290
291 SkPoint center;
292 center.iset(0, 300);
293 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
294 SkShader* shader = SkGradientShader::CreateRadial(
295 SkPoint(center),
bungeman@google.com01744a42011-10-06 19:37:39 +0000296 SkIntToScalar(200), gColors, NULL, 5,
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000297 SkShader::kClamp_TileMode, NULL);
298 paint.setShader(shader);
299 canvas->drawRect(r, paint);
300 shader->unref();
301 }
302
303private:
304 typedef GM INHERITED;
305};
306
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000307/// Checks quality of large radial gradients, which may display
308/// some banding.
309
310class RadialGradientGM : public GM {
311public:
312 RadialGradientGM() {}
313
314protected:
315 SkString onShortName() { return SkString("radial_gradient"); }
reed@google.combb0948f2012-01-31 14:44:13 +0000316 virtual SkISize onISize() { return make_isize(1280, 1280); }
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000317 void drawBG(SkCanvas* canvas) {
318 canvas->drawColor(0xFF000000);
319 }
320 virtual void onDraw(SkCanvas* canvas) {
reed@google.combb0948f2012-01-31 14:44:13 +0000321 const SkISize dim = this->getISize();
322
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000323 this->drawBG(canvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000324
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000325 SkPaint paint;
326 paint.setDither(true);
327 SkPoint center;
reed@google.combb0948f2012-01-31 14:44:13 +0000328 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2);
329 SkScalar radius = SkIntToScalar(dim.width())/2;
330 const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 };
robertphillips@google.com09042b82012-04-06 20:01:46 +0000331 const SkScalar pos[] = { SkFloatToScalar(0.0f),
332 SkFloatToScalar(0.35f),
333 SkFloatToScalar(1.0f) };
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000334 SkShader* shader =
335 SkGradientShader::CreateRadial(center, radius, colors,
reed@google.combb0948f2012-01-31 14:44:13 +0000336 pos, SK_ARRAY_COUNT(pos),
337 SkShader::kClamp_TileMode);
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000338 paint.setShader(shader)->unref();
reed@google.combb0948f2012-01-31 14:44:13 +0000339 SkRect r = {
340 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height())
341 };
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000342 canvas->drawRect(r, paint);
343 }
344private:
345 typedef GM INHERITED;
346};
347
348
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000349
reed@android.com42309d42009-06-22 02:06:35 +0000350///////////////////////////////////////////////////////////////////////////////
351
352static GM* MyFactory(void*) { return new GradientsGM; }
353static GMRegistry reg(MyFactory);
354
reed@google.comac864a92011-06-27 18:11:17 +0000355static GM* MyFactory2(void*) { return new GradientsDegenrate2PointGM; }
356static GMRegistry reg2(MyFactory2);
357
tomhudson@google.com5ea050f2011-09-26 15:03:55 +0000358static GM* MyFactory3(void*) { return new ClampedGradientsGM; }
359static GMRegistry reg3(MyFactory3);
360
tomhudson@google.comb18e58c2012-01-30 20:00:13 +0000361static GM* MyFactory4(void*) { return new RadialGradientGM; }
362static GMRegistry reg4(MyFactory4);
rileya@google.com5cf2c282012-07-09 14:42:16 +0000363
364static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; }
365static GMRegistry reg5(MyFactory5);
366
367static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; }
368static GMRegistry reg6(MyFactory6);
reed@android.com42309d42009-06-22 02:06:35 +0000369}