1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkTypes.h"
17
18 typedef SkScalar (*MakePathProc)(SkPath*);
19
make_triangle(SkPath * path)20 static SkScalar make_triangle(SkPath* path) {
21 constexpr int gCoord[] = {
22 10, 20, 15, 5, 30, 30
23 };
24 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
25 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
26 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
27 path->close();
28 path->offset(SkIntToScalar(10), SkIntToScalar(0));
29 return SkIntToScalar(30);
30 }
31
make_rect(SkPath * path)32 static SkScalar make_rect(SkPath* path) {
33 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
34 SkIntToScalar(30), SkIntToScalar(30) };
35 path->addRect(r);
36 path->offset(SkIntToScalar(10), SkIntToScalar(0));
37 return SkIntToScalar(30);
38 }
39
make_oval(SkPath * path)40 static SkScalar make_oval(SkPath* path) {
41 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
42 SkIntToScalar(30), SkIntToScalar(30) };
43 path->addOval(r);
44 path->offset(SkIntToScalar(10), SkIntToScalar(0));
45 return SkIntToScalar(30);
46 }
47
make_star(SkPath * path,int n)48 static SkScalar make_star(SkPath* path, int n) {
49 const SkScalar c = SkIntToScalar(45);
50 const SkScalar r = SkIntToScalar(20);
51
52 SkScalar rad = -SK_ScalarPI / 2;
53 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
54
55 path->moveTo(c, c - r);
56 for (int i = 1; i < n; i++) {
57 rad += drad;
58 path->lineTo(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
59 }
60 path->close();
61 return r * 2 * 6 / 5;
62 }
63
make_star_5(SkPath * path)64 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
make_star_13(SkPath * path)65 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
66
make_three_line(SkPath * path)67 static SkScalar make_three_line(SkPath* path) {
68 static SkScalar xOffset = 34.f;
69 static SkScalar yOffset = 50.f;
70 path->moveTo(-32.5f + xOffset, 0.0f + yOffset);
71 path->lineTo(32.5f + xOffset, 0.0f + yOffset);
72
73 path->moveTo(-32.5f + xOffset, 19 + yOffset);
74 path->lineTo(32.5f + xOffset, 19 + yOffset);
75
76 path->moveTo(-32.5f + xOffset, -19 + yOffset);
77 path->lineTo(32.5f + xOffset, -19 + yOffset);
78 path->lineTo(-32.5f + xOffset, -19 + yOffset);
79
80 path->close();
81
82 return SkIntToScalar(70);
83 }
84
make_arrow(SkPath * path)85 static SkScalar make_arrow(SkPath* path) {
86 static SkScalar xOffset = 34.f;
87 static SkScalar yOffset = 40.f;
88 path->moveTo(-26.f + xOffset, 0.0f + yOffset);
89 path->lineTo(26.f + xOffset, 0.0f + yOffset);
90
91 path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
92 path->lineTo(0 + xOffset, 25.525126f + yOffset);
93
94 path->moveTo(-28.f + xOffset, 2.4748745f + yOffset);
95 path->lineTo(0 + xOffset, -25.525126f + yOffset);
96 path->lineTo(-28.f + xOffset, 2.4748745f + yOffset);
97
98 path->close();
99
100 return SkIntToScalar(70);
101 }
102
make_curve(SkPath * path)103 static SkScalar make_curve(SkPath* path) {
104 static SkScalar xOffset = -382.f;
105 static SkScalar yOffset = -50.f;
106 path->moveTo(491 + xOffset, 56 + yOffset);
107 path->conicTo(435.93292f + xOffset, 56.000031f + yOffset,
108 382.61078f + xOffset, 69.752716f + yOffset,
109 0.9920463f);
110
111 return SkIntToScalar(40);
112 }
113
make_battery(SkPath * path)114 static SkScalar make_battery(SkPath* path) {
115 static SkScalar xOffset = 5.0f;
116
117 path->moveTo(24.67f + xOffset, 0.33000004f);
118 path->lineTo(8.3299999f + xOffset, 0.33000004f);
119 path->lineTo(8.3299999f + xOffset, 5.3299999f);
120 path->lineTo(0.33000004f + xOffset, 5.3299999f);
121 path->lineTo(0.33000004f + xOffset, 50.669998f);
122 path->lineTo(32.669998f + xOffset, 50.669998f);
123 path->lineTo(32.669998f + xOffset, 5.3299999f);
124 path->lineTo(24.67f + xOffset, 5.3299999f);
125 path->lineTo(24.67f + xOffset, 0.33000004f);
126 path->close();
127
128 path->moveTo(25.727224f + xOffset, 12.886665f);
129 path->lineTo(10.907918f + xOffset, 12.886665f);
130 path->lineTo(7.5166659f + xOffset, 28.683645f);
131 path->lineTo(14.810181f + xOffset, 28.683645f);
132 path->lineTo(7.7024879f + xOffset, 46.135998f);
133 path->lineTo(28.049999f + xOffset, 25.136419f);
134 path->lineTo(16.854223f + xOffset, 25.136419f);
135 path->lineTo(25.727224f + xOffset, 12.886665f);
136 path->close();
137 return SkIntToScalar(50);
138 }
139
make_battery2(SkPath * path)140 static SkScalar make_battery2(SkPath* path) {
141 static SkScalar xOffset = 225.625f;
142
143 path->moveTo(32.669998f + xOffset, 9.8640003f);
144 path->lineTo(0.33000004f + xOffset, 9.8640003f);
145 path->lineTo(0.33000004f + xOffset, 50.669998f);
146 path->lineTo(32.669998f + xOffset, 50.669998f);
147 path->lineTo(32.669998f + xOffset, 9.8640003f);
148 path->close();
149
150 path->moveTo(10.907918f + xOffset, 12.886665f);
151 path->lineTo(25.727224f + xOffset, 12.886665f);
152 path->lineTo(16.854223f + xOffset, 25.136419f);
153 path->lineTo(28.049999f + xOffset, 25.136419f);
154 path->lineTo(7.7024879f + xOffset, 46.135998f);
155 path->lineTo(14.810181f + xOffset, 28.683645f);
156 path->lineTo(7.5166659f + xOffset, 28.683645f);
157 path->lineTo(10.907918f + xOffset, 12.886665f);
158 path->close();
159
160 return SkIntToScalar(70);
161 }
162
163 constexpr MakePathProc gProcs[] = {
164 make_triangle,
165 make_rect,
166 make_oval,
167 make_star_5,
168 make_star_13,
169 make_three_line,
170 make_arrow,
171 make_curve,
172 make_battery,
173 make_battery2
174 };
175
176 constexpr SkScalar gWidths[] = {
177 2.0f,
178 3.0f,
179 4.0f,
180 5.0f,
181 6.0f,
182 7.0f,
183 7.0f,
184 14.0f,
185 0.0f,
186 0.0f,
187 };
188
189 constexpr SkScalar gMiters[] = {
190 2.0f,
191 3.0f,
192 3.0f,
193 3.0f,
194 4.0f,
195 4.0f,
196 4.0f,
197 4.0f,
198 4.0f,
199 4.0f,
200 };
201
202 constexpr SkScalar gXTranslate[] = {
203 0.0f,
204 0.0f,
205 0.0f,
206 0.0f,
207 0.0f,
208 0.0f,
209 0.0f,
210 0.0f,
211 -220.625f,
212 0.0f,
213 };
214
215 #define N SK_ARRAY_COUNT(gProcs)
216
217 // This GM tests out drawing small paths (i.e., for Ganesh, using the Distance
218 // Field path renderer) which are filled, stroked and filledAndStroked. In
219 // particular this ensures that any cache keys in use include the stroking
220 // parameters.
221 class SmallPathsGM : public skiagm::GM {
222 SkPath fPath[N];
223 SkScalar fDY[N];
224 protected:
onOnceBeforeDraw()225 void onOnceBeforeDraw() override {
226 for (size_t i = 0; i < N; i++) {
227 fDY[i] = gProcs[i](&fPath[i]);
228 }
229 }
230
onShortName()231 SkString onShortName() override {
232 return SkString("smallpaths");
233 }
234
onISize()235 SkISize onISize() override {
236 return SkISize::Make(640, 480);
237 }
238
onDraw(SkCanvas * canvas)239 void onDraw(SkCanvas* canvas) override {
240 SkPaint paint;
241 paint.setAntiAlias(true);
242
243 // first column: filled paths
244 canvas->save();
245 for (size_t i = 0; i < N; i++) {
246 canvas->drawPath(fPath[i], paint);
247 canvas->translate(gXTranslate[i], fDY[i]);
248 }
249 canvas->restore();
250 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
251
252 // second column: stroked paths
253 canvas->save();
254 paint.setStyle(SkPaint::kStroke_Style);
255 paint.setStrokeCap(SkPaint::kButt_Cap);
256 for (size_t i = 0; i < N; i++) {
257 paint.setStrokeWidth(gWidths[i]);
258 paint.setStrokeMiter(gMiters[i]);
259 canvas->drawPath(fPath[i], paint);
260 canvas->translate(gXTranslate[i], fDY[i]);
261 }
262 canvas->restore();
263 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
264
265 // third column: stroked paths with different widths
266 canvas->save();
267 paint.setStyle(SkPaint::kStroke_Style);
268 paint.setStrokeCap(SkPaint::kButt_Cap);
269 for (size_t i = 0; i < N; i++) {
270 paint.setStrokeWidth(gWidths[i] + 2.0f);
271 paint.setStrokeMiter(gMiters[i]);
272 canvas->drawPath(fPath[i], paint);
273 canvas->translate(gXTranslate[i], fDY[i]);
274 }
275 canvas->restore();
276 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
277
278 // fourth column: stroked and filled paths
279 paint.setStyle(SkPaint::kStrokeAndFill_Style);
280 paint.setStrokeCap(SkPaint::kButt_Cap);
281 for (size_t i = 0; i < N; i++) {
282 paint.setStrokeWidth(gWidths[i]);
283 paint.setStrokeMiter(gMiters[i]);
284 canvas->drawPath(fPath[i], paint);
285 canvas->translate(gXTranslate[i], fDY[i]);
286 }
287
288 }
289
290 private:
291 typedef skiagm::GM INHERITED;
292 };
293
294 DEF_GM(return new SmallPathsGM;)
295