Debian Codegraph @

gm/gradients.cpp

raw
1 /*
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 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPicture.h"
17 #include "include/core/SkPictureRecorder.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradient.h"
28 #include "include/private/base/SkAssert.h"
29 #include "include/private/base/SkFloatingPoint.h"
30 #include "src/core/SkColorPriv.h"
31 #include "tools/ToolUtils.h"
32 #include "tools/fonts/FontToolUtils.h"
33
34 #include <initializer_list>
35 #include <math.h>
36
37 namespace {
38
39 struct GradData {
40 SkSpan<const SkColor4f> fColors;
41 SkSpan<const float> fPos;
42 };
43
44 constexpr SkColor4f gColors[] ={
45 SkColors::kRed,
46 SkColors::kGreen,
47 SkColors::kBlue,
48 SkColors::kWhite,
49 SkColors::kBlack,
50 };
51 constexpr SkScalar gPos0[] = { 0, SK_Scalar1 };
52 constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
53 constexpr SkScalar gPos2[] = {
54 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
55 };
56
57 constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f};
58 constexpr SkColor4f gColor4fClamp[] = {
59 SkColors::kRed,
60 SkColors::kGreen,
61 SkColors::kGreen,
62 SkColors::kBlue,
63 };
64 constexpr GradData gGradData[] = {
65 { {gColors, 2}, {} },
66 { {gColors, 2}, gPos0 },
67 { {gColors, 2}, gPos1 },
68 { {gColors, 5}, {} },
69 { {gColors, 5}, gPos2 },
70 { gColor4fClamp, gPosClamp }
71 };
72
73 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
74 SkTileMode tm, const SkMatrix& localMatrix) {
75 return SkShaders::LinearGradient(pts, {{data.fColors, data.fPos, tm}, {}}, &localMatrix);
76 }
77
78 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
79 SkTileMode tm, const SkMatrix& localMatrix) {
80 SkPoint center;
81 center.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
82 sk_float_midpoint(pts[0].fY, pts[1].fY));
83 return SkShaders::RadialGradient(center, center.fX, {{data.fColors, data.fPos, tm}, {}},
84 &localMatrix);
85 }
86
87 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
88 SkTileMode tm, const SkMatrix& localMatrix) {
89 SkPoint center;
90 center.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
91 sk_float_midpoint(pts[0].fY, pts[1].fY));
92 return SkShaders::SweepGradient(center, {{data.fColors, data.fPos, tm}, {}}, &localMatrix);
93 }
94
95 static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data,
96 SkTileMode tm, const SkMatrix& localMatrix) {
97 SkPoint center0, center1;
98 center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX),
99 sk_float_midpoint(pts[0].fY, pts[1].fY));
100 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
101 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
102 return SkShaders::TwoPointConicalGradient(center1, (pts[1].fX - pts[0].fX) / 7,
103 center0, (pts[1].fX - pts[0].fX) / 2,
104 {{data.fColors, data.fPos, tm}, {}}, &localMatrix);
105 }
106
107 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
108 SkTileMode tm, const SkMatrix& localMatrix) {
109 SkPoint center0, center1;
110 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
111 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
112 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
113 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
114 return SkShaders::TwoPointConicalGradient(center1, radius1, center0, radius0,
115 {{data.fColors, data.fPos, tm}, {}}, &localMatrix);
116 }
117
118 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
119 SkTileMode tm, const SkMatrix& localMatrix);
120 constexpr GradMaker gGradMakers[] = {
121 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
122 };
123
124 ///////////////////////////////////////////////////////////////////////////////
125
126 class GradientsGM : public skiagm::GM {
127 public:
128 GradientsGM(bool dither) : fDither(dither) {}
129
130 protected:
131 const bool fDither;
132
133 void onDraw(SkCanvas* canvas) override {
134 SkPoint pts[2] = {
135 { 0, 0 },
136 { SkIntToScalar(100), SkIntToScalar(100) }
137 };
138 SkTileMode tm = SkTileMode::kClamp;
139 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
140 SkPaint paint;
141 paint.setAntiAlias(true);
142 paint.setDither(fDither);
143
144 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
145 for (size_t i = 0; i < std::size(gGradData); i++) {
146 canvas->save();
147 for (size_t j = 0; j < std::size(gGradMakers); j++) {
148 SkMatrix scale = SkMatrix::I();
149
150 if (i == 5) { // if the clamp case
151 scale.setScale(0.5f, 0.5f);
152 scale.postTranslate(25.f, 25.f);
153 }
154
155 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, scale));
156 canvas->drawRect(r, paint);
157 canvas->translate(0, SkIntToScalar(120));
158 }
159 canvas->restore();
160 canvas->translate(SkIntToScalar(120), 0);
161 }
162 }
163
164 private:
165 void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
166
167 SkString getName() const override {
168 return SkString(fDither ? "gradients" : "gradients_nodither");
169 }
170
171 SkISize getISize() override { return {840, 815}; }
172 };
173 DEF_GM( return new GradientsGM(true); )
174 DEF_GM( return new GradientsGM(false); )
175
176 // Based on the original gradient slide, but with perspective applied to the
177 // gradient shaders' local matrices
178 class GradientsLocalPerspectiveGM : public skiagm::GM {
179 public:
180 GradientsLocalPerspectiveGM(bool dither) : fDither(dither) {
181 this->setBGColor(0xFFDDDDDD);
182 }
183
184 private:
185 SkString getName() const override {
186 return SkString(fDither ? "gradients_local_perspective" :
187 "gradients_local_perspective_nodither");
188 }
189
190 SkISize getISize() override { return {840, 815}; }
191
192 void onDraw(SkCanvas* canvas) override {
193 SkPoint pts[2] = {
194 { 0, 0 },
195 { SkIntToScalar(100), SkIntToScalar(100) }
196 };
197 SkTileMode tm = SkTileMode::kClamp;
198 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
199 SkPaint paint;
200 paint.setAntiAlias(true);
201 paint.setDither(fDither);
202
203 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
204 for (size_t i = 0; i < std::size(gGradData); i++) {
205 canvas->save();
206 for (size_t j = 0; j < std::size(gGradMakers); j++) {
207 // apply an increasing y perspective as we move to the right
208 SkMatrix perspective;
209 perspective.setIdentity();
210 perspective.setPerspY(SkIntToScalar(i+1) / 500);
211 perspective.setSkewX(SkIntToScalar(i+1) / 10);
212
213 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, perspective));
214 canvas->drawRect(r, paint);
215 canvas->translate(0, SkIntToScalar(120));
216 }
217 canvas->restore();
218 canvas->translate(SkIntToScalar(120), 0);
219 }
220 }
221
222 bool fDither;
223 };
224 DEF_GM( return new GradientsLocalPerspectiveGM(true); )
225 DEF_GM( return new GradientsLocalPerspectiveGM(false); )
226
227 // Based on the original gradient slide, but with perspective applied to
228 // the view matrix
229 class GradientsViewPerspectiveGM : public GradientsGM {
230 public:
231 GradientsViewPerspectiveGM(bool dither) : INHERITED(dither) { }
232
233 private:
234 SkString getName() const override {
235 return SkString(fDither ? "gradients_view_perspective" :
236 "gradients_view_perspective_nodither");
237 }
238
239 SkISize getISize() override { return {840, 500}; }
240
241 void onDraw(SkCanvas* canvas) override {
242 SkMatrix perspective;
243 perspective.setIdentity();
244 perspective.setPerspY(0.001f);
245 perspective.setSkewX(SkIntToScalar(8) / 25);
246 canvas->concat(perspective);
247 this->INHERITED::onDraw(canvas);
248 }
249
250 private:
251 using INHERITED = GradientsGM;
252 };
253 DEF_GM( return new GradientsViewPerspectiveGM(true); )
254 DEF_GM( return new GradientsViewPerspectiveGM(false); )
255
256 /*
257 Inspired by this <canvas> javascript, where we need to detect that we are not
258 solving a quadratic equation, but must instead solve a linear (since our X^2
259 coefficient is 0)
260
261 ctx.fillStyle = '#f00';
262 ctx.fillRect(0, 0, 100, 50);
263
264 var g = ctx.createRadialGradient(-80, 25, 70, 0, 25, 150);
265 g.addColorStop(0, '#f00');
266 g.addColorStop(0.01, '#0f0');
267 g.addColorStop(0.99, '#0f0');
268 g.addColorStop(1, '#f00');
269 ctx.fillStyle = g;
270 ctx.fillRect(0, 0, 100, 50);
271 */
272 class GradientsDegenrate2PointGM : public skiagm::GM {
273 public:
274 GradientsDegenrate2PointGM(bool dither) : fDither(dither) {}
275
276 private:
277 SkString getName() const override {
278 return SkString(fDither ? "gradients_degenerate_2pt" : "gradients_degenerate_2pt_nodither");
279 }
280
281 SkISize getISize() override { return {320, 320}; }
282
283 void onDraw(SkCanvas* canvas) override {
284 canvas->drawColor(SK_ColorBLUE);
285
286 SkColor4f colors[] = { SkColors::kRed, SkColors::kGreen, SkColors::kGreen, SkColors::kRed };
287 SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 };
288 SkPoint c0;
289 c0.iset(-80, 25);
290 SkScalar r0 = SkIntToScalar(70);
291 SkPoint c1;
292 c1.iset(0, 25);
293 SkScalar r1 = SkIntToScalar(150);
294 SkPaint paint;
295 paint.setShader(SkShaders::TwoPointConicalGradient(c0, r0, c1, r1,
296 {{colors, pos, SkTileMode::kClamp}, {}}));
297 paint.setDither(fDither);
298 canvas->drawPaint(paint);
299 }
300
301 bool fDither;
302 };
303 DEF_GM( return new GradientsDegenrate2PointGM(true); )
304 DEF_GM( return new GradientsDegenrate2PointGM(false); )
305
306 /* skbug.com/40031542
307 <canvas id="canvas"></canvas>
308 <script>
309 var c = document.getElementById("canvas");
310 var ctx = c.getContext("2d");
311 ctx.fillStyle = '#ff0';
312 ctx.fillRect(0, 0, 100, 50);
313
314 var g = ctx.createRadialGradient(200, 25, 20, 200, 25, 10);
315 g.addColorStop(0, '#0f0');
316 g.addColorStop(0.003, '#f00'); // 0.004 makes this work
317 g.addColorStop(1, '#ff0');
318 ctx.fillStyle = g;
319 ctx.fillRect(0, 0, 100, 50);
320 </script>
321 */
322
323 // should draw only green
324 DEF_SIMPLE_GM(small_color_stop, canvas, 100, 150) {
325 SkColor4f colors[] = { SkColors::kGreen, SkColors::kRed, SkColors::kYellow };
326 SkScalar pos[] = { 0, 0.003f, SK_Scalar1 }; // 0.004f makes this work
327 SkPoint c0 = { 200, 25 };
328 SkScalar r0 = 20;
329 SkPoint c1 = { 200, 25 };
330 SkScalar r1 = 10;
331
332 SkPaint paint;
333 paint.setColor(SK_ColorYELLOW);
334 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
335 paint.setShader(SkShaders::TwoPointConicalGradient(c0, r0, c1, r1,
336 {{colors, pos, SkTileMode::kClamp}, {}}));
337 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
338 }
339
340
341 /// Tests correctness of *optimized* codepaths in gradients.
342
343 class ClampedGradientsGM : public skiagm::GM {
344 public:
345 ClampedGradientsGM(bool dither) : fDither(dither) {}
346
347 private:
348 SkString getName() const override {
349 return SkString(fDither ? "clamped_gradients" : "clamped_gradients_nodither");
350 }
351
352 SkISize getISize() override { return {640, 510}; }
353
354 void onDraw(SkCanvas* canvas) override {
355 canvas->drawColor(0xFFDDDDDD);
356
357 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) };
358 SkPaint paint;
359 paint.setDither(fDither);
360 paint.setAntiAlias(true);
361
362 SkPoint center;
363 center.iset(0, 300);
364 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
365 paint.setShader(SkShaders::RadialGradient(
366 center, 200, {{gColors, {}, SkTileMode::kClamp}, {}}));
367 canvas->drawRect(r, paint);
368 }
369
370 bool fDither;
371 };
372 DEF_GM( return new ClampedGradientsGM(true); )
373 DEF_GM( return new ClampedGradientsGM(false); )
374
375 /// Checks quality of large radial gradients, which may display
376 /// some banding.
377
378 class RadialGradientGM : public skiagm::GM {
379 SkString getName() const override { return SkString("radial_gradient"); }
380
381 SkISize getISize() override { return {1280, 1280}; }
382
383 void onDraw(SkCanvas* canvas) override {
384 const SkISize dim = this->getISize();
385
386 canvas->drawColor(0xFF000000);
387
388 SkPaint paint;
389 paint.setDither(true);
390 SkPoint center;
391 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2);
392 SkScalar radius = SkIntToScalar(dim.width())/2;
393 const SkScalar pos[] = { 0.0f,
394 0.35f,
395 1.0f };
396 SkColorConverter conv({ 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 });
397 paint.setShader(SkShaders::RadialGradient(center, radius,
398 {{conv.colors4f(), pos, SkTileMode::kClamp}, {}}));
399 SkRect r = {
400 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height())
401 };
402 canvas->drawRect(r, paint);
403 }
404 };
405 DEF_GM( return new RadialGradientGM; )
406
407 class RadialGradient2GM : public skiagm::GM {
408 public:
409 RadialGradient2GM(bool dither) : fDither(dither) {}
410
411 private:
412 SkString getName() const override {
413 return SkString(fDither ? "radial_gradient2" : "radial_gradient2_nodither");
414 }
415
416 SkISize getISize() override { return {800, 400}; }
417
418 // Reproduces the example given in b/7671058.
419 void onDraw(SkCanvas* canvas) override {
420 SkPaint paint1, paint2, paint3;
421 paint1.setStyle(SkPaint::kFill_Style);
422 paint2.setStyle(SkPaint::kFill_Style);
423 paint3.setStyle(SkPaint::kFill_Style);
424
425 const SkColor4f sweep_colors[] =
426 { {1,0,0,1}, {1,1,0,1}, {0,1,0,1}, {0,1,1,1}, {0,0,1,1}, {1,0,1,1}, {1,0,0,1} };
427 const SkColor4f colors1[] = { {1,1,1,1}, {0,0,0,0} };
428 const SkColor4f colors2[] = { {0,0,0,1}, {0,0,0,0} };
429
430 const SkScalar cx = 200, cy = 200, radius = 150;
431 SkPoint center;
432 center.set(cx, cy);
433
434 // We can either interpolate endpoints and premultiply each point (default, more precision),
435 // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap).
436 const SkGradient::Interpolation::InPremul flags[] = {
437 SkGradient::Interpolation::InPremul::kNo,
438 SkGradient::Interpolation::InPremul::kYes,
439 };
440 const SkTileMode tm = SkTileMode::kClamp;
441
442 for (size_t i = 0; i < std::size(flags); i++) {
443 SkGradient::Interpolation terp{flags[i]};
444 paint1.setShader(SkShaders::SweepGradient(center, {{sweep_colors, {}, tm}, terp}));
445 paint2.setShader(SkShaders::RadialGradient(center, radius, {{colors1, {}, tm}, terp}));
446 paint3.setShader(SkShaders::RadialGradient(center, radius, {{colors2, {}, tm}, terp}));
447 paint1.setDither(fDither);
448 paint2.setDither(fDither);
449 paint3.setDither(fDither);
450
451 canvas->drawCircle(cx, cy, radius, paint1);
452 canvas->drawCircle(cx, cy, radius, paint3);
453 canvas->drawCircle(cx, cy, radius, paint2);
454
455 canvas->translate(400, 0);
456 }
457 }
458
459 private:
460 bool fDither;
461
462 using INHERITED = GM;
463 };
464 DEF_GM( return new RadialGradient2GM(true); )
465 DEF_GM( return new RadialGradient2GM(false); )
466
467 // Shallow radial (shows banding on raster)
468 class RadialGradient3GM : public skiagm::GM {
469 public:
470 RadialGradient3GM(bool dither) : fDither(dither) { }
471
472 private:
473 SkString getName() const override {
474 return SkString(fDither ? "radial_gradient3" : "radial_gradient3_nodither");
475 }
476
477 SkISize getISize() override { return {500, 500}; }
478
479 bool runAsBench() const override { return true; }
480
481 void onOnceBeforeDraw() override {
482 const SkPoint center = { 0, 0 };
483 const SkScalar kRadius = 3000;
484 const SkColor4f kColors[] = { {1,1,1,1}, {0,0,0,1} };
485 fShader = SkShaders::RadialGradient(center, kRadius,
486 {{kColors, {}, SkTileMode::kClamp}, {}});
487 }
488
489 void onDraw(SkCanvas* canvas) override {
490 SkPaint paint;
491 paint.setShader(fShader);
492 paint.setDither(fDither);
493 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
494 }
495
496 private:
497 sk_sp<SkShader> fShader;
498 bool fDither;
499
500 using INHERITED = GM;
501 };
502 DEF_GM( return new RadialGradient3GM(true); )
503 DEF_GM( return new RadialGradient3GM(false); )
504
505 class RadialGradient4GM : public skiagm::GM {
506 public:
507 RadialGradient4GM(bool dither) : fDither(dither) { }
508
509 private:
510 SkString getName() const override {
511 return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodither");
512 }
513
514 SkISize getISize() override { return {500, 500}; }
515
516 void onOnceBeforeDraw() override {
517 const SkPoint center = { 250, 250 };
518 const SkScalar kRadius = 250;
519 const SkColor4f colors[] = {
520 SkColors::kRed, SkColors::kRed, SkColors::kWhite, SkColors::kWhite,SkColors::kRed };
521 const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f };
522 fShader = SkShaders::RadialGradient(center, kRadius,
523 {{colors, pos, SkTileMode::kClamp}, {}});
524 }
525
526 void onDraw(SkCanvas* canvas) override {
527 SkPaint paint;
528 paint.setAntiAlias(true);
529 paint.setDither(fDither);
530 paint.setShader(fShader);
531 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
532 }
533
534 private:
535 sk_sp<SkShader> fShader;
536 bool fDither;
537
538 using INHERITED = GM;
539 };
540 DEF_GM( return new RadialGradient4GM(true); )
541 DEF_GM( return new RadialGradient4GM(false); )
542
543 class LinearGradientGM : public skiagm::GM {
544 public:
545 LinearGradientGM(bool dither) : fDither(dither) { }
546
547 private:
548 SkString getName() const override {
549 return SkString(fDither ? "linear_gradient" : "linear_gradient_nodither");
550 }
551
552 const SkScalar kWidthBump = 30.f;
553 const SkScalar kHeight = 5.f;
554 const SkScalar kMinWidth = 540.f;
555
556 SkISize getISize() override { return {500, 500}; }
557
558 void onOnceBeforeDraw() override {
559 SkPoint pts[2] = { {0, 0}, {0, 0} };
560 const SkColor4f colors[] = {
561 SkColors::kWhite, SkColors::kWhite,
562 SkColor4f::FromColor(0xFF008200), SkColor4f::FromColor(0xFF008200),
563 SkColors::kWhite, SkColors::kWhite,
564 };
565 const SkScalar unitPos[] = { 0, 50, 70, 500, 540 };
566 SkScalar pos[6];
567 pos[5] = 1;
568 for (int index = 0; index < (int) std::size(fShader); ++index) {
569 pts[1].fX = 500.f + index * kWidthBump;
570 for (int inner = 0; inner < (int) std::size(unitPos); ++inner) {
571 pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump);
572 }
573 fShader[index] = SkShaders::LinearGradient(pts,
574 {{colors, pos, SkTileMode::kClamp}, {}});
575 }
576 }
577
578 void onDraw(SkCanvas* canvas) override {
579 SkPaint paint;
580 paint.setAntiAlias(true);
581 paint.setDither(fDither);
582 for (int index = 0; index < (int) std::size(fShader); ++index) {
583 paint.setShader(fShader[index]);
584 canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + index * kWidthBump,
585 (index + 1) * kHeight), paint);
586 }
587 }
588
589 private:
590 sk_sp<SkShader> fShader[100];
591 bool fDither;
592
593 using INHERITED = GM;
594 };
595 DEF_GM( return new LinearGradientGM(true); )
596 DEF_GM( return new LinearGradientGM(false); )
597
598 class LinearGradientTinyGM : public skiagm::GM {
599 SkString getName() const override { return SkString("linear_gradient_tiny"); }
600
601 SkISize getISize() override { return {600, 500}; }
602
603 void onDraw(SkCanvas* canvas) override {
604 const SkScalar kRectSize = 100;
605 const unsigned kStopCount = 3;
606 const SkColor4f colors[kStopCount] = { SkColors::kGreen, SkColors::kRed, SkColors::kGreen };
607 const struct {
608 SkPoint pts[2];
609 SkScalar pos[kStopCount];
610 } configs[] = {
611 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999f, 1 }},
612 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000001f, 1 }},
613 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.999999999f, 1 }},
614 { { SkPoint::Make(0, 0), SkPoint::Make(10, 0) }, { 0, 0.000000001f, 1 }},
615
616 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999f, 1 }},
617 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000001f, 1 }},
618 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.999999999f, 1 }},
619 { { SkPoint::Make(0, 0), SkPoint::Make(0, 10) }, { 0, 0.000000001f, 1 }},
620
621 { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0.5f, 1 }},
622 { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0.5f, 1 }},
623 { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0.5f, 1 }},
624 { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0.5f, 1 }},
625 };
626
627 SkPaint paint;
628 for (unsigned i = 0; i < std::size(configs); ++i) {
629 SkAutoCanvasRestore acr(canvas, true);
630 paint.setShader(SkShaders::LinearGradient(configs[i].pts,
631 {{colors, configs[i].pos, SkTileMode::kClamp},
632 {}}));
633 canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f),
634 kRectSize * ((i / 4) * 1.5f + 0.25f));
635
636 canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint);
637 }
638 }
639 };
640
641 DEF_GM( return new LinearGradientTinyGM; )
642 } // namespace
643
644 ///////////////////////////////////////////////////////////////////////////////////////////////////
645
646 struct GradRun {
647 SkColor4f fColors[4];
648 SkScalar fPos[4];
649 size_t fCount;
650
651 SkGradient grad(SkTileMode tm) const {
652 return {{{fColors, fCount}, {fPos, fCount}, tm}, {}};
653 }
654 };
655
656 #define SIZE 121
657
658 static sk_sp<SkShader> make_linear(const GradRun& run, SkTileMode mode) {
659 const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } };
660 return SkShaders::LinearGradient(pts, run.grad(mode));
661 }
662
663 static sk_sp<SkShader> make_radial(const GradRun& run, SkTileMode mode) {
664 const SkScalar half = SIZE * 0.5f;
665 return SkShaders::RadialGradient({half,half}, half - 10, run.grad(mode));
666 }
667
668 static sk_sp<SkShader> make_conical(const GradRun& run, SkTileMode mode) {
669 const SkScalar half = SIZE * 0.5f;
670 const SkPoint center { half, half };
671 return SkShaders::TwoPointConicalGradient(center, 20, center, half - 10, run.grad(mode));
672 }
673
674 static sk_sp<SkShader> make_sweep(const GradRun& run, SkTileMode mode) {
675 const SkScalar half = SIZE * 0.5f;
676 return SkShaders::SweepGradient({half, half}, run.grad(mode));
677 }
678
679 /*
680 * Exercise duplicate color-stops, at the ends, and in the middle
681 *
682 * At the time of this writing, only Linear correctly deals with duplicates at the ends,
683 * and then only correctly on CPU backend.
684 */
685 DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) {
686 const SkColor4f preColor = SkColors::kRed; // clamp color before start
687 const SkColor4f postColor = SkColors::kBlue; // clamp color after end
688 const SkColor4f color0 = SkColors::kBlack;
689 const SkColor4f color1 = SkColors::kGreen;
690 // should never be seen, fills out fixed-size array
691 const SkColor4f badColor = SkColor4f::FromColor(0xFF3388BB);
692
693 const GradRun runs[] = {
694 { { color0, color1, badColor, badColor },
695 { 0, 1, -1, -1 },
696 2,
697 },
698 { { preColor, color0, color1, badColor },
699 { 0, 0, 1, -1 },
700 3,
701 },
702 { { color0, color1, postColor, badColor },
703 { 0, 1, 1, -1 },
704 3,
705 },
706 { { preColor, color0, color1, postColor },
707 { 0, 0, 1, 1 },
708 4,
709 },
710 { { color0, color0, color1, color1 },
711 { 0, 0.5f, 0.5f, 1 },
712 4,
713 },
714 };
715 sk_sp<SkShader> (*factories[])(const GradRun&, SkTileMode) {
716 make_linear, make_radial, make_conical, make_sweep
717 };
718
719 const SkRect rect = SkRect::MakeWH(SIZE, SIZE);
720 const SkScalar dx = SIZE + 20;
721 const SkScalar dy = SIZE + 20;
722 const SkTileMode mode = SkTileMode::kClamp;
723
724 SkPaint paint;
725 canvas->translate(10, 10 - dy);
726 for (auto factory : factories) {
727 canvas->translate(0, dy);
728 SkAutoCanvasRestore acr(canvas, true);
729 for (const auto& run : runs) {
730 paint.setShader(factory(run, mode));
731 canvas->drawRect(rect, paint);
732 canvas->translate(dx, 0);
733 }
734 }
735 }
736
737 static void draw_many_stops(SkCanvas* canvas) {
738 const unsigned kStopCount = 200;
739 const SkPoint pts[] = { {50, 50}, {450, 450}};
740
741 SkColor4f colors[kStopCount];
742 for (unsigned i = 0; i < kStopCount; i++) {
743 switch (i % 5) {
744 case 0: colors[i] = SkColors::kRed; break;
745 case 1: colors[i] = SkColors::kGreen; break;
746 case 2: colors[i] = SkColors::kGreen; break;
747 case 3: colors[i] = SkColors::kBlue; break;
748 case 4: colors[i] = SkColors::kRed; break;
749 }
750 }
751
752 SkPaint p;
753 p.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}));
754
755 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
756 }
757
758 DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) {
759 draw_many_stops(canvas);
760 }
761
762 static void draw_many_hard_stops(SkCanvas* canvas) {
763 const unsigned kStopCount = 300;
764 const SkPoint pts[] = {{50, 50}, {450, 450}};
765
766 SkColor4f colors[kStopCount];
767 SkScalar pos[kStopCount];
768 for (unsigned i = 0; i < kStopCount; i++) {
769 switch (i % 6) {
770 case 0: colors[i] = SkColors::kRed; break;
771 case 1: colors[i] = SkColors::kGreen; break;
772 case 2: colors[i] = SkColors::kGreen; break;
773 case 3: colors[i] = SkColors::kBlue; break;
774 case 4: colors[i] = SkColors::kBlue; break;
775 case 5: colors[i] = SkColors::kRed; break;
776 }
777 pos[i] = (2.0f * (i / 2)) / kStopCount;
778 }
779
780 SkPaint p;
781 p.setShader(SkShaders::LinearGradient(pts, {{colors, pos, SkTileMode::kClamp}, {}}));
782
783 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
784 }
785
786 DEF_SIMPLE_GM(gradient_many_hard_stops, canvas, 500, 500) {
787 draw_many_hard_stops(canvas);
788 }
789
790 static void draw_circle_shader(SkCanvas* canvas, SkScalar cx, SkScalar cy, SkScalar r,
791 sk_sp<SkShader> (*shaderFunc)()) {
792 SkPaint p;
793 p.setAntiAlias(true);
794 p.setShader(shaderFunc());
795 canvas->drawCircle(cx, cy, r, p);
796
797 p.setShader(nullptr);
798 p.setColor(SK_ColorGRAY);
799 p.setStyle(SkPaint::kStroke_Style);
800 p.setStrokeWidth(2);
801 canvas->drawCircle(cx, cy, r, p);
802 }
803
804 DEF_SIMPLE_GM(fancy_gradients, canvas, 800, 300) {
805 draw_circle_shader(canvas, 150, 150, 100, []() -> sk_sp<SkShader> {
806 // Checkerboard using two linear gradients + picture shader.
807 SkScalar kTileSize = 80 / sqrtf(2);
808 SkColor4f colors1[] = { {0,0,0,1}, {0,0,0,1},
809 {1,1,1,1}, {1,1,1,1},
810 {0,0,0,1}, {0,0,0,1} };
811 SkColor4f colors2[] = { {0,0,0,1}, {0,0,0,1},
812 {0,0,0,0}, {0,0,0,0},
813 {0,0,0,1}, {0,0,0,1} };
814 SkScalar pos[] = { 0, .25f, .25f, .75f, .75f, 1 };
815 static_assert(std::size(colors1) == std::size(pos), "color/pos size mismatch");
816 static_assert(std::size(colors2) == std::size(pos), "color/pos size mismatch");
817
818 SkPictureRecorder recorder;
819 recorder.beginRecording(SkRect::MakeWH(kTileSize, kTileSize));
820
821 SkPaint p;
822
823 SkPoint pts1[] = { { 0, 0 }, { kTileSize, kTileSize }};
824 p.setShader(SkShaders::LinearGradient(pts1, {{colors1, pos, SkTileMode::kClamp}, {}}));
825 recorder.getRecordingCanvas()->drawPaint(p);
826
827 SkPoint pts2[] = { { 0, kTileSize }, { kTileSize, 0 }};
828 p.setShader(SkShaders::LinearGradient(pts2, {{colors2, pos, SkTileMode::kClamp}, {}}));
829 recorder.getRecordingCanvas()->drawPaint(p);
830
831 SkMatrix m = SkMatrix::I();
832 m.preRotate(45);
833 return recorder.finishRecordingAsPicture()->makeShader(
834 SkTileMode::kRepeat, SkTileMode::kRepeat,
835 SkFilterMode::kNearest, &m, nullptr);
836 });
837
838 draw_circle_shader(canvas, 400, 150, 100, []() -> sk_sp<SkShader> {
839 // Checkerboard using a sweep gradient + picture shader.
840 SkScalar kTileSize = 80;
841 SkColor4f colors[] = { {0,0,0,1}, {0,0,0,1},
842 {1,1,1,1}, {1,1,1,1},
843 {0,0,0,1}, {0,0,0,1},
844 {1,1,1,1}, {1,1,1,1} };
845 SkScalar pos[] = { 0, .25f, .25f, .5f, .5f, .75f, .75f, 1 };
846 static_assert(std::size(colors) == std::size(pos), "color/pos size mismatch");
847
848 SkPaint p;
849 p.setShader(SkShaders::SweepGradient({kTileSize / 2, kTileSize / 2},
850 {{colors, pos, SkTileMode::kClamp}, {}}));
851 SkPictureRecorder recorder;
852 recorder.beginRecording(SkRect::MakeWH(kTileSize, kTileSize))->drawPaint(p);
853
854 return recorder.finishRecordingAsPicture()->makeShader(
855 SkTileMode::kRepeat,
856 SkTileMode::kRepeat, SkFilterMode::kNearest);
857 });
858
859 draw_circle_shader(canvas, 650, 150, 100, []() -> sk_sp<SkShader> {
860 // Dartboard using sweep + radial.
861 const SkColor4f a = {1,1,1,1};
862 const SkColor4f b = {0,0,0,1};
863 SkColor4f colors[] = { a, a, b, b, a, a, b, b, a, a, b, b, a, a, b, b};
864 SkScalar pos[] = { 0, .125f, .125f, .25f, .25f, .375f, .375f, .5f, .5f,
865 .625f, .625f, .75f, .75f, .875f, .875f, 1};
866 static_assert(std::size(colors) == std::size(pos), "color/pos size mismatch");
867
868 SkPoint center = { 650, 150 };
869 sk_sp<SkShader> sweep1 = SkShaders::SweepGradient(center,
870 {{colors, pos, SkTileMode::kClamp}, {}});
871 SkMatrix m = SkMatrix::I();
872 m.preRotate(22.5f, center.x(), center.y());
873 sk_sp<SkShader> sweep2 = SkShaders::SweepGradient(center,
874 {{colors, pos, SkTileMode::kClamp}, {}}, &m);
875
876 sk_sp<SkShader> sweep(SkShaders::Blend(SkBlendMode::kExclusion, sweep1, sweep2));
877
878 SkScalar radialPos[] = { 0, .02f, .02f, .04f, .04f, .08f, .08f, .16f, .16f, .31f, .31f,
879 .62f, .62f, 1, 1, 1 };
880 static_assert(std::size(colors) == std::size(radialPos),
881 "color/pos size mismatch");
882
883 return SkShaders::Blend(SkBlendMode::kExclusion, sweep,
884 SkShaders::RadialGradient(center, 100,
885 {{colors, radialPos, SkTileMode::kClamp}, {}}));
886 });
887 }
888
889 DEF_SIMPLE_GM(sweep_tiling, canvas, 690, 512) {
890 static constexpr SkScalar size = 160;
891 static constexpr SkColor4f colors[] = { SkColors::kBlue, SkColors::kYellow, SkColors::kGreen };
892 static constexpr SkScalar pos[] = { 0, .25f, .50f };
893 static_assert(std::size(colors) == std::size(pos), "size mismatch");
894
895 static constexpr SkTileMode modes[] = { SkTileMode::kClamp,
896 SkTileMode::kRepeat,
897 SkTileMode::kMirror };
898
899 static const struct {
900 SkScalar start, end;
901 } angles[] = {
902 { -330, -270 },
903 { 30, 90 },
904 { 390, 450 },
905 { -30, 800 },
906 };
907
908 SkPaint p;
909 const SkRect r = SkRect::MakeWH(size, size);
910
911 for (auto mode : modes) {
912 {
913 SkAutoCanvasRestore acr(canvas, true);
914
915 for (auto angle : angles) {
916 p.setShader(SkShaders::SweepGradient({size / 2, size / 2}, angle.start, angle.end,
917 {{colors, pos, mode}, {}}));
918
919 canvas->drawRect(r, p);
920 canvas->translate(size * 1.1f, 0);
921 }
922 }
923 canvas->translate(0, size * 1.1f);
924 }
925 }
926
927 DEF_SIMPLE_GM(rgbw_sweep_gradient, canvas, 100, 100) {
928 static constexpr SkScalar size = 100;
929 static constexpr SkColor4f colors[] = {SkColors::kWhite, SkColors::kWhite,
930 SkColors::kBlue, SkColors::kBlue,
931 SkColors::kRed, SkColors::kRed,
932 SkColors::kGreen, SkColors::kGreen};
933 static constexpr SkScalar pos[] = { 0, .25f, .25f, .50f, .50f, .75, .75, 1 };
934 static_assert(std::size(colors) == std::size(pos), "size mismatch");
935
936 SkPaint p;
937 p.setShader(SkShaders::SweepGradient({size / 2, size / 2},
938 {{colors, pos, SkTileMode::kClamp}, {}}));
939 canvas->drawRect(SkRect::MakeWH(size, size), p);
940 }
941
942 // Exercises the special-case Ganesh gradient effects.
943 DEF_SIMPLE_GM(gradients_interesting, canvas, 640, 1300) {
944 static const SkColor4f colors2[] = { SkColors::kRed, SkColors::kBlue };
945 static const SkColor4f colors3[] = { SkColors::kRed, SkColors::kYellow, SkColors::kBlue };
946 static const SkColor4f colors4[] = {
947 SkColors::kRed, SkColors::kYellow, SkColors::kYellow, SkColors::kBlue };
948
949 static const SkScalar softRight[] = { 0, .999f, 1 }; // Based on Android launcher "clipping"
950 static const SkScalar hardLeft[] = { 0, 0, 1 };
951 static const SkScalar hardRight[] = { 0, 1, 1 };
952 static const SkScalar hardCenter[] = { 0, .5f, .5f, 1 };
953
954 static const struct {
955 SkSpan<const SkColor4f> colors;
956 SkSpan<const float> pos;
957 } configs[] = {
958 { colors2, {} }, // kTwo_ColorType
959 { colors3, {} }, // kThree_ColorType (simple)
960 { colors3, softRight }, // kThree_ColorType (tricky)
961 { colors3, hardLeft }, // kHardStopLeftEdged_ColorType
962 { colors3, hardRight }, // kHardStopRightEdged_ColorType
963 { colors4, hardCenter }, // kSingleHardStop_ColorType
964 };
965
966 static const SkTileMode modes[] = {
967 SkTileMode::kClamp,
968 SkTileMode::kRepeat,
969 SkTileMode::kMirror,
970 };
971
972 static constexpr SkScalar size = 200;
973 static const SkPoint pts[] = { { size / 3, size / 3 }, { size * 2 / 3, size * 2 / 3} };
974
975 SkPaint p;
976 for (const auto& cfg : configs) {
977 {
978 SkAutoCanvasRestore acr(canvas, true);
979 for (auto mode : modes) {
980 SkGradient grad = {{cfg.colors, cfg.pos, mode}, {}};
981 p.setShader(SkShaders::LinearGradient(pts, grad));
982 canvas->drawRect(SkRect::MakeWH(size, size), p);
983 canvas->translate(size * 1.1f, 0);
984 }
985 }
986 canvas->translate(0, size * 1.1f);
987 }
988 }
989
990 // TODO(skbug.com/40044214): Still need to test degenerate gradients in strange color spaces
991 DEF_SIMPLE_GM_BG(gradients_color_space, canvas, 265, 355, SK_ColorGRAY) {
992 using CS = SkGradient::Interpolation::ColorSpace;
993
994 struct Config {
995 CS fColorSpace;
996 const char* fLabel;
997 };
998 static const Config kConfigs[] = {
999 {CS::kSRGB, "sRGB"},
1000 {CS::kSRGBLinear, "Linear"},
1001 {CS::kLab, "Lab"},
1002 {CS::kOKLab, "OKLab"},
1003 {CS::kOKLabGamutMap, "OKLabGamutMap"},
1004 {CS::kLCH, "LCH"},
1005 {CS::kOKLCH, "OKLCH"},
1006 {CS::kOKLCHGamutMap, "OKLCHGamutMap"},
1007 {CS::kHSL, "HSL"},
1008 {CS::kHWB, "HWB"},
1009 {CS::kA98RGB, "a98RGB"},
1010 {CS::kProphotoRGB, "ProPhotoRGB"},
1011 {CS::kDisplayP3, "DisplayP3"},
1012 {CS::kRec2020, "Rec2020"},
1013 };
1014
1015 SkPoint pts[] = {{0, 0}, {200, 0}};
1016 SkColor4f colors[] = {SkColors::kBlue, SkColors::kYellow};
1017
1018 SkPaint labelPaint;
1019 SkPaint p;
1020 SkGradient::Interpolation interpolation;
1021 canvas->translate(5, 5);
1022 SkFont font = ToolUtils::DefaultPortableFont();
1023
1024 for (const Config& config : kConfigs) {
1025 interpolation.fColorSpace = config.fColorSpace;
1026 SkGradient g = {{colors, {}, SkTileMode::kClamp, SkColorSpace::MakeSRGB()}, interpolation};
1027 p.setShader(SkShaders::LinearGradient(pts, g));
1028 canvas->drawRect({0, 0, 200, 20}, p);
1029 canvas->drawSimpleText(config.fLabel, strlen(config.fLabel), SkTextEncoding::kUTF8, 210, 15,
1030 font, labelPaint);
1031 canvas->translate(0, 25);
1032 }
1033 }
1034
1035 DEF_SIMPLE_GM_BG(gradients_hue_method, canvas, 285, 155, SK_ColorGRAY) {
1036 using HM = SkGradient::Interpolation::HueMethod;
1037
1038 struct Config {
1039 HM fHueMethod;
1040 const char* fLabel;
1041 };
1042 static const Config kConfigs[] = {
1043 { HM::kShorter, "Shorter" },
1044 { HM::kLonger, "Longer" },
1045 { HM::kIncreasing, "Increasing" },
1046 { HM::kDecreasing, "Decreasing" },
1047 };
1048
1049 SkPoint pts[] = {{0, 0}, {200, 0}};
1050 SkColor4f colors[] = {SkColors::kRed, SkColors::kGreen, SkColors::kRed, SkColors::kRed };
1051
1052 SkPaint labelPaint;
1053 SkPaint p;
1054 SkGradient::Interpolation interpolation;
1055 interpolation.fColorSpace = SkGradient::Interpolation::ColorSpace::kHSL;
1056 canvas->translate(5, 5);
1057 SkFont font = ToolUtils::DefaultPortableFont();
1058
1059 for (const Config& config : kConfigs) {
1060 interpolation.fHueMethod = config.fHueMethod;
1061 SkGradient g = {{colors, {}, SkTileMode::kClamp, SkColorSpace::MakeSRGB()}, interpolation};
1062 p.setShader(SkShaders::LinearGradient(pts, g));
1063 canvas->drawRect({0, 0, 200, 20}, p);
1064 canvas->drawSimpleText(config.fLabel, strlen(config.fLabel), SkTextEncoding::kUTF8, 210, 15,
1065 font, labelPaint);
1066 canvas->translate(0, 25);
1067 }
1068
1069 // Test a bug (skbug.com/40044215) with how gradient shaders handle explicit positions.
1070 // If there are no explicit positions at 0 or 1, those are automatically added, with copies of
1071 // the first/last color. When using kLonger, this can produce extra gradient that should
1072 // actually be solid. This gradient *should* be:
1073 // |- solid red -|- red to green, the long way -|- solid green -|
1074 interpolation.fHueMethod = HM::kLonger;
1075 SkScalar middlePos[] = { 0.3f, 0.7f };
1076 SkGradient g = {{{colors, 2}, middlePos, SkTileMode::kClamp, SkColorSpace::MakeSRGB()},
1077 interpolation};
1078 p.setShader(SkShaders::LinearGradient(pts, g));
1079 canvas->drawRect({0, 0, 200, 20}, p);
1080 canvas->translate(0, 25);
1081
1082 // However... if the user explicitly includes those duplicate color stops in kLonger mode,
1083 // we expect the gradient to do a full rotation in those regions:
1084 // |- full circle, red to red -|- red to green -|- full circle, green to green -|
1085 colors[0] = colors[1] = SkColors::kRed;
1086 colors[2] = colors[3] = SkColors::kGreen;
1087 SkScalar allPos[] = { 0.0f, 0.3f, 0.7f, 1.0f };
1088 g = {{colors, allPos, SkTileMode::kClamp, SkColorSpace::MakeSRGB()}, interpolation};
1089 p.setShader(SkShaders::LinearGradient(pts, g));
1090 canvas->drawRect({0, 0, 200, 20}, p);
1091 canvas->translate(0, 25);
1092 }
1093
1094 DEF_SIMPLE_GM_BG(gradients_color_space_tilemode, canvas, 360, 105, SK_ColorGRAY) {
1095 // Test exotic (CSS) gradient color spaces in conjunction with tile modes. Rather than test
1096 // every combination, we pick one color space that has a sufficiently strange interpolated
1097 // representation (OKLCH) and just use that. We're mostly interested in making sure that things
1098 // like decal mode are implemented at the correct time in the pipeline, relative to hue
1099 // conversion, re-premultiplication, etc.
1100 SkPoint pts[] = {{20, 0}, {120, 0}};
1101 SkColor4f colors[] = {SkColors::kBlue, SkColors::kYellow};
1102
1103 SkPaint p;
1104 SkGradient::Interpolation interpolation;
1105 interpolation.fColorSpace = SkGradient::Interpolation::ColorSpace::kOKLCH;
1106
1107 canvas->translate(5, 5);
1108
1109 for (int tm = 0; tm < kSkTileModeCount; ++tm) {
1110 SkGradient g = {{colors, {}, static_cast<SkTileMode>(tm)}, interpolation};
1111 p.setShader(SkShaders::LinearGradient(pts, g));
1112 canvas->drawRect({0, 0, 350, 20}, p);
1113 canvas->translate(0, 25);
1114 }
1115 }
1116
1117 DEF_SIMPLE_GM_BG(gradients_color_space_many_stops, canvas, 500, 500, SK_ColorGRAY) {
1118 // Test exotic (CSS) gradient color spaces with many stops. Rather than test every combination,
1119 // we pick one color space that has a sufficiently strange interpolated representation (OKLCH)
1120 // and just use that. We're mostly interested in making sure that the texture fallback on GPU
1121 // works correctly.
1122 const SkPoint pts[] = { {50, 50}, {450, 465}};
1123
1124 const unsigned kStopCount = 200;
1125 SkColor4f colors[kStopCount];
1126 for (unsigned i = 0; i < kStopCount; i++) {
1127 switch (i % 5) {
1128 case 0: colors[i] = SkColors::kRed; break;
1129 case 1: colors[i] = SkColors::kGreen; break;
1130 case 2: colors[i] = SkColors::kGreen; break;
1131 case 3: colors[i] = SkColors::kBlue; break;
1132 case 4: colors[i] = SkColors::kRed; break;
1133 }
1134 }
1135
1136 SkPaint p;
1137
1138 SkGradient::Interpolation interpolation;
1139 interpolation.fColorSpace = SkGradient::Interpolation::ColorSpace::kOKLCH;
1140 p.setShader(SkShaders::LinearGradient(pts,
1141 {{colors, {}, SkTileMode::kClamp, SkColorSpace::MakeSRGB()}, interpolation}));
1142
1143 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
1144 }
1145
1146 DEF_SIMPLE_GM(gradients_alpha_many_stops, canvas, 100, 100) {
1147 static const SkPoint kPts[] = {{0.f, 0.f}, {0.f, 100.f}};
1148
1149 // From https://issues.chromium.org/issues/401546700, this encounters Graphite's
1150 // storage buffer option for storing gradient buffers AND uses colors that emphasize
1151 // premul vs. unpremul handling of the color data.
1152 static const float kPos[] = {0.f, 0.19f, 0.34f, 0.47f, 0.565f, 0.65f,
1153 0.73f, 0.802f, 0.861f, 0.91f, 0.952f, 0.982f, 1.f};
1154
1155 static constexpr float kG = 34 / 255.f;
1156 static const SkColor4f kColors[] = {{kG, kG, kG, 1.f},
1157 {kG, kG, kG, 0.738f},
1158 {kG, kG, kG, 0.541f},
1159 {kG, kG, kG, 0.382f},
1160 {kG, kG, kG, 0.278f},
1161 {kG, kG, kG, 0.194f},
1162 {kG, kG, kG, 0.126f},
1163 {kG, kG, kG, 0.075f},
1164 {kG, kG, kG, 0.042f},
1165 {kG, kG, kG, 0.021f},
1166 {kG, kG, kG, 0.008f},
1167 {kG, kG, kG, 0.002f},
1168 {kG, kG, kG, 0.f}};
1169
1170 canvas->clear(SkColor4f{0.5f, 0.5f, 0.5f, 1.f});
1171
1172 SkPaint paint;
1173 paint.setShader(SkShaders::LinearGradient(kPts, {{kColors, kPos, SkTileMode::kClamp}, {}}));
1174 canvas->drawPaint(paint);
1175 }
1176
1177 static void draw_powerless_hue_gradients(SkCanvas* canvas,
1178 SkGradient::Interpolation::ColorSpace colorSpace) {
1179 ToolUtils::draw_checkerboard(canvas);
1180
1181 auto nextRow = [=]() {
1182 canvas->restore();
1183 canvas->translate(0, 25);
1184 canvas->save();
1185 };
1186
1187 auto gradient = [&](std::initializer_list<SkColor4f> colors,
1188 std::initializer_list<float> pos,
1189 bool inPremul = false) {
1190 using Interpolation = SkGradient::Interpolation;
1191 SkASSERT(pos.size() == 0 || pos.size() == colors.size());
1192 SkPaint paint;
1193 SkPoint pts[] = {{0, 0}, {200, 0}};
1194 Interpolation interpolation;
1195 interpolation.fColorSpace = colorSpace;
1196 interpolation.fInPremul = static_cast<Interpolation::InPremul>(inPremul);
1197 paint.setShader(SkShaders::LinearGradient(pts, {{colors, pos, SkTileMode::kClamp},
1198 interpolation}));
1199 canvas->drawRect({0, 0, 200, 20}, paint);
1200 canvas->translate(205, 0); // next column
1201 };
1202
1203 canvas->translate(5, 5);
1204 canvas->save();
1205
1206 // For each test case, the first gradient (first column) has an under-specified result due to a
1207 // powerless component after conversion to LCH. The second gradient (second column) "hints" the
1208 // correct result, by slightly tinting the otherwise powerless color.
1209
1210 gradient({SkColors::kWhite, SkColors::kBlue}, {});
1211 gradient({{0.99f, 0.99f, 1.00f, 1.0f}, SkColors::kBlue}, {}); // white, with blue hue
1212 nextRow();
1213
1214 gradient({SkColors::kBlack, SkColors::kBlue}, {});
1215 gradient({{0.00f, 0.00f, 0.01f, 1.0f}, SkColors::kBlue}, {}); // black, with blue hue
1216 nextRow();
1217
1218 // Transparent cases are done in both premul and unpremul interpolation:
1219
1220 gradient({SkColors::kTransparent, SkColors::kBlue}, {}, /*inPremul=*/false);
1221 gradient({{0.00f, 0.00f, 0.01f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/false);
1222 nextRow();
1223
1224 gradient({SkColors::kTransparent, SkColors::kBlue}, {}, /*inPremul=*/true);
1225 gradient({{0.00f, 0.00f, 0.01f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/true);
1226 nextRow();
1227
1228 gradient({{1.00f, 1.00f, 1.00f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/false);
1229 gradient({{0.99f, 0.99f, 1.00f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/false);
1230 nextRow();
1231
1232 gradient({{1.00f, 1.00f, 1.00f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/true);
1233 gradient({{0.99f, 0.99f, 1.00f, 0.0f}, SkColors::kBlue}, {}, /*inPremul=*/true);
1234 nextRow();
1235
1236 // Now we test three-stop gradients, where the middle stop needs to be "split" to handle the
1237 // different hues on either side. Again, the second column explicitly injects those to produce
1238 // a reference result. See: https://github.com/w3c/csswg-drafts/issues/9295
1239
1240 gradient({SkColors::kRed, SkColors::kWhite, SkColors::kBlue}, {});
1241 gradient({SkColors::kRed,
1242 {1.00f, 0.99f, 0.99f, 1.0f},
1243 {0.99f, 0.99f, 1.00f, 1.0f},
1244 SkColors::kBlue},
1245 {0.0f, 0.5f, 0.5f, 1.0f});
1246 nextRow();
1247
1248 gradient({SkColors::kRed, SkColors::kBlack, SkColors::kBlue}, {});
1249 gradient({SkColors::kRed,
1250 {0.01f, 0.00f, 0.00f, 1.0f},
1251 {0.00f, 0.00f, 0.01f, 1.0f},
1252 SkColors::kBlue},
1253 {0.0f, 0.5f, 0.5f, 1.0f});
1254 nextRow();
1255
1256 gradient({SkColors::kRed, SkColors::kTransparent, SkColors::kBlue}, {});
1257 gradient({SkColors::kRed,
1258 {0.01f, 0.00f, 0.00f, 0.0f},
1259 {0.00f, 0.00f, 0.01f, 0.0f},
1260 SkColors::kBlue},
1261 {0.0f, 0.5f, 0.5f, 1.0f});
1262 nextRow();
1263
1264 // Now do a few black-white tests, to ensure that the hue propagation works correctly, even
1265 // when there isn't any hue in the adjacent stops.
1266 using HueMethod = SkGradient::Interpolation::HueMethod;
1267 auto blackWhiteGradient = [&](HueMethod hm) {
1268 using Interpolation = SkGradient::Interpolation;
1269 SkPaint paint;
1270 SkPoint pts[] = {{0, 0}, {405, 0}};
1271 Interpolation interpolation;
1272 interpolation.fColorSpace = colorSpace;
1273 interpolation.fHueMethod = hm;
1274 const SkColor4f colors[] = {SkColors::kWhite, SkColors::kGray,
1275 SkColors::kWhite, SkColors::kDkGray,
1276 SkColors::kWhite, SkColors::kBlack};
1277 paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp},
1278 interpolation}));
1279 canvas->drawRect({0, 0, 405, 20}, paint);
1280 nextRow();
1281 };
1282
1283 blackWhiteGradient(HueMethod::kShorter);
1284 blackWhiteGradient(HueMethod::kIncreasing);
1285 blackWhiteGradient(HueMethod::kDecreasing);
1286 blackWhiteGradient(HueMethod::kLonger);
1287 }
1288
1289 #define DEF_POWERLESS_HUE_GM(colorSpace) \
1290 DEF_SIMPLE_GM(gradients_powerless_hue_##colorSpace, canvas, 415, 330) { \
1291 draw_powerless_hue_gradients(canvas, \
1292 SkGradient::Interpolation::ColorSpace::k##colorSpace); \
1293 }
1294
1295 DEF_POWERLESS_HUE_GM(LCH)
1296 DEF_POWERLESS_HUE_GM(OKLCH)
1297 DEF_POWERLESS_HUE_GM(HSL)
1298 DEF_POWERLESS_HUE_GM(HWB)