blob: 6d7155dafaabd9d6b619642a8e734927ba9c18d6 [file] [log] [blame]
robertphillips72729352015-04-28 07:42:04 -07001/*
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.h"
Jim Van Verth8664a1d2018-06-28 16:26:50 -04009#include "SkPolyUtils.h"
reed026beb52015-06-10 14:23:15 -070010#include "SkPathPriv.h"
robertphillips72729352015-04-28 07:42:04 -070011
12static void create_ngon(int n, SkPoint* pts, SkScalar width, SkScalar height) {
13 float angleStep = 360.0f / n, angle = 0.0f, sin, cos;
14 if ((n % 2) == 1) {
15 angle = angleStep/2.0f;
16 }
17
18 for (int i = 0; i < n; ++i) {
19 sin = SkScalarSinCos(SkDegreesToRadians(angle), &cos);
20 pts[i].fX = -sin * width;
21 pts[i].fY = cos * height;
22 angle += angleStep;
23 }
24}
25
Brian Salomonab664fa2017-03-24 16:07:20 +000026namespace ConvexLineOnlyData {
27// narrow rect
28const SkPoint gPoints0[] = {
29 { -1.5f, -50.0f },
30 { 1.5f, -50.0f },
31 { 1.5f, 50.0f },
32 { -1.5f, 50.0f }
33};
34// narrow rect on an angle
35const SkPoint gPoints1[] = {
36 { -50.0f, -49.0f },
37 { -49.0f, -50.0f },
38 { 50.0f, 49.0f },
39 { 49.0f, 50.0f }
40};
41// trap - narrow on top - wide on bottom
42const SkPoint gPoints2[] = {
43 { -10.0f, -50.0f },
44 { 10.0f, -50.0f },
45 { 50.0f, 50.0f },
46 { -50.0f, 50.0f }
47};
48// wide skewed rect
49const SkPoint gPoints3[] = {
50 { -50.0f, -50.0f },
51 { 0.0f, -50.0f },
52 { 50.0f, 50.0f },
53 { 0.0f, 50.0f }
54};
55// thin rect with colinear-ish lines
56const SkPoint gPoints4[] = {
57 { -6.0f, -50.0f },
58 { 4.0f, -50.0f },
59 { 5.0f, -25.0f },
60 { 6.0f, 0.0f },
61 { 5.0f, 25.0f },
62 { 4.0f, 50.0f },
63 { -4.0f, 50.0f }
64};
65// degenerate
66const SkPoint gPoints5[] = {
67 { -0.025f, -0.025f },
68 { 0.025f, -0.025f },
69 { 0.025f, 0.025f },
70 { -0.025f, 0.025f }
71};
72// Triangle in which the first point should fuse with last
73const SkPoint gPoints6[] = {
74 { -20.0f, -13.0f },
75 { -20.0f, -13.05f },
76 { 20.0f, -13.0f },
77 { 20.0f, 27.0f }
78};
79// thin rect with colinear lines
80const SkPoint gPoints7[] = {
81 { -10.0f, -50.0f },
82 { 10.0f, -50.0f },
83 { 10.0f, -25.0f },
84 { 10.0f, 0.0f },
85 { 10.0f, 25.0f },
86 { 10.0f, 50.0f },
87 { -10.0f, 50.0f }
88};
89// capped teardrop
90const SkPoint gPoints8[] = {
91 { 50.00f, 50.00f },
92 { 0.00f, 50.00f },
93 { -15.45f, 47.55f },
94 { -29.39f, 40.45f },
95 { -40.45f, 29.39f },
96 { -47.55f, 15.45f },
97 { -50.00f, 0.00f },
98 { -47.55f, -15.45f },
99 { -40.45f, -29.39f },
100 { -29.39f, -40.45f },
101 { -15.45f, -47.55f },
102 { 0.00f, -50.00f },
103 { 50.00f, -50.00f }
104};
105// teardrop
106const SkPoint gPoints9[] = {
107 { 4.39f, 40.45f },
108 { -9.55f, 47.55f },
109 { -25.00f, 50.00f },
110 { -40.45f, 47.55f },
111 { -54.39f, 40.45f },
112 { -65.45f, 29.39f },
113 { -72.55f, 15.45f },
114 { -75.00f, 0.00f },
115 { -72.55f, -15.45f },
116 { -65.45f, -29.39f },
117 { -54.39f, -40.45f },
118 { -40.45f, -47.55f },
119 { -25.0f, -50.0f },
120 { -9.55f, -47.55f },
121 { 4.39f, -40.45f },
122 { 75.00f, 0.00f }
123};
124// clipped triangle
125const SkPoint gPoints10[] = {
126 { -10.0f, -50.0f },
127 { 10.0f, -50.0f },
128 { 50.0f, 31.0f },
129 { 40.0f, 50.0f },
130 { -40.0f, 50.0f },
131 { -50.0f, 31.0f },
132};
133
134const SkPoint* gPoints[] = {
135 gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6,
136 gPoints7, gPoints8, gPoints9, gPoints10,
137};
138
139const size_t gSizes[] = {
140 SK_ARRAY_COUNT(gPoints0),
141 SK_ARRAY_COUNT(gPoints1),
142 SK_ARRAY_COUNT(gPoints2),
143 SK_ARRAY_COUNT(gPoints3),
144 SK_ARRAY_COUNT(gPoints4),
145 SK_ARRAY_COUNT(gPoints5),
146 SK_ARRAY_COUNT(gPoints6),
147 SK_ARRAY_COUNT(gPoints7),
148 SK_ARRAY_COUNT(gPoints8),
149 SK_ARRAY_COUNT(gPoints9),
150 SK_ARRAY_COUNT(gPoints10),
151};
152static_assert(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), "array_mismatch");
153}
154
robertphillips72729352015-04-28 07:42:04 -0700155namespace skiagm {
156
157// This GM is intended to exercise Ganesh's handling of convex line-only
158// paths
159class ConvexLineOnlyPathsGM : public GM {
160public:
robertphillipsad234462016-08-26 05:30:19 -0700161 ConvexLineOnlyPathsGM(bool doStrokeAndFill) : fDoStrokeAndFill(doStrokeAndFill) {
robertphillips72729352015-04-28 07:42:04 -0700162 this->setBGColor(0xFFFFFFFF);
163 }
164
165protected:
robertphillipsad234462016-08-26 05:30:19 -0700166 SkString onShortName() override {
167 if (fDoStrokeAndFill) {
168 return SkString("convex-lineonly-paths-stroke-and-fill");
169 }
170 return SkString("convex-lineonly-paths");
171 }
robertphillips72729352015-04-28 07:42:04 -0700172 SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); }
173 bool runAsBench() const override { return true; }
174
Brian Salomonab664fa2017-03-24 16:07:20 +0000175 static SkPath GetPath(int index, SkPath::Direction dir) {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400176 std::unique_ptr<SkPoint[]> data(nullptr);
robertphillips72729352015-04-28 07:42:04 -0700177 const SkPoint* points;
178 int numPts;
Brian Salomonab664fa2017-03-24 16:07:20 +0000179 if (index < (int) SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
robertphillips72729352015-04-28 07:42:04 -0700180 // manually specified
Brian Salomonab664fa2017-03-24 16:07:20 +0000181 points = ConvexLineOnlyData::gPoints[index];
182 numPts = (int)ConvexLineOnlyData::gSizes[index];
robertphillips72729352015-04-28 07:42:04 -0700183 } else {
184 // procedurally generated
185 SkScalar width = kMaxPathHeight/2;
186 SkScalar height = kMaxPathHeight/2;
Brian Salomonab664fa2017-03-24 16:07:20 +0000187 switch (index-SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
robertphillips72729352015-04-28 07:42:04 -0700188 case 0:
189 numPts = 3;
190 break;
191 case 1:
192 numPts = 4;
193 break;
194 case 2:
195 numPts = 5;
196 break;
197 case 3: // squashed pentagon
198 numPts = 5;
199 width = kMaxPathHeight/5;
200 break;
201 case 4:
202 numPts = 6;
203 break;
204 case 5:
205 numPts = 8;
206 break;
207 case 6: // squashed octogon
208 numPts = 8;
209 width = kMaxPathHeight/5;
210 break;
211 case 7:
212 numPts = 20;
213 break;
214 case 8:
215 numPts = 100;
216 break;
217 default:
218 numPts = 3;
219 break;
220 }
221
halcanary385fe4d2015-08-26 13:07:48 -0700222 data.reset(new SkPoint[numPts]);
robertphillips72729352015-04-28 07:42:04 -0700223
224 create_ngon(numPts, data.get(), width, height);
225 points = data.get();
226 }
227
228 SkPath path;
229
230 if (SkPath::kCW_Direction == dir) {
231 path.moveTo(points[0]);
232 for (int i = 1; i < numPts; ++i) {
233 path.lineTo(points[i]);
234 }
235 } else {
236 path.moveTo(points[numPts-1]);
237 for (int i = numPts-2; i >= 0; --i) {
238 path.lineTo(points[i]);
239 }
240 }
241
242 path.close();
243#ifdef SK_DEBUG
244 // Each path this method returns should be convex, only composed of
245 // lines, wound the right direction, and short enough to fit in one
246 // of the GMs rows.
247 SkASSERT(path.isConvex());
248 SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks());
reed026beb52015-06-10 14:23:15 -0700249 SkPathPriv::FirstDirection actualDir;
250 SkASSERT(SkPathPriv::CheapComputeFirstDirection(path, &actualDir));
251 SkASSERT(SkPathPriv::AsFirstDirection(dir) == actualDir);
robertphillips72729352015-04-28 07:42:04 -0700252 SkRect bounds = path.getBounds();
253 SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f));
254 SkASSERT(bounds.height() <= kMaxPathHeight);
255#endif
256 return path;
257 }
258
259 // Draw a single path several times, shrinking it, flipping its direction
260 // and changing its start vertex each time.
robertphillips2a974622015-05-08 07:08:13 -0700261 void drawPath(SkCanvas* canvas, int index, SkPoint* offset) {
robertphillips72729352015-04-28 07:42:04 -0700262
263 SkPoint center;
264 {
Brian Salomonab664fa2017-03-24 16:07:20 +0000265 SkPath path = GetPath(index, SkPath::kCW_Direction);
robertphillips2a974622015-05-08 07:08:13 -0700266 if (offset->fX+path.getBounds().width() > kGMWidth) {
267 offset->fX = 0;
268 offset->fY += kMaxPathHeight;
robertphillipsad234462016-08-26 05:30:19 -0700269 if (fDoStrokeAndFill) {
270 offset->fX += kStrokeWidth / 2.0f;
271 offset->fY += kStrokeWidth / 2.0f;
272 }
robertphillips72729352015-04-28 07:42:04 -0700273 }
robertphillips2a974622015-05-08 07:08:13 -0700274 center = { offset->fX + SkScalarHalf(path.getBounds().width()), offset->fY};
275 offset->fX += path.getBounds().width();
robertphillipsad234462016-08-26 05:30:19 -0700276 if (fDoStrokeAndFill) {
277 offset->fX += kStrokeWidth;
278 }
robertphillips72729352015-04-28 07:42:04 -0700279 }
280
281 const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
282 const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_Direction };
283 const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f };
mtkleindbfd7ab2016-09-01 11:24:54 -0700284 const SkPaint::Join joins[3] = { SkPaint::kRound_Join,
robertphillipsad234462016-08-26 05:30:19 -0700285 SkPaint::kBevel_Join,
286 SkPaint::kMiter_Join };
robertphillips72729352015-04-28 07:42:04 -0700287
288 SkPaint paint;
289 paint.setAntiAlias(true);
290
291 for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) {
Brian Salomonab664fa2017-03-24 16:07:20 +0000292 SkPath path = GetPath(index, dirs[i%2]);
robertphillipsad234462016-08-26 05:30:19 -0700293 if (fDoStrokeAndFill) {
294 paint.setStyle(SkPaint::kStrokeAndFill_Style);
295 paint.setStrokeJoin(joins[i%3]);
296 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth));
297 }
robertphillips72729352015-04-28 07:42:04 -0700298
299 canvas->save();
300 canvas->translate(center.fX, center.fY);
301 canvas->scale(scales[i], scales[i]);
302 paint.setColor(colors[i%2]);
303 canvas->drawPath(path, paint);
304 canvas->restore();
305 }
306 }
307
308 void onDraw(SkCanvas* canvas) override {
robertphillips2a974622015-05-08 07:08:13 -0700309 // the right edge of the last drawn path
halcanary9d524f22016-03-29 09:03:52 -0700310 SkPoint offset = { 0, SkScalarHalf(kMaxPathHeight) };
robertphillipsad234462016-08-26 05:30:19 -0700311 if (fDoStrokeAndFill) {
312 offset.fX += kStrokeWidth / 2.0f;
313 offset.fY += kStrokeWidth / 2.0f;
314 }
robertphillips2a974622015-05-08 07:08:13 -0700315
robertphillips72729352015-04-28 07:42:04 -0700316 for (int i = 0; i < kNumPaths; ++i) {
robertphillips2a974622015-05-08 07:08:13 -0700317 this->drawPath(canvas, i, &offset);
robertphillips72729352015-04-28 07:42:04 -0700318 }
319
robertphillips72729352015-04-28 07:42:04 -0700320 {
Brian Salomon0235c642018-08-31 12:04:18 -0400321 // Repro for crbug.com/472723 (Missing AA on portions of graphic with GPU rasterization)
robertphillips72729352015-04-28 07:42:04 -0700322
323 SkPaint p;
324 p.setAntiAlias(true);
robertphillipsad234462016-08-26 05:30:19 -0700325 if (fDoStrokeAndFill) {
326 p.setStyle(SkPaint::kStrokeAndFill_Style);
327 p.setStrokeJoin(SkPaint::kMiter_Join);
328 p.setStrokeWidth(SkIntToScalar(kStrokeWidth));
329 }
robertphillips72729352015-04-28 07:42:04 -0700330
331 SkPath p1;
332 p1.moveTo(60.8522949f, 364.671021f);
333 p1.lineTo(59.4380493f, 364.671021f);
334 p1.lineTo(385.414276f, 690.647217f);
335 p1.lineTo(386.121399f, 689.940125f);
Brian Salomon0235c642018-08-31 12:04:18 -0400336 canvas->save();
337 canvas->translate(356.0f, 50.0f);
robertphillips72729352015-04-28 07:42:04 -0700338 canvas->drawPath(p1, p);
Brian Salomon0235c642018-08-31 12:04:18 -0400339 canvas->restore();
340
341 // Repro for crbug.com/869172 (SVG path incorrectly simplified when using GPU
342 // Rasterization). This will only draw anything in the stroke-and-fill version.
343 SkPath p2;
344 p2.moveTo(10.f, 0.f);
345 p2.lineTo(38.f, 0.f);
346 p2.lineTo(66.f, 0.f);
347 p2.lineTo(94.f, 0.f);
348 p2.lineTo(122.f, 0.f);
349 p2.lineTo(150.f, 0.f);
350 p2.lineTo(150.f, 0.f);
351 p2.lineTo(122.f, 0.f);
352 p2.lineTo(94.f, 0.f);
353 p2.lineTo(66.f, 0.f);
354 p2.lineTo(38.f, 0.f);
355 p2.lineTo(10.f, 0.f);
356 p2.close();
357 canvas->save();
358 canvas->translate(0.0f, 500.0f);
359 canvas->drawPath(p2, p);
360 canvas->restore();
Brian Salomon8e449eb2018-09-05 15:08:49 -0400361
362 // Repro for crbug.com/856137. This path previously caused GrAAConvexTessellator to turn
363 // inset rings into outsets when adjacent bisector angles converged outside the previous
364 // ring due to accumulated error.
365 SkPath p3;
366 p3.setFillType(SkPath::kEvenOdd_FillType);
367 p3.moveTo(1184.96f, 982.557f);
368 p3.lineTo(1183.71f, 982.865f);
369 p3.lineTo(1180.99f, 982.734f);
370 p3.lineTo(1178.5f, 981.541f);
371 p3.lineTo(1176.35f, 979.367f);
372 p3.lineTo(1178.94f, 938.854f);
373 p3.lineTo(1181.35f, 936.038f);
374 p3.lineTo(1183.96f, 934.117f);
375 p3.lineTo(1186.67f, 933.195f);
376 p3.lineTo(1189.36f, 933.342f);
377 p3.lineTo(1191.58f, 934.38f);
378 p3.close();
379 canvas->save();
380 SkMatrix m;
381 m.setAll(0.0893210843f, 0, 79.1197586f, 0, 0.0893210843f, 300, 0, 0, 1);
382 canvas->concat(m);
383 canvas->drawPath(p3, p);
384 canvas->restore();
robertphillips72729352015-04-28 07:42:04 -0700385 }
386 }
387
388private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700389 static constexpr int kStrokeWidth = 10;
390 static constexpr int kNumPaths = 20;
391 static constexpr int kMaxPathHeight = 100;
392 static constexpr int kGMWidth = 512;
393 static constexpr int kGMHeight = 512;
robertphillips72729352015-04-28 07:42:04 -0700394
robertphillipsad234462016-08-26 05:30:19 -0700395 bool fDoStrokeAndFill;
396
robertphillips72729352015-04-28 07:42:04 -0700397 typedef GM INHERITED;
398};
399
400//////////////////////////////////////////////////////////////////////////////
401
robertphillipsad234462016-08-26 05:30:19 -0700402DEF_GM(return new ConvexLineOnlyPathsGM(false);)
403DEF_GM(return new ConvexLineOnlyPathsGM(true);)
robertphillips72729352015-04-28 07:42:04 -0700404}