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, const SkMatrix& localMatrix)
{ |
44 const SkMatrix& localMatrix) { | |
45 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, | 44 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
46 data.fCount, tm, mapper, 0, &localMatr
ix); | 45 data.fCount, tm, 0, &localMatrix); |
47 } | 46 } |
48 | 47 |
49 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, | 48 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
50 SkShader::TileMode tm, SkUnitMapper* mapper, | 49 SkShader::TileMode tm, const SkMatrix& localMatrix)
{ |
51 const SkMatrix& localMatrix) { | |
52 SkPoint center; | 50 SkPoint center; |
53 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 51 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
54 SkScalarAve(pts[0].fY, pts[1].fY)); | 52 SkScalarAve(pts[0].fY, pts[1].fY)); |
55 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 53 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
56 data.fPos, data.fCount, tm, mapper, 0,
&localMatrix); | 54 data.fPos, data.fCount, tm, 0, &localM
atrix); |
57 } | 55 } |
58 | 56 |
59 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, | 57 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
60 SkShader::TileMode, SkUnitMapper* mapper, | 58 SkShader::TileMode, const SkMatrix& localMatrix) { |
61 const SkMatrix& localMatrix) { | |
62 SkPoint center; | 59 SkPoint center; |
63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 60 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
64 SkScalarAve(pts[0].fY, pts[1].fY)); | 61 SkScalarAve(pts[0].fY, pts[1].fY)); |
65 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, | 62 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
66 data.fPos, data.fCount, mapper, 0, &loc
alMatrix); | 63 data.fPos, data.fCount, 0, &localMatrix
); |
67 } | 64 } |
68 | 65 |
69 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, | 66 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
70 SkShader::TileMode tm, SkUnitMapper* mapper, | 67 SkShader::TileMode tm, const SkMatrix& localMatrix)
{ |
71 const SkMatrix& localMatrix) { | |
72 SkPoint center0, center1; | 68 SkPoint center0, center1; |
73 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 69 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
74 SkScalarAve(pts[0].fY, pts[1].fY)); | 70 SkScalarAve(pts[0].fY, pts[1].fY)); |
75 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 71 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
76 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 72 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
77 return SkGradientShader::CreateTwoPointRadial( | 73 return SkGradientShader::CreateTwoPointRadial( |
78 center1, (pts[1].fX - pts[0].f
X) / 7, | 74 center1, (pts[1].fX - pts[0].f
X) / 7, |
79 center0, (pts[1].fX - pts[0].f
X) / 2, | 75 center0, (pts[1].fX - pts[0].f
X) / 2, |
80 data.fColors, data.fPos, data.
fCount, tm, mapper, | 76 data.fColors, data.fPos, data.
fCount, tm, |
81 0, &localMatrix); | 77 0, &localMatrix); |
82 } | 78 } |
83 | 79 |
84 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, | 80 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
85 SkShader::TileMode tm, SkUnitMapper* mapper, | 81 SkShader::TileMode tm, const SkMatrix& localMatrix)
{ |
86 const SkMatrix& localMatrix) { | |
87 SkPoint center0, center1; | 82 SkPoint center0, center1; |
88 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); | 83 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
89 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); | 84 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
90 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 85 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
91 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 86 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
92 return SkGradientShader::CreateTwoPointConical(center1, radius1, | 87 return SkGradientShader::CreateTwoPointConical(center1, radius1, |
93 center0, radius0, | 88 center0, radius0, |
94 data.fColors, data.fPos, | 89 data.fColors, data.fPos, |
95 data.fCount, tm, mapper, | 90 data.fCount, tm, 0, &localMat
rix); |
96 0, &localMatrix); | |
97 } | 91 } |
98 | 92 |
99 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, | 93 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
100 SkShader::TileMode tm, SkUnitMapper* mapper, | 94 SkShader::TileMode tm, const SkMatrix& localMatri
x); |
101 const SkMatrix& localMatrix); | |
102 static const GradMaker gGradMakers[] = { | 95 static const GradMaker gGradMakers[] = { |
103 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical | 96 MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical |
104 }; | 97 }; |
105 | 98 |
106 /////////////////////////////////////////////////////////////////////////////// | 99 /////////////////////////////////////////////////////////////////////////////// |
107 | 100 |
108 class GradientsGM : public GM { | 101 class GradientsGM : public GM { |
109 public: | 102 public: |
110 GradientsGM() { | 103 GradientsGM() { |
111 this->setBGColor(0xFFDDDDDD); | 104 this->setBGColor(0xFFDDDDDD); |
(...skipping 25 matching lines...) Expand all Loading... |
137 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { | 130 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
138 canvas->save(); | 131 canvas->save(); |
139 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 132 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
140 SkMatrix scale = SkMatrix::I(); | 133 SkMatrix scale = SkMatrix::I(); |
141 | 134 |
142 if (i == 5) { // if the clamp case | 135 if (i == 5) { // if the clamp case |
143 scale.setScale(0.5f, 0.5f); | 136 scale.setScale(0.5f, 0.5f); |
144 scale.postTranslate(25.f, 25.f); | 137 scale.postTranslate(25.f, 25.f); |
145 } | 138 } |
146 | 139 |
147 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL, s
cale); | 140 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, scale); |
148 | 141 |
149 paint.setShader(shader); | 142 paint.setShader(shader); |
150 canvas->drawRect(r, paint); | 143 canvas->drawRect(r, paint); |
151 shader->unref(); | 144 shader->unref(); |
152 canvas->translate(0, SkIntToScalar(120)); | 145 canvas->translate(0, SkIntToScalar(120)); |
153 } | 146 } |
154 canvas->restore(); | 147 canvas->restore(); |
155 canvas->translate(SkIntToScalar(120), 0); | 148 canvas->translate(SkIntToScalar(120), 0); |
156 } | 149 } |
157 } | 150 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 canvas->save(); | 188 canvas->save(); |
196 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { | 189 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
197 // apply an increasing y perspective as we move to the right | 190 // apply an increasing y perspective as we move to the right |
198 SkMatrix perspective; | 191 SkMatrix perspective; |
199 perspective.setIdentity(); | 192 perspective.setIdentity(); |
200 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 193 perspective.setPerspY(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
201 SkIntToScalar(500))); | 194 SkIntToScalar(500))); |
202 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), | 195 perspective.setSkewX(SkScalarDiv(SkIntToScalar((unsigned) i+1), |
203 SkIntToScalar(10))); | 196 SkIntToScalar(10))); |
204 | 197 |
205 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL, p
erspective); | 198 SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, perspec
tive); |
206 | 199 |
207 paint.setShader(shader); | 200 paint.setShader(shader); |
208 canvas->drawRect(r, paint); | 201 canvas->drawRect(r, paint); |
209 shader->unref(); | 202 shader->unref(); |
210 canvas->translate(0, SkIntToScalar(120)); | 203 canvas->translate(0, SkIntToScalar(120)); |
211 } | 204 } |
212 canvas->restore(); | 205 canvas->restore(); |
213 canvas->translate(SkIntToScalar(120), 0); | 206 canvas->translate(SkIntToScalar(120), 0); |
214 } | 207 } |
215 } | 208 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; | 313 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(300) }; |
321 SkPaint paint; | 314 SkPaint paint; |
322 paint.setAntiAlias(true); | 315 paint.setAntiAlias(true); |
323 | 316 |
324 SkPoint center; | 317 SkPoint center; |
325 center.iset(0, 300); | 318 center.iset(0, 300); |
326 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); | 319 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
327 SkShader* shader = SkGradientShader::CreateRadial( | 320 SkShader* shader = SkGradientShader::CreateRadial( |
328 SkPoint(center), | 321 SkPoint(center), |
329 SkIntToScalar(200), gColors, NULL, 5, | 322 SkIntToScalar(200), gColors, NULL, 5, |
330 SkShader::kClamp_TileMode, NULL); | 323 SkShader::kClamp_TileMode); |
331 paint.setShader(shader); | 324 paint.setShader(shader); |
332 canvas->drawRect(r, paint); | 325 canvas->drawRect(r, paint); |
333 shader->unref(); | 326 shader->unref(); |
334 } | 327 } |
335 | 328 |
336 private: | 329 private: |
337 typedef GM INHERITED; | 330 typedef GM INHERITED; |
338 }; | 331 }; |
339 | 332 |
340 /// Checks quality of large radial gradients, which may display | 333 /// Checks quality of large radial gradients, which may display |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 center.set(cx, cy); | 408 center.set(cx, cy); |
416 | 409 |
417 // We can either interpolate endpoints and premultiply each point (defau
lt, more precision), | 410 // We can either interpolate endpoints and premultiply each point (defau
lt, more precision), |
418 // or premultiply the endpoints first, avoiding the need to premultiply
each point (cheap). | 411 // or premultiply the endpoints first, avoiding the need to premultiply
each point (cheap). |
419 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPrem
ul_Flag }; | 412 const uint32_t flags[] = { 0, SkGradientShader::kInterpolateColorsInPrem
ul_Flag }; |
420 | 413 |
421 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { | 414 for (size_t i = 0; i < SK_ARRAY_COUNT(flags); i++) { |
422 SkAutoTUnref<SkShader> sweep( | 415 SkAutoTUnref<SkShader> sweep( |
423 SkGradientShader::CreateSweep(cx, cy, sweep_colors, | 416 SkGradientShader::CreateSweep(cx, cy, sweep_colors, |
424 NULL, SK_ARRAY_COUNT(sweep_col
ors), | 417 NULL, SK_ARRAY_COUNT(sweep_col
ors), |
425 NULL, flags[i])); | 418 flags[i], NULL)); |
426 SkAutoTUnref<SkShader> radial1( | 419 SkAutoTUnref<SkShader> radial1( |
427 SkGradientShader::CreateRadial(center, radius, colors1, | 420 SkGradientShader::CreateRadial(center, radius, colors1, |
428 NULL, SK_ARRAY_COUNT(colors1)
, | 421 NULL, SK_ARRAY_COUNT(colors1)
, |
429 SkShader::kClamp_TileMode, | 422 SkShader::kClamp_TileMode, |
430 NULL, flags[i])); | 423 flags[i], NULL)); |
431 SkAutoTUnref<SkShader> radial2( | 424 SkAutoTUnref<SkShader> radial2( |
432 SkGradientShader::CreateRadial(center, radius, colors2, | 425 SkGradientShader::CreateRadial(center, radius, colors2, |
433 NULL, SK_ARRAY_COUNT(colors2)
, | 426 NULL, SK_ARRAY_COUNT(colors2)
, |
434 SkShader::kClamp_TileMode, | 427 SkShader::kClamp_TileMode, |
435 NULL, flags[i])); | 428 flags[i], NULL)); |
436 paint1.setShader(sweep); | 429 paint1.setShader(sweep); |
437 paint2.setShader(radial1); | 430 paint2.setShader(radial1); |
438 paint3.setShader(radial2); | 431 paint3.setShader(radial2); |
439 | 432 |
440 canvas->drawCircle(cx, cy, radius, paint1); | 433 canvas->drawCircle(cx, cy, radius, paint1); |
441 canvas->drawCircle(cx, cy, radius, paint3); | 434 canvas->drawCircle(cx, cy, radius, paint3); |
442 canvas->drawCircle(cx, cy, radius, paint2); | 435 canvas->drawCircle(cx, cy, radius, paint2); |
443 | 436 |
444 canvas->translate(400, 0); | 437 canvas->translate(400, 0); |
445 } | 438 } |
(...skipping 19 matching lines...) Expand all Loading... |
465 | 458 |
466 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } | 459 static GM* MyFactory5(void*) { return new GradientsLocalPerspectiveGM; } |
467 static GMRegistry reg5(MyFactory5); | 460 static GMRegistry reg5(MyFactory5); |
468 | 461 |
469 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } | 462 static GM* MyFactory6(void*) { return new GradientsViewPerspectiveGM; } |
470 static GMRegistry reg6(MyFactory6); | 463 static GMRegistry reg6(MyFactory6); |
471 | 464 |
472 static GM* MyFactory7(void*) { return new RadialGradient2GM; } | 465 static GM* MyFactory7(void*) { return new RadialGradient2GM; } |
473 static GMRegistry reg7(MyFactory7); | 466 static GMRegistry reg7(MyFactory7); |
474 } | 467 } |
OLD | NEW |