blob: e4a959cfc723bef8a51b90362e87b32440a15343 [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 },
Cary Clarkc9b7c722018-12-12 14:50:23 -050059#if SK_TREAT_COLINEAR_DIAGONAL_POINTS_AS_CONCAVE == 0
60 { 5.0f, -25.0f }, // remove if collinear diagonal points are not concave
61#endif
Brian Salomonab664fa2017-03-24 16:07:20 +000062 { 6.0f, 0.0f },
Cary Clarkc9b7c722018-12-12 14:50:23 -050063#if SK_TREAT_COLINEAR_DIAGONAL_POINTS_AS_CONCAVE == 0
64 { 5.0f, 25.0f }, // remove if collinear diagonal points are not concave
65#endif
Brian Salomonab664fa2017-03-24 16:07:20 +000066 { 4.0f, 50.0f },
67 { -4.0f, 50.0f }
68};
69// degenerate
70const SkPoint gPoints5[] = {
71 { -0.025f, -0.025f },
72 { 0.025f, -0.025f },
73 { 0.025f, 0.025f },
74 { -0.025f, 0.025f }
75};
76// Triangle in which the first point should fuse with last
77const SkPoint gPoints6[] = {
78 { -20.0f, -13.0f },
79 { -20.0f, -13.05f },
80 { 20.0f, -13.0f },
81 { 20.0f, 27.0f }
82};
83// thin rect with colinear lines
84const SkPoint gPoints7[] = {
85 { -10.0f, -50.0f },
86 { 10.0f, -50.0f },
87 { 10.0f, -25.0f },
88 { 10.0f, 0.0f },
89 { 10.0f, 25.0f },
90 { 10.0f, 50.0f },
91 { -10.0f, 50.0f }
92};
93// capped teardrop
94const SkPoint gPoints8[] = {
95 { 50.00f, 50.00f },
96 { 0.00f, 50.00f },
97 { -15.45f, 47.55f },
98 { -29.39f, 40.45f },
99 { -40.45f, 29.39f },
100 { -47.55f, 15.45f },
101 { -50.00f, 0.00f },
102 { -47.55f, -15.45f },
103 { -40.45f, -29.39f },
104 { -29.39f, -40.45f },
105 { -15.45f, -47.55f },
106 { 0.00f, -50.00f },
107 { 50.00f, -50.00f }
108};
109// teardrop
110const SkPoint gPoints9[] = {
111 { 4.39f, 40.45f },
112 { -9.55f, 47.55f },
113 { -25.00f, 50.00f },
114 { -40.45f, 47.55f },
115 { -54.39f, 40.45f },
116 { -65.45f, 29.39f },
117 { -72.55f, 15.45f },
118 { -75.00f, 0.00f },
119 { -72.55f, -15.45f },
120 { -65.45f, -29.39f },
121 { -54.39f, -40.45f },
122 { -40.45f, -47.55f },
123 { -25.0f, -50.0f },
124 { -9.55f, -47.55f },
125 { 4.39f, -40.45f },
126 { 75.00f, 0.00f }
127};
128// clipped triangle
129const SkPoint gPoints10[] = {
130 { -10.0f, -50.0f },
131 { 10.0f, -50.0f },
132 { 50.0f, 31.0f },
133 { 40.0f, 50.0f },
134 { -40.0f, 50.0f },
135 { -50.0f, 31.0f },
136};
137
138const SkPoint* gPoints[] = {
139 gPoints0, gPoints1, gPoints2, gPoints3, gPoints4, gPoints5, gPoints6,
140 gPoints7, gPoints8, gPoints9, gPoints10,
141};
142
143const size_t gSizes[] = {
144 SK_ARRAY_COUNT(gPoints0),
145 SK_ARRAY_COUNT(gPoints1),
146 SK_ARRAY_COUNT(gPoints2),
147 SK_ARRAY_COUNT(gPoints3),
148 SK_ARRAY_COUNT(gPoints4),
149 SK_ARRAY_COUNT(gPoints5),
150 SK_ARRAY_COUNT(gPoints6),
151 SK_ARRAY_COUNT(gPoints7),
152 SK_ARRAY_COUNT(gPoints8),
153 SK_ARRAY_COUNT(gPoints9),
154 SK_ARRAY_COUNT(gPoints10),
155};
156static_assert(SK_ARRAY_COUNT(gSizes) == SK_ARRAY_COUNT(gPoints), "array_mismatch");
157}
158
robertphillips72729352015-04-28 07:42:04 -0700159namespace skiagm {
160
161// This GM is intended to exercise Ganesh's handling of convex line-only
162// paths
163class ConvexLineOnlyPathsGM : public GM {
164public:
robertphillipsad234462016-08-26 05:30:19 -0700165 ConvexLineOnlyPathsGM(bool doStrokeAndFill) : fDoStrokeAndFill(doStrokeAndFill) {
robertphillips72729352015-04-28 07:42:04 -0700166 this->setBGColor(0xFFFFFFFF);
167 }
168
169protected:
robertphillipsad234462016-08-26 05:30:19 -0700170 SkString onShortName() override {
171 if (fDoStrokeAndFill) {
172 return SkString("convex-lineonly-paths-stroke-and-fill");
173 }
174 return SkString("convex-lineonly-paths");
175 }
robertphillips72729352015-04-28 07:42:04 -0700176 SkISize onISize() override { return SkISize::Make(kGMWidth, kGMHeight); }
177 bool runAsBench() const override { return true; }
178
Brian Salomonab664fa2017-03-24 16:07:20 +0000179 static SkPath GetPath(int index, SkPath::Direction dir) {
Ben Wagner7ecc5962016-11-02 17:07:33 -0400180 std::unique_ptr<SkPoint[]> data(nullptr);
robertphillips72729352015-04-28 07:42:04 -0700181 const SkPoint* points;
182 int numPts;
Brian Salomonab664fa2017-03-24 16:07:20 +0000183 if (index < (int) SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
robertphillips72729352015-04-28 07:42:04 -0700184 // manually specified
Brian Salomonab664fa2017-03-24 16:07:20 +0000185 points = ConvexLineOnlyData::gPoints[index];
186 numPts = (int)ConvexLineOnlyData::gSizes[index];
robertphillips72729352015-04-28 07:42:04 -0700187 } else {
188 // procedurally generated
189 SkScalar width = kMaxPathHeight/2;
190 SkScalar height = kMaxPathHeight/2;
Brian Salomonab664fa2017-03-24 16:07:20 +0000191 switch (index-SK_ARRAY_COUNT(ConvexLineOnlyData::gPoints)) {
robertphillips72729352015-04-28 07:42:04 -0700192 case 0:
193 numPts = 3;
194 break;
195 case 1:
196 numPts = 4;
197 break;
198 case 2:
199 numPts = 5;
200 break;
201 case 3: // squashed pentagon
202 numPts = 5;
203 width = kMaxPathHeight/5;
204 break;
205 case 4:
206 numPts = 6;
207 break;
208 case 5:
209 numPts = 8;
210 break;
211 case 6: // squashed octogon
212 numPts = 8;
213 width = kMaxPathHeight/5;
214 break;
215 case 7:
216 numPts = 20;
217 break;
218 case 8:
219 numPts = 100;
220 break;
221 default:
222 numPts = 3;
223 break;
224 }
225
halcanary385fe4d2015-08-26 13:07:48 -0700226 data.reset(new SkPoint[numPts]);
robertphillips72729352015-04-28 07:42:04 -0700227
228 create_ngon(numPts, data.get(), width, height);
229 points = data.get();
230 }
231
232 SkPath path;
233
234 if (SkPath::kCW_Direction == dir) {
235 path.moveTo(points[0]);
236 for (int i = 1; i < numPts; ++i) {
237 path.lineTo(points[i]);
238 }
239 } else {
240 path.moveTo(points[numPts-1]);
241 for (int i = numPts-2; i >= 0; --i) {
242 path.lineTo(points[i]);
243 }
244 }
245
246 path.close();
247#ifdef SK_DEBUG
248 // Each path this method returns should be convex, only composed of
249 // lines, wound the right direction, and short enough to fit in one
250 // of the GMs rows.
251 SkASSERT(path.isConvex());
252 SkASSERT(SkPath::kLine_SegmentMask == path.getSegmentMasks());
reed026beb52015-06-10 14:23:15 -0700253 SkPathPriv::FirstDirection actualDir;
254 SkASSERT(SkPathPriv::CheapComputeFirstDirection(path, &actualDir));
255 SkASSERT(SkPathPriv::AsFirstDirection(dir) == actualDir);
robertphillips72729352015-04-28 07:42:04 -0700256 SkRect bounds = path.getBounds();
257 SkASSERT(SkScalarNearlyEqual(bounds.centerX(), 0.0f));
258 SkASSERT(bounds.height() <= kMaxPathHeight);
259#endif
260 return path;
261 }
262
263 // Draw a single path several times, shrinking it, flipping its direction
264 // and changing its start vertex each time.
robertphillips2a974622015-05-08 07:08:13 -0700265 void drawPath(SkCanvas* canvas, int index, SkPoint* offset) {
robertphillips72729352015-04-28 07:42:04 -0700266
267 SkPoint center;
268 {
Brian Salomonab664fa2017-03-24 16:07:20 +0000269 SkPath path = GetPath(index, SkPath::kCW_Direction);
robertphillips2a974622015-05-08 07:08:13 -0700270 if (offset->fX+path.getBounds().width() > kGMWidth) {
271 offset->fX = 0;
272 offset->fY += kMaxPathHeight;
robertphillipsad234462016-08-26 05:30:19 -0700273 if (fDoStrokeAndFill) {
274 offset->fX += kStrokeWidth / 2.0f;
275 offset->fY += kStrokeWidth / 2.0f;
276 }
robertphillips72729352015-04-28 07:42:04 -0700277 }
robertphillips2a974622015-05-08 07:08:13 -0700278 center = { offset->fX + SkScalarHalf(path.getBounds().width()), offset->fY};
279 offset->fX += path.getBounds().width();
robertphillipsad234462016-08-26 05:30:19 -0700280 if (fDoStrokeAndFill) {
281 offset->fX += kStrokeWidth;
282 }
robertphillips72729352015-04-28 07:42:04 -0700283 }
284
285 const SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
286 const SkPath::Direction dirs[2] = { SkPath::kCW_Direction, SkPath::kCCW_Direction };
287 const float scales[] = { 1.0f, 0.75f, 0.5f, 0.25f, 0.1f, 0.01f, 0.001f };
mtkleindbfd7ab2016-09-01 11:24:54 -0700288 const SkPaint::Join joins[3] = { SkPaint::kRound_Join,
robertphillipsad234462016-08-26 05:30:19 -0700289 SkPaint::kBevel_Join,
290 SkPaint::kMiter_Join };
robertphillips72729352015-04-28 07:42:04 -0700291
292 SkPaint paint;
293 paint.setAntiAlias(true);
294
295 for (size_t i = 0; i < SK_ARRAY_COUNT(scales); ++i) {
Brian Salomonab664fa2017-03-24 16:07:20 +0000296 SkPath path = GetPath(index, dirs[i%2]);
robertphillipsad234462016-08-26 05:30:19 -0700297 if (fDoStrokeAndFill) {
298 paint.setStyle(SkPaint::kStrokeAndFill_Style);
299 paint.setStrokeJoin(joins[i%3]);
300 paint.setStrokeWidth(SkIntToScalar(kStrokeWidth));
301 }
robertphillips72729352015-04-28 07:42:04 -0700302
303 canvas->save();
304 canvas->translate(center.fX, center.fY);
305 canvas->scale(scales[i], scales[i]);
306 paint.setColor(colors[i%2]);
307 canvas->drawPath(path, paint);
308 canvas->restore();
309 }
310 }
311
312 void onDraw(SkCanvas* canvas) override {
robertphillips2a974622015-05-08 07:08:13 -0700313 // the right edge of the last drawn path
halcanary9d524f22016-03-29 09:03:52 -0700314 SkPoint offset = { 0, SkScalarHalf(kMaxPathHeight) };
robertphillipsad234462016-08-26 05:30:19 -0700315 if (fDoStrokeAndFill) {
316 offset.fX += kStrokeWidth / 2.0f;
317 offset.fY += kStrokeWidth / 2.0f;
318 }
robertphillips2a974622015-05-08 07:08:13 -0700319
robertphillips72729352015-04-28 07:42:04 -0700320 for (int i = 0; i < kNumPaths; ++i) {
robertphillips2a974622015-05-08 07:08:13 -0700321 this->drawPath(canvas, i, &offset);
robertphillips72729352015-04-28 07:42:04 -0700322 }
323
robertphillips72729352015-04-28 07:42:04 -0700324 {
Brian Salomon0235c642018-08-31 12:04:18 -0400325 // Repro for crbug.com/472723 (Missing AA on portions of graphic with GPU rasterization)
robertphillips72729352015-04-28 07:42:04 -0700326
327 SkPaint p;
328 p.setAntiAlias(true);
robertphillipsad234462016-08-26 05:30:19 -0700329 if (fDoStrokeAndFill) {
330 p.setStyle(SkPaint::kStrokeAndFill_Style);
331 p.setStrokeJoin(SkPaint::kMiter_Join);
332 p.setStrokeWidth(SkIntToScalar(kStrokeWidth));
333 }
robertphillips72729352015-04-28 07:42:04 -0700334
335 SkPath p1;
336 p1.moveTo(60.8522949f, 364.671021f);
337 p1.lineTo(59.4380493f, 364.671021f);
338 p1.lineTo(385.414276f, 690.647217f);
339 p1.lineTo(386.121399f, 689.940125f);
Brian Salomon0235c642018-08-31 12:04:18 -0400340 canvas->save();
341 canvas->translate(356.0f, 50.0f);
robertphillips72729352015-04-28 07:42:04 -0700342 canvas->drawPath(p1, p);
Brian Salomon0235c642018-08-31 12:04:18 -0400343 canvas->restore();
344
345 // Repro for crbug.com/869172 (SVG path incorrectly simplified when using GPU
346 // Rasterization). This will only draw anything in the stroke-and-fill version.
347 SkPath p2;
348 p2.moveTo(10.f, 0.f);
349 p2.lineTo(38.f, 0.f);
350 p2.lineTo(66.f, 0.f);
351 p2.lineTo(94.f, 0.f);
352 p2.lineTo(122.f, 0.f);
353 p2.lineTo(150.f, 0.f);
354 p2.lineTo(150.f, 0.f);
355 p2.lineTo(122.f, 0.f);
356 p2.lineTo(94.f, 0.f);
357 p2.lineTo(66.f, 0.f);
358 p2.lineTo(38.f, 0.f);
359 p2.lineTo(10.f, 0.f);
360 p2.close();
361 canvas->save();
362 canvas->translate(0.0f, 500.0f);
363 canvas->drawPath(p2, p);
364 canvas->restore();
Brian Salomon8e449eb2018-09-05 15:08:49 -0400365
366 // Repro for crbug.com/856137. This path previously caused GrAAConvexTessellator to turn
367 // inset rings into outsets when adjacent bisector angles converged outside the previous
368 // ring due to accumulated error.
369 SkPath p3;
370 p3.setFillType(SkPath::kEvenOdd_FillType);
371 p3.moveTo(1184.96f, 982.557f);
372 p3.lineTo(1183.71f, 982.865f);
373 p3.lineTo(1180.99f, 982.734f);
374 p3.lineTo(1178.5f, 981.541f);
375 p3.lineTo(1176.35f, 979.367f);
376 p3.lineTo(1178.94f, 938.854f);
377 p3.lineTo(1181.35f, 936.038f);
378 p3.lineTo(1183.96f, 934.117f);
379 p3.lineTo(1186.67f, 933.195f);
380 p3.lineTo(1189.36f, 933.342f);
381 p3.lineTo(1191.58f, 934.38f);
382 p3.close();
383 canvas->save();
384 SkMatrix m;
385 m.setAll(0.0893210843f, 0, 79.1197586f, 0, 0.0893210843f, 300, 0, 0, 1);
386 canvas->concat(m);
387 canvas->drawPath(p3, p);
388 canvas->restore();
robertphillips72729352015-04-28 07:42:04 -0700389 }
390 }
391
392private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700393 static constexpr int kStrokeWidth = 10;
394 static constexpr int kNumPaths = 20;
395 static constexpr int kMaxPathHeight = 100;
396 static constexpr int kGMWidth = 512;
397 static constexpr int kGMHeight = 512;
robertphillips72729352015-04-28 07:42:04 -0700398
robertphillipsad234462016-08-26 05:30:19 -0700399 bool fDoStrokeAndFill;
400
robertphillips72729352015-04-28 07:42:04 -0700401 typedef GM INHERITED;
402};
403
404//////////////////////////////////////////////////////////////////////////////
405
robertphillipsad234462016-08-26 05:30:19 -0700406DEF_GM(return new ConvexLineOnlyPathsGM(false);)
407DEF_GM(return new ConvexLineOnlyPathsGM(true);)
robertphillips72729352015-04-28 07:42:04 -0700408}