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