OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 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 "SkPath.h" | 11 #include "SkPath.h" |
12 | 12 |
13 namespace skiagm { | 13 namespace skiagm { |
14 | 14 |
15 static void makebm(SkBitmap* bm, int w, int h) { | 15 static void makebm(SkBitmap* bm, int w, int h) { |
16 bm->allocN32Pixels(w, h); | 16 bm->allocN32Pixels(w, h); |
17 bm->eraseColor(SK_ColorTRANSPARENT); | 17 bm->eraseColor(SK_ColorTRANSPARENT); |
18 | 18 |
19 SkCanvas canvas(*bm); | 19 SkCanvas canvas(*bm); |
20 SkScalar s = SkIntToScalar(SkMin32(w, h)); | 20 SkScalar s = SkIntToScalar(SkMin32(w, h)); |
21 SkPoint pts[] = { { 0, 0 }, { s, s } }; | 21 SkPoint pts[] = { { 0, 0 }, { s, s } }; |
22 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; | 22 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
23 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; | 23 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
24 SkPaint paint; | 24 SkPaint paint; |
25 | 25 |
26 paint.setDither(true); | 26 paint.setDither(true); |
27 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, | 27 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos, SK_ARRAY_COUN
T(colors), |
28 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode))->unref(); | 28 SkShader::kClamp_TileMode)); |
29 canvas.drawPaint(paint); | 29 canvas.drawPaint(paint); |
30 } | 30 } |
31 | 31 |
32 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
33 | 33 |
34 struct GradData { | 34 struct GradData { |
35 int fCount; | 35 int fCount; |
36 const SkColor* fColors; | 36 const SkColor* fColors; |
37 const SkScalar* fPos; | 37 const SkScalar* fPos; |
38 }; | 38 }; |
39 | 39 |
40 static const SkColor gColors[] = { | 40 static const SkColor gColors[] = { |
41 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK | 41 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
42 }; | 42 }; |
43 | 43 |
44 static const GradData gGradData[] = { | 44 static const GradData gGradData[] = { |
45 { 2, gColors, nullptr }, | 45 { 2, gColors, nullptr }, |
46 { 5, gColors, nullptr }, | 46 { 5, gColors, nullptr }, |
47 }; | 47 }; |
48 | 48 |
49 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 49 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, Sk
Shader::TileMode tm) { |
50 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); | 50 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun
t, tm); |
51 } | 51 } |
52 | 52 |
53 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader
::TileMode tm) { | 53 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, Sk
Shader::TileMode tm) { |
54 SkPoint center; | 54 SkPoint center; |
55 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 55 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
56 SkScalarAve(pts[0].fY, pts[1].fY)); | 56 SkScalarAve(pts[0].fY, pts[1].fY)); |
57 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, | 57 return SkGradientShader::MakeRadial(center, center.fX, data.fColors, data.fP
os, data.fCount, |
58 data.fPos, data.fCount, tm); | 58 tm); |
59 } | 59 } |
60 | 60 |
61 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader:
:TileMode) { | 61 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkS
hader::TileMode) { |
62 SkPoint center; | 62 SkPoint center; |
63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
64 SkScalarAve(pts[0].fY, pts[1].fY)); | 64 SkScalarAve(pts[0].fY, pts[1].fY)); |
65 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat
a.fPos, data.fCount); | 65 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.
fPos, data.fCount); |
66 } | 66 } |
67 | 67 |
68 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShad
er::TileMode tm) { | 68 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data,
SkShader::TileMode tm) { |
69 SkPoint center0, center1; | 69 SkPoint center0, center1; |
70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 70 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
71 SkScalarAve(pts[0].fY, pts[1].fY)); | 71 SkScalarAve(pts[0].fY, pts[1].fY)); |
72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 72 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 73 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
74 return SkGradientShader::CreateTwoPointConical( | 74 return SkGradientShader::MakeTwoPointConical( |
75 center1, (pts[1].fX - pts[0].fX) / 7, | 75 center1, (pts[1].fX - pts[0].fX) / 7, |
76 center0, (pts[1].fX - pts[0].fX) / 2, | 76 center0, (pts[1].fX - pts[0].fX) / 2, |
77 data.fColors, data.fPos, data.fCount, tm); | 77 data.fColors, data.fPos, data.fCount, tm); |
78 } | 78 } |
79 | 79 |
80 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha
der::TileMode tm); | 80 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
SkShader::TileMode tm); |
81 | 81 |
82 static const GradMaker gGradMakers[] = { | 82 static const GradMaker gGradMakers[] = { |
83 MakeLinear, MakeRadial, MakeSweep, Make2Conical | 83 MakeLinear, MakeRadial, MakeSweep, Make2Conical |
84 }; | 84 }; |
85 | 85 |
86 /////////////////////////////////////////////////////////////////////////////// | 86 /////////////////////////////////////////////////////////////////////////////// |
87 | 87 |
88 class ShaderTextGM : public GM { | 88 class ShaderTextGM : public GM { |
89 public: | 89 public: |
90 ShaderTextGM() { | 90 ShaderTextGM() { |
(...skipping 25 matching lines...) Expand all Loading... |
116 SkShader::TileMode tileModes[] = { | 116 SkShader::TileMode tileModes[] = { |
117 SkShader::kClamp_TileMode, | 117 SkShader::kClamp_TileMode, |
118 SkShader::kRepeat_TileMode, | 118 SkShader::kRepeat_TileMode, |
119 SkShader::kMirror_TileMode | 119 SkShader::kMirror_TileMode |
120 }; | 120 }; |
121 | 121 |
122 static const int gradCount = SK_ARRAY_COUNT(gGradData) * | 122 static const int gradCount = SK_ARRAY_COUNT(gGradData) * |
123 SK_ARRAY_COUNT(gGradMakers); | 123 SK_ARRAY_COUNT(gGradMakers); |
124 static const int bmpCount = SK_ARRAY_COUNT(tileModes) * | 124 static const int bmpCount = SK_ARRAY_COUNT(tileModes) * |
125 SK_ARRAY_COUNT(tileModes); | 125 SK_ARRAY_COUNT(tileModes); |
126 SkShader* shaders[gradCount + bmpCount]; | 126 sk_sp<SkShader> shaders[gradCount + bmpCount]; |
127 | 127 |
128 int shdIdx = 0; | 128 int shdIdx = 0; |
129 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { | 129 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { |
130 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { | 130 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { |
131 shaders[shdIdx++] = gGradMakers[m](pts, | 131 shaders[shdIdx++] = gGradMakers[m](pts, |
132 gGradData[d], | 132 gGradData[d], |
133 SkShader::kClamp_TileMode); | 133 SkShader::kClamp_TileMode); |
134 } | 134 } |
135 } | 135 } |
136 | 136 |
137 SkBitmap bm; | 137 SkBitmap bm; |
138 makebm(&bm, w/16, h/4); | 138 makebm(&bm, w/16, h/4); |
139 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { | 139 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { |
140 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { | 140 for (size_t ty = 0; ty < SK_ARRAY_COUNT(tileModes); ++ty) { |
141 shaders[shdIdx++] = SkShader::CreateBitmapShader(bm, tileModes[t
x], tileModes[ty]); | 141 shaders[shdIdx++] = SkShader::MakeBitmapShader(bm, tileModes[tx]
, tileModes[ty]); |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 SkPaint paint; | 145 SkPaint paint; |
146 paint.setDither(true); | 146 paint.setDither(true); |
147 paint.setAntiAlias(true); | 147 paint.setAntiAlias(true); |
148 sk_tool_utils::set_portable_typeface(&paint); | 148 sk_tool_utils::set_portable_typeface(&paint); |
149 paint.setTextSize(SkIntToScalar(pointSize)); | 149 paint.setTextSize(SkIntToScalar(pointSize)); |
150 | 150 |
151 canvas->save(); | 151 canvas->save(); |
152 canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); | 152 canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
153 | 153 |
154 SkPath path; | 154 SkPath path; |
155 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15), | 155 path.arcTo(SkRect::MakeXYWH(SkIntToScalar(-40), SkIntToScalar(15), |
156 SkIntToScalar(300), SkIntToScalar(90)), | 156 SkIntToScalar(300), SkIntToScalar(90)), |
157 SkIntToScalar(225), SkIntToScalar(90), | 157 SkIntToScalar(225), SkIntToScalar(90), |
158 false); | 158 false); |
159 path.close(); | 159 path.close(); |
160 | 160 |
161 static const int testsPerCol = 8; | 161 static const int testsPerCol = 8; |
162 static const int rowHeight = 60; | 162 static const int rowHeight = 60; |
163 static const int colWidth = 300; | 163 static const int colWidth = 300; |
164 canvas->save(); | 164 canvas->save(); |
165 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) { | 165 for (int s = 0; s < static_cast<int>(SK_ARRAY_COUNT(shaders)); s++) { |
166 canvas->save(); | 166 canvas->save(); |
167 int i = 2*s; | 167 int i = 2*s; |
168 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), | 168 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
169 SkIntToScalar((i % testsPerCol) * rowHeight)); | 169 SkIntToScalar((i % testsPerCol) * rowHeight)); |
170 paint.setShader(shaders[s])->unref(); | 170 paint.setShader(shaders[s]); |
171 canvas->drawText(text, textLen, 0, textBase, paint); | 171 canvas->drawText(text, textLen, 0, textBase, paint); |
172 canvas->restore(); | 172 canvas->restore(); |
173 canvas->save(); | 173 canvas->save(); |
174 ++i; | 174 ++i; |
175 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), | 175 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), |
176 SkIntToScalar((i % testsPerCol) * rowHeight)); | 176 SkIntToScalar((i % testsPerCol) * rowHeight)); |
177 canvas->drawTextOnPath(text, textLen, path, nullptr, paint); | 177 canvas->drawTextOnPath(text, textLen, path, nullptr, paint); |
178 canvas->restore(); | 178 canvas->restore(); |
179 } | 179 } |
180 canvas->restore(); | 180 canvas->restore(); |
181 | 181 |
182 } | 182 } |
183 | 183 |
184 private: | 184 private: |
185 typedef GM INHERITED; | 185 typedef GM INHERITED; |
186 }; | 186 }; |
187 | 187 |
188 /////////////////////////////////////////////////////////////////////////////// | 188 /////////////////////////////////////////////////////////////////////////////// |
189 | 189 |
190 static GM* MyFactory(void*) { return new ShaderTextGM; } | 190 static GM* MyFactory(void*) { return new ShaderTextGM; } |
191 static GMRegistry reg(MyFactory); | 191 static GMRegistry reg(MyFactory); |
192 } | 192 } |
OLD | NEW |