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