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, NULL }, | 23 { 1, gColors, NULL }, |
24 { 2, gColors, NULL }, | 24 { 2, gColors, NULL }, |
25 { 3, gColors, NULL }, | 25 { 3, gColors, NULL }, |
26 { 4, gColors, NULL }, | 26 { 4, gColors, NULL }, |
27 }; | 27 }; |
28 | 28 |
29 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 29 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
30 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); | 30 SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 31 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 32 data.fCount, tm, mapper); |
31 } | 33 } |
32 | 34 |
33 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 35 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 36 SkShader::TileMode tm, SkUnitMapper* mapper) { |
34 SkPoint center; | 37 SkPoint center; |
35 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 38 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
36 SkScalarAve(pts[0].fY, pts[1].fY)); | 39 SkScalarAve(pts[0].fY, pts[1].fY)); |
37 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 40 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
38 data.fPos, data.fCount, tm); | 41 data.fPos, data.fCount, tm, mapper); |
39 } | 42 } |
40 | 43 |
41 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader:
:TileMode) { | 44 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 45 SkShader::TileMode, SkUnitMapper* mapper) { |
42 SkPoint center; | 46 SkPoint center; |
43 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 47 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
44 SkScalarAve(pts[0].fY, pts[1].fY)); | 48 SkScalarAve(pts[0].fY, pts[1].fY)); |
45 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat
a.fPos, data.fCount); | 49 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 50 data.fPos, data.fCount, mapper); |
46 } | 51 } |
47 | 52 |
48 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShade
r::TileMode tm) { | 53 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 54 SkShader::TileMode tm, SkUnitMapper* mapper) { |
49 SkPoint center0, center1; | 55 SkPoint center0, center1; |
50 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 56 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
51 SkScalarAve(pts[0].fY, pts[1].fY)); | 57 SkScalarAve(pts[0].fY, pts[1].fY)); |
52 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 58 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
53 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 59 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
54 return SkGradientShader::CreateTwoPointRadial( | 60 return SkGradientShader::CreateTwoPointRadial( |
55 center1, (pts[1].fX - pts[0].fX) / 7, | 61 center1, (pts[1].fX - pts[0].fX) / 7, |
56 center0, (pts[1].fX - pts[0].fX) / 2, | 62 center0, (pts[1].fX - pts[0].fX) / 2, |
57 data.fColors, data.fPos, data.fCount, tm); | 63 data.fColors, data.fPos, data.fCount, tm, mapper); |
58 } | 64 } |
59 | 65 |
60 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShad
er::TileMode tm) { | 66 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
| 67 SkShader::TileMode tm, SkUnitMapper* mapper) { |
61 SkPoint center0, center1; | 68 SkPoint center0, center1; |
62 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); | 69 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
63 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); | 70 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
64 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 71 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
65 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 72 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
66 return SkGradientShader::CreateTwoPointConical(center1, radius1, | 73 return SkGradientShader::CreateTwoPointConical(center1, radius1, |
67 center0, radius0, | 74 center0, radius0, |
68 data.fColors, data.fPos, | 75 data.fColors, data.fPos, |
69 data.fCount, tm); | 76 data.fCount, tm, mapper); |
70 } | 77 } |
71 | 78 |
72 | 79 |
73 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha
der::TileMode tm); | 80 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
74 | 81 SkShader::TileMode tm, SkUnitMapper* mapper); |
75 static const GradMaker gGradMakers[] = { | 82 static const GradMaker gGradMakers[] = { |
76 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, | 83 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, |
77 }; | 84 }; |
78 | 85 |
79 /////////////////////////////////////////////////////////////////////////////// | 86 /////////////////////////////////////////////////////////////////////////////// |
80 | 87 |
81 class GradientsNoTextureGM : public GM { | 88 class GradientsNoTextureGM : public GM { |
82 public: | 89 public: |
83 GradientsNoTextureGM() { | 90 GradientsNoTextureGM() { |
84 this->setBGColor(0xFFDDDDDD); | 91 this->setBGColor(0xFFDDDDDD); |
(...skipping 14 matching lines...) Expand all Loading... |
99 SkRect kRect = { 0, 0, SkIntToScalar(50), SkIntToScalar(50) }; | 106 SkRect kRect = { 0, 0, SkIntToScalar(50), SkIntToScalar(50) }; |
100 SkPaint paint; | 107 SkPaint paint; |
101 paint.setAntiAlias(true); | 108 paint.setAntiAlias(true); |
102 | 109 |
103 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 110 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
104 static const uint8_t kAlphas[] = { 0xff, 0x40 }; | 111 static const uint8_t kAlphas[] = { 0xff, 0x40 }; |
105 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphas); ++a) { | 112 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphas); ++a) { |
106 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); ++i) { | 113 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); ++i) { |
107 canvas->save(); | 114 canvas->save(); |
108 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); ++j) { | 115 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); ++j) { |
109 SkShader* shader = gGradMakers[j](kPts, gGradData[i], kTM); | 116 SkShader* shader = gGradMakers[j](kPts, gGradData[i], kTM, N
ULL); |
110 paint.setShader(shader)->unref(); | 117 paint.setShader(shader)->unref(); |
111 paint.setAlpha(kAlphas[a]); | 118 paint.setAlpha(kAlphas[a]); |
112 canvas->drawRect(kRect, paint); | 119 canvas->drawRect(kRect, paint); |
113 canvas->translate(0, SkIntToScalar(kRect.height() + 20)); | 120 canvas->translate(0, SkIntToScalar(kRect.height() + 20)); |
114 } | 121 } |
115 canvas->restore(); | 122 canvas->restore(); |
116 canvas->translate(SkIntToScalar(kRect.width() + 20), 0); | 123 canvas->translate(SkIntToScalar(kRect.width() + 20), 0); |
117 } | 124 } |
118 } | 125 } |
119 } | 126 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 } | 256 } |
250 | 257 |
251 private: | 258 private: |
252 typedef GM INHERITED; | 259 typedef GM INHERITED; |
253 }; | 260 }; |
254 | 261 |
255 /////////////////////////////////////////////////////////////////////////////// | 262 /////////////////////////////////////////////////////////////////////////////// |
256 | 263 |
257 DEF_GM( return SkNEW(GradientsNoTextureGM)); | 264 DEF_GM( return SkNEW(GradientsNoTextureGM)); |
258 DEF_GM( return SkNEW(GradientsManyColorsGM)); | 265 DEF_GM( return SkNEW(GradientsManyColorsGM)); |
OLD | NEW |