OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 #include "gm.h" | 7 #include "gm.h" |
8 #include "SkGradientShader.h" | 8 #include "SkGradientShader.h" |
9 | 9 |
10 using namespace skiagm; | 10 using namespace skiagm; |
11 | 11 |
12 struct GradData { | 12 struct GradData { |
13 int fCount; | 13 int fCount; |
14 const SkColor* fColors; | 14 const SkColor* fColors; |
15 const SkScalar* fPos; | 15 const SkScalar* fPos; |
16 }; | 16 }; |
17 | 17 |
18 static const SkColor gColors[] = { | 18 static const SkColor gColors[] = { |
19 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, | 19 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, |
20 }; | 20 }; |
21 | 21 |
22 static const GradData gGradData[] = { | 22 static const GradData gGradData[] = { |
23 { 1, gColors, nullptr }, | 23 { 1, gColors, nullptr }, |
24 { 2, gColors, nullptr }, | 24 { 2, gColors, nullptr }, |
25 { 3, gColors, nullptr }, | 25 { 3, gColors, nullptr }, |
26 { 4, gColors, nullptr }, | 26 { 4, gColors, nullptr }, |
27 }; | 27 }; |
28 | 28 |
29 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 29 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, Sk
Shader::TileMode tm) { |
30 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); | 30 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun
t, tm); |
31 } | 31 } |
32 | 32 |
33 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 33 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, Sk
Shader::TileMode tm) { |
34 SkPoint center; | 34 SkPoint center; |
35 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 35 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
36 SkScalarAve(pts[0].fY, pts[1].fY)); | 36 SkScalarAve(pts[0].fY, pts[1].fY)); |
37 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 37 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fP
os, data.fCount, tm); |
38 data.fPos, data.fCount, tm); | |
39 } | 38 } |
40 | 39 |
41 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader:
:TileMode) { | 40 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkS
hader::TileMode) { |
42 SkPoint center; | 41 SkPoint center; |
43 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 42 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
44 SkScalarAve(pts[0].fY, pts[1].fY)); | 43 SkScalarAve(pts[0].fY, pts[1].fY)); |
45 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat
a.fPos, data.fCount); | 44 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.
fPos, data.fCount); |
46 } | 45 } |
47 | 46 |
48 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShade
r::TileMode tm) { | 47 static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& data, S
kShader::TileMode tm) { |
49 SkPoint center0, center1; | 48 SkPoint center0, center1; |
50 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 49 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
51 SkScalarAve(pts[0].fY, pts[1].fY)); | 50 SkScalarAve(pts[0].fY, pts[1].fY)); |
52 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 51 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
53 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 52 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
54 return SkGradientShader::CreateTwoPointConical( | 53 return SkGradientShader::MakeTwoPointConical( |
55 center1, (pts[1].fX - pts[0].fX) / 7, | 54 center1, (pts[1].fX - pts[0].fX) / 7, |
56 center0, (pts[1].fX - pts[0].fX) / 2, | 55 center0, (pts[1].fX - pts[0].fX) / 2, |
57 data.fColors, data.fPos, data.fCount, tm); | 56 data.fColors, data.fPos, data.fCount, tm); |
58 } | 57 } |
59 | 58 |
60 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShad
er::TileMode tm) { | 59 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
SkShader::TileMode tm) { |
61 SkPoint center0, center1; | 60 SkPoint center0, center1; |
62 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; | 61 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
63 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; | 62 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
64 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 63 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
65 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 64 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
66 return SkGradientShader::CreateTwoPointConical(center1, radius1, | 65 return SkGradientShader::MakeTwoPointConical(center1, radius1, |
67 center0, radius0, | 66 center0, radius0, |
68 data.fColors, data.fPos, | 67 data.fColors, data.fPos, |
69 data.fCount, tm); | 68 data.fCount, tm); |
70 } | 69 } |
71 | 70 |
72 | 71 |
73 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha
der::TileMode tm); | 72 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
SkShader::TileMode tm); |
74 | 73 |
75 static const GradMaker gGradMakers[] = { | 74 static const GradMaker gGradMakers[] = { |
76 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, | 75 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, |
77 }; | 76 }; |
78 | 77 |
79 /////////////////////////////////////////////////////////////////////////////// | 78 /////////////////////////////////////////////////////////////////////////////// |
80 | 79 |
81 class GradientsNoTextureGM : public GM { | 80 class GradientsNoTextureGM : public GM { |
82 public: | 81 public: |
83 GradientsNoTextureGM(bool dither) : fDither(dither) { | 82 GradientsNoTextureGM(bool dither) : fDither(dither) { |
(...skipping 16 matching lines...) Expand all Loading... |
100 SkPaint paint; | 99 SkPaint paint; |
101 paint.setAntiAlias(true); | 100 paint.setAntiAlias(true); |
102 paint.setDither(fDither); | 101 paint.setDither(fDither); |
103 | 102 |
104 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 103 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
105 static const uint8_t kAlphas[] = { 0xff, 0x40 }; | 104 static const uint8_t kAlphas[] = { 0xff, 0x40 }; |
106 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphas); ++a) { | 105 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphas); ++a) { |
107 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); ++i) { | 106 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); ++i) { |
108 canvas->save(); | 107 canvas->save(); |
109 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); ++j) { | 108 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); ++j) { |
110 SkShader* shader = gGradMakers[j](kPts, gGradData[i], kTM); | 109 paint.setShader(gGradMakers[j](kPts, gGradData[i], kTM)); |
111 paint.setShader(shader)->unref(); | |
112 paint.setAlpha(kAlphas[a]); | 110 paint.setAlpha(kAlphas[a]); |
113 canvas->drawRect(kRect, paint); | 111 canvas->drawRect(kRect, paint); |
114 canvas->translate(0, SkIntToScalar(kRect.height() + 20)); | 112 canvas->translate(0, SkIntToScalar(kRect.height() + 20)); |
115 } | 113 } |
116 canvas->restore(); | 114 canvas->restore(); |
117 canvas->translate(SkIntToScalar(kRect.width() + 20), 0); | 115 canvas->translate(SkIntToScalar(kRect.width() + 20), 0); |
118 } | 116 } |
119 } | 117 } |
120 } | 118 } |
121 | 119 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 for (int i = 0; i <= 8; ++i) { | 247 for (int i = 0; i <= 8; ++i) { |
250 SkScalar x = r.width() * i / 8; | 248 SkScalar x = r.width() * i / 8; |
251 canvas->drawLine(x, 0, x, 10000, paint); | 249 canvas->drawLine(x, 0, x, 10000, paint); |
252 } | 250 } |
253 | 251 |
254 // expand the drawing rect so we exercise clampping in the gradients | 252 // expand the drawing rect so we exercise clampping in the gradients |
255 const SkRect drawR = r.makeOutset(20, 0); | 253 const SkRect drawR = r.makeOutset(20, 0); |
256 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { | 254 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
257 ColorPos rec; | 255 ColorPos rec; |
258 procs[i](&rec); | 256 procs[i](&rec); |
259 SkShader* s = SkGradientShader::CreateLinear(pts, rec.fColors, rec.f
Pos, rec.fCount, | 257 paint.setShader(SkGradientShader::MakeLinear(pts, rec.fColors, rec.f
Pos, rec.fCount, |
260 SkShader::kClamp_TileMo
de); | 258 SkShader::kClamp_TileMo
de)); |
261 paint.setShader(s)->unref(); | |
262 canvas->drawRect(drawR, paint); | 259 canvas->drawRect(drawR, paint); |
263 | 260 |
264 canvas->save(); | 261 canvas->save(); |
265 canvas->translate(r.centerX(), r.height() + 4); | 262 canvas->translate(r.centerX(), r.height() + 4); |
266 canvas->scale(-1, 1); | 263 canvas->scale(-1, 1); |
267 canvas->translate(-r.centerX(), 0); | 264 canvas->translate(-r.centerX(), 0); |
268 canvas->drawRect(drawR, paint); | 265 canvas->drawRect(drawR, paint); |
269 canvas->restore(); | 266 canvas->restore(); |
270 | 267 |
271 canvas->translate(0, r.height() + 2*r.height() + 8); | 268 canvas->translate(0, r.height() + 2*r.height() + 8); |
272 } | 269 } |
273 } | 270 } |
274 | 271 |
275 private: | 272 private: |
276 bool fDither; | 273 bool fDither; |
277 | 274 |
278 typedef GM INHERITED; | 275 typedef GM INHERITED; |
279 }; | 276 }; |
280 | 277 |
281 /////////////////////////////////////////////////////////////////////////////// | 278 /////////////////////////////////////////////////////////////////////////////// |
282 | 279 |
283 DEF_GM(return new GradientsNoTextureGM(true);) | 280 DEF_GM(return new GradientsNoTextureGM(true);) |
284 DEF_GM(return new GradientsNoTextureGM(false);) | 281 DEF_GM(return new GradientsNoTextureGM(false);) |
285 DEF_GM(return new GradientsManyColorsGM(true);) | 282 DEF_GM(return new GradientsManyColorsGM(true);) |
286 DEF_GM(return new GradientsManyColorsGM(false);) | 283 DEF_GM(return new GradientsManyColorsGM(false);) |
OLD | NEW |