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.setTranslateX(25.f); | |
reed1
2013/08/06 21:19:17
nit: more natural (perhaps) to say postTrnaslate(2
| |
134 scale.setTranslateY(25.f); | |
135 shader->setLocalMatrix(scale); | |
136 } | |
137 | |
123 paint.setShader(shader); | 138 paint.setShader(shader); |
124 canvas->drawRect(r, paint); | 139 canvas->drawRect(r, paint); |
125 shader->unref(); | 140 shader->unref(); |
126 canvas->translate(0, SkIntToScalar(120)); | 141 canvas->translate(0, SkIntToScalar(120)); |
127 } | 142 } |
128 canvas->restore(); | 143 canvas->restore(); |
129 canvas->translate(SkIntToScalar(120), 0); | 144 canvas->translate(SkIntToScalar(120), 0); |
130 } | 145 } |
131 } | 146 } |
132 | 147 |
(...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++) { | 181 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
167 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); | 182 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
168 | 183 |
169 // apply an increasing y perspective as we move to the right | 184 // apply an increasing y perspective as we move to the right |
170 SkMatrix perspective; | 185 SkMatrix perspective; |
171 perspective.setIdentity(); | 186 perspective.setIdentity(); |
172 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 187 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
173 SkIntToScalar(500))); | 188 SkIntToScalar(500))); |
174 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 189 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
175 SkIntToScalar(10))); | 190 SkIntToScalar(10))); |
191 | |
176 shader->setLocalMatrix(perspective); | 192 shader->setLocalMatrix(perspective); |
177 | 193 |
178 paint.setShader(shader); | 194 paint.setShader(shader); |
179 canvas->drawRect(r, paint); | 195 canvas->drawRect(r, paint); |
180 shader->unref(); | 196 shader->unref(); |
181 canvas->translate(0, SkIntToScalar(120)); | 197 canvas->translate(0, SkIntToScalar(120)); |
182 } | 198 } |
183 canvas->restore(); | 199 canvas->restore(); |
184 canvas->translate(SkIntToScalar(120), 0); | 200 canvas->translate(SkIntToScalar(120), 0); |
185 } | 201 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 | 376 |
361 static GM* MyFactory4(void*) { return new RadialGradientGM; } | 377 static GM* MyFactory4(void*) { return new RadialGradientGM; } |
362 static GMRegistry reg4(MyFactory4); | 378 static GMRegistry reg4(MyFactory4); |
363 | 379 |
364 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } | 380 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } |
365 static GMRegistry reg5(MyFactory5); | 381 static GMRegistry reg5(MyFactory5); |
366 | 382 |
367 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } | 383 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } |
368 static GMRegistry reg6(MyFactory6); | 384 static GMRegistry reg6(MyFactory6); |
369 } | 385 } |
OLD | NEW |