• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/pathops/SkPathOps.h"
18 
19 typedef SkScalar (*MakePathProc)(SkPath*);
20 
make_triangle(SkPath * path)21 static SkScalar make_triangle(SkPath* path) {
22     constexpr int gCoord[] = {
23         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 
make_rect(SkPath * path)33 static 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 
make_oval(SkPath * path)41 static 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 
make_star(SkPath * path,int n)49 static 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;
59         path->lineTo(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
60     }
61     path->close();
62     return r * 2 * 6 / 5;
63 }
64 
make_star_5(SkPath * path)65 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
make_star_13(SkPath * path)66 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
67 
make_three_line(SkPath * path)68 static 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);
73 
74     path->moveTo(-32.5f + xOffset, 19 + yOffset);
75     path->lineTo(32.5f + xOffset, 19 + yOffset);
76 
77     path->moveTo(-32.5f + xOffset, -19 + yOffset);
78     path->lineTo(32.5f + xOffset, -19 + yOffset);
79     path->lineTo(-32.5f + xOffset, -19 + yOffset);
80 
81     path->close();
82 
83     return SkIntToScalar(70);
84 }
85 
make_arrow(SkPath * path)86 static 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);
91 
92     path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
93     path->lineTo(0 + xOffset, 25.525126f + yOffset);
94 
95     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();
100 
101     return SkIntToScalar(70);
102 }
103 
make_curve(SkPath * path)104 static 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);
111 
112     return SkIntToScalar(40);
113 }
114 
make_battery(SkPath * path)115 static SkScalar make_battery(SkPath* path) {
116     static SkScalar xOffset = 5.0f;
117 
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();
128 
129     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();
138     return SkIntToScalar(50);
139 }
140 
make_battery2(SkPath * path)141 static SkScalar make_battery2(SkPath* path) {
142     static SkScalar xOffset = 225.625f;
143 
144     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();
150 
151     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();
160 
161     return SkIntToScalar(60);
162 }
163 
make_ring(SkPath * path)164 static 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 
239 constexpr MakePathProc gProcs[] = {
240     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,
249     make_battery2,
250     make_ring
251 };
252 
253 constexpr SkScalar gWidths[] = {
254     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,
264     0.0f
265 };
266 static_assert(SK_ARRAY_COUNT(gWidths) == SK_ARRAY_COUNT(gProcs));
267 
268 constexpr SkScalar gMiters[] = {
269     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,
279     4.0f,
280 };
281 static_assert(SK_ARRAY_COUNT(gMiters) == SK_ARRAY_COUNT(gProcs));
282 
283 constexpr 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,
294     0.0f,
295 };
296 static_assert(SK_ARRAY_COUNT(gXTranslate) == SK_ARRAY_COUNT(gProcs));
297 
298 #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.
304 class SmallPathsGM : public skiagm::GM {
305     SkPath  fPath[N];
306     SkScalar fDY[N];
307 protected:
onOnceBeforeDraw()308     void onOnceBeforeDraw() override {
309         for (size_t i = 0; i < N; i++) {
310             fDY[i] = gProcs[i](&fPath[i]);
311         }
312     }
313 
onShortName()314     SkString onShortName() override {
315         return SkString("smallpaths");
316     }
317 
onISize()318     SkISize onISize() override {
319         return SkISize::Make(640, 512);
320     }
321 
onDraw(SkCanvas * canvas)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);
330             canvas->translate(gXTranslate[i], fDY[i]);
331         }
332         canvas->restore();
333         canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
334 
335         // 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);
343             canvas->translate(gXTranslate[i], fDY[i]);
344         }
345         canvas->restore();
346         canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
347 
348         // 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);
356             canvas->translate(gXTranslate[i], fDY[i]);
357         }
358         canvas->restore();
359         canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
360 
361         // 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);
368             canvas->translate(gXTranslate[i], fDY[i]);
369         }
370 
371     }
372 
373 private:
374     typedef skiagm::GM INHERITED;
375 };
376 
377 DEF_GM(return new SmallPathsGM;)
378