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