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