blob: beb4b6d20afcb5af66b797f94451f7f1ceb555a9 [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"
jvanverth8450cc32015-11-18 13:40:15 -080017
18typedef SkScalar (*MakePathProc)(SkPath*);
19
20static SkScalar make_triangle(SkPath* path) {
mtkleindbfd7ab2016-09-01 11:24:54 -070021 constexpr int gCoord[] = {
jvanverth8450cc32015-11-18 13:40:15 -080022 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
32static 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
40static 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
48static 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;
Brian Osman4428f2c2019-04-02 10:59:28 -040058 path->lineTo(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
jvanverth8450cc32015-11-18 13:40:15 -080059 }
60 path->close();
61 return r * 2 * 6 / 5;
62}
63
64static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
65static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
66
67static 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);
halcanary9d524f22016-03-29 09:03:52 -070072
jvanverth8450cc32015-11-18 13:40:15 -080073 path->moveTo(-32.5f + xOffset, 19 + yOffset);
74 path->lineTo(32.5f + xOffset, 19 + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070075
jvanverth8450cc32015-11-18 13:40:15 -080076 path->moveTo(-32.5f + xOffset, -19 + yOffset);
77 path->lineTo(32.5f + xOffset, -19 + yOffset);
78 path->lineTo(-32.5f + xOffset, -19 + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070079
jvanverth8450cc32015-11-18 13:40:15 -080080 path->close();
halcanary9d524f22016-03-29 09:03:52 -070081
jvanverth8450cc32015-11-18 13:40:15 -080082 return SkIntToScalar(70);
83}
84
85static 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);
halcanary9d524f22016-03-29 09:03:52 -070090
jvanverth8450cc32015-11-18 13:40:15 -080091 path->moveTo(-28.f + xOffset, -2.4748745f + yOffset);
92 path->lineTo(0 + xOffset, 25.525126f + yOffset);
halcanary9d524f22016-03-29 09:03:52 -070093
jvanverth8450cc32015-11-18 13:40:15 -080094 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();
halcanary9d524f22016-03-29 09:03:52 -070099
jvanverth8450cc32015-11-18 13:40:15 -0800100 return SkIntToScalar(70);
101}
102
103static 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);
halcanary9d524f22016-03-29 09:03:52 -0700110
jvanverth8450cc32015-11-18 13:40:15 -0800111 return SkIntToScalar(40);
112}
113
114static SkScalar make_battery(SkPath* path) {
Jim Van Verth90432c02018-03-09 15:35:00 -0500115 static SkScalar xOffset = 5.0f;
jvanverth8450cc32015-11-18 13:40:15 -0800116
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();
halcanary9d524f22016-03-29 09:03:52 -0700127
jvanverth8450cc32015-11-18 13:40:15 -0800128 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();
jvanverth76f160c2015-11-19 07:46:07 -0800137 return SkIntToScalar(50);
jvanverth8450cc32015-11-18 13:40:15 -0800138}
139
140static SkScalar make_battery2(SkPath* path) {
Jim Van Verth90432c02018-03-09 15:35:00 -0500141 static SkScalar xOffset = 225.625f;
halcanary9d524f22016-03-29 09:03:52 -0700142
jvanverth8450cc32015-11-18 13:40:15 -0800143 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();
halcanary9d524f22016-03-29 09:03:52 -0700149
jvanverth8450cc32015-11-18 13:40:15 -0800150 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();
halcanary9d524f22016-03-29 09:03:52 -0700159
jvanverth8450cc32015-11-18 13:40:15 -0800160 return SkIntToScalar(70);
161}
162
mtkleindbfd7ab2016-09-01 11:24:54 -0700163constexpr MakePathProc gProcs[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800164 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
mtkleindbfd7ab2016-09-01 11:24:54 -0700176constexpr SkScalar gWidths[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800177 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
mtkleindbfd7ab2016-09-01 11:24:54 -0700189constexpr SkScalar gMiters[] = {
jvanverth8450cc32015-11-18 13:40:15 -0800190 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
Jim Van Verth90432c02018-03-09 15:35:00 -0500202constexpr 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
jvanverth8450cc32015-11-18 13:40:15 -0800215#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.
221class SmallPathsGM : public skiagm::GM {
222 SkPath fPath[N];
223 SkScalar fDY[N];
224protected:
225 void onOnceBeforeDraw() override {
226 for (size_t i = 0; i < N; i++) {
227 fDY[i] = gProcs[i](&fPath[i]);
228 }
229 }
230
231 SkString onShortName() override {
232 return SkString("smallpaths");
233 }
234
235 SkISize onISize() override {
236 return SkISize::Make(640, 480);
237 }
238
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);
Jim Van Verth90432c02018-03-09 15:35:00 -0500247 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800248 }
249 canvas->restore();
250 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700251
jvanverth8450cc32015-11-18 13:40:15 -0800252 // 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);
Jim Van Verth90432c02018-03-09 15:35:00 -0500260 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800261 }
262 canvas->restore();
263 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700264
jvanverth8450cc32015-11-18 13:40:15 -0800265 // 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);
Jim Van Verth90432c02018-03-09 15:35:00 -0500273 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800274 }
275 canvas->restore();
276 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
halcanary9d524f22016-03-29 09:03:52 -0700277
jvanverth8450cc32015-11-18 13:40:15 -0800278 // 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);
Jim Van Verth90432c02018-03-09 15:35:00 -0500285 canvas->translate(gXTranslate[i], fDY[i]);
jvanverth8450cc32015-11-18 13:40:15 -0800286 }
halcanary9d524f22016-03-29 09:03:52 -0700287
jvanverth8450cc32015-11-18 13:40:15 -0800288 }
289
290private:
291 typedef skiagm::GM INHERITED;
292};
293
294DEF_GM(return new SmallPathsGM;)