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

Side by Side Diff: gm/gradients.cpp

Issue 1793303002: Reland of "more shader-->sp conversions (patchset #5 id:80001 of https://codereview.chromium… (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: make pictureRef a value, so its clearer what's going on Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gm/gradient_matrix.cpp ('k') | gm/gradients_2pt_conical.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkGradientShader.h" 9 #include "SkGradientShader.h"
10 #include "SkLinearGradient.h" 10 #include "SkLinearGradient.h"
(...skipping 22 matching lines...) Expand all
33 33
34 static const GradData gGradData[] = { 34 static const GradData gGradData[] = {
35 { 2, gColors, nullptr }, 35 { 2, gColors, nullptr },
36 { 2, gColors, gPos0 }, 36 { 2, gColors, gPos0 },
37 { 2, gColors, gPos1 }, 37 { 2, gColors, gPos1 },
38 { 5, gColors, nullptr }, 38 { 5, gColors, nullptr },
39 { 5, gColors, gPos2 }, 39 { 5, gColors, gPos2 },
40 { 4, gColorClamp, gPosClamp } 40 { 4, gColorClamp, gPosClamp }
41 }; 41 };
42 42
43 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, 43 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data,
44 SkShader::TileMode tm, const SkMatrix& localMatrix) { 44 SkShader::TileMode tm, const SkMatrix& localMa trix) {
45 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, 45 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun t, tm, 0,
46 data.fCount, tm, 0, &localMatrix); 46 &localMatrix);
47 } 47 }
48 48
49 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, 49 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data,
50 SkShader::TileMode tm, const SkMatrix& localMatrix) { 50 SkShader::TileMode tm, const SkMatrix& localMa trix) {
51 SkPoint center; 51 SkPoint center;
52 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 52 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
53 SkScalarAve(pts[0].fY, pts[1].fY)); 53 SkScalarAve(pts[0].fY, pts[1].fY));
54 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, 54 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fP os, data.fCount,
55 data.fPos, data.fCount, tm, 0, &localM atrix); 55 tm, 0, &localMatrix);
56 } 56 }
57 57
58 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, 58 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data,
59 SkShader::TileMode, const SkMatrix& localMatrix) { 59 SkShader::TileMode, const SkMatrix& localMatrix ) {
60 SkPoint center; 60 SkPoint center;
61 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 61 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
62 SkScalarAve(pts[0].fY, pts[1].fY)); 62 SkScalarAve(pts[0].fY, pts[1].fY));
63 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, 63 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data. fPos, data.fCount,
64 data.fPos, data.fCount, 0, &localMatrix ); 64 0, &localMatrix);
65 } 65 }
66 66
67 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, 67 static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data,
68 SkShader::TileMode tm, const SkMatrix& localMatrix) { 68 SkShader::TileMode tm, const SkMatrix& localM atrix) {
69 SkPoint center0, center1; 69 SkPoint center0, center1;
70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), 70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
71 SkScalarAve(pts[0].fY, pts[1].fY)); 71 SkScalarAve(pts[0].fY, pts[1].fY));
72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), 72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); 73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
74 return SkGradientShader::CreateTwoPointConical( 74 return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX ) / 7,
75 center1, (pts[1].fX - pts[0]. fX) / 7, 75 center0, (pts[1].fX - pts[0].fX ) / 2,
76 center0, (pts[1].fX - pts[0]. fX) / 2, 76 data.fColors, data.fPos, data.f Count, tm,
77 data.fColors, data.fPos, data .fCount, tm, 77 0, &localMatrix);
78 0, &localMatrix);
79 } 78 }
80 79
81 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, 80 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
82 SkShader::TileMode tm, const SkMatrix& localMatrix) { 81 SkShader::TileMode tm, const SkMatrix& localMatrix) {
83 SkPoint center0, center1; 82 SkPoint center0, center1;
84 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; 83 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
85 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; 84 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
86 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); 85 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
87 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); 86 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
88 return SkGradientShader::CreateTwoPointConical(center1, radius1, 87 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radi us0,
89 center0, radius0, 88 data.fColors, data.fPos,
90 data.fColors, data.fPos, 89 data.fCount, tm, 0, &localMatri x);
91 data.fCount, tm, 0, &localMat rix);
92 } 90 }
93 91
94 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, 92 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
95 SkShader::TileMode tm, const SkMatrix& localMatri x); 93 SkShader::TileMode tm, const SkMatrix& loca lMatrix);
96 static const GradMaker gGradMakers[] = { 94 static const GradMaker gGradMakers[] = {
97 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical 95 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical
98 }; 96 };
99 97
100 /////////////////////////////////////////////////////////////////////////////// 98 ///////////////////////////////////////////////////////////////////////////////
101 99
102 class GradientsGM : public GM { 100 class GradientsGM : public GM {
103 public: 101 public:
104 GradientsGM(bool dither) : fDither(dither) { 102 GradientsGM(bool dither) : fDither(dither) {
105 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); 103 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
(...skipping 23 matching lines...) Expand all
129 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { 127 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
130 canvas->save(); 128 canvas->save();
131 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { 129 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
132 SkMatrix scale = SkMatrix::I(); 130 SkMatrix scale = SkMatrix::I();
133 131
134 if (i == 5) { // if the clamp case 132 if (i == 5) { // if the clamp case
135 scale.setScale(0.5f, 0.5f); 133 scale.setScale(0.5f, 0.5f);
136 scale.postTranslate(25.f, 25.f); 134 scale.postTranslate(25.f, 25.f);
137 } 135 }
138 136
139 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); 137 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, scale));
140
141 paint.setShader(shader);
142 canvas->drawRect(r, paint); 138 canvas->drawRect(r, paint);
143 shader->unref();
144 canvas->translate(0, SkIntToScalar(120)); 139 canvas->translate(0, SkIntToScalar(120));
145 } 140 }
146 canvas->restore(); 141 canvas->restore();
147 canvas->translate(SkIntToScalar(120), 0); 142 canvas->translate(SkIntToScalar(120), 0);
148 } 143 }
149 } 144 }
150 145
151 protected: 146 protected:
152 bool fDither; 147 bool fDither;
153 148
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 184 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
190 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { 185 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
191 canvas->save(); 186 canvas->save();
192 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { 187 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
193 // apply an increasing y perspective as we move to the right 188 // apply an increasing y perspective as we move to the right
194 SkMatrix perspective; 189 SkMatrix perspective;
195 perspective.setIdentity(); 190 perspective.setIdentity();
196 perspective.setPerspY(SkIntToScalar(i+1) / 500); 191 perspective.setPerspY(SkIntToScalar(i+1) / 500);
197 perspective.setSkewX(SkIntToScalar(i+1) / 10); 192 perspective.setSkewX(SkIntToScalar(i+1) / 10);
198 193
199 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspec tive); 194 paint.setShader(gGradMakers[j](pts, gGradData[i], tm, perspectiv e));
200
201 paint.setShader(shader);
202 canvas->drawRect(r, paint); 195 canvas->drawRect(r, paint);
203 shader->unref();
204 canvas->translate(0, SkIntToScalar(120)); 196 canvas->translate(0, SkIntToScalar(120));
205 } 197 }
206 canvas->restore(); 198 canvas->restore();
207 canvas->translate(SkIntToScalar(120), 0); 199 canvas->translate(SkIntToScalar(120), 0);
208 } 200 }
209 } 201 }
210 202
211 private: 203 private:
212 bool fDither; 204 bool fDither;
213 205
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 this->drawBG(canvas); 272 this->drawBG(canvas);
281 273
282 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_Color RED }; 274 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_Color RED };
283 SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 }; 275 SkScalar pos[] = { 0, 0.01f, 0.99f, SK_Scalar1 };
284 SkPoint c0; 276 SkPoint c0;
285 c0.iset(-80, 25); 277 c0.iset(-80, 25);
286 SkScalar r0 = SkIntToScalar(70); 278 SkScalar r0 = SkIntToScalar(70);
287 SkPoint c1; 279 SkPoint c1;
288 c1.iset(0, 25); 280 c1.iset(0, 25);
289 SkScalar r1 = SkIntToScalar(150); 281 SkScalar r1 = SkIntToScalar(150);
290 SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, co lors, 282 SkPaint paint;
283 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, co lors,
291 pos, SK_ARRAY_COUN T(pos), 284 pos, SK_ARRAY_COUN T(pos),
292 SkShader::kClamp_T ileMode); 285 SkShader::kClamp_T ileMode));
293 SkPaint paint;
294 paint.setDither(fDither); 286 paint.setDither(fDither);
295 paint.setShader(s)->unref();
296 canvas->drawPaint(paint); 287 canvas->drawPaint(paint);
297 } 288 }
298 289
299 private: 290 private:
300 bool fDither; 291 bool fDither;
301 292
302 typedef GM INHERITED; 293 typedef GM INHERITED;
303 }; 294 };
304 DEF_GM( return new GradientsDegenrate2PointGM(true); ) 295 DEF_GM( return new GradientsDegenrate2PointGM(true); )
305 DEF_GM( return new GradientsDegenrate2PointGM(false); ) 296 DEF_GM( return new GradientsDegenrate2PointGM(false); )
(...skipping 16 matching lines...) Expand all
322 */ 313 */
323 314
324 // should draw only green 315 // should draw only green
325 DEF_SIMPLE_GM(small_color_stop, canvas, 100, 150) { 316 DEF_SIMPLE_GM(small_color_stop, canvas, 100, 150) {
326 SkColor colors[] = { SK_ColorGREEN, SK_ColorRED, SK_ColorYELLOW }; 317 SkColor colors[] = { SK_ColorGREEN, SK_ColorRED, SK_ColorYELLOW };
327 SkScalar pos[] = { 0, 0.003f, SK_Scalar1 }; // 0.004f makes this work 318 SkScalar pos[] = { 0, 0.003f, SK_Scalar1 }; // 0.004f makes this work
328 SkPoint c0 = { 200, 25 }; 319 SkPoint c0 = { 200, 25 };
329 SkScalar r0 = 20; 320 SkScalar r0 = 20;
330 SkPoint c1 = { 200, 25 }; 321 SkPoint c1 = { 200, 25 };
331 SkScalar r1 = 10; 322 SkScalar r1 = 10;
332 SkShader* s = SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, colors , 323
333 pos, SK_ARRAY_COUNT(po s),
334 SkShader::kClamp_TileM ode);
335 SkPaint paint; 324 SkPaint paint;
336 paint.setColor(SK_ColorYELLOW); 325 paint.setColor(SK_ColorYELLOW);
337 canvas->drawRect(SkRect::MakeWH(100, 150), paint); 326 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
338 paint.setShader(s)->unref(); 327 paint.setShader(SkGradientShader::MakeTwoPointConical(c0, r0, c1, r1, colors , pos,
328 SK_ARRAY_COUNT(pos),
329 SkShader::kClamp_TileM ode));
339 canvas->drawRect(SkRect::MakeWH(100, 150), paint); 330 canvas->drawRect(SkRect::MakeWH(100, 150), paint);
340 } 331 }
341 332
342 333
343 /// Tests correctness of *optimized* codepaths in gradients. 334 /// Tests correctness of *optimized* codepaths in gradients.
344 335
345 class ClampedGradientsGM : public GM { 336 class ClampedGradientsGM : public GM {
346 public: 337 public:
347 ClampedGradientsGM(bool dither) : fDither(dither) {} 338 ClampedGradientsGM(bool dither) : fDither(dither) {}
348 339
(...skipping 12 matching lines...) Expand all
361 this->drawBG(canvas); 352 this->drawBG(canvas);
362 353
363 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; 354 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) };
364 SkPaint paint; 355 SkPaint paint;
365 paint.setDither(fDither); 356 paint.setDither(fDither);
366 paint.setAntiAlias(true); 357 paint.setAntiAlias(true);
367 358
368 SkPoint center; 359 SkPoint center;
369 center.iset(0, 300); 360 center.iset(0, 300);
370 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 361 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
371 SkShader* shader = SkGradientShader::CreateRadial( 362 paint.setShader(SkGradientShader::MakeRadial(
372 SkPoint(center), 363 SkPoint(center),
373 SkIntToScalar(200), gColors, nullptr, 5, 364 SkIntToScalar(200), gColors, nullptr, 5,
374 SkShader::kClamp_TileMode); 365 SkShader::kClamp_TileMode));
375 paint.setShader(shader);
376 canvas->drawRect(r, paint); 366 canvas->drawRect(r, paint);
377 shader->unref();
378 } 367 }
379 368
380 private: 369 private:
381 bool fDither; 370 bool fDither;
382 371
383 typedef GM INHERITED; 372 typedef GM INHERITED;
384 }; 373 };
385 DEF_GM( return new ClampedGradientsGM(true); ) 374 DEF_GM( return new ClampedGradientsGM(true); )
386 DEF_GM( return new ClampedGradientsGM(false); ) 375 DEF_GM( return new ClampedGradientsGM(false); )
387 376
(...skipping 18 matching lines...) Expand all
406 395
407 SkPaint paint; 396 SkPaint paint;
408 paint.setDither(true); 397 paint.setDither(true);
409 SkPoint center; 398 SkPoint center;
410 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2); 399 center.set(SkIntToScalar(dim.width())/2, SkIntToScalar(dim.height())/2);
411 SkScalar radius = SkIntToScalar(dim.width())/2; 400 SkScalar radius = SkIntToScalar(dim.width())/2;
412 const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 }; 401 const SkColor colors[] = { 0x7f7f7f7f, 0x7f7f7f7f, 0xb2000000 };
413 const SkScalar pos[] = { 0.0f, 402 const SkScalar pos[] = { 0.0f,
414 0.35f, 403 0.35f,
415 1.0f }; 404 1.0f };
416 SkShader* shader = 405 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos ,
417 SkGradientShader::CreateRadial(center, radius, colors, 406 SK_ARRAY_COUNT(pos),
418 pos, SK_ARRAY_COUNT(pos), 407 SkShader::kClamp_TileMode)) ;
419 SkShader::kClamp_TileMode);
420 paint.setShader(shader)->unref();
421 SkRect r = { 408 SkRect r = {
422 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height()) 409 0, 0, SkIntToScalar(dim.width()), SkIntToScalar(dim.height())
423 }; 410 };
424 canvas->drawRect(r, paint); 411 canvas->drawRect(r, paint);
425 } 412 }
426 private: 413 private:
427 typedef GM INHERITED; 414 typedef GM INHERITED;
428 }; 415 };
429 DEF_GM( return new RadialGradientGM; ) 416 DEF_GM( return new RadialGradientGM; )
430 417
(...skipping 26 matching lines...) Expand all
457 444
458 const SkScalar cx = 200, cy = 200, radius = 150; 445 const SkScalar cx = 200, cy = 200, radius = 150;
459 SkPoint center; 446 SkPoint center;
460 center.set(cx, cy); 447 center.set(cx, cy);
461 448
462 // We can either interpolate endpoints and premultiply each point (defau lt, more precision), 449 // We can either interpolate endpoints and premultiply each point (defau lt, more precision),
463 // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap). 450 // or premultiply the endpoints first, avoiding the need to premultiply each point (cheap).
464 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPrem ul_Flag }; 451 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPrem ul_Flag };
465 452
466 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { 453 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) {
467 SkAutoTUnref<SkShader> sweep( 454 paint1.setShader(SkGradientShader::MakeSweep(cx, cy, sweep_colors,
468 SkGradientShader::CreateSweep(cx, cy, sweep_colors, 455 nullptr, SK_ARRAY_COUNT (sweep_colors),
469 nullptr, SK_ARRAY_COUNT(sweep_ colors), 456 flags[i], nullptr));
470 flags[i], nullptr)); 457 paint2.setShader(SkGradientShader::MakeRadial(center, radius, colors 1,
471 SkAutoTUnref<SkShader> radial1( 458 nullptr, SK_ARRAY_COUN T(colors1),
472 SkGradientShader::CreateRadial(center, radius, colors1, 459 SkShader::kClamp_TileM ode,
473 nullptr, SK_ARRAY_COUNT(color s1), 460 flags[i], nullptr));
474 SkShader::kClamp_TileMode, 461 paint3.setShader(SkGradientShader::MakeRadial(center, radius, colors 2,
475 flags[i], nullptr)); 462 nullptr, SK_ARRAY_COUN T(colors2),
476 SkAutoTUnref<SkShader> radial2( 463 SkShader::kClamp_TileM ode,
477 SkGradientShader::CreateRadial(center, radius, colors2, 464 flags[i], nullptr));
478 nullptr, SK_ARRAY_COUNT(color s2),
479 SkShader::kClamp_TileMode,
480 flags[i], nullptr));
481 paint1.setShader(sweep);
482 paint1.setDither(fDither); 465 paint1.setDither(fDither);
483 paint2.setShader(radial1);
484 paint2.setDither(fDither); 466 paint2.setDither(fDither);
485 paint3.setShader(radial2);
486 paint3.setDither(fDither); 467 paint3.setDither(fDither);
487 468
488 canvas->drawCircle(cx, cy, radius, paint1); 469 canvas->drawCircle(cx, cy, radius, paint1);
489 canvas->drawCircle(cx, cy, radius, paint3); 470 canvas->drawCircle(cx, cy, radius, paint3);
490 canvas->drawCircle(cx, cy, radius, paint2); 471 canvas->drawCircle(cx, cy, radius, paint2);
491 472
492 canvas->translate(400, 0); 473 canvas->translate(400, 0);
493 } 474 }
494 } 475 }
495 476
(...skipping 16 matching lines...) Expand all
512 } 493 }
513 494
514 SkISize onISize() override { return SkISize::Make(500, 500); } 495 SkISize onISize() override { return SkISize::Make(500, 500); }
515 496
516 bool runAsBench() const override { return true; } 497 bool runAsBench() const override { return true; }
517 498
518 void onOnceBeforeDraw() override { 499 void onOnceBeforeDraw() override {
519 const SkPoint center = { 0, 0 }; 500 const SkPoint center = { 0, 0 };
520 const SkScalar kRadius = 3000; 501 const SkScalar kRadius = 3000;
521 const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 }; 502 const SkColor gColors[] = { 0xFFFFFFFF, 0xFF000000 };
522 fShader.reset(SkGradientShader::CreateRadial(center, kRadius, gColors, n ullptr, 2, 503 fShader = SkGradientShader::MakeRadial(center, kRadius, gColors, nullptr , 2,
523 SkShader::kClamp_TileMode)) ; 504 SkShader::kClamp_TileMode);
524 } 505 }
525 506
526 void onDraw(SkCanvas* canvas) override { 507 void onDraw(SkCanvas* canvas) override {
527 SkPaint paint; 508 SkPaint paint;
528 paint.setShader(fShader); 509 paint.setShader(fShader);
529 paint.setDither(fDither); 510 paint.setDither(fDither);
530 canvas->drawRect(SkRect::MakeWH(500, 500), paint); 511 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
531 } 512 }
532 513
533 private: 514 private:
534 SkAutoTUnref<SkShader> fShader; 515 sk_sp<SkShader> fShader;
535 bool fDither; 516 bool fDither;
536 517
537 typedef GM INHERITED; 518 typedef GM INHERITED;
538 }; 519 };
539 DEF_GM( return new RadialGradient3GM(true); ) 520 DEF_GM( return new RadialGradient3GM(true); )
540 DEF_GM( return new RadialGradient3GM(false); ) 521 DEF_GM( return new RadialGradient3GM(false); )
541 522
542 class RadialGradient4GM : public GM { 523 class RadialGradient4GM : public GM {
543 public: 524 public:
544 RadialGradient4GM(bool dither) : fDither(dither) { } 525 RadialGradient4GM(bool dither) : fDither(dither) { }
545 526
546 protected: 527 protected:
547 SkString onShortName() override { 528 SkString onShortName() override {
548 return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodithe r"); 529 return SkString(fDither ? "radial_gradient4" : "radial_gradient4_nodithe r");
549 } 530 }
550 531
551 SkISize onISize() override { return SkISize::Make(500, 500); } 532 SkISize onISize() override { return SkISize::Make(500, 500); }
552 533
553 void onOnceBeforeDraw() override { 534 void onOnceBeforeDraw() override {
554 const SkPoint center = { 250, 250 }; 535 const SkPoint center = { 250, 250 };
555 const SkScalar kRadius = 250; 536 const SkScalar kRadius = 250;
556 const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_C olorWHITE, 537 const SkColor colors[] = { SK_ColorRED, SK_ColorRED, SK_ColorWHITE, SK_C olorWHITE,
557 SK_ColorRED }; 538 SK_ColorRED };
558 const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 }; 539 const SkScalar pos[] = { 0, .4f, .4f, .8f, .8f, 1 };
559 fShader.reset(SkGradientShader::CreateRadial(center, kRadius, colors, po s, 540 fShader = SkGradientShader::MakeRadial(center, kRadius, colors, pos,
560 SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); 541 SK_ARRAY_COUNT(gColors), SkShader ::kClamp_TileMode);
561 } 542 }
562 543
563 void onDraw(SkCanvas* canvas) override { 544 void onDraw(SkCanvas* canvas) override {
564 SkPaint paint; 545 SkPaint paint;
565 paint.setAntiAlias(true); 546 paint.setAntiAlias(true);
566 paint.setDither(fDither); 547 paint.setDither(fDither);
567 paint.setShader(fShader); 548 paint.setShader(fShader);
568 canvas->drawRect(SkRect::MakeWH(500, 500), paint); 549 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
569 } 550 }
570 551
571 private: 552 private:
572 SkAutoTUnref<SkShader> fShader; 553 sk_sp<SkShader> fShader;
573 bool fDither; 554 bool fDither;
574 555
575 typedef GM INHERITED; 556 typedef GM INHERITED;
576 }; 557 };
577 DEF_GM( return new RadialGradient4GM(true); ) 558 DEF_GM( return new RadialGradient4GM(true); )
578 DEF_GM( return new RadialGradient4GM(false); ) 559 DEF_GM( return new RadialGradient4GM(false); )
579 560
580 class LinearGradientGM : public GM { 561 class LinearGradientGM : public GM {
581 public: 562 public:
582 LinearGradientGM(bool dither) : fDither(dither) { } 563 LinearGradientGM(bool dither) : fDither(dither) { }
(...skipping 14 matching lines...) Expand all
597 const SkColor colors[] = { SK_ColorWHITE, SK_ColorWHITE, 0xFF008200, 0xF F008200, 578 const SkColor colors[] = { SK_ColorWHITE, SK_ColorWHITE, 0xFF008200, 0xF F008200,
598 SK_ColorWHITE, SK_ColorWHITE }; 579 SK_ColorWHITE, SK_ColorWHITE };
599 const SkScalar unitPos[] = { 0, 50, 70, 500, 540 }; 580 const SkScalar unitPos[] = { 0, 50, 70, 500, 540 };
600 SkScalar pos[6]; 581 SkScalar pos[6];
601 pos[5] = 1; 582 pos[5] = 1;
602 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { 583 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) {
603 pts[1].fX = 500.f + index * kWidthBump; 584 pts[1].fX = 500.f + index * kWidthBump;
604 for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) { 585 for (int inner = 0; inner < (int) SK_ARRAY_COUNT(unitPos); ++inner) {
605 pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump); 586 pos[inner] = unitPos[inner] / (kMinWidth + index * kWidthBump);
606 } 587 }
607 fShader[index].reset(SkGradientShader::CreateLinear(pts, colors, pos , 588 fShader[index] = SkGradientShader::MakeLinear(pts, colors, pos,
608 SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode)); 589 SK_ARRAY_COUNT(gColors), SkShader::kClamp_TileMode);
609 } 590 }
610 } 591 }
611 592
612 void onDraw(SkCanvas* canvas) override { 593 void onDraw(SkCanvas* canvas) override {
613 SkPaint paint; 594 SkPaint paint;
614 paint.setAntiAlias(true); 595 paint.setAntiAlias(true);
615 paint.setDither(fDither); 596 paint.setDither(fDither);
616 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) { 597 for (int index = 0; index < (int) SK_ARRAY_COUNT(fShader); ++index) {
617 paint.setShader(fShader[index]); 598 paint.setShader(fShader[index]);
618 canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + in dex * kWidthBump, 599 canvas->drawRect(SkRect::MakeLTRB(0, index * kHeight, kMinWidth + in dex * kWidthBump,
619 (index + 1) * kHeight), paint); 600 (index + 1) * kHeight), paint);
620 } 601 }
621 } 602 }
622 603
623 private: 604 private:
624 SkAutoTUnref<SkShader> fShader[100]; 605 sk_sp<SkShader> fShader[100];
625 bool fDither; 606 bool fDither;
626 607
627 typedef GM INHERITED; 608 typedef GM INHERITED;
628 }; 609 };
629 DEF_GM( return new LinearGradientGM(true); ) 610 DEF_GM( return new LinearGradientGM(true); )
630 DEF_GM( return new LinearGradientGM(false); ) 611 DEF_GM( return new LinearGradientGM(false); )
631 612
632 class LinearGradientTinyGM : public GM { 613 class LinearGradientTinyGM : public GM {
633 public: 614 public:
634 LinearGradientTinyGM(uint32_t flags, const char* suffix = nullptr) 615 LinearGradientTinyGM(uint32_t flags, const char* suffix = nullptr)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 647
667 { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0 .5f, 1 }}, 648 { { SkPoint::Make(0, 0), SkPoint::Make(0.00001f, 0) }, { 0, 0 .5f, 1 }},
668 { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0 .5f, 1 }}, 649 { { SkPoint::Make(9.99999f, 0), SkPoint::Make(10, 0) }, { 0, 0 .5f, 1 }},
669 { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0 .5f, 1 }}, 650 { { SkPoint::Make(0, 0), SkPoint::Make(0, 0.00001f) }, { 0, 0 .5f, 1 }},
670 { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0 .5f, 1 }}, 651 { { SkPoint::Make(0, 9.99999f), SkPoint::Make(0, 10) }, { 0, 0 .5f, 1 }},
671 }; 652 };
672 653
673 SkPaint paint; 654 SkPaint paint;
674 for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) { 655 for (unsigned i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
675 SkAutoCanvasRestore acr(canvas, true); 656 SkAutoCanvasRestore acr(canvas, true);
676 SkAutoTUnref<SkShader> gradient( 657 paint.setShader(SkGradientShader::MakeLinear(configs[i].pts, colors, configs[i].pos,
677 SkGradientShader::CreateLinear(configs[i].pts, colors, configs[i ].pos, kStopCount, 658 kStopCount, SkShader::k Clamp_TileMode,
678 SkShader::kClamp_TileMode, fFlags , nullptr)); 659 fFlags, nullptr));
679 canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f), 660 canvas->translate(kRectSize * ((i % 4) * 1.5f + 0.25f),
680 kRectSize * ((i / 4) * 1.5f + 0.25f)); 661 kRectSize * ((i / 4) * 1.5f + 0.25f));
681 662
682 paint.setShader(gradient);
683 canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint); 663 canvas->drawRect(SkRect::MakeWH(kRectSize, kRectSize), paint);
684 } 664 }
685 } 665 }
686 666
687 private: 667 private:
688 typedef GM INHERITED; 668 typedef GM INHERITED;
689 669
690 SkString fName; 670 SkString fName;
691 uint32_t fFlags; 671 uint32_t fFlags;
692 }; 672 };
693 DEF_GM( return new LinearGradientTinyGM(0); ) 673 DEF_GM( return new LinearGradientTinyGM(0); )
694 DEF_GM( return new LinearGradientTinyGM(SkLinearGradient::kForce4fContext_Privat eFlag, "_4f"); ) 674 DEF_GM( return new LinearGradientTinyGM(SkLinearGradient::kForce4fContext_Privat eFlag, "_4f"); )
695 } 675 }
696 676
697 //////////////////////////////////////////////////////////////////////////////// /////////////////// 677 //////////////////////////////////////////////////////////////////////////////// ///////////////////
698 678
699 struct GradRun { 679 struct GradRun {
700 SkColor fColors[4]; 680 SkColor fColors[4];
701 SkScalar fPos[4]; 681 SkScalar fPos[4];
702 int fCount; 682 int fCount;
703 }; 683 };
704 684
705 #define SIZE 121 685 #define SIZE 121
706 686
707 static SkShader* make_linear(const GradRun& run, SkShader::TileMode mode) { 687 static sk_sp<SkShader> make_linear(const GradRun& run, SkShader::TileMode mode) {
708 const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } }; 688 const SkPoint pts[] { { 30, 30 }, { SIZE - 30, SIZE - 30 } };
709 return SkGradientShader::CreateLinear(pts, run.fColors, run.fPos, run.fCount , mode); 689 return SkGradientShader::MakeLinear(pts, run.fColors, run.fPos, run.fCount, mode);
710 } 690 }
711 691
712 static SkShader* make_radial(const GradRun& run, SkShader::TileMode mode) { 692 static sk_sp<SkShader> make_radial(const GradRun& run, SkShader::TileMode mode) {
713 const SkScalar half = SIZE * 0.5f; 693 const SkScalar half = SIZE * 0.5f;
714 return SkGradientShader::CreateRadial({half,half}, half - 10, 694 return SkGradientShader::MakeRadial({half,half}, half - 10, run.fColors, run .fPos,
715 run.fColors, run.fPos, run.fCount, mod e); 695 run.fCount, mode);
716 } 696 }
717 697
718 static SkShader* make_conical(const GradRun& run, SkShader::TileMode mode) { 698 static sk_sp<SkShader> make_conical(const GradRun& run, SkShader::TileMode mode) {
719 const SkScalar half = SIZE * 0.5f; 699 const SkScalar half = SIZE * 0.5f;
720 const SkPoint center { half, half }; 700 const SkPoint center { half, half };
721 return SkGradientShader::CreateTwoPointConical(center, 20, center, half - 10 , 701 return SkGradientShader::MakeTwoPointConical(center, 20, center, half - 10,
722 run.fColors, run.fPos, run.fCount, mod e); 702 run.fColors, run.fPos, run.fCou nt, mode);
723 } 703 }
724 704
725 static SkShader* make_sweep(const GradRun& run, SkShader::TileMode) { 705 static sk_sp<SkShader> make_sweep(const GradRun& run, SkShader::TileMode) {
726 const SkScalar half = SIZE * 0.5f; 706 const SkScalar half = SIZE * 0.5f;
727 return SkGradientShader::CreateSweep(half, half, run.fColors, run.fPos, run. fCount); 707 return SkGradientShader::MakeSweep(half, half, run.fColors, run.fPos, run.fC ount);
728 } 708 }
729 709
730 /* 710 /*
731 * Exercise duplicate color-stops, at the ends, and in the middle 711 * Exercise duplicate color-stops, at the ends, and in the middle
732 * 712 *
733 * At the time of this writing, only Linear correctly deals with duplicates at the ends, 713 * At the time of this writing, only Linear correctly deals with duplicates at the ends,
734 * and then only correctly on CPU backend. 714 * and then only correctly on CPU backend.
735 */ 715 */
736 DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) { 716 DEF_SIMPLE_GM(gradients_dup_color_stops, canvas, 704, 564) {
737 const SkColor preColor = 0xFFFF0000; // clamp color before start 717 const SkColor preColor = 0xFFFF0000; // clamp color before start
(...skipping 17 matching lines...) Expand all
755 }, 735 },
756 { { preColor, color0, color1, postColor }, 736 { { preColor, color0, color1, postColor },
757 { 0, 0, 1, 1 }, 737 { 0, 0, 1, 1 },
758 4, 738 4,
759 }, 739 },
760 { { color0, color0, color1, color1 }, 740 { { color0, color0, color1, color1 },
761 { 0, 0.5f, 0.5f, 1 }, 741 { 0, 0.5f, 0.5f, 1 },
762 4, 742 4,
763 }, 743 },
764 }; 744 };
765 SkShader* (*factories[])(const GradRun&, SkShader::TileMode) { 745 sk_sp<SkShader> (*factories[])(const GradRun&, SkShader::TileMode) {
766 make_linear, make_radial, make_conical, make_sweep 746 make_linear, make_radial, make_conical, make_sweep
767 }; 747 };
768 748
769 const SkRect rect = SkRect::MakeWH(SIZE, SIZE); 749 const SkRect rect = SkRect::MakeWH(SIZE, SIZE);
770 const SkScalar dx = SIZE + 20; 750 const SkScalar dx = SIZE + 20;
771 const SkScalar dy = SIZE + 20; 751 const SkScalar dy = SIZE + 20;
772 const SkShader::TileMode mode = SkShader::kClamp_TileMode; 752 const SkShader::TileMode mode = SkShader::kClamp_TileMode;
773 753
774 SkPaint paint; 754 SkPaint paint;
775 canvas->translate(10, 10 - dy); 755 canvas->translate(10, 10 - dy);
776 for (auto factory : factories) { 756 for (auto factory : factories) {
777 canvas->translate(0, dy); 757 canvas->translate(0, dy);
778 SkAutoCanvasRestore acr(canvas, true); 758 SkAutoCanvasRestore acr(canvas, true);
779 for (const auto& run : runs) { 759 for (const auto& run : runs) {
780 paint.setShader(factory(run, mode))->unref(); 760 paint.setShader(factory(run, mode));
781 canvas->drawRect(rect, paint); 761 canvas->drawRect(rect, paint);
782 canvas->translate(dx, 0); 762 canvas->translate(dx, 0);
783 } 763 }
784 } 764 }
785 } 765 }
786 766
787 static void draw_many_stops(SkCanvas* canvas, uint32_t flags) { 767 static void draw_many_stops(SkCanvas* canvas, uint32_t flags) {
788 const unsigned kStopCount = 200; 768 const unsigned kStopCount = 200;
789 const SkPoint pts[] = { {50, 50}, {450, 465}}; 769 const SkPoint pts[] = { {50, 50}, {450, 465}};
790 770
791 SkColor colors[kStopCount]; 771 SkColor colors[kStopCount];
792 for (unsigned i = 0; i < kStopCount; i++) { 772 for (unsigned i = 0; i < kStopCount; i++) {
793 switch (i % 5) { 773 switch (i % 5) {
794 case 0: colors[i] = SK_ColorRED; break; 774 case 0: colors[i] = SK_ColorRED; break;
795 case 1: colors[i] = SK_ColorGREEN; break; 775 case 1: colors[i] = SK_ColorGREEN; break;
796 case 2: colors[i] = SK_ColorGREEN; break; 776 case 2: colors[i] = SK_ColorGREEN; break;
797 case 3: colors[i] = SK_ColorBLUE; break; 777 case 3: colors[i] = SK_ColorBLUE; break;
798 case 4: colors[i] = SK_ColorRED; break; 778 case 4: colors[i] = SK_ColorRED; break;
799 } 779 }
800 } 780 }
801 781
802 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear( 782 SkPaint p;
783 p.setShader(SkGradientShader::MakeLinear(
803 pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, flags, nullptr)); 784 pts, colors, nullptr, SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, flags, nullptr));
804 785
805 SkPaint p;
806 p.setShader(shader);
807
808 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p); 786 canvas->drawRect(SkRect::MakeXYWH(0, 0, 500, 500), p);
809 } 787 }
810 788
811 DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) { 789 DEF_SIMPLE_GM(gradient_many_stops, canvas, 500, 500) {
812 draw_many_stops(canvas, 0); 790 draw_many_stops(canvas, 0);
813 } 791 }
814 792
815 DEF_SIMPLE_GM(gradient_many_stops_4f, canvas, 500, 500) { 793 DEF_SIMPLE_GM(gradient_many_stops_4f, canvas, 500, 500) {
816 draw_many_stops(canvas, SkLinearGradient::kForce4fContext_PrivateFlag); 794 draw_many_stops(canvas, SkLinearGradient::kForce4fContext_PrivateFlag);
817 } 795 }
OLDNEW
« no previous file with comments | « gm/gradient_matrix.cpp ('k') | gm/gradients_2pt_conical.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698