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

Side by Side Diff: gm/gradients.cpp

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