OLD | NEW |
| 1 |
1 /* | 2 /* |
2 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
3 * | 4 * |
4 * 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 |
5 * found in the LICENSE file. | 6 * found in the LICENSE file. |
6 */ | 7 */ |
7 | |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
| 11 #include "SkUnitMappers.h" |
11 | 12 |
12 namespace skiagm { | 13 namespace skiagm { |
13 | 14 |
14 static void makebm(SkBitmap* bm, int w, int h) { | 15 static void makebm(SkBitmap* bm, int w, int h) { |
15 bm->allocN32Pixels(w, h); | 16 bm->allocN32Pixels(w, h); |
16 bm->eraseColor(SK_ColorTRANSPARENT); | 17 bm->eraseColor(SK_ColorTRANSPARENT); |
17 | 18 |
18 SkCanvas canvas(*bm); | 19 SkCanvas canvas(*bm); |
19 SkScalar s = SkIntToScalar(SkMin32(w, h)); | 20 SkScalar s = SkIntToScalar(SkMin32(w, h)); |
20 SkPoint pts[] = { { 0, 0 }, { s, s } }; | 21 SkPoint pts[] = { { 0, 0 }, { s, s } }; |
21 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; | 22 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
22 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | 23 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
23 SkPaint paint; | 24 SkPaint paint; |
24 | 25 |
| 26 SkUnitMapper* um = NULL; |
| 27 |
| 28 um = new SkCosineMapper; |
| 29 |
| 30 SkAutoUnref au(um); |
| 31 |
25 paint.setDither(true); | 32 paint.setDither(true); |
26 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, | 33 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
27 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref(); | 34 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref()
; |
28 canvas.drawPaint(paint); | 35 canvas.drawPaint(paint); |
29 } | 36 } |
30 | 37 |
31 static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty, | 38 static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty, |
32 int w, int h) { | 39 int w, int h) { |
33 static SkBitmap bmp; | 40 static SkBitmap bmp; |
34 if (bmp.isNull()) { | 41 if (bmp.isNull()) { |
35 makebm(&bmp, w/2, h/4); | 42 makebm(&bmp, w/2, h/4); |
36 } | 43 } |
37 return SkShader::CreateBitmapShader(bmp, tx, ty); | 44 return SkShader::CreateBitmapShader(bmp, tx, ty); |
38 } | 45 } |
39 | 46 |
40 /////////////////////////////////////////////////////////////////////////////// | 47 /////////////////////////////////////////////////////////////////////////////// |
41 | 48 |
42 struct GradData { | 49 struct GradData { |
43 int fCount; | 50 int fCount; |
44 const SkColor* fColors; | 51 const SkColor* fColors; |
45 const SkScalar* fPos; | 52 const SkScalar* fPos; |
46 }; | 53 }; |
47 | 54 |
48 static const SkColor gColors[] = { | 55 static const SkColor gColors[] = { |
49 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK | 56 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
50 }; | 57 }; |
51 | 58 |
52 static const GradData gGradData[] = { | 59 static const GradData gGradData[] = { |
53 { 2, gColors, NULL }, | 60 { 2, gColors, NULL }, |
54 { 5, gColors, NULL }, | 61 { 5, gColors, NULL }, |
55 }; | 62 }; |
56 | 63 |
57 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 64 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
58 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); | 65 SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 66 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 67 data.fCount, tm, mapper); |
59 } | 68 } |
60 | 69 |
61 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 70 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 71 SkShader::TileMode tm, SkUnitMapper* mapper) { |
62 SkPoint center; | 72 SkPoint center; |
63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 73 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
64 SkScalarAve(pts[0].fY, pts[1].fY)); | 74 SkScalarAve(pts[0].fY, pts[1].fY)); |
65 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 75 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
66 data.fPos, data.fCount, tm); | 76 data.fPos, data.fCount, tm, mapper); |
67 } | 77 } |
68 | 78 |
69 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader:
:TileMode) { | 79 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 80 SkShader::TileMode, SkUnitMapper* mapper) { |
70 SkPoint center; | 81 SkPoint center; |
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 82 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
72 SkScalarAve(pts[0].fY, pts[1].fY)); | 83 SkScalarAve(pts[0].fY, pts[1].fY)); |
73 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat
a.fPos, data.fCount); | 84 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 85 data.fPos, data.fCount, mapper); |
74 } | 86 } |
75 | 87 |
76 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, SkShade
r::TileMode tm) { | 88 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 89 SkShader::TileMode tm, SkUnitMapper* mapper) { |
77 SkPoint center0, center1; | 90 SkPoint center0, center1; |
78 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 91 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
79 SkScalarAve(pts[0].fY, pts[1].fY)); | 92 SkScalarAve(pts[0].fY, pts[1].fY)); |
80 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 93 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
81 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 94 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
82 return SkGradientShader::CreateTwoPointRadial( | 95 return SkGradientShader::CreateTwoPointRadial( |
83 center1, (pts[1].fX - pts[0].fX) / 7, | 96 center1, (pts[1].fX - pts[0].fX) / 7, |
84 center0, (pts[1].fX - pts[0].fX) / 2, | 97 center0, (pts[1].fX - pts[0].fX) / 2, |
85 data.fColors, data.fPos, data.fCount, tm); | 98 data.fColors, data.fPos, data.fCount, tm, mapper); |
86 } | 99 } |
87 | 100 |
88 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha
der::TileMode tm); | 101 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
89 | 102 SkShader::TileMode tm, SkUnitMapper* mapper); |
90 static const GradMaker gGradMakers[] = { | 103 static const GradMaker gGradMakers[] = { |
91 MakeLinear, MakeRadial, MakeSweep, Make2Radial | 104 MakeLinear, MakeRadial, MakeSweep, Make2Radial |
92 }; | 105 }; |
93 | 106 |
94 /////////////////////////////////////////////////////////////////////////////// | 107 /////////////////////////////////////////////////////////////////////////////// |
95 | 108 |
96 class ShaderTextGM : public GM { | 109 class ShaderTextGM : public GM { |
97 public: | 110 public: |
98 ShaderTextGM() { | 111 ShaderTextGM() { |
99 this->setBGColor(0xFFDDDDDD); | 112 this->setBGColor(0xFFDDDDDD); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 SK_ARRAY_COUNT(gGradMakers); | 147 SK_ARRAY_COUNT(gGradMakers); |
135 static const int bmpCount = SK_ARRAY_COUNT(tileModes) * | 148 static const int bmpCount = SK_ARRAY_COUNT(tileModes) * |
136 SK_ARRAY_COUNT(tileModes); | 149 SK_ARRAY_COUNT(tileModes); |
137 SkShader* shaders[gradCount + bmpCount]; | 150 SkShader* shaders[gradCount + bmpCount]; |
138 | 151 |
139 int shdIdx = 0; | 152 int shdIdx = 0; |
140 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { | 153 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { |
141 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { | 154 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { |
142 shaders[shdIdx++] = gGradMakers[m](pts, | 155 shaders[shdIdx++] = gGradMakers[m](pts, |
143 gGradData[d], | 156 gGradData[d], |
144 SkShader::kClamp_TileMode); | 157 SkShader::kClamp_TileMode, |
| 158 NULL); |
145 } | 159 } |
146 } | 160 } |
147 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { | 161 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { |
148 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { | 162 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { |
149 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx], | 163 shaders[shdIdx++] = MakeBitmapShader(tileModes[tx], |
150 tileModes[ty], | 164 tileModes[ty], |
151 w/8, h); | 165 w/8, h); |
152 } | 166 } |
153 } | 167 } |
154 | 168 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 206 |
193 private: | 207 private: |
194 typedef GM INHERITED; | 208 typedef GM INHERITED; |
195 }; | 209 }; |
196 | 210 |
197 /////////////////////////////////////////////////////////////////////////////// | 211 /////////////////////////////////////////////////////////////////////////////// |
198 | 212 |
199 static GM* MyFactory(void*) { return new ShaderTextGM; } | 213 static GM* MyFactory(void*) { return new ShaderTextGM; } |
200 static GMRegistry reg(MyFactory); | 214 static GMRegistry reg(MyFactory); |
201 } | 215 } |
OLD | NEW |