2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 typedef SkScalar (*MakePathProc)(SkPath*);
13 static SkScalar make_frame(SkPath* path) {
14 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
15 SkIntToScalar(630), SkIntToScalar(470) };
16 path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
19 paint.setStyle(SkPaint::kStroke_Style);
20 paint.setStrokeWidth(SkIntToScalar(5));
21 paint.getFillPath(*path, path);
22 return SkIntToScalar(15);
25 static SkScalar make_triangle(SkPath* path) {
26 constexpr int gCoord[] = {
29 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
30 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
31 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
33 path->offset(SkIntToScalar(10), SkIntToScalar(0));
34 return SkIntToScalar(30);
37 static SkScalar make_rect(SkPath* path) {
38 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
39 SkIntToScalar(30), SkIntToScalar(30) };
41 path->offset(SkIntToScalar(10), SkIntToScalar(0));
42 return SkIntToScalar(30);
45 static SkScalar make_oval(SkPath* path) {
46 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
47 SkIntToScalar(30), SkIntToScalar(30) };
49 path->offset(SkIntToScalar(10), SkIntToScalar(0));
50 return SkIntToScalar(30);
53 static SkScalar make_sawtooth(SkPath* path, int teeth) {
54 SkScalar x = SkIntToScalar(20);
55 SkScalar y = SkIntToScalar(20);
56 const SkScalar x0 = x;
57 const SkScalar dx = SkIntToScalar(5);
58 const SkScalar dy = SkIntToScalar(10);
61 for (int i = 0; i < teeth; i++) {
63 path->lineTo(x, y - dy);
65 path->lineTo(x, y + dy);
67 path->lineTo(x, y + (2 * dy));
68 path->lineTo(x0, y + (2 * dy));
70 return SkIntToScalar(30);
73 static SkScalar make_sawtooth_3(SkPath* path) { return make_sawtooth(path, 3); }
74 static SkScalar make_sawtooth_32(SkPath* path) { return make_sawtooth(path, 32); }
76 static SkScalar make_house(SkPath* path) {
78 path->lineTo(21, 11.534f);
79 path->lineTo(22.327f, 12.741f);
80 path->lineTo(23.673f, 11.261f);
81 path->lineTo(12, 0.648f);
82 path->lineTo(8, 4.285f);
85 path->lineTo(4, 7.921f);
86 path->lineTo(0.327f, 11.26f);
87 path->lineTo(1.673f, 12.74f);
88 path->lineTo(3, 11.534f);
99 path->lineTo(5, 9.715f);
100 path->lineTo(12, 3.351f);
101 path->lineTo(19, 9.715f);
102 path->lineTo(19, 21);
103 path->lineTo(15, 21);
104 path->lineTo(15, 16);
108 return SkIntToScalar(30);
111 static SkScalar make_star(SkPath* path, int n) {
112 const SkScalar c = SkIntToScalar(45);
113 const SkScalar r = SkIntToScalar(20);
115 SkScalar rad = -SK_ScalarPI / 2;
116 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
118 path->moveTo(c, c - r);
119 for (int i = 1; i < n; i++) {
121 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
122 path->lineTo(c + cosV * r, c + sinV * r);
125 return r * 2 * 6 / 5;
128 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
129 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
131 // We don't expect any output from this path.
132 static SkScalar make_line(SkPath* path) {
133 path->moveTo(SkIntToScalar(30), SkIntToScalar(30));
134 path->lineTo(SkIntToScalar(120), SkIntToScalar(40));
136 path->moveTo(SkIntToScalar(150), SkIntToScalar(30));
137 path->lineTo(SkIntToScalar(150), SkIntToScalar(30));
138 path->lineTo(SkIntToScalar(300), SkIntToScalar(40));
140 return SkIntToScalar(40);
143 static void make_info(SkPath* path) {
145 path->cubicTo(12.94999980926514f,
157 path->cubicTo(35.04999923706055f,
170 path->moveTo(26, 34);
171 path->lineTo(22, 34);
172 path->lineTo(22, 22);
173 path->lineTo(26, 22);
174 path->lineTo(26, 34);
176 path->moveTo(26, 18);
177 path->lineTo(22, 18);
178 path->lineTo(22, 14);
179 path->lineTo(26, 14);
180 path->lineTo(26, 18);
184 static void make_accessibility(SkPath* path) {
186 path->cubicTo(13.10000038146973f,
198 path->cubicTo(10.89999961853027f,
213 path->lineTo(15, 22);
214 path->lineTo(13, 22);
215 path->lineTo(13, 16);
216 path->lineTo(11, 16);
217 path->lineTo(11, 22);
227 // test case for http://crbug.com/695196
228 static void make_visualizer(SkPath* path) {
229 path->moveTo(1.9520f, 2.0000f);
230 path->conicTo(1.5573f, 1.9992f, 1.2782f, 2.2782f, 0.9235f);
231 path->conicTo(0.9992f, 2.5573f, 1.0000f, 2.9520f, 0.9235f);
232 path->lineTo(1.0000f, 5.4300f);
233 path->lineTo(17.0000f, 5.4300f);
234 path->lineTo(17.0000f, 2.9520f);
235 path->conicTo(17.0008f, 2.5573f, 16.7218f, 2.2782f, 0.9235f);
236 path->conicTo(16.4427f, 1.9992f, 16.0480f, 2.0000f, 0.9235f);
237 path->lineTo(1.9520f, 2.0000f);
239 path->moveTo(2.7140f, 3.1430f);
240 path->conicTo(3.0547f, 3.1287f, 3.2292f, 3.4216f, 0.8590f);
241 path->conicTo(3.4038f, 3.7145f, 3.2292f, 4.0074f, 0.8590f);
242 path->conicTo(3.0547f, 4.3003f, 2.7140f, 4.2860f, 0.8590f);
243 path->conicTo(2.1659f, 4.2631f, 2.1659f, 3.7145f, 0.7217f);
244 path->conicTo(2.1659f, 3.1659f, 2.7140f, 3.1430f, 0.7217f);
245 path->lineTo(2.7140f, 3.1430f);
247 path->moveTo(5.0000f, 3.1430f);
248 path->conicTo(5.3407f, 3.1287f, 5.5152f, 3.4216f, 0.8590f);
249 path->conicTo(5.6898f, 3.7145f, 5.5152f, 4.0074f, 0.8590f);
250 path->conicTo(5.3407f, 4.3003f, 5.0000f, 4.2860f, 0.8590f);
251 path->conicTo(4.4519f, 4.2631f, 4.4519f, 3.7145f, 0.7217f);
252 path->conicTo(4.4519f, 3.1659f, 5.0000f, 3.1430f, 0.7217f);
253 path->lineTo(5.0000f, 3.1430f);
255 path->moveTo(7.2860f, 3.1430f);
256 path->conicTo(7.6267f, 3.1287f, 7.8012f, 3.4216f, 0.8590f);
257 path->conicTo(7.9758f, 3.7145f, 7.8012f, 4.0074f, 0.8590f);
258 path->conicTo(7.6267f, 4.3003f, 7.2860f, 4.2860f, 0.8590f);
259 path->conicTo(6.7379f, 4.2631f, 6.7379f, 3.7145f, 0.7217f);
260 path->conicTo(6.7379f, 3.1659f, 7.2860f, 3.1430f, 0.7217f);
262 path->moveTo(1.0000f, 6.1900f);
263 path->lineTo(1.0000f, 14.3810f);
264 path->conicTo(0.9992f, 14.7757f, 1.2782f, 15.0548f, 0.9235f);
265 path->conicTo(1.5573f, 15.3338f, 1.9520f, 15.3330f, 0.9235f);
266 path->lineTo(16.0480f, 15.3330f);
267 path->conicTo(16.4427f, 15.3338f, 16.7218f, 15.0548f, 0.9235f);
268 path->conicTo(17.0008f, 14.7757f, 17.0000f, 14.3810f, 0.9235f);
269 path->lineTo(17.0000f, 6.1910f);
270 path->lineTo(1.0000f, 6.1910f);
271 path->lineTo(1.0000f, 6.1900f);
275 constexpr MakePathProc gProcs[] = {
288 #define N SK_ARRAY_COUNT(gProcs)
290 class PathFillGM : public skiagm::GM {
294 SkPath fAccessibilityPath;
295 SkPath fVisualizerPath;
297 void onOnceBeforeDraw() override {
298 for (size_t i = 0; i < N; i++) {
299 fDY[i] = gProcs[i](&fPath[i]);
302 make_info(&fInfoPath);
303 make_accessibility(&fAccessibilityPath);
304 make_visualizer(&fVisualizerPath);
308 SkString onShortName() override {
309 return SkString("pathfill");
312 SkISize onISize() override {
313 return SkISize::Make(640, 480);
316 void onDraw(SkCanvas* canvas) override {
318 paint.setAntiAlias(true);
320 for (size_t i = 0; i < N; i++) {
321 canvas->drawPath(fPath[i], paint);
322 canvas->translate(SkIntToScalar(0), fDY[i]);
326 canvas->scale(0.300000011920929f, 0.300000011920929f);
327 canvas->translate(50, 50);
328 canvas->drawPath(fInfoPath, paint);
332 canvas->translate(5, 15);
333 canvas->drawPath(fAccessibilityPath, paint);
335 canvas->scale(0.5f, 0.5f);
336 canvas->translate(5, 50);
337 canvas->drawPath(fVisualizerPath, paint);
341 typedef skiagm::GM INHERITED;
344 // test inverse-fill w/ a clip that completely excludes the geometry
345 class PathInverseFillGM : public skiagm::GM {
349 void onOnceBeforeDraw() override {
350 for (size_t i = 0; i < N; i++) {
351 fDY[i] = gProcs[i](&fPath[i]);
355 SkString onShortName() override {
356 return SkString("pathinvfill");
359 SkISize onISize() override {
360 return SkISize::Make(450, 220);
363 static void show(SkCanvas* canvas, const SkPath& path, const SkPaint& paint,
364 const SkRect* clip, SkScalar top, const SkScalar bottom) {
372 canvas->drawPath(path, paint);
376 void onDraw(SkCanvas* canvas) override {
379 path.addCircle(SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(40));
380 path.toggleInverseFillType();
382 SkRect clipR = { 0, 0, SkIntToScalar(100), SkIntToScalar(200) };
384 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
386 for (int doclip = 0; doclip <= 1; ++doclip) {
387 for (int aa = 0; aa <= 1; ++aa) {
389 paint.setAntiAlias(SkToBool(aa));
392 canvas->clipRect(clipR);
394 const SkRect* clipPtr = doclip ? &clipR : nullptr;
396 show(canvas, path, paint, clipPtr, clipR.fTop, clipR.centerY());
397 show(canvas, path, paint, clipPtr, clipR.centerY(), clipR.fBottom);
400 canvas->translate(SkIntToScalar(110), 0);
406 typedef skiagm::GM INHERITED;
409 DEF_SIMPLE_GM(rotatedcubicpath, canvas, 200, 200) {
411 p.setAntiAlias(true);
412 p.setStyle(SkPaint::kFill_Style);
414 canvas->translate(50, 50);
417 path.cubicTo(48,-29.5, 6,-30, 6,-30);
418 path.cubicTo(6,-30, 2,0, 2,0);
419 path.cubicTo(2,0, 44,-21.5, 48,-23);
422 p.setColor(SK_ColorBLUE);
423 canvas->drawPath(path, p);
425 // Rotated path, which is not antialiased on GPU
426 p.setColor(SK_ColorRED);
428 canvas->drawPath(path, p);
431 ///////////////////////////////////////////////////////////////////////////////
433 DEF_GM( return new PathFillGM; )
434 DEF_GM( return new PathInverseFillGM; )