OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkGradientShader.h" | 9 #include "SkGradientShader.h" |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 static const GradData gGradData[] = { | 33 static const GradData gGradData[] = { |
34 { 2, gColors, NULL }, | 34 { 2, gColors, NULL }, |
35 { 2, gColors, gPos0 }, | 35 { 2, gColors, gPos0 }, |
36 { 2, gColors, gPos1 }, | 36 { 2, gColors, gPos1 }, |
37 { 5, gColors, NULL }, | 37 { 5, gColors, NULL }, |
38 { 5, gColors, gPos2 }, | 38 { 5, gColors, gPos2 }, |
39 { 4, gColorClamp, gPosClamp } | 39 { 4, gColorClamp, gPosClamp } |
40 }; | 40 }; |
41 | 41 |
42 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, | 42 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
43 SkShader::TileMode tm, SkUnitMapper* mapper) { | 43 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 44 const SkMatrix& localMatrix) { |
44 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, | 45 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
45 data.fCount, tm, mapper); | 46 data.fCount, tm, mapper, 0, &localMatr
ix); |
46 } | 47 } |
47 | 48 |
48 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, | 49 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
49 SkShader::TileMode tm, SkUnitMapper* mapper) { | 50 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 51 const SkMatrix& localMatrix) { |
50 SkPoint center; | 52 SkPoint center; |
51 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 53 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
52 SkScalarAve(pts[0].fY, pts[1].fY)); | 54 SkScalarAve(pts[0].fY, pts[1].fY)); |
53 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 55 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
54 data.fPos, data.fCount, tm, mapper); | 56 data.fPos, data.fCount, tm, mapper, 0,
&localMatrix); |
55 } | 57 } |
56 | 58 |
57 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, | 59 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
58 SkShader::TileMode, SkUnitMapper* mapper) { | 60 SkShader::TileMode, SkUnitMapper* mapper, |
| 61 const SkMatrix& localMatrix) { |
59 SkPoint center; | 62 SkPoint center; |
60 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
61 SkScalarAve(pts[0].fY, pts[1].fY)); | 64 SkScalarAve(pts[0].fY, pts[1].fY)); |
62 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, | 65 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
63 data.fPos, data.fCount, mapper); | 66 data.fPos, data.fCount, mapper, 0, &loc
alMatrix); |
64 } | 67 } |
65 | 68 |
66 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, | 69 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
67 SkShader::TileMode tm, SkUnitMapper* mapper) { | 70 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 71 const SkMatrix& localMatrix) { |
68 SkPoint center0, center1; | 72 SkPoint center0, center1; |
69 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 73 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
70 SkScalarAve(pts[0].fY, pts[1].fY)); | 74 SkScalarAve(pts[0].fY, pts[1].fY)); |
71 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 75 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
72 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 76 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
73 return SkGradientShader::CreateTwoPointRadial( | 77 return SkGradientShader::CreateTwoPointRadial( |
74 center1, (pts[1].fX - pts[0].f
X) / 7, | 78 center1, (pts[1].fX - pts[0].f
X) / 7, |
75 center0, (pts[1].fX - pts[0].f
X) / 2, | 79 center0, (pts[1].fX - pts[0].f
X) / 2, |
76 data.fColors, data.fPos, data.
fCount, tm, mapper); | 80 data.fColors, data.fPos, data.
fCount, tm, mapper, |
| 81 0, &localMatrix); |
77 } | 82 } |
78 | 83 |
79 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, | 84 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
80 SkShader::TileMode tm, SkUnitMapper* mapper) { | 85 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 86 const SkMatrix& localMatrix) { |
81 SkPoint center0, center1; | 87 SkPoint center0, center1; |
82 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); | 88 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
83 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); | 89 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
84 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 90 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
85 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 91 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
86 return SkGradientShader::CreateTwoPointConical(center1, radius1, | 92 return SkGradientShader::CreateTwoPointConical(center1, radius1, |
87 center0, radius0, | 93 center0, radius0, |
88 data.fColors, data.fPos, | 94 data.fColors, data.fPos, |
89 data.fCount, tm, mapper); | 95 data.fCount, tm, mapper, |
| 96 0, &localMatrix); |
90 } | 97 } |
91 | 98 |
92 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, | 99 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
93 SkShader::TileMode tm, SkUnitMapper* mapper); | 100 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 101 const SkMatrix& localMatrix); |
94 static const GradMaker gGradMakers[] = { | 102 static const GradMaker gGradMakers[] = { |
95 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical | 103 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical |
96 }; | 104 }; |
97 | 105 |
98 /////////////////////////////////////////////////////////////////////////////// | 106 /////////////////////////////////////////////////////////////////////////////// |
99 | 107 |
100 class GradientsGM : public GM { | 108 class GradientsGM : public GM { |
101 public: | 109 public: |
102 GradientsGM() { | 110 GradientsGM() { |
103 this->setBGColor(0xFFDDDDDD); | 111 this->setBGColor(0xFFDDDDDD); |
(...skipping 14 matching lines...) Expand all Loading... |
118 }; | 126 }; |
119 SkShader::TileMode tm = SkShader::kClamp_TileMode; | 127 SkShader::TileMode tm = SkShader::kClamp_TileMode; |
120 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; | 128 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
121 SkPaint paint; | 129 SkPaint paint; |
122 paint.setAntiAlias(true); | 130 paint.setAntiAlias(true); |
123 | 131 |
124 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 132 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
125 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { | 133 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
126 canvas->save(); | 134 canvas->save(); |
127 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 135 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
128 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); | 136 SkMatrix scale = SkMatrix::I(); |
129 | 137 |
130 if (i == 5) { // if the clamp case | 138 if (i == 5) { // if the clamp case |
131 SkMatrix scale; | |
132 scale.setScale(0.5f, 0.5f); | 139 scale.setScale(0.5f, 0.5f); |
133 scale.postTranslate(25.f, 25.f); | 140 scale.postTranslate(25.f, 25.f); |
134 shader->setLocalMatrix(scale); | |
135 } | 141 } |
136 | 142 |
| 143 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL, s
cale); |
| 144 |
137 paint.setShader(shader); | 145 paint.setShader(shader); |
138 canvas->drawRect(r, paint); | 146 canvas->drawRect(r, paint); |
139 shader->unref(); | 147 shader->unref(); |
140 canvas->translate(0, SkIntToScalar(120)); | 148 canvas->translate(0, SkIntToScalar(120)); |
141 } | 149 } |
142 canvas->restore(); | 150 canvas->restore(); |
143 canvas->translate(SkIntToScalar(120), 0); | 151 canvas->translate(SkIntToScalar(120), 0); |
144 } | 152 } |
145 } | 153 } |
146 | 154 |
(...skipping 24 matching lines...) Expand all Loading... |
171 }; | 179 }; |
172 SkShader::TileMode tm = SkShader::kClamp_TileMode; | 180 SkShader::TileMode tm = SkShader::kClamp_TileMode; |
173 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; | 181 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
174 SkPaint paint; | 182 SkPaint paint; |
175 paint.setAntiAlias(true); | 183 paint.setAntiAlias(true); |
176 | 184 |
177 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 185 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
178 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { | 186 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
179 canvas->save(); | 187 canvas->save(); |
180 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 188 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
181 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); | |
182 | |
183 // apply an increasing y perspective as we move to the right | 189 // apply an increasing y perspective as we move to the right |
184 SkMatrix perspective; | 190 SkMatrix perspective; |
185 perspective.setIdentity(); | 191 perspective.setIdentity(); |
186 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 192 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
187 SkIntToScalar(500))); | 193 SkIntToScalar(500))); |
188 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 194 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
189 SkIntToScalar(10))); | 195 SkIntToScalar(10))); |
190 | 196 |
191 shader->setLocalMatrix(perspective); | 197 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL, p
erspective); |
192 | 198 |
193 paint.setShader(shader); | 199 paint.setShader(shader); |
194 canvas->drawRect(r, paint); | 200 canvas->drawRect(r, paint); |
195 shader->unref(); | 201 shader->unref(); |
196 canvas->translate(0, SkIntToScalar(120)); | 202 canvas->translate(0, SkIntToScalar(120)); |
197 } | 203 } |
198 canvas->restore(); | 204 canvas->restore(); |
199 canvas->translate(SkIntToScalar(120), 0); | 205 canvas->translate(SkIntToScalar(120), 0); |
200 } | 206 } |
201 } | 207 } |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 445 |
440 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } | 446 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } |
441 static GMRegistry reg5(MyFactory5); | 447 static GMRegistry reg5(MyFactory5); |
442 | 448 |
443 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } | 449 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } |
444 static GMRegistry reg6(MyFactory6); | 450 static GMRegistry reg6(MyFactory6); |
445 | 451 |
446 static GM* MyFactory7(void*) { return new RadialGradient2GM; } | 452 static GM* MyFactory7(void*) { return new RadialGradient2GM; } |
447 static GMRegistry reg7(MyFactory7); | 453 static GMRegistry reg7(MyFactory7); |
448 } | 454 } |
OLD | NEW |