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 |
11 namespace skiagm { | 11 namespace skiagm { |
12 | 12 |
13 struct GradData { | 13 struct GradData { |
14 int fCount; | 14 int fCount; |
15 const SkColor* fColors; | 15 const SkColor* fColors; |
16 const SkScalar* fPos; | 16 const SkScalar* fPos; |
17 }; | 17 }; |
18 | 18 |
19 static const SkColor gColors[] = { | 19 static const SkColor gColors[] = { |
20 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK | 20 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
21 }; | 21 }; |
22 static const SkScalar gPos0[] = { 0, SK_Scalar1 }; | 22 static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
23 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; | 23 static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
24 static const SkScalar gPos2[] = { | 24 static const SkScalar gPos2[] = { |
25 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 | 25 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
26 }; | 26 }; |
27 | 27 |
| 28 static const SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 29 static const SkColor gColorClamp[] = { |
| 30 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE |
| 31 }; |
| 32 |
28 static const GradData gGradData[] = { | 33 static const GradData gGradData[] = { |
29 { 2, gColors, NULL }, | 34 { 2, gColors, NULL }, |
30 { 2, gColors, gPos0 }, | 35 { 2, gColors, gPos0 }, |
31 { 2, gColors, gPos1 }, | 36 { 2, gColors, gPos1 }, |
32 { 5, gColors, NULL }, | 37 { 5, gColors, NULL }, |
33 { 5, gColors, gPos2 } | 38 { 5, gColors, gPos2 }, |
| 39 { 4, gColorClamp, gPosClamp } |
34 }; | 40 }; |
35 | 41 |
36 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, | 42 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
37 SkShader::TileMode tm, SkUnitMapper* mapper) { | 43 SkShader::TileMode tm, SkUnitMapper* mapper) { |
38 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, | 44 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
39 data.fCount, tm, mapper); | 45 data.fCount, tm, mapper); |
40 } | 46 } |
41 | 47 |
42 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, | 48 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
43 SkShader::TileMode tm, SkUnitMapper* mapper) { | 49 SkShader::TileMode tm, SkUnitMapper* mapper) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 SkShader::TileMode tm = SkShader::kClamp_TileMode; | 119 SkShader::TileMode tm = SkShader::kClamp_TileMode; |
114 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; | 120 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
115 SkPaint paint; | 121 SkPaint paint; |
116 paint.setAntiAlias(true); | 122 paint.setAntiAlias(true); |
117 | 123 |
118 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 124 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
119 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { | 125 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
120 canvas->save(); | 126 canvas->save(); |
121 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 127 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
122 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); | 128 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
| 129 |
| 130 if (i == 5) { // if the clamp case |
| 131 SkMatrix scale; |
| 132 scale.setScale(0.5f, 0.5f); |
| 133 scale.postTranslate(25.f, 25.f); |
| 134 shader->setLocalMatrix(scale); |
| 135 } |
| 136 |
123 paint.setShader(shader); | 137 paint.setShader(shader); |
124 canvas->drawRect(r, paint); | 138 canvas->drawRect(r, paint); |
125 shader->unref(); | 139 shader->unref(); |
126 canvas->translate(0, SkIntToScalar(120)); | 140 canvas->translate(0, SkIntToScalar(120)); |
127 } | 141 } |
128 canvas->restore(); | 142 canvas->restore(); |
129 canvas->translate(SkIntToScalar(120), 0); | 143 canvas->translate(SkIntToScalar(120), 0); |
130 } | 144 } |
131 } | 145 } |
132 | 146 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 180 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
167 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); | 181 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
168 | 182 |
169 // apply an increasing y perspective as we move to the right | 183 // apply an increasing y perspective as we move to the right |
170 SkMatrix perspective; | 184 SkMatrix perspective; |
171 perspective.setIdentity(); | 185 perspective.setIdentity(); |
172 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 186 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
173 SkIntToScalar(500))); | 187 SkIntToScalar(500))); |
174 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 188 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
175 SkIntToScalar(10))); | 189 SkIntToScalar(10))); |
| 190 |
176 shader->setLocalMatrix(perspective); | 191 shader->setLocalMatrix(perspective); |
177 | 192 |
178 paint.setShader(shader); | 193 paint.setShader(shader); |
179 canvas->drawRect(r, paint); | 194 canvas->drawRect(r, paint); |
180 shader->unref(); | 195 shader->unref(); |
181 canvas->translate(0, SkIntToScalar(120)); | 196 canvas->translate(0, SkIntToScalar(120)); |
182 } | 197 } |
183 canvas->restore(); | 198 canvas->restore(); |
184 canvas->translate(SkIntToScalar(120), 0); | 199 canvas->translate(SkIntToScalar(120), 0); |
185 } | 200 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 375 |
361 static GM* MyFactory4(void*) { return new RadialGradientGM; } | 376 static GM* MyFactory4(void*) { return new RadialGradientGM; } |
362 static GMRegistry reg4(MyFactory4); | 377 static GMRegistry reg4(MyFactory4); |
363 | 378 |
364 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } | 379 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } |
365 static GMRegistry reg5(MyFactory5); | 380 static GMRegistry reg5(MyFactory5); |
366 | 381 |
367 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } | 382 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } |
368 static GMRegistry reg6(MyFactory6); | 383 static GMRegistry reg6(MyFactory6); |
369 } | 384 } |
OLD | NEW |