blob: 869d7e12e9a0a73dfc007379b25fccca7da6127d [file] [log] [blame]
jvanverth8450cc32015-11-18 13:40:15 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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"
Jim Van Verthde30e422019-12-19 10:13:23 -050017#include "include/pathops/SkPathOps.h"
jvanverth8450cc32015-11-18 13:40:15 -080018
19typedef SkScalar (*MakePathProc)(SkPath*);
20
21static SkScalar make_triangle(SkPath* path) {
mtkleindbfd7ab2016-09-01 11:24:54 -070022 constexpr int gCoord[] = {
jvanverth8450cc32015-11-18 13:40:15 -080023 10, 20, 15, 5, 30, 30
24 };
25 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
26 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
27 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
28 path->close();
29 path->offset(SkIntToScalar(10), SkIntToScalar(0));
30 return SkIntToScalar(30);
31}
32
33static SkScalar make_rect(SkPath* path) {
34 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
35 SkIntToScalar(30), SkIntToScalar(30) };
36 path->addRect(r);
37 path->offset(SkIntToScalar(10), SkIntToScalar(0));
38 return SkIntToScalar(30);
39}
40
41static SkScalar make_oval(SkPath* path) {
42 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
43 SkIntToScalar(30), SkIntToScalar(30) };
44 path->addOval(r);
45 path->offset(SkIntToScalar(10), SkIntToScalar(0));
46 return SkIntToScalar(30);
47}
48
49static SkScalar make_star(SkPath* path, int n) {
50 const SkScalar c = SkIntToScalar(45);
51 const SkScalar r = SkIntToScalar(20);
52
53 SkScalar rad = -SK_ScalarPI / 2;
54 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
55
56 path->moveTo(c, c - r);
57 for (int i = 1; i < n; i++) {
58 rad += drad;
Brian Osman4428f2c2019-04-02 10:59:28 -040059 path->lineTo(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
jvanverth8450cc32015-11-18 13:40:15 -080060 }
61 path->close();
62 return r * 2 * 6 / 5;
63}
64
65static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
66static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
67
68static SkScalar make_three_line(SkPath* path) {
69 static SkScalar xOffset = 34.f;
70 static SkScalar yOffset = 50.f;
71 path->moveTo(-32.5f + xOffset, 0.0f + yOffset);
72 path->lineTo(32.5f + xOffset, 0.0f + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070073
jvanverth8450cc32015-11-18 13:40:15 -080074 path->moveTo(-32.5f + xOffset, 19 + yOffset);
75 path->lineTo(32.5f + xOffset, 19 + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070076
jvanverth8450cc32015-11-18 13:40:15 -080077 path->moveTo(-32.5f + xOffset, -19 + yOffset);
78 path->lineTo(32.5f + xOffset, -19 + yOffset);
79 path->lineTo(-32.5f + xOffset, -19 + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070080
jvanverth8450cc32015-11-18 13:40:15 -080081 path->close();
halcanary9d524f22016-03-29 09:03:52 -070082
jvanverth8450cc32015-11-18 13:40:15 -080083 return SkIntToScalar(70);
84}
85
86static SkScalar make_arrow(SkPath* path) {
87 static SkScalar xOffset = 34.f;
88 static SkScalar yOffset = 40.f;
89 path->moveTo(-26.f + xOffset, 0.0f + yOffset);
90 path->lineTo(26.f + xOffset, 0.0f + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070091
jvanverth8450cc32015-11-18 13:40:15 -080092 path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
93 path->lineTo(0 + xOffset, 25.525126f + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070094
jvanverth8450cc32015-11-18 13:40:15 -080095 path->moveTo(-28.f + xOffset, 2.4748745f + yOffset);
96 path->lineTo(0 + xOffset, -25.525126f + yOffset);
97 path->lineTo(-28.f + xOffset, 2.4748745f + yOffset);
98
99 path->close();
halcanary9d524f22016-03-29 09:03:52 -0700100
jvanverth8450cc32015-11-18 13:40:15 -0800101 return SkIntToScalar(70);
102}
103
104static SkScalar make_curve(SkPath* path) {
105 static SkScalar xOffset = -382.f;
106 static SkScalar yOffset = -50.f;
107 path->moveTo(491 + xOffset, 56 + yOffset);
108 path->conicTo(435.93292f + xOffset, 56.000031f + yOffset,
109 382.61078f + xOffset, 69.752716f + yOffset,
110 0.9920463f);
halcanary9d524f22016-03-29 09:03:52 -0700111
jvanverth8450cc32015-11-18 13:40:15 -0800112 return SkIntToScalar(40);
113}
114
115static SkScalar make_battery(SkPath* path) {
Jim Van Verth90432c02018-03-09 15:35:00 -0500116 static SkScalar xOffset = 5.0f;
jvanverth8450cc32015-11-18 13:40:15 -0800117
118 path->moveTo(24.67f + xOffset, 0.33000004f);
119 path->lineTo(8.3299999f + xOffset, 0.33000004f);
120 path->lineTo(8.3299999f + xOffset, 5.3299999f);
121 path->lineTo(0.33000004f + xOffset, 5.3299999f);
122 path->lineTo(0.33000004f + xOffset, 50.669998f);
123 path->lineTo(32.669998f + xOffset, 50.669998f);
124 path->lineTo(32.669998f + xOffset, 5.3299999f);
125 path->lineTo(24.67f + xOffset, 5.3299999f);
126 path->lineTo(24.67f + xOffset, 0.33000004f);
127 path->close();
halcanary9d524f22016-03-29 09:03:52 -0700128
jvanverth8450cc32015-11-18 13:40:15 -0800129 path->moveTo(25.727224f + xOffset, 12.886665f);
130 path->lineTo(10.907918f + xOffset, 12.886665f);
131 path->lineTo(7.5166659f + xOffset, 28.683645f);
132 path->lineTo(14.810181f + xOffset, 28.683645f);
133 path->lineTo(7.7024879f + xOffset, 46.135998f);
134 path->lineTo(28.049999f + xOffset, 25.136419f);
135 path->lineTo(16.854223f + xOffset, 25.136419f);
136 path->lineTo(25.727224f + xOffset, 12.886665f);
137 path->close();
jvanverth76f160c2015-11-19 07:46:07 -0800138 return SkIntToScalar(50);
jvanverth8450cc32015-11-18 13:40:15 -0800139}
140
141static SkScalar make_battery2(SkPath* path) {
Jim Van Verth90432c02018-03-09 15:35:00 -0500142 static SkScalar xOffset = 225.625f;
halcanary9d524f22016-03-29 09:03:52 -0700143
jvanverth8450cc32015-11-18 13:40:15 -0800144 path->moveTo(32.669998f + xOffset, 9.8640003f);
145 path->lineTo(0.33000004f + xOffset, 9.8640003f);
146 path->lineTo(0.33000004f + xOffset, 50.669998f);
147 path->lineTo(32.669998f + xOffset, 50.669998f);
148 path->lineTo(32.669998f + xOffset, 9.8640003f);
149 path->close();
halcanary9d524f22016-03-29 09:03:52 -0700150
jvanverth8450cc32015-11-18 13:40:15 -0800151 path->moveTo(10.907918f + xOffset, 12.886665f);
152 path->lineTo(25.727224f + xOffset, 12.886665f);
153 path->lineTo(16.854223f + xOffset, 25.136419f);
154 path->lineTo(28.049999f + xOffset, 25.136419f);
155 path->lineTo(7.7024879f + xOffset, 46.135998f);
156 path->lineTo(14.810181f + xOffset, 28.683645f);
157 path->lineTo(7.5166659f + xOffset, 28.683645f);
158 path->lineTo(10.907918f + xOffset, 12.886665f);
159 path->close();
halcanary9d524f22016-03-29 09:03:52 -0700160
Jim Van Verth2e57c132019-12-19 13:08:15 -0500161 return SkIntToScalar(60);
jvanverth8450cc32015-11-18 13:40:15 -0800162}
163
Jim Van Verthde30e422019-12-19 10:13:23 -0500164static SkScalar make_ring(SkPath* path) {
165 static SkScalar xOffset = 120;
166 static SkScalar yOffset = -270.f;
167
168 path->setFillType(SkPathFillType::kWinding);
169 path->moveTo(xOffset + 144.859f, yOffset + 285.172f);
170 path->lineTo(xOffset + 144.859f, yOffset + 285.172f);
171 path->lineTo(xOffset + 144.859f, yOffset + 285.172f);
172 path->lineTo(xOffset + 143.132f, yOffset + 284.617f);
173 path->lineTo(xOffset + 144.859f, yOffset + 285.172f);
174 path->close();
175 path->moveTo(xOffset + 135.922f, yOffset + 286.844f);
176 path->lineTo(xOffset + 135.922f, yOffset + 286.844f);
177 path->lineTo(xOffset + 135.922f, yOffset + 286.844f);
178 path->lineTo(xOffset + 135.367f, yOffset + 288.571f);
179 path->lineTo(xOffset + 135.922f, yOffset + 286.844f);
180 path->close();
181 path->moveTo(xOffset + 135.922f, yOffset + 286.844f);
182 path->cubicTo(xOffset + 137.07f, yOffset + 287.219f, xOffset + 138.242f, yOffset + 287.086f,
183 xOffset + 139.242f, yOffset + 286.578f);
184 path->cubicTo(xOffset + 140.234f, yOffset + 286.078f, xOffset + 141.031f, yOffset + 285.203f,
185 xOffset + 141.406f, yOffset + 284.055f);
186 path->lineTo(xOffset + 144.859f, yOffset + 285.172f);
187 path->cubicTo(xOffset + 143.492f, yOffset + 289.375f, xOffset + 138.992f, yOffset + 291.656f,
188 xOffset + 134.797f, yOffset + 290.297f);
189 path->lineTo(xOffset + 135.922f, yOffset + 286.844f);
190 path->close();
191 path->moveTo(xOffset + 129.68f, yOffset + 280.242f);
192 path->lineTo(xOffset + 129.68f, yOffset + 280.242f);
193 path->lineTo(xOffset + 129.68f, yOffset + 280.242f);
194 path->lineTo(xOffset + 131.407f, yOffset + 280.804f);
195 path->lineTo(xOffset + 129.68f, yOffset + 280.242f);
196 path->close();
197 path->moveTo(xOffset + 133.133f, yOffset + 281.367f);
198 path->cubicTo(xOffset + 132.758f, yOffset + 282.508f, xOffset + 132.883f, yOffset + 283.687f,
199 xOffset + 133.391f, yOffset + 284.679f);
200 path->cubicTo(xOffset + 133.907f, yOffset + 285.679f, xOffset + 134.774f, yOffset + 286.468f,
201 xOffset + 135.922f, yOffset + 286.843f);
202 path->lineTo(xOffset + 134.797f, yOffset + 290.296f);
203 path->cubicTo(xOffset + 130.602f, yOffset + 288.929f, xOffset + 128.313f, yOffset + 284.437f,
204 xOffset + 129.68f, yOffset + 280.241f);
205 path->lineTo(xOffset + 133.133f, yOffset + 281.367f);
206 path->close();
207 path->moveTo(xOffset + 139.742f, yOffset + 275.117f);
208 path->lineTo(xOffset + 139.742f, yOffset + 275.117f);
209 path->lineTo(xOffset + 139.18f, yOffset + 276.844f);
210 path->lineTo(xOffset + 139.742f, yOffset + 275.117f);
211 path->close();
212 path->moveTo(xOffset + 138.609f, yOffset + 278.57f);
213 path->cubicTo(xOffset + 137.461f, yOffset + 278.203f, xOffset + 136.297f, yOffset + 278.328f,
214 xOffset + 135.297f, yOffset + 278.836f);
215 path->cubicTo(xOffset + 134.297f, yOffset + 279.344f, xOffset + 133.508f, yOffset + 280.219f,
216 xOffset + 133.133f, yOffset + 281.367f);
217 path->lineTo(xOffset + 129.68f, yOffset + 280.242f);
218 path->cubicTo(xOffset + 131.047f, yOffset + 276.039f, xOffset + 135.539f, yOffset + 273.758f,
219 xOffset + 139.742f, yOffset + 275.117f);
220 path->lineTo(xOffset + 138.609f, yOffset + 278.57f);
221 path->close();
222 path->moveTo(xOffset + 141.406f, yOffset + 284.055f);
223 path->cubicTo(xOffset + 141.773f, yOffset + 282.907f, xOffset + 141.648f, yOffset + 281.735f,
224 xOffset + 141.148f, yOffset + 280.735f);
225 path->cubicTo(xOffset + 140.625f, yOffset + 279.735f, xOffset + 139.757f, yOffset + 278.946f,
226 xOffset + 138.609f, yOffset + 278.571f);
227 path->lineTo(xOffset + 139.742f, yOffset + 275.118f);
228 path->cubicTo(xOffset + 143.937f, yOffset + 276.493f, xOffset + 146.219f, yOffset + 280.977f,
229 xOffset + 144.859f, yOffset + 285.173f);
230 path->lineTo(xOffset + 141.406f, yOffset + 284.055f);
231 path->close();
232
233 // uncomment to reveal PathOps bug, see https://bugs.chromium.org/p/skia/issues/detail?id=9732
234 // (void) Simplify(*path, path);
235
236 return SkIntToScalar(15);
237}
238
mtkleindbfd7ab2016-09-01 11:24:54 -0700239constexpr MakePathProc gProcs[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800240 make_triangle,
241 make_rect,
242 make_oval,
243 make_star_5,
244 make_star_13,
245 make_three_line,
246 make_arrow,
247 make_curve,
248 make_battery,
Jim Van Verthde30e422019-12-19 10:13:23 -0500249 make_battery2,
250 make_ring
jvanverth8450cc32015-11-18 13:40:15 -0800251};
252
mtkleindbfd7ab2016-09-01 11:24:54 -0700253constexpr SkScalar gWidths[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800254 2.0f,
255 3.0f,
256 4.0f,
257 5.0f,
258 6.0f,
259 7.0f,
260 7.0f,
261 14.0f,
262 0.0f,
263 0.0f,
Jim Van Verthde30e422019-12-19 10:13:23 -0500264 0.0f
jvanverth8450cc32015-11-18 13:40:15 -0800265};
Jim Van Verthde30e422019-12-19 10:13:23 -0500266static_assert(SK_ARRAY_COUNT(gWidths) == SK_ARRAY_COUNT(gProcs));
jvanverth8450cc32015-11-18 13:40:15 -0800267
mtkleindbfd7ab2016-09-01 11:24:54 -0700268constexpr SkScalar gMiters[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800269 2.0f,
270 3.0f,
271 3.0f,
272 3.0f,
273 4.0f,
274 4.0f,
275 4.0f,
276 4.0f,
277 4.0f,
278 4.0f,
Jim Van Verthde30e422019-12-19 10:13:23 -0500279 4.0f,
jvanverth8450cc32015-11-18 13:40:15 -0800280};
Jim Van Verthde30e422019-12-19 10:13:23 -0500281static_assert(SK_ARRAY_COUNT(gMiters) == SK_ARRAY_COUNT(gProcs));
jvanverth8450cc32015-11-18 13:40:15 -0800282
Jim Van Verth90432c02018-03-09 15:35:00 -0500283constexpr SkScalar gXTranslate[] = {
284 0.0f,
285 0.0f,
286 0.0f,
287 0.0f,
288 0.0f,
289 0.0f,
290 0.0f,
291 0.0f,
292 -220.625f,
293 0.0f,
Jim Van Verthde30e422019-12-19 10:13:23 -0500294 0.0f,
Jim Van Verth90432c02018-03-09 15:35:00 -0500295};
Jim Van Verthde30e422019-12-19 10:13:23 -0500296static_assert(SK_ARRAY_COUNT(gXTranslate) == SK_ARRAY_COUNT(gProcs));
Jim Van Verth90432c02018-03-09 15:35:00 -0500297
jvanverth8450cc32015-11-18 13:40:15 -0800298#define N SK_ARRAY_COUNT(gProcs)
299
300// This GM tests out drawing small paths (i.e., for Ganesh, using the Distance
301// Field path renderer) which are filled, stroked and filledAndStroked. In
302// particular this ensures that any cache keys in use include the stroking
303// parameters.
304class SmallPathsGM : public skiagm::GM {
305 SkPath fPath[N];
306 SkScalar fDY[N];
307protected:
308 void onOnceBeforeDraw() override {
309 for (size_t i = 0; i < N; i++) {
310 fDY[i] = gProcs[i](&fPath[i]);
311 }
312 }
313
314 SkString onShortName() override {
315 return SkString("smallpaths");
316 }
317
318 SkISize onISize() override {
Jim Van Verth2e57c132019-12-19 13:08:15 -0500319 return SkISize::Make(640, 512);
jvanverth8450cc32015-11-18 13:40:15 -0800320 }
321
322 void onDraw(SkCanvas* canvas) override {
323 SkPaint paint;
324 paint.setAntiAlias(true);
325
326 // first column: filled paths
327 canvas->save();
328 for (size_t i = 0; i < N; i++) {
329 canvas->drawPath(fPath[i], paint);
Jim Van Verth90432c02018-03-09 15:35:00 -0500330 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800331 }
332 canvas->restore();
333 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700334
jvanverth8450cc32015-11-18 13:40:15 -0800335 // second column: stroked paths
336 canvas->save();
337 paint.setStyle(SkPaint::kStroke_Style);
338 paint.setStrokeCap(SkPaint::kButt_Cap);
339 for (size_t i = 0; i < N; i++) {
340 paint.setStrokeWidth(gWidths[i]);
341 paint.setStrokeMiter(gMiters[i]);
342 canvas->drawPath(fPath[i], paint);
Jim Van Verth90432c02018-03-09 15:35:00 -0500343 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800344 }
345 canvas->restore();
346 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700347
jvanverth8450cc32015-11-18 13:40:15 -0800348 // third column: stroked paths with different widths
349 canvas->save();
350 paint.setStyle(SkPaint::kStroke_Style);
351 paint.setStrokeCap(SkPaint::kButt_Cap);
352 for (size_t i = 0; i < N; i++) {
353 paint.setStrokeWidth(gWidths[i] + 2.0f);
354 paint.setStrokeMiter(gMiters[i]);
355 canvas->drawPath(fPath[i], paint);
Jim Van Verth90432c02018-03-09 15:35:00 -0500356 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800357 }
358 canvas->restore();
359 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700360
jvanverth8450cc32015-11-18 13:40:15 -0800361 // fourth column: stroked and filled paths
362 paint.setStyle(SkPaint::kStrokeAndFill_Style);
363 paint.setStrokeCap(SkPaint::kButt_Cap);
364 for (size_t i = 0; i < N; i++) {
365 paint.setStrokeWidth(gWidths[i]);
366 paint.setStrokeMiter(gMiters[i]);
367 canvas->drawPath(fPath[i], paint);
Jim Van Verth90432c02018-03-09 15:35:00 -0500368 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800369 }
halcanary9d524f22016-03-29 09:03:52 -0700370
jvanverth8450cc32015-11-18 13:40:15 -0800371 }
372
373private:
374 typedef skiagm::GM INHERITED;
375};
376
377DEF_GM(return new SmallPathsGM;)