blob: 54526fe4f7ff43d6ad50e26d156016d8b57944c4 [file] [log] [blame]
Yuqian Li3154a532017-09-06 13:33:30 -04001/*
2 * Copyright 2011 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 "include/core/SkCanvas.h"
9#include "include/core/SkFont.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkPathEffect.h"
12#include "include/core/SkRRect.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkStream.h"
15#include "include/core/SkStrokeRec.h"
16#include "include/core/SkSurface.h"
17#include "include/private/SkTo.h"
18#include "include/utils/SkNullCanvas.h"
19#include "include/utils/SkParse.h"
20#include "include/utils/SkParsePath.h"
21#include "include/utils/SkRandom.h"
22#include "src/core/SkAutoMalloc.h"
23#include "src/core/SkGeometry.h"
24#include "src/core/SkPathPriv.h"
25#include "src/core/SkReader32.h"
26#include "src/core/SkWriter32.h"
27#include "tests/Test.h"
Yuqian Li3154a532017-09-06 13:33:30 -040028
Hal Canaryc640d0d2018-06-13 09:59:02 -040029#include <cmath>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040030#include <utility>
Cary Clarkc9b7c722018-12-12 14:50:23 -050031#include <vector>
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040032
Yuqian Li3154a532017-09-06 13:33:30 -040033static void set_radii(SkVector radii[4], int index, float rad) {
34 sk_bzero(radii, sizeof(SkVector) * 4);
35 radii[index].set(rad, rad);
36}
37
38static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
39 const SkVector radii[4]) {
40 SkRRect rrect;
41 rrect.setRectRadii(bounds, radii);
42 REPORTER_ASSERT(reporter, bounds == rrect.rect());
43
44 SkPath path;
45 // this line should not assert in the debug build (from validate)
46 path.addRRect(rrect);
47 REPORTER_ASSERT(reporter, bounds == path.getBounds());
48}
49
50static void test_skbug_3469(skiatest::Reporter* reporter) {
51 SkPath path;
52 path.moveTo(20, 20);
53 path.quadTo(20, 50, 80, 50);
54 path.quadTo(20, 50, 20, 80);
55 REPORTER_ASSERT(reporter, !path.isConvex());
56}
57
58static void test_skbug_3239(skiatest::Reporter* reporter) {
59 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
60 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
61 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
62
63 const float rad = 33436320;
64
65 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
66 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
67
68 SkVector radii[4];
69 for (int i = 0; i < 4; ++i) {
70 set_radii(radii, i, rad);
71 test_add_rrect(reporter, rectx, radii);
72 test_add_rrect(reporter, recty, radii);
73 }
74}
75
76static void make_path_crbug364224(SkPath* path) {
77 path->reset();
78 path->moveTo(3.747501373f, 2.724499941f);
79 path->lineTo(3.747501373f, 3.75f);
80 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
81 path->lineTo(0.7475013733f, 4.0f);
82 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
83 path->lineTo(0.4975013733f, 1.0f);
84 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
85 path->lineTo(3.497501373f, 0.75f);
86 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
87 path->lineTo(3.715001345f, 0.5512499809f);
88 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
89 path->lineTo(0.7475013733f, 0.4999999702f);
90 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
91 path->lineTo(0.2475013733f, 3.75f);
92 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
93 path->lineTo(3.497501373f, 4.25f);
94 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
95 path->lineTo(3.997501373f, 2.474750042f);
96 path->lineTo(3.747501373f, 2.724499941f);
97 path->close();
98}
99
100static void make_path_crbug364224_simplified(SkPath* path) {
101 path->moveTo(3.747501373f, 2.724499941f);
102 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
103 path->close();
104}
105
106static void test_sect_with_horizontal_needs_pinning() {
107 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
108 // intersection.
109 SkPath path;
110 path.reset();
111 path.moveTo(-540000, -720000);
112 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
113 path.lineTo(1, 1);
114
115 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
116 SkPaint paint;
117 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
118}
119
120static void test_path_crbug364224() {
121 SkPath path;
122 SkPaint paint;
123 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
124 SkCanvas* canvas = surface->getCanvas();
125
126 make_path_crbug364224_simplified(&path);
127 canvas->drawPath(path, paint);
128
129 make_path_crbug364224(&path);
130 canvas->drawPath(path, paint);
131}
132
Yuqian Lif13beef2017-09-14 17:15:04 -0400133static void test_draw_AA_path(int width, int height, const SkPath& path) {
134 auto surface(SkSurface::MakeRasterN32Premul(width, height));
135 SkCanvas* canvas = surface->getCanvas();
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
Yuqian Li3154a532017-09-06 13:33:30 -0400141// this is a unit test instead of a GM because it doesn't draw anything
142static void test_fuzz_crbug_638223() {
Yuqian Li3154a532017-09-06 13:33:30 -0400143 SkPath path;
144 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
145 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
146 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
147 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
Yuqian Lif13beef2017-09-14 17:15:04 -0400148 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400149}
150
151static void test_fuzz_crbug_643933() {
Yuqian Li3154a532017-09-06 13:33:30 -0400152 SkPath path;
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
155 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
156 SkBits2Float(0x721aee0c)); // 3.0687e+30f
Yuqian Lif13beef2017-09-14 17:15:04 -0400157 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400158 path.reset();
159 path.moveTo(0, 0);
160 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
161 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
162 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
Yuqian Lif13beef2017-09-14 17:15:04 -0400163 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400164}
165
166static void test_fuzz_crbug_647922() {
Yuqian Li3154a532017-09-06 13:33:30 -0400167 SkPath path;
168 path.moveTo(0, 0);
169 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
170 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
171 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
Yuqian Lif13beef2017-09-14 17:15:04 -0400172 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400173}
174
175static void test_fuzz_crbug_662780() {
176 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
177 SkCanvas* canvas = surface->getCanvas();
178 SkPaint paint;
179 paint.setAntiAlias(true);
180 SkPath path;
181 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
182 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
183 // 8, 8, 8.00002f, 8, 0.707107f
184 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
185 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
186 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
187 // 308, 8, 308, 8, 0.707107f
188 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
189 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
190 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
191 // 308, 158, 308, 158, 0.707107f
192 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
193 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
194 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
195 // 8, 158, 8, 158, 0.707107f
196 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
197 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
198 path.close();
199 canvas->clipPath(path, true);
200 canvas->drawRect(SkRect::MakeWH(250, 250), paint);
201}
202
203static void test_mask_overflow() {
Yuqian Li3154a532017-09-06 13:33:30 -0400204 SkPath path;
205 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
206 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
207 // 440.47f, 341, 437, 344.47f, 437, 348.75f
208 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
209 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
210 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
211 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
212 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
213 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
Yuqian Lif13beef2017-09-14 17:15:04 -0400214 test_draw_AA_path(500, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400215}
216
217static void test_fuzz_crbug_668907() {
Yuqian Li3154a532017-09-06 13:33:30 -0400218 SkPath path;
219 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
220 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
221 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
222 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
223 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -0400224 test_draw_AA_path(400, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400225}
226
227/**
228 * In debug mode, this path was causing an assertion to fail in
229 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
230 */
231static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
232 SkPoint orig, p1, p2, p3;
233 orig = SkPoint::Make(1.f, 1.f);
234 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
235 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
236 p3 = SkPoint::Make(2.f, 2.f);
237
238 path->reset();
239 path->moveTo(orig);
240 path->cubicTo(p1, p2, p3);
241 path->close();
242}
243
244static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
245 SkPath path;
246 make_path_crbugskia2820(&path, reporter);
247
248 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
249 stroke.setStrokeStyle(2 * SK_Scalar1);
250 stroke.applyToPath(&path, path);
251}
252
253static void test_path_crbugskia5995() {
Yuqian Li3154a532017-09-06 13:33:30 -0400254 SkPath path;
255 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
256 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
257 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
258 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
Yuqian Lif13beef2017-09-14 17:15:04 -0400259 test_draw_AA_path(500, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400260}
261
262static void make_path0(SkPath* path) {
263 // from * https://code.google.com/p/skia/issues/detail?id=1706
264
265 path->moveTo(146.939f, 1012.84f);
266 path->lineTo(181.747f, 1009.18f);
267 path->lineTo(182.165f, 1013.16f);
268 path->lineTo(147.357f, 1016.82f);
269 path->lineTo(146.939f, 1012.84f);
270 path->close();
271}
272
273static void make_path1(SkPath* path) {
274 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
275}
276
277typedef void (*PathProc)(SkPath*);
278
279/*
280 * Regression test: we used to crash (overwrite internal storage) during
281 * construction of the region when the path was INVERSE. That is now fixed,
282 * so test these regions (which used to assert/crash).
283 *
284 * https://code.google.com/p/skia/issues/detail?id=1706
285 */
286static void test_path_to_region(skiatest::Reporter* reporter) {
287 PathProc procs[] = {
288 make_path0,
289 make_path1,
290 };
291
292 SkRegion clip;
Mike Reed92b33352019-08-24 19:39:13 -0400293 clip.setRect({0, 0, 1255, 1925});
Yuqian Li3154a532017-09-06 13:33:30 -0400294
295 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
296 SkPath path;
297 procs[i](&path);
298
299 SkRegion rgn;
300 rgn.setPath(path, clip);
301 path.toggleInverseFillType();
302 rgn.setPath(path, clip);
303 }
304}
305
306#ifdef SK_BUILD_FOR_WIN
307 #define SUPPRESS_VISIBILITY_WARNING
308#else
309 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
310#endif
311
312static void test_path_close_issue1474(skiatest::Reporter* reporter) {
313 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
314 // are relative to the point we close to, not relative to the point we close from.
315 SkPath path;
316 SkPoint last;
317
318 // Test rLineTo().
319 path.rLineTo(0, 100);
320 path.rLineTo(100, 0);
321 path.close(); // Returns us back to 0,0.
322 path.rLineTo(50, 50); // This should go to 50,50.
323
324 path.getLastPt(&last);
325 REPORTER_ASSERT(reporter, 50 == last.fX);
326 REPORTER_ASSERT(reporter, 50 == last.fY);
327
328 // Test rQuadTo().
329 path.rewind();
330 path.rLineTo(0, 100);
331 path.rLineTo(100, 0);
332 path.close();
333 path.rQuadTo(50, 50, 75, 75);
334
335 path.getLastPt(&last);
336 REPORTER_ASSERT(reporter, 75 == last.fX);
337 REPORTER_ASSERT(reporter, 75 == last.fY);
338
339 // Test rConicTo().
340 path.rewind();
341 path.rLineTo(0, 100);
342 path.rLineTo(100, 0);
343 path.close();
344 path.rConicTo(50, 50, 85, 85, 2);
345
346 path.getLastPt(&last);
347 REPORTER_ASSERT(reporter, 85 == last.fX);
348 REPORTER_ASSERT(reporter, 85 == last.fY);
349
350 // Test rCubicTo().
351 path.rewind();
352 path.rLineTo(0, 100);
353 path.rLineTo(100, 0);
354 path.close();
355 path.rCubicTo(50, 50, 85, 85, 95, 95);
356
357 path.getLastPt(&last);
358 REPORTER_ASSERT(reporter, 95 == last.fX);
359 REPORTER_ASSERT(reporter, 95 == last.fY);
360}
361
362static void test_gen_id(skiatest::Reporter* reporter) {
363 SkPath a, b;
364 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
365
366 a.moveTo(0, 0);
367 const uint32_t z = a.getGenerationID();
368 REPORTER_ASSERT(reporter, z != b.getGenerationID());
369
370 a.reset();
371 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
372
373 a.moveTo(1, 1);
374 const uint32_t y = a.getGenerationID();
375 REPORTER_ASSERT(reporter, z != y);
376
377 b.moveTo(2, 2);
378 const uint32_t x = b.getGenerationID();
379 REPORTER_ASSERT(reporter, x != y && x != z);
380
381 a.swap(b);
382 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
383
384 b = a;
385 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
386
387 SkPath c(a);
388 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
389
390 c.lineTo(3, 3);
391 const uint32_t w = c.getGenerationID();
392 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
393 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
394 REPORTER_ASSERT(reporter, w != x);
395
396#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
397 static bool kExpectGenIDToIgnoreFill = false;
398#else
399 static bool kExpectGenIDToIgnoreFill = true;
400#endif
401
402 c.toggleInverseFillType();
403 const uint32_t v = c.getGenerationID();
404 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
405
406 c.rewind();
407 REPORTER_ASSERT(reporter, v != c.getGenerationID());
408}
409
410// This used to assert in the debug build, as the edges did not all line-up.
411static void test_bad_cubic_crbug234190() {
412 SkPath path;
413 path.moveTo(13.8509f, 3.16858f);
414 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
415 -2.38991e+08f, -4.26573e+08f,
416 -2.41016e+08f, -4.30188e+08f);
Yuqian Lif13beef2017-09-14 17:15:04 -0400417 test_draw_AA_path(84, 88, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400418}
419
420static void test_bad_cubic_crbug229478() {
421 const SkPoint pts[] = {
422 { 4595.91064f, -11596.9873f },
423 { 4597.2168f, -11595.9414f },
424 { 4598.52344f, -11594.8955f },
425 { 4599.83008f, -11593.8496f },
426 };
427
428 SkPath path;
429 path.moveTo(pts[0]);
430 path.cubicTo(pts[1], pts[2], pts[3]);
431
432 SkPaint paint;
433 paint.setStyle(SkPaint::kStroke_Style);
434 paint.setStrokeWidth(20);
435
436 SkPath dst;
437 // Before the fix, this would infinite-recurse, and run out of stack
438 // because we would keep trying to subdivide a degenerate cubic segment.
439 paint.getFillPath(path, &dst, nullptr);
440}
441
442static void build_path_170666(SkPath& path) {
443 path.moveTo(17.9459f, 21.6344f);
444 path.lineTo(139.545f, -47.8105f);
445 path.lineTo(139.545f, -47.8105f);
446 path.lineTo(131.07f, -47.3888f);
447 path.lineTo(131.07f, -47.3888f);
448 path.lineTo(122.586f, -46.9532f);
449 path.lineTo(122.586f, -46.9532f);
450 path.lineTo(18076.6f, 31390.9f);
451 path.lineTo(18076.6f, 31390.9f);
452 path.lineTo(18085.1f, 31390.5f);
453 path.lineTo(18085.1f, 31390.5f);
454 path.lineTo(18076.6f, 31390.9f);
455 path.lineTo(18076.6f, 31390.9f);
456 path.lineTo(17955, 31460.3f);
457 path.lineTo(17955, 31460.3f);
458 path.lineTo(17963.5f, 31459.9f);
459 path.lineTo(17963.5f, 31459.9f);
460 path.lineTo(17971.9f, 31459.5f);
461 path.lineTo(17971.9f, 31459.5f);
462 path.lineTo(17.9551f, 21.6205f);
463 path.lineTo(17.9551f, 21.6205f);
464 path.lineTo(9.47091f, 22.0561f);
465 path.lineTo(9.47091f, 22.0561f);
466 path.lineTo(17.9459f, 21.6344f);
467 path.lineTo(17.9459f, 21.6344f);
468 path.close();path.moveTo(0.995934f, 22.4779f);
469 path.lineTo(0.986725f, 22.4918f);
470 path.lineTo(0.986725f, 22.4918f);
471 path.lineTo(17955, 31460.4f);
472 path.lineTo(17955, 31460.4f);
473 path.lineTo(17971.9f, 31459.5f);
474 path.lineTo(17971.9f, 31459.5f);
475 path.lineTo(18093.6f, 31390.1f);
476 path.lineTo(18093.6f, 31390.1f);
477 path.lineTo(18093.6f, 31390);
478 path.lineTo(18093.6f, 31390);
479 path.lineTo(139.555f, -47.8244f);
480 path.lineTo(139.555f, -47.8244f);
481 path.lineTo(122.595f, -46.9671f);
482 path.lineTo(122.595f, -46.9671f);
483 path.lineTo(0.995934f, 22.4779f);
484 path.lineTo(0.995934f, 22.4779f);
485 path.close();
486 path.moveTo(5.43941f, 25.5223f);
487 path.lineTo(798267, -28871.1f);
488 path.lineTo(798267, -28871.1f);
489 path.lineTo(3.12512e+06f, -113102);
490 path.lineTo(3.12512e+06f, -113102);
491 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
492 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
493 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
494 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
495 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
496 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
497 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
498 path.lineTo(2.78271e+08f, -1.00733e+07f);
499 path.lineTo(2.78271e+08f, -1.00733e+07f);
500 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
501 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
502 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
503 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
504 path.lineTo(2.77473e+08f, -1.00444e+07f);
505 path.lineTo(2.77473e+08f, -1.00444e+07f);
506 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
507 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
508 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
509 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
510 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
511 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
512 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
513 path.lineTo(798284, -28872);
514 path.lineTo(798284, -28872);
515 path.lineTo(22.4044f, 24.6677f);
516 path.lineTo(22.4044f, 24.6677f);
517 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
518 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
519 path.close();
520}
521
522static void build_path_simple_170666(SkPath& path) {
523 path.moveTo(126.677f, 24.1591f);
524 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
525}
526
527// This used to assert in the SK_DEBUG build, as the clip step would fail with
528// too-few interations in our cubic-line intersection code. That code now runs
529// 24 interations (instead of 16).
530static void test_crbug_170666() {
531 SkPath path;
Yuqian Li3154a532017-09-06 13:33:30 -0400532 build_path_simple_170666(path);
Yuqian Lif13beef2017-09-14 17:15:04 -0400533 test_draw_AA_path(1000, 1000, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400534
535 build_path_170666(path);
Yuqian Lif13beef2017-09-14 17:15:04 -0400536 test_draw_AA_path(1000, 1000, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400537}
538
539
540static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
541 SkScalar tx, SkScalar ty, SkScalar scale) {
542 SkPath smallPath;
543 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
544 bool smallConvex = smallPath.isConvex();
545 SkPath largePath;
546 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
547 SkMatrix matrix;
548 matrix.reset();
549 matrix.preTranslate(100, 100);
550 matrix.preScale(scale, scale);
551 largePath.transform(matrix);
552 bool largeConvex = largePath.isConvex();
553 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
554}
555
556static void test_crbug_493450(skiatest::Reporter* reporter) {
557 const char reducedCase[] =
558 "M0,0"
559 "L0.0002, 0"
560 "L0.0002, 0.0002"
561 "L0.0001, 0.0001"
562 "L0,0.0002"
563 "Z";
564 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
565 const char originalFiddleData[] =
566 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
567 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
568 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
569 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
570 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
571 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
572 826357.3384828606f);
573}
574
575static void test_crbug_495894(skiatest::Reporter* reporter) {
576 const char originalFiddleData[] =
577 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
578 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
579 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
580 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
581 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
582 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
583 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
584 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
585 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
586 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
587 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
588 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
589 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
590 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
591 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
592 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
593 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
594 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
595 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
596 "L-0.33993994441916414,-0.11288906492739594Z";
597 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
598 65536);
599}
600
601static void test_crbug_613918() {
602 SkPath path;
603 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
604 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
605 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
606
607 // This call could lead to an assert or uninitialized read due to a failure
608 // to check the return value from SkCubicClipper::ChopMonoAtY.
609 path.contains(-1.84817e-08f, 1.15465e-08f);
610}
611
612static void test_addrect(skiatest::Reporter* reporter) {
613 SkPath path;
614 path.lineTo(0, 0);
615 path.addRect(SkRect::MakeWH(50, 100));
616 REPORTER_ASSERT(reporter, path.isRect(nullptr));
617
618 path.reset();
619 path.lineTo(FLT_EPSILON, FLT_EPSILON);
620 path.addRect(SkRect::MakeWH(50, 100));
621 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
622
623 path.reset();
624 path.quadTo(0, 0, 0, 0);
625 path.addRect(SkRect::MakeWH(50, 100));
626 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
627
628 path.reset();
629 path.conicTo(0, 0, 0, 0, 0.5f);
630 path.addRect(SkRect::MakeWH(50, 100));
631 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
632
633 path.reset();
634 path.cubicTo(0, 0, 0, 0, 0, 0);
635 path.addRect(SkRect::MakeWH(50, 100));
636 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
637}
638
639// Make sure we stay non-finite once we get there (unless we reset or rewind).
640static void test_addrect_isfinite(skiatest::Reporter* reporter) {
641 SkPath path;
642
643 path.addRect(SkRect::MakeWH(50, 100));
644 REPORTER_ASSERT(reporter, path.isFinite());
645
646 path.moveTo(0, 0);
647 path.lineTo(SK_ScalarInfinity, 42);
648 REPORTER_ASSERT(reporter, !path.isFinite());
649
650 path.addRect(SkRect::MakeWH(50, 100));
651 REPORTER_ASSERT(reporter, !path.isFinite());
652
653 path.reset();
654 REPORTER_ASSERT(reporter, path.isFinite());
655
656 path.addRect(SkRect::MakeWH(50, 100));
657 REPORTER_ASSERT(reporter, path.isFinite());
658}
659
660static void build_big_path(SkPath* path, bool reducedCase) {
661 if (reducedCase) {
662 path->moveTo(577330, 1971.72f);
663 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
664 } else {
665 path->moveTo(60.1631f, 7.70567f);
666 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
667 path->lineTo(577379, 1977.77f);
668 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
669 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
670 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
671 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
672 path->quadTo(577198, 1972, 577238, 1971.31f);
673 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
674 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
675 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
676 path->moveTo(306.718f, -32.912f);
677 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
678 }
679}
680
681static void test_clipped_cubic() {
682 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
683
684 // This path used to assert, because our cubic-chopping code incorrectly
685 // moved control points after the chop. This test should be run in SK_DEBUG
686 // mode to ensure that we no long assert.
687 SkPath path;
688 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
689 build_big_path(&path, SkToBool(doReducedCase));
690
691 SkPaint paint;
692 for (int doAA = 0; doAA <= 1; ++doAA) {
693 paint.setAntiAlias(SkToBool(doAA));
694 surface->getCanvas()->drawPath(path, paint);
695 }
696 }
697}
698
699static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
700 if (expected != bounds) {
701 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
702 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
703 expected.left(), expected.top(), expected.right(), expected.bottom());
704 }
705}
706
707static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
708 SkPath path;
709#if 0
710 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
711 // rewritten them to avoid this (compiler-bug?).
712 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
713
714 path.moveTo(-5, -8);
715 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
716
717 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
718 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
719
720 path.moveTo(1, 2);
721 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
722#else
723 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
724
725 path.moveTo(-5, -8); // should set the bounds
726 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
727
728 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
729 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
730
731 path.moveTo(1, 2); // don't expect this to have changed the bounds
732 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
733#endif
734}
735
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500736#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -0400737static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
738 SkPath path;
739 path.moveTo(0, 0);
740 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
Yuqian Lif13beef2017-09-14 17:15:04 -0400741 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400742}
743
744// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
745// which triggered an assert, from a tricky cubic. This test replicates that
746// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
747// assert in the SK_DEBUG build.
748static void test_tricky_cubic() {
749 const SkPoint pts[] = {
750 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
751 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
752 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
753 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
754 };
755
756 SkPath path;
757 path.moveTo(pts[0]);
758 path.cubicTo(pts[1], pts[2], pts[3]);
Yuqian Lif13beef2017-09-14 17:15:04 -0400759 test_draw_AA_path(19, 130, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400760}
761
762// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
763//
764static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
765 SkPath path;
766 path.quadTo(157, 366, 286, 208);
767 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
768
769 SkMatrix matrix;
770 matrix.setScale(1000*1000, 1000*1000);
771
772 // Be sure that path::transform correctly updates isFinite and the bounds
773 // if the transformation overflows. The previous bug was that isFinite was
774 // set to true in this case, but the bounds were not set to empty (which
775 // they should be).
776 while (path.isFinite()) {
777 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
778 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
779 path.transform(matrix);
780 }
781 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
782
783 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
784 path.transform(matrix);
785 // we need to still be non-finite
786 REPORTER_ASSERT(reporter, !path.isFinite());
787 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
788}
789
790static void add_corner_arc(SkPath* path, const SkRect& rect,
791 SkScalar xIn, SkScalar yIn,
792 int startAngle)
793{
794
795 SkScalar rx = SkMinScalar(rect.width(), xIn);
796 SkScalar ry = SkMinScalar(rect.height(), yIn);
797
798 SkRect arcRect;
Mike Reed92b33352019-08-24 19:39:13 -0400799 arcRect.setLTRB(-rx, -ry, rx, ry);
Yuqian Li3154a532017-09-06 13:33:30 -0400800 switch (startAngle) {
801 case 0:
802 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
803 break;
804 case 90:
805 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
806 break;
807 case 180:
808 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
809 break;
810 case 270:
811 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
812 break;
813 default:
814 break;
815 }
816
817 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
818}
819
820static void make_arb_round_rect(SkPath* path, const SkRect& r,
821 SkScalar xCorner, SkScalar yCorner) {
822 // we are lazy here and use the same x & y for each corner
823 add_corner_arc(path, r, xCorner, yCorner, 270);
824 add_corner_arc(path, r, xCorner, yCorner, 0);
825 add_corner_arc(path, r, xCorner, yCorner, 90);
826 add_corner_arc(path, r, xCorner, yCorner, 180);
827 path->close();
828}
829
830// Chrome creates its own round rects with each corner possibly being different.
831// Performance will suffer if they are not convex.
832// Note: PathBench::ArbRoundRectBench performs almost exactly
833// the same test (but with drawing)
834static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
835 SkRandom rand;
836 SkRect r;
837
838 for (int i = 0; i < 5000; ++i) {
839
840 SkScalar size = rand.nextUScalar1() * 30;
841 if (size < SK_Scalar1) {
842 continue;
843 }
844 r.fLeft = rand.nextUScalar1() * 300;
845 r.fTop = rand.nextUScalar1() * 300;
846 r.fRight = r.fLeft + 2 * size;
847 r.fBottom = r.fTop + 2 * size;
848
849 SkPath temp;
850
851 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
852
853 REPORTER_ASSERT(reporter, temp.isConvex());
854 }
855}
856
857// Chrome will sometimes create a 0 radius round rect. The degenerate
858// quads prevent the path from being converted to a rect
859// Note: PathBench::ArbRoundRectBench performs almost exactly
860// the same test (but with drawing)
861static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
862 SkRandom rand;
863 SkRect r;
864
865 for (int i = 0; i < 5000; ++i) {
866
867 SkScalar size = rand.nextUScalar1() * 30;
868 if (size < SK_Scalar1) {
869 continue;
870 }
871 r.fLeft = rand.nextUScalar1() * 300;
872 r.fTop = rand.nextUScalar1() * 300;
873 r.fRight = r.fLeft + 2 * size;
874 r.fBottom = r.fTop + 2 * size;
875
876 SkPath temp;
877
878 make_arb_round_rect(&temp, r, 0, 0);
879
880 SkRect result;
881 REPORTER_ASSERT(reporter, temp.isRect(&result));
882 REPORTER_ASSERT(reporter, r == result);
883 }
884}
885
886static void test_rect_isfinite(skiatest::Reporter* reporter) {
887 const SkScalar inf = SK_ScalarInfinity;
888 const SkScalar negInf = SK_ScalarNegativeInfinity;
889 const SkScalar nan = SK_ScalarNaN;
890
891 SkRect r;
892 r.setEmpty();
893 REPORTER_ASSERT(reporter, r.isFinite());
Mike Reed92b33352019-08-24 19:39:13 -0400894 r.setLTRB(0, 0, inf, negInf);
Yuqian Li3154a532017-09-06 13:33:30 -0400895 REPORTER_ASSERT(reporter, !r.isFinite());
Mike Reed92b33352019-08-24 19:39:13 -0400896 r.setLTRB(0, 0, nan, 0);
Yuqian Li3154a532017-09-06 13:33:30 -0400897 REPORTER_ASSERT(reporter, !r.isFinite());
898
899 SkPoint pts[] = {
900 { 0, 0 },
901 { SK_Scalar1, 0 },
902 { 0, SK_Scalar1 },
903 };
904
905 bool isFine = r.setBoundsCheck(pts, 3);
906 REPORTER_ASSERT(reporter, isFine);
907 REPORTER_ASSERT(reporter, !r.isEmpty());
908
909 pts[1].set(inf, 0);
910 isFine = r.setBoundsCheck(pts, 3);
911 REPORTER_ASSERT(reporter, !isFine);
912 REPORTER_ASSERT(reporter, r.isEmpty());
913
914 pts[1].set(nan, 0);
915 isFine = r.setBoundsCheck(pts, 3);
916 REPORTER_ASSERT(reporter, !isFine);
917 REPORTER_ASSERT(reporter, r.isEmpty());
918}
919
920static void test_path_isfinite(skiatest::Reporter* reporter) {
921 const SkScalar inf = SK_ScalarInfinity;
922 const SkScalar negInf = SK_ScalarNegativeInfinity;
923 const SkScalar nan = SK_ScalarNaN;
924
925 SkPath path;
926 REPORTER_ASSERT(reporter, path.isFinite());
927
928 path.reset();
929 REPORTER_ASSERT(reporter, path.isFinite());
930
931 path.reset();
932 path.moveTo(SK_Scalar1, 0);
933 REPORTER_ASSERT(reporter, path.isFinite());
934
935 path.reset();
936 path.moveTo(inf, negInf);
937 REPORTER_ASSERT(reporter, !path.isFinite());
938
939 path.reset();
940 path.moveTo(nan, 0);
941 REPORTER_ASSERT(reporter, !path.isFinite());
942}
943
944static void test_isfinite(skiatest::Reporter* reporter) {
945 test_rect_isfinite(reporter);
946 test_path_isfinite(reporter);
947}
948
949static void test_islastcontourclosed(skiatest::Reporter* reporter) {
950 SkPath path;
951 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
952 path.moveTo(0, 0);
953 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
954 path.close();
955 REPORTER_ASSERT(reporter, path.isLastContourClosed());
956 path.lineTo(100, 100);
957 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
958 path.moveTo(200, 200);
959 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
960 path.close();
961 REPORTER_ASSERT(reporter, path.isLastContourClosed());
962 path.moveTo(0, 0);
963 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
964}
965
966// assert that we always
967// start with a moveTo
968// only have 1 moveTo
969// only have Lines after that
970// end with a single close
971// only have (at most) 1 close
972//
973static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
974 const SkPoint srcPts[], bool expectClose) {
975 SkPath::RawIter iter(path);
976 SkPoint pts[4];
977
978 bool firstTime = true;
979 bool foundClose = false;
980 for (;;) {
981 switch (iter.next(pts)) {
982 case SkPath::kMove_Verb:
983 REPORTER_ASSERT(reporter, firstTime);
984 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
985 srcPts++;
986 firstTime = false;
987 break;
988 case SkPath::kLine_Verb:
989 REPORTER_ASSERT(reporter, !firstTime);
990 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
991 srcPts++;
992 break;
993 case SkPath::kQuad_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -0500994 REPORTER_ASSERT(reporter, false, "unexpected quad verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400995 break;
996 case SkPath::kConic_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -0500997 REPORTER_ASSERT(reporter, false, "unexpected conic verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400998 break;
999 case SkPath::kCubic_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -05001000 REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
Yuqian Li3154a532017-09-06 13:33:30 -04001001 break;
1002 case SkPath::kClose_Verb:
1003 REPORTER_ASSERT(reporter, !firstTime);
1004 REPORTER_ASSERT(reporter, !foundClose);
1005 REPORTER_ASSERT(reporter, expectClose);
1006 foundClose = true;
1007 break;
1008 case SkPath::kDone_Verb:
1009 goto DONE;
1010 }
1011 }
1012DONE:
1013 REPORTER_ASSERT(reporter, foundClose == expectClose);
1014}
1015
1016static void test_addPoly(skiatest::Reporter* reporter) {
1017 SkPoint pts[32];
1018 SkRandom rand;
1019
1020 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1021 pts[i].fX = rand.nextSScalar1();
1022 pts[i].fY = rand.nextSScalar1();
1023 }
1024
1025 for (int doClose = 0; doClose <= 1; ++doClose) {
1026 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1027 SkPath path;
1028 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
1029 test_poly(reporter, path, pts, SkToBool(doClose));
1030 }
1031 }
1032}
1033
1034static void test_strokerec(skiatest::Reporter* reporter) {
1035 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1036 REPORTER_ASSERT(reporter, rec.isFillStyle());
1037
1038 rec.setHairlineStyle();
1039 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
1040
1041 rec.setStrokeStyle(SK_Scalar1, false);
1042 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
1043
1044 rec.setStrokeStyle(SK_Scalar1, true);
1045 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
1046
1047 rec.setStrokeStyle(0, false);
1048 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
1049
1050 rec.setStrokeStyle(0, true);
1051 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1052}
1053
1054// Set this for paths that don't have a consistent direction such as a bowtie.
1055// (cheapComputeDirection is not expected to catch these.)
Mike Kleinf6c85402018-08-13 12:19:28 -04001056// Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
1057const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(3);
Yuqian Li3154a532017-09-06 13:33:30 -04001058
1059static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
1060 SkPathPriv::FirstDirection expected) {
1061 if (expected == kDontCheckDir) {
1062 return;
1063 }
1064 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1065
1066 SkPathPriv::FirstDirection dir;
1067 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
1068 REPORTER_ASSERT(reporter, dir == expected);
1069 } else {
1070 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
1071 }
1072}
1073
1074static void test_direction(skiatest::Reporter* reporter) {
1075 size_t i;
1076 SkPath path;
1077 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1078 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1079 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1080 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
1081
1082 static const char* gDegen[] = {
1083 "M 10 10",
1084 "M 10 10 M 20 20",
1085 "M 10 10 L 20 20",
1086 "M 10 10 L 10 10 L 10 10",
1087 "M 10 10 Q 10 10 10 10",
1088 "M 10 10 C 10 10 10 10 10 10",
1089 };
1090 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1091 path.reset();
1092 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1093 REPORTER_ASSERT(reporter, valid);
1094 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1095 }
1096
1097 static const char* gCW[] = {
1098 "M 10 10 L 10 10 Q 20 10 20 20",
1099 "M 10 10 C 20 10 20 20 20 20",
1100 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1101 // rect with top two corners replaced by cubics with identical middle
1102 // control points
1103 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1104 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
1105 };
1106 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1107 path.reset();
1108 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1109 REPORTER_ASSERT(reporter, valid);
1110 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1111 }
1112
1113 static const char* gCCW[] = {
1114 "M 10 10 L 10 10 Q 20 10 20 -20",
1115 "M 10 10 C 20 10 20 -20 20 -20",
1116 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1117 // rect with top two corners replaced by cubics with identical middle
1118 // control points
1119 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1120 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
1121 };
1122 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1123 path.reset();
1124 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1125 REPORTER_ASSERT(reporter, valid);
1126 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1127 }
1128
1129 // Test two donuts, each wound a different direction. Only the outer contour
1130 // determines the cheap direction
1131 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001132 path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCW);
1133 path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001134 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1135
1136 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001137 path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCW);
1138 path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001139 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1140
1141 // triangle with one point really far from the origin.
1142 path.reset();
1143 // the first point is roughly 1.05e10, 1.05e10
1144 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1145 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1146 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1147 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1148
1149 path.reset();
1150 path.conicTo(20, 0, 20, 20, 0.5f);
1151 path.close();
1152 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1153
1154 path.reset();
1155 path.lineTo(1, 1e7f);
1156 path.lineTo(1e7f, 2e7f);
1157 path.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001158 REPORTER_ASSERT(reporter, path.isConvex());
Yuqian Li3154a532017-09-06 13:33:30 -04001159 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1160}
1161
1162static void add_rect(SkPath* path, const SkRect& r) {
1163 path->moveTo(r.fLeft, r.fTop);
1164 path->lineTo(r.fRight, r.fTop);
1165 path->lineTo(r.fRight, r.fBottom);
1166 path->lineTo(r.fLeft, r.fBottom);
1167 path->close();
1168}
1169
1170static void test_bounds(skiatest::Reporter* reporter) {
1171 static const SkRect rects[] = {
1172 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1173 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1174 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1175 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1176 };
1177
1178 SkPath path0, path1;
1179 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1180 path0.addRect(rects[i]);
1181 add_rect(&path1, rects[i]);
1182 }
1183
1184 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1185}
1186
1187static void stroke_cubic(const SkPoint pts[4]) {
1188 SkPath path;
1189 path.moveTo(pts[0]);
1190 path.cubicTo(pts[1], pts[2], pts[3]);
1191
1192 SkPaint paint;
1193 paint.setStyle(SkPaint::kStroke_Style);
1194 paint.setStrokeWidth(SK_Scalar1 * 2);
1195
1196 SkPath fill;
1197 paint.getFillPath(path, &fill);
1198}
1199
1200// just ensure this can run w/o any SkASSERTS firing in the debug build
1201// we used to assert due to differences in how we determine a degenerate vector
1202// but that was fixed with the introduction of SkPoint::CanNormalize
1203static void stroke_tiny_cubic() {
1204 SkPoint p0[] = {
1205 { 372.0f, 92.0f },
1206 { 372.0f, 92.0f },
1207 { 372.0f, 92.0f },
1208 { 372.0f, 92.0f },
1209 };
1210
1211 stroke_cubic(p0);
1212
1213 SkPoint p1[] = {
1214 { 372.0f, 92.0f },
1215 { 372.0007f, 92.000755f },
1216 { 371.99927f, 92.003922f },
1217 { 371.99826f, 92.003899f },
1218 };
1219
1220 stroke_cubic(p1);
1221}
1222
1223static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1224 for (int i = 0; i < 2; ++i) {
1225 SkPath::Iter iter(path, SkToBool(i));
1226 SkPoint mv;
1227 SkPoint pts[4];
1228 SkPath::Verb v;
1229 int nMT = 0;
1230 int nCL = 0;
1231 mv.set(0, 0);
1232 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1233 switch (v) {
1234 case SkPath::kMove_Verb:
1235 mv = pts[0];
1236 ++nMT;
1237 break;
1238 case SkPath::kClose_Verb:
1239 REPORTER_ASSERT(reporter, mv == pts[0]);
1240 ++nCL;
1241 break;
1242 default:
1243 break;
1244 }
1245 }
1246 // if we force a close on the interator we should have a close
1247 // for every moveTo
1248 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1249 }
1250}
1251
1252static void test_close(skiatest::Reporter* reporter) {
1253 SkPath closePt;
1254 closePt.moveTo(0, 0);
1255 closePt.close();
1256 check_close(reporter, closePt);
1257
1258 SkPath openPt;
1259 openPt.moveTo(0, 0);
1260 check_close(reporter, openPt);
1261
1262 SkPath empty;
1263 check_close(reporter, empty);
1264 empty.close();
1265 check_close(reporter, empty);
1266
1267 SkPath rect;
1268 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1269 check_close(reporter, rect);
1270 rect.close();
1271 check_close(reporter, rect);
1272
1273 SkPath quad;
1274 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1275 check_close(reporter, quad);
1276 quad.close();
1277 check_close(reporter, quad);
1278
1279 SkPath cubic;
1280 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1281 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1282 check_close(reporter, cubic);
1283 cubic.close();
1284 check_close(reporter, cubic);
1285
1286 SkPath line;
1287 line.moveTo(SK_Scalar1, SK_Scalar1);
1288 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1289 check_close(reporter, line);
1290 line.close();
1291 check_close(reporter, line);
1292
1293 SkPath rect2;
1294 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1295 rect2.close();
1296 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1297 check_close(reporter, rect2);
1298 rect2.close();
1299 check_close(reporter, rect2);
1300
1301 SkPath oval3;
1302 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1303 oval3.close();
1304 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1305 check_close(reporter, oval3);
1306 oval3.close();
1307 check_close(reporter, oval3);
1308
1309 SkPath moves;
1310 moves.moveTo(SK_Scalar1, SK_Scalar1);
1311 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1312 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1313 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1314 check_close(reporter, moves);
1315
1316 stroke_tiny_cubic();
1317}
1318
1319static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
Mike Reed30bc5272019-11-22 18:34:02 +00001320 SkPathConvexityType expected) {
Yuqian Li3154a532017-09-06 13:33:30 -04001321 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
Mike Reed30bc5272019-11-22 18:34:02 +00001322 SkPathConvexityType c = copy.getConvexityType();
Yuqian Li3154a532017-09-06 13:33:30 -04001323 REPORTER_ASSERT(reporter, c == expected);
Mike Reed8ee77d82019-11-22 12:59:01 -05001324
Cary Clarkc9b7c722018-12-12 14:50:23 -05001325 // test points-by-array interface
1326 SkPath::Iter iter(path, true);
1327 int initialMoves = 0;
1328 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -04001329 while (SkPath::kMove_Verb == iter.next(pts)) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001330 ++initialMoves;
1331 }
1332 if (initialMoves > 0) {
1333 std::vector<SkPoint> points;
1334 points.resize(path.getPoints(nullptr, 0));
1335 (void) path.getPoints(&points.front(), points.size());
1336 int skip = initialMoves - 1;
1337 bool isConvex = SkPathPriv::IsConvex(&points.front() + skip, points.size() - skip);
Mike Reed30bc5272019-11-22 18:34:02 +00001338 REPORTER_ASSERT(reporter, isConvex == (SkPathConvexityType::kConvex == expected));
Cary Clarkc9b7c722018-12-12 14:50:23 -05001339 }
Yuqian Li3154a532017-09-06 13:33:30 -04001340}
1341
1342static void test_path_crbug389050(skiatest::Reporter* reporter) {
1343 SkPath tinyConvexPolygon;
1344 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1345 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1346 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1347 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1348 tinyConvexPolygon.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001349 tinyConvexPolygon.getConvexityType();
Brian Osmancf4ec502019-04-19 12:58:21 -04001350 // This is convex, but so small that it fails many of our checks, and the three "backwards"
1351 // bends convince the checker that it's concave. That's okay though, we draw it correctly.
Mike Reed30bc5272019-11-22 18:34:02 +00001352 check_convexity(reporter, tinyConvexPolygon, SkPathConvexityType::kConcave);
Brian Osmancf4ec502019-04-19 12:58:21 -04001353 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
Yuqian Li3154a532017-09-06 13:33:30 -04001354
1355 SkPath platTriangle;
1356 platTriangle.moveTo(0, 0);
1357 platTriangle.lineTo(200, 0);
1358 platTriangle.lineTo(100, 0.04f);
1359 platTriangle.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001360 platTriangle.getConvexityType();
Yuqian Li3154a532017-09-06 13:33:30 -04001361 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1362
1363 platTriangle.reset();
1364 platTriangle.moveTo(0, 0);
1365 platTriangle.lineTo(200, 0);
1366 platTriangle.lineTo(100, 0.03f);
1367 platTriangle.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001368 platTriangle.getConvexityType();
Yuqian Li3154a532017-09-06 13:33:30 -04001369 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1370}
1371
1372static void test_convexity2(skiatest::Reporter* reporter) {
1373 SkPath pt;
1374 pt.moveTo(0, 0);
1375 pt.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001376 check_convexity(reporter, pt, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001377 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
1378
1379 SkPath line;
1380 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1381 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1382 line.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001383 check_convexity(reporter, line, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001384 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
1385
1386 SkPath triLeft;
1387 triLeft.moveTo(0, 0);
1388 triLeft.lineTo(SK_Scalar1, 0);
1389 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1390 triLeft.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001391 check_convexity(reporter, triLeft, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001392 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
1393
1394 SkPath triRight;
1395 triRight.moveTo(0, 0);
1396 triRight.lineTo(-SK_Scalar1, 0);
1397 triRight.lineTo(SK_Scalar1, SK_Scalar1);
1398 triRight.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001399 check_convexity(reporter, triRight, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001400 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
1401
1402 SkPath square;
1403 square.moveTo(0, 0);
1404 square.lineTo(SK_Scalar1, 0);
1405 square.lineTo(SK_Scalar1, SK_Scalar1);
1406 square.lineTo(0, SK_Scalar1);
1407 square.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001408 check_convexity(reporter, square, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001409 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
1410
1411 SkPath redundantSquare;
1412 redundantSquare.moveTo(0, 0);
1413 redundantSquare.lineTo(0, 0);
1414 redundantSquare.lineTo(0, 0);
1415 redundantSquare.lineTo(SK_Scalar1, 0);
1416 redundantSquare.lineTo(SK_Scalar1, 0);
1417 redundantSquare.lineTo(SK_Scalar1, 0);
1418 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1419 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1420 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1421 redundantSquare.lineTo(0, SK_Scalar1);
1422 redundantSquare.lineTo(0, SK_Scalar1);
1423 redundantSquare.lineTo(0, SK_Scalar1);
1424 redundantSquare.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001425 check_convexity(reporter, redundantSquare, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001426 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
1427
1428 SkPath bowTie;
1429 bowTie.moveTo(0, 0);
1430 bowTie.lineTo(0, 0);
1431 bowTie.lineTo(0, 0);
1432 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1433 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1434 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1435 bowTie.lineTo(SK_Scalar1, 0);
1436 bowTie.lineTo(SK_Scalar1, 0);
1437 bowTie.lineTo(SK_Scalar1, 0);
1438 bowTie.lineTo(0, SK_Scalar1);
1439 bowTie.lineTo(0, SK_Scalar1);
1440 bowTie.lineTo(0, SK_Scalar1);
1441 bowTie.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001442 check_convexity(reporter, bowTie, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001443 check_direction(reporter, bowTie, kDontCheckDir);
1444
1445 SkPath spiral;
1446 spiral.moveTo(0, 0);
1447 spiral.lineTo(100*SK_Scalar1, 0);
1448 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1449 spiral.lineTo(0, 100*SK_Scalar1);
1450 spiral.lineTo(0, 50*SK_Scalar1);
1451 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1452 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1453 spiral.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001454 check_convexity(reporter, spiral, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001455 check_direction(reporter, spiral, kDontCheckDir);
1456
1457 SkPath dent;
1458 dent.moveTo(0, 0);
1459 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1460 dent.lineTo(0, 100*SK_Scalar1);
1461 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1462 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1463 dent.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001464 check_convexity(reporter, dent, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001465 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
1466
1467 // https://bug.skia.org/2235
1468 SkPath strokedSin;
1469 for (int i = 0; i < 2000; i++) {
1470 SkScalar x = SkIntToScalar(i) / 2;
1471 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1472 if (0 == i) {
1473 strokedSin.moveTo(x, y);
1474 } else {
1475 strokedSin.lineTo(x, y);
1476 }
1477 }
1478 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1479 stroke.setStrokeStyle(2 * SK_Scalar1);
1480 stroke.applyToPath(&strokedSin, strokedSin);
Mike Reed30bc5272019-11-22 18:34:02 +00001481 check_convexity(reporter, strokedSin, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001482 check_direction(reporter, strokedSin, kDontCheckDir);
1483
1484 // http://crbug.com/412640
1485 SkPath degenerateConcave;
1486 degenerateConcave.moveTo(148.67912f, 191.875f);
1487 degenerateConcave.lineTo(470.37695f, 7.5f);
1488 degenerateConcave.lineTo(148.67912f, 191.875f);
1489 degenerateConcave.lineTo(41.446522f, 376.25f);
1490 degenerateConcave.lineTo(-55.971577f, 460.0f);
1491 degenerateConcave.lineTo(41.446522f, 376.25f);
Mike Reed30bc5272019-11-22 18:34:02 +00001492 check_convexity(reporter, degenerateConcave, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001493 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
1494
1495 // http://crbug.com/433683
1496 SkPath badFirstVector;
1497 badFirstVector.moveTo(501.087708f, 319.610352f);
1498 badFirstVector.lineTo(501.087708f, 319.610352f);
1499 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1500 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1501 badFirstVector.lineTo(301.557678f, 98.044601f);
1502 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1503 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1504 badFirstVector.lineTo(504.912292f, 316.389648f);
1505 badFirstVector.lineTo(501.087708f, 319.610352f);
1506 badFirstVector.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001507 check_convexity(reporter, badFirstVector, SkPathConvexityType::kConcave);
Brian Osman4be31e22019-08-20 15:35:26 -04001508
1509 // http://crbug.com/993330
1510 SkPath falseBackEdge;
1511 falseBackEdge.moveTo(-217.83430557928145f, -382.14948768484857f);
1512 falseBackEdge.lineTo(-227.73867866614847f, -399.52485512718323f);
1513 falseBackEdge.cubicTo(-158.3541047666846f, -439.0757140459542f,
1514 -79.8654464485281f, -459.875f,
1515 -1.1368683772161603e-13f, -459.875f);
1516 falseBackEdge.lineTo(-8.08037266162413e-14f, -439.875f);
1517 falseBackEdge.lineTo(-8.526512829121202e-14f, -439.87499999999994f);
1518 falseBackEdge.cubicTo(-76.39209188702645f, -439.87499999999994f,
1519 -151.46727226799754f, -419.98027663161537f,
1520 -217.83430557928145f, -382.14948768484857f);
1521 falseBackEdge.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001522 check_convexity(reporter, falseBackEdge, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001523}
1524
Cary Clarkc9b7c722018-12-12 14:50:23 -05001525static void test_convexity_doubleback(skiatest::Reporter* reporter) {
1526 SkPath doubleback;
1527 doubleback.lineTo(1, 1);
Mike Reed30bc5272019-11-22 18:34:02 +00001528 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001529 doubleback.lineTo(2, 2);
Mike Reed30bc5272019-11-22 18:34:02 +00001530 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001531 doubleback.reset();
1532 doubleback.lineTo(1, 0);
Mike Reed30bc5272019-11-22 18:34:02 +00001533 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001534 doubleback.lineTo(2, 0);
Mike Reed30bc5272019-11-22 18:34:02 +00001535 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001536 doubleback.lineTo(1, 0);
Mike Reed30bc5272019-11-22 18:34:02 +00001537 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001538 doubleback.reset();
1539 doubleback.quadTo(1, 1, 2, 2);
Mike Reed30bc5272019-11-22 18:34:02 +00001540 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001541 doubleback.reset();
1542 doubleback.quadTo(1, 0, 2, 0);
Mike Reed30bc5272019-11-22 18:34:02 +00001543 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001544 doubleback.quadTo(1, 0, 0, 0);
Mike Reed30bc5272019-11-22 18:34:02 +00001545 check_convexity(reporter, doubleback, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001546}
1547
Yuqian Li3154a532017-09-06 13:33:30 -04001548static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1549 const SkRect& bounds) {
1550 REPORTER_ASSERT(reporter, p.isConvex());
1551 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1552
1553 SkPath p2(p);
1554 REPORTER_ASSERT(reporter, p2.isConvex());
1555 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1556
1557 SkPath other;
1558 other.swap(p2);
1559 REPORTER_ASSERT(reporter, other.isConvex());
1560 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1561}
1562
1563static void setFromString(SkPath* path, const char str[]) {
1564 bool first = true;
1565 while (str) {
1566 SkScalar x, y;
1567 str = SkParse::FindScalar(str, &x);
1568 if (nullptr == str) {
1569 break;
1570 }
1571 str = SkParse::FindScalar(str, &y);
1572 SkASSERT(str);
1573 if (first) {
1574 path->moveTo(x, y);
1575 first = false;
1576 } else {
1577 path->lineTo(x, y);
1578 }
1579 }
1580}
1581
1582static void test_convexity(skiatest::Reporter* reporter) {
1583 SkPath path;
1584
Mike Reed30bc5272019-11-22 18:34:02 +00001585 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001586 path.addCircle(0, 0, SkIntToScalar(10));
Mike Reed30bc5272019-11-22 18:34:02 +00001587 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001588 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
Mike Reed30bc5272019-11-22 18:34:02 +00001589 check_convexity(reporter, path, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001590
1591 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001592 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCCW);
1593 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001594 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1595
1596 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001597 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCW);
1598 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001599 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1600
1601 path.reset();
Cary Clarkc9b7c722018-12-12 14:50:23 -05001602 path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
Mike Reed30bc5272019-11-22 18:34:02 +00001603 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001604
1605 static const struct {
1606 const char* fPathStr;
Mike Reed30bc5272019-11-22 18:34:02 +00001607 SkPathConvexityType fExpectedConvexity;
Yuqian Li3154a532017-09-06 13:33:30 -04001608 SkPathPriv::FirstDirection fExpectedDirection;
1609 } gRec[] = {
Mike Reed30bc5272019-11-22 18:34:02 +00001610 { "", SkPathConvexityType::kConvex, SkPathPriv::kUnknown_FirstDirection },
1611 { "0 0", SkPathConvexityType::kConvex, SkPathPriv::kUnknown_FirstDirection },
1612 { "0 0 10 10", SkPathConvexityType::kConvex, SkPathPriv::kUnknown_FirstDirection },
1613 { "0 0 10 10 20 20 0 0 10 10", SkPathConvexityType::kConcave, SkPathPriv::kUnknown_FirstDirection },
1614 { "0 0 10 10 10 20", SkPathConvexityType::kConvex, SkPathPriv::kCW_FirstDirection },
1615 { "0 0 10 10 10 0", SkPathConvexityType::kConvex, SkPathPriv::kCCW_FirstDirection },
1616 { "0 0 10 10 10 0 0 10", SkPathConvexityType::kConcave, kDontCheckDir },
1617 { "0 0 10 0 0 10 -10 -10", SkPathConvexityType::kConcave, SkPathPriv::kCW_FirstDirection },
Yuqian Li3154a532017-09-06 13:33:30 -04001618 };
1619
1620 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001621 path.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04001622 setFromString(&path, gRec[i].fPathStr);
1623 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1624 check_direction(reporter, path, gRec[i].fExpectedDirection);
1625 // check after setting the initial convex and direction
1626 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1627 SkPath copy(path);
1628 SkPathPriv::FirstDirection dir;
1629 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1630 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
1631 ^ foundDir);
1632 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1633 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1634 }
Mike Reed30bc5272019-11-22 18:34:02 +00001635 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexityType());
Yuqian Li3154a532017-09-06 13:33:30 -04001636 check_direction(reporter, path, gRec[i].fExpectedDirection);
1637 }
1638
1639 static const SkPoint nonFinitePts[] = {
1640 { SK_ScalarInfinity, 0 },
1641 { 0, SK_ScalarInfinity },
1642 { SK_ScalarInfinity, SK_ScalarInfinity },
1643 { SK_ScalarNegativeInfinity, 0},
1644 { 0, SK_ScalarNegativeInfinity },
1645 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1646 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1647 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1648 { SK_ScalarNaN, 0 },
1649 { 0, SK_ScalarNaN },
1650 { SK_ScalarNaN, SK_ScalarNaN },
1651 };
1652
1653 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1654
Cary Clarkc9b7c722018-12-12 14:50:23 -05001655 static const SkPoint axisAlignedPts[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04001656 { SK_ScalarMax, 0 },
1657 { 0, SK_ScalarMax },
Yuqian Li3154a532017-09-06 13:33:30 -04001658 { SK_ScalarMin, 0 },
1659 { 0, SK_ScalarMin },
Yuqian Li3154a532017-09-06 13:33:30 -04001660 };
1661
Cary Clarkc9b7c722018-12-12 14:50:23 -05001662 const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
Yuqian Li3154a532017-09-06 13:33:30 -04001663
Cary Clarkc9b7c722018-12-12 14:50:23 -05001664 for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
Yuqian Li3154a532017-09-06 13:33:30 -04001665 int i = (int) (index % nonFinitePtsCount);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001666 int f = (int) (index % axisAlignedPtsCount);
1667 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001668 path.reset();
1669 switch (index % 13) {
1670 case 0: path.lineTo(nonFinitePts[i]); break;
1671 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001672 case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
1673 case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
1674 case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
1675 case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
1676 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
1677 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
1678 case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
1679 case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001680 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001681 case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001682 case 12: path.moveTo(nonFinitePts[i]); break;
1683 }
Mike Reed30bc5272019-11-22 18:34:02 +00001684 check_convexity(reporter, path, SkPathConvexityType::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001685 }
1686
Cary Clarkc9b7c722018-12-12 14:50:23 -05001687 for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
1688 int f = (int) (index % axisAlignedPtsCount);
1689 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001690 path.reset();
1691 int curveSelect = index % 11;
1692 switch (curveSelect) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001693 case 0: path.moveTo(axisAlignedPts[f]); break;
1694 case 1: path.lineTo(axisAlignedPts[f]); break;
1695 case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
1696 case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
1697 case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
1698 case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
1699 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
1700 case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
1701 case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
1702 case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
1703 case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001704 }
Cary Clarkc9b7c722018-12-12 14:50:23 -05001705 if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
Mike Reed30bc5272019-11-22 18:34:02 +00001706 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001707 } else {
1708 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
Mike Reed30bc5272019-11-22 18:34:02 +00001709 SkPathConvexityType c = copy.getConvexityType();
1710 REPORTER_ASSERT(reporter, SkPathConvexityType::kUnknown == c
1711 || SkPathConvexityType::kConcave == c);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001712 }
Yuqian Li3154a532017-09-06 13:33:30 -04001713 }
1714
Cary Clarkc9b7c722018-12-12 14:50:23 -05001715 static const SkPoint diagonalPts[] = {
1716 { SK_ScalarMax, SK_ScalarMax },
1717 { SK_ScalarMin, SK_ScalarMin },
1718 };
1719
1720 const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
1721
1722 for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
1723 int f = (int) (index % diagonalPtsCount);
1724 int g = (int) ((f + 1) % diagonalPtsCount);
1725 path.reset();
1726 int curveSelect = index % 11;
1727 switch (curveSelect) {
1728 case 0: path.moveTo(diagonalPts[f]); break;
1729 case 1: path.lineTo(diagonalPts[f]); break;
1730 case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
1731 case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
1732 case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
1733 case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
1734 case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
1735 case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
1736 case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
1737 case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
1738 case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
1739 }
1740 if (curveSelect == 0) {
Mike Reed30bc5272019-11-22 18:34:02 +00001741 check_convexity(reporter, path, SkPathConvexityType::kConvex);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001742 } else {
1743 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
Mike Reed30bc5272019-11-22 18:34:02 +00001744 SkPathConvexityType c = copy.getConvexityType();
1745 REPORTER_ASSERT(reporter, SkPathConvexityType::kUnknown == c
1746 || SkPathConvexityType::kConcave == c);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001747 }
1748 }
1749
1750
Yuqian Li3154a532017-09-06 13:33:30 -04001751 path.reset();
1752 path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1753 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
1754 path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
1755 path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
1756 path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
1757 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1758 path.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001759 check_convexity(reporter, path, SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04001760
1761}
1762
1763static void test_isLine(skiatest::Reporter* reporter) {
1764 SkPath path;
1765 SkPoint pts[2];
1766 const SkScalar value = SkIntToScalar(5);
1767
1768 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1769
1770 // set some non-zero values
1771 pts[0].set(value, value);
1772 pts[1].set(value, value);
1773 REPORTER_ASSERT(reporter, !path.isLine(pts));
1774 // check that pts was untouched
1775 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1776 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1777
1778 const SkScalar moveX = SkIntToScalar(1);
1779 const SkScalar moveY = SkIntToScalar(2);
1780 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1781
1782 path.moveTo(moveX, moveY);
1783 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1784 REPORTER_ASSERT(reporter, !path.isLine(pts));
1785 // check that pts was untouched
1786 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1787 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1788
1789 const SkScalar lineX = SkIntToScalar(2);
1790 const SkScalar lineY = SkIntToScalar(2);
1791 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1792
1793 path.lineTo(lineX, lineY);
1794 REPORTER_ASSERT(reporter, path.isLine(nullptr));
1795
1796 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1797 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1798 REPORTER_ASSERT(reporter, path.isLine(pts));
1799 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1800 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1801
1802 path.lineTo(0, 0); // too many points/verbs
1803 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1804 REPORTER_ASSERT(reporter, !path.isLine(pts));
1805 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1806 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1807
1808 path.reset();
1809 path.quadTo(1, 1, 2, 2);
1810 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1811}
1812
1813static void test_conservativelyContains(skiatest::Reporter* reporter) {
1814 SkPath path;
1815
1816 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1817 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1818
1819 // A circle that bounds kBaseRect (with a significant amount of slop)
1820 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1821 circleR *= 1.75f / 2;
1822 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1823
1824 // round-rect radii
1825 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1826
1827 static const struct SUPPRESS_VISIBILITY_WARNING {
1828 SkRect fQueryRect;
1829 bool fInRect;
1830 bool fInCircle;
1831 bool fInRR;
1832 bool fInCubicRR;
1833 } kQueries[] = {
1834 {kBaseRect, true, true, false, false},
1835
1836 // rect well inside of kBaseRect
1837 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1838 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1839 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1840 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1841 true, true, true, true},
1842
1843 // rects with edges off by one from kBaseRect's edges
1844 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1845 kBaseRect.width(), kBaseRect.height() + 1),
1846 false, true, false, false},
1847 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1848 kBaseRect.width() + 1, kBaseRect.height()),
1849 false, true, false, false},
1850 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1851 kBaseRect.width() + 1, kBaseRect.height() + 1),
1852 false, true, false, false},
1853 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1854 kBaseRect.width(), kBaseRect.height()),
1855 false, true, false, false},
1856 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1857 kBaseRect.width(), kBaseRect.height()),
1858 false, true, false, false},
1859 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1860 kBaseRect.width() + 2, kBaseRect.height()),
1861 false, true, false, false},
1862 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1863 kBaseRect.width() + 2, kBaseRect.height()),
1864 false, true, false, false},
1865
1866 // zero-w/h rects at each corner of kBaseRect
1867 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1868 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1869 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1870 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1871
1872 // far away rect
1873 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1874 SkIntToScalar(10), SkIntToScalar(10)),
1875 false, false, false, false},
1876
1877 // very large rect containing kBaseRect
1878 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1879 kBaseRect.fTop - 5 * kBaseRect.height(),
1880 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1881 false, false, false, false},
1882
1883 // skinny rect that spans same y-range as kBaseRect
1884 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1885 SkIntToScalar(1), kBaseRect.height()),
1886 true, true, true, true},
1887
1888 // short rect that spans same x-range as kBaseRect
1889 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1890 true, true, true, true},
1891
1892 // skinny rect that spans slightly larger y-range than kBaseRect
1893 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1894 SkIntToScalar(1), kBaseRect.height() + 1),
1895 false, true, false, false},
1896
1897 // short rect that spans slightly larger x-range than kBaseRect
1898 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1899 kBaseRect.width() + 1, SkScalar(1)),
1900 false, true, false, false},
1901 };
1902
1903 for (int inv = 0; inv < 4; ++inv) {
1904 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1905 SkRect qRect = kQueries[q].fQueryRect;
1906 if (inv & 0x1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001907 using std::swap;
1908 swap(qRect.fLeft, qRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04001909 }
1910 if (inv & 0x2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001911 using std::swap;
1912 swap(qRect.fTop, qRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04001913 }
1914 for (int d = 0; d < 2; ++d) {
Mike Reed30bc5272019-11-22 18:34:02 +00001915 SkPathDirection dir = d ? SkPathDirection::kCCW : SkPathDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04001916 path.reset();
1917 path.addRect(kBaseRect, dir);
1918 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1919 path.conservativelyContainsRect(qRect));
1920
1921 path.reset();
1922 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1923 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1924 path.conservativelyContainsRect(qRect));
1925
1926 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00001927 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
Yuqian Li3154a532017-09-06 13:33:30 -04001928 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1929 path.conservativelyContainsRect(qRect));
1930
1931 path.reset();
1932 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1933 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1934 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1935 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1936 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1937 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1938 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1939 path.close();
1940 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1941 path.conservativelyContainsRect(qRect));
1942
1943 }
1944 // Slightly non-convex shape, shouldn't contain any rects.
1945 path.reset();
1946 path.moveTo(0, 0);
1947 path.lineTo(SkIntToScalar(50), 0.05f);
1948 path.lineTo(SkIntToScalar(100), 0);
1949 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1950 path.lineTo(0, SkIntToScalar(100));
1951 path.close();
1952 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1953 }
1954 }
1955
1956 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1957 path.reset();
1958 path.moveTo(0, 0);
1959 path.lineTo(SkIntToScalar(100), 0);
1960 path.lineTo(0, SkIntToScalar(100));
1961
1962 // inside, on along top edge
1963 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1964 SkIntToScalar(10),
1965 SkIntToScalar(10))));
1966 // above
1967 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1968 SkRect::MakeXYWH(SkIntToScalar(50),
1969 SkIntToScalar(-10),
1970 SkIntToScalar(10),
1971 SkIntToScalar(10))));
1972 // to the left
1973 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1974 SkIntToScalar(5),
1975 SkIntToScalar(5),
1976 SkIntToScalar(5))));
1977
1978 // outside the diagonal edge
1979 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1980 SkIntToScalar(200),
1981 SkIntToScalar(20),
1982 SkIntToScalar(5))));
1983
1984
1985 // Test that multiple move commands do not cause asserts.
Yuqian Li3154a532017-09-06 13:33:30 -04001986 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
Yuqian Li3154a532017-09-06 13:33:30 -04001987 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1988 SkIntToScalar(10),
1989 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001990
1991 // Same as above path and first test but with an extra moveTo.
1992 path.reset();
1993 path.moveTo(100, 100);
1994 path.moveTo(0, 0);
1995 path.lineTo(SkIntToScalar(100), 0);
1996 path.lineTo(0, SkIntToScalar(100));
Brian Osman205a1262017-09-18 13:13:48 +00001997 // Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
1998 // path non-convex.
1999 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2000 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2001 SkIntToScalar(10),
2002 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04002003
2004 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
2005 // following the non-empty sub-path. Verifies that this does not trigger assertions.
2006 path.reset();
2007 path.moveTo(0, 0);
2008 path.lineTo(SkIntToScalar(100), 0);
2009 path.lineTo(0, SkIntToScalar(100));
2010 path.moveTo(100, 100);
2011
2012 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
2013 SkIntToScalar(10),
2014 SkIntToScalar(10))));
2015
2016 // Test that multiple move commands do not cause asserts and that the function
2017 // is not confused by the multiple moves.
2018 path.reset();
2019 path.moveTo(0, 0);
2020 path.lineTo(SkIntToScalar(100), 0);
2021 path.lineTo(0, SkIntToScalar(100));
2022 path.moveTo(0, SkIntToScalar(200));
2023 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
2024 path.lineTo(0, SkIntToScalar(300));
2025
2026 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2027 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2028 SkIntToScalar(10),
2029 SkIntToScalar(10))));
2030
2031 path.reset();
2032 path.lineTo(100, 100);
2033 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
2034
2035 // An empty path should not contain any rectangle. It's questionable whether an empty path
2036 // contains an empty rectangle. However, since it is a conservative test it is ok to
2037 // return false.
2038 path.reset();
2039 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2040 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2041}
2042
2043static void test_isRect_open_close(skiatest::Reporter* reporter) {
2044 SkPath path;
2045 bool isClosed;
2046
2047 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
2048 path.close();
2049
2050 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
2051 REPORTER_ASSERT(reporter, isClosed);
2052}
2053
2054// Simple isRect test is inline TestPath, below.
2055// test_isRect provides more extensive testing.
2056static void test_isRect(skiatest::Reporter* reporter) {
2057 test_isRect_open_close(reporter);
2058
2059 // passing tests (all moveTo / lineTo...
2060 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2061 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2062 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2063 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2064 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
2065 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2066 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2067 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2068 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2069 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
2070 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
2071 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
2072 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
2073 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
2074 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
2075
2076 // failing tests
2077 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2078 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2079 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2080 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2081 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2082 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2083 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2084 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2085 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2086 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2087 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
2088
2089 // no close, but we should detect them as fillably the same as a rect
2090 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2091 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2092 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2093
2094 // like c2, but we double-back on ourselves
2095 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2096 // like c2, but we overshoot the start point
2097 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2098 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2099
2100 struct IsRectTest {
2101 SkPoint *fPoints;
2102 int fPointCount;
2103 bool fClose;
2104 bool fIsRect;
2105 } tests[] = {
2106 { r1, SK_ARRAY_COUNT(r1), true, true },
2107 { r2, SK_ARRAY_COUNT(r2), true, true },
2108 { r3, SK_ARRAY_COUNT(r3), true, true },
2109 { r4, SK_ARRAY_COUNT(r4), true, true },
2110 { r5, SK_ARRAY_COUNT(r5), true, true },
2111 { r6, SK_ARRAY_COUNT(r6), true, true },
2112 { r7, SK_ARRAY_COUNT(r7), true, true },
2113 { r8, SK_ARRAY_COUNT(r8), true, true },
2114 { r9, SK_ARRAY_COUNT(r9), true, true },
2115 { ra, SK_ARRAY_COUNT(ra), true, true },
2116 { rb, SK_ARRAY_COUNT(rb), true, true },
2117 { rc, SK_ARRAY_COUNT(rc), true, true },
2118 { rd, SK_ARRAY_COUNT(rd), true, true },
2119 { re, SK_ARRAY_COUNT(re), true, true },
2120 { rf, SK_ARRAY_COUNT(rf), true, true },
2121
2122 { f1, SK_ARRAY_COUNT(f1), true, false },
2123 { f2, SK_ARRAY_COUNT(f2), true, false },
2124 { f3, SK_ARRAY_COUNT(f3), true, false },
2125 { f4, SK_ARRAY_COUNT(f4), true, false },
2126 { f5, SK_ARRAY_COUNT(f5), true, false },
2127 { f6, SK_ARRAY_COUNT(f6), true, false },
2128 { f7, SK_ARRAY_COUNT(f7), true, false },
2129 { f8, SK_ARRAY_COUNT(f8), true, false },
2130 { f9, SK_ARRAY_COUNT(f9), true, false },
2131 { fa, SK_ARRAY_COUNT(fa), true, false },
2132 { fb, SK_ARRAY_COUNT(fb), true, false },
2133
2134 { c1, SK_ARRAY_COUNT(c1), false, true },
2135 { c2, SK_ARRAY_COUNT(c2), false, true },
2136 { c3, SK_ARRAY_COUNT(c3), false, true },
2137
2138 { d1, SK_ARRAY_COUNT(d1), false, false },
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002139 { d2, SK_ARRAY_COUNT(d2), false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002140 { d3, SK_ARRAY_COUNT(d3), false, false },
2141 };
2142
2143 const size_t testCount = SK_ARRAY_COUNT(tests);
2144 int index;
2145 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2146 SkPath path;
2147 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2148 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2149 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2150 }
2151 if (tests[testIndex].fClose) {
2152 path.close();
2153 }
2154 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2155
2156 if (tests[testIndex].fIsRect) {
2157 SkRect computed, expected;
2158 bool isClosed;
Mike Reed30bc5272019-11-22 18:34:02 +00002159 SkPathDirection direction;
Yuqian Li3154a532017-09-06 13:33:30 -04002160 SkPathPriv::FirstDirection cheapDirection;
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002161 int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
Mike Reed92b33352019-08-24 19:39:13 -04002162 expected.setBounds(tests[testIndex].fPoints, pointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002163 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
2164 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2165 REPORTER_ASSERT(reporter, expected == computed);
2166 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2167 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2168 } else {
2169 SkRect computed;
Mike Reed92b33352019-08-24 19:39:13 -04002170 computed.setLTRB(123, 456, 789, 1011);
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002171 for (auto c : {true, false})
Mike Reed30bc5272019-11-22 18:34:02 +00002172 for (auto d : {SkPathDirection::kCW, SkPathDirection::kCCW}) {
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002173 bool isClosed = c;
Mike Reed30bc5272019-11-22 18:34:02 +00002174 SkPathDirection direction = d;
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002175 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2176 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2177 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2178 REPORTER_ASSERT(reporter, isClosed == c);
2179 REPORTER_ASSERT(reporter, direction == d);
2180 }
Yuqian Li3154a532017-09-06 13:33:30 -04002181 }
2182 }
2183
2184 // fail, close then line
2185 SkPath path1;
2186 path1.moveTo(r1[0].fX, r1[0].fY);
2187 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2188 path1.lineTo(r1[index].fX, r1[index].fY);
2189 }
2190 path1.close();
2191 path1.lineTo(1, 0);
2192 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2193
2194 // fail, move in the middle
2195 path1.reset();
2196 path1.moveTo(r1[0].fX, r1[0].fY);
2197 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2198 if (index == 2) {
2199 path1.moveTo(1, .5f);
2200 }
2201 path1.lineTo(r1[index].fX, r1[index].fY);
2202 }
2203 path1.close();
2204 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2205
2206 // fail, move on the edge
2207 path1.reset();
2208 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2209 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2210 path1.lineTo(r1[index].fX, r1[index].fY);
2211 }
2212 path1.close();
2213 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2214
2215 // fail, quad
2216 path1.reset();
2217 path1.moveTo(r1[0].fX, r1[0].fY);
2218 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2219 if (index == 2) {
2220 path1.quadTo(1, .5f, 1, .5f);
2221 }
2222 path1.lineTo(r1[index].fX, r1[index].fY);
2223 }
2224 path1.close();
2225 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2226
2227 // fail, cubic
2228 path1.reset();
2229 path1.moveTo(r1[0].fX, r1[0].fY);
2230 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2231 if (index == 2) {
2232 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2233 }
2234 path1.lineTo(r1[index].fX, r1[index].fY);
2235 }
2236 path1.close();
2237 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2238}
2239
2240static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
Mike Reed30bc5272019-11-22 18:34:02 +00002241 const SkRect& rect, SkPathDirection dir, unsigned start) {
Yuqian Li3154a532017-09-06 13:33:30 -04002242 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00002243 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002244 unsigned s = ~0U;
2245
2246 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2247 REPORTER_ASSERT(reporter, r == rect);
2248 REPORTER_ASSERT(reporter, d == dir);
2249 REPORTER_ASSERT(reporter, s == start);
2250}
2251
2252static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002253 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002254 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00002255 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002256 unsigned s = ~0U;
2257
2258 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2259 const SkRect emptyRect = SkRect::MakeEmpty();
2260 SkPath path;
2261 for (int start = 0; start < 4; ++start) {
Mike Reed30bc5272019-11-22 18:34:02 +00002262 for (auto dir : {SkPathDirection::kCCW, SkPathDirection::kCW}) {
Yuqian Li3154a532017-09-06 13:33:30 -04002263 SkPath path;
2264 path.addRect(testRect, dir, start);
2265 check_simple_closed_rect(reporter, path, testRect, dir, start);
2266 path.close();
2267 check_simple_closed_rect(reporter, path, testRect, dir, start);
2268 SkPath path2 = path;
2269 path2.lineTo(10, 10);
2270 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2271 path2 = path;
2272 path2.moveTo(10, 10);
2273 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2274 path2 = path;
2275 path2.addRect(testRect, dir, start);
2276 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2277 // Make the path by hand, manually closing it.
2278 path2.reset();
2279 SkPath::RawIter iter(path);
2280 SkPath::Verb v;
2281 SkPoint verbPts[4];
2282 SkPoint firstPt = {0.f, 0.f};
2283 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2284 switch(v) {
2285 case SkPath::kMove_Verb:
2286 firstPt = verbPts[0];
2287 path2.moveTo(verbPts[0]);
2288 break;
2289 case SkPath::kLine_Verb:
2290 path2.lineTo(verbPts[1]);
2291 break;
2292 default:
2293 break;
2294 }
2295 }
2296 // We haven't closed it yet...
2297 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2298 // ... now we do and test again.
2299 path2.lineTo(firstPt);
2300 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2301 // A redundant close shouldn't cause a failure.
2302 path2.close();
2303 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2304 // Degenerate point and line rects are not allowed
2305 path2.reset();
2306 path2.addRect(emptyRect, dir, start);
2307 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2308 SkRect degenRect = testRect;
2309 degenRect.fLeft = degenRect.fRight;
2310 path2.reset();
2311 path2.addRect(degenRect, dir, start);
2312 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2313 degenRect = testRect;
2314 degenRect.fTop = degenRect.fBottom;
2315 path2.reset();
2316 path2.addRect(degenRect, dir, start);
2317 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2318 // An inverted rect makes a rect path, but changes the winding dir and start point.
Mike Reed30bc5272019-11-22 18:34:02 +00002319 SkPathDirection swapDir = (dir == SkPathDirection::kCW)
2320 ? SkPathDirection::kCCW
2321 : SkPathDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002322 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2323 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2324 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002325 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002326 path2.reset();
2327 path2.addRect(swapRect, dir, start);
2328 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2329 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002330 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002331 path2.reset();
2332 path2.addRect(swapRect, dir, start);
2333 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2334 }
2335 }
2336 // down, up, left, close
2337 path.reset();
2338 path.moveTo(1, 1);
2339 path.lineTo(1, 2);
2340 path.lineTo(1, 1);
2341 path.lineTo(0, 1);
2342 SkRect rect;
Mike Reed30bc5272019-11-22 18:34:02 +00002343 SkPathDirection dir;
Yuqian Li3154a532017-09-06 13:33:30 -04002344 unsigned start;
2345 path.close();
2346 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2347 // right, left, up, close
2348 path.reset();
2349 path.moveTo(1, 1);
2350 path.lineTo(2, 1);
2351 path.lineTo(1, 1);
2352 path.lineTo(1, 0);
2353 path.close();
2354 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2355 // parallelogram with horizontal edges
2356 path.reset();
2357 path.moveTo(1, 0);
2358 path.lineTo(3, 0);
2359 path.lineTo(2, 1);
2360 path.lineTo(0, 1);
2361 path.close();
2362 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2363 // parallelogram with vertical edges
2364 path.reset();
2365 path.moveTo(0, 1);
2366 path.lineTo(0, 3);
2367 path.lineTo(1, 2);
2368 path.lineTo(1, 0);
2369 path.close();
2370 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2371
2372}
2373
2374static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2375 // passing tests (all moveTo / lineTo...
2376 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2377 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2378 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2379 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2380 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2381 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2382 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2383 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2384 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2385 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2386 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2387 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2388 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2389 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2390
2391 // failing tests
2392 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2393 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2394 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2395 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2396 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2397 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2398 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2399 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2400
2401 // success, no close is OK
2402 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2403 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2404
2405 struct IsNestedRectTest {
2406 SkPoint *fPoints;
2407 int fPointCount;
2408 SkPathPriv::FirstDirection fDirection;
2409 bool fClose;
2410 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2411 } tests[] = {
2412 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2413 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2414 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2415 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2416 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2417 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2418 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2419 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2420 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2421 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2422 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2423 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2424 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2425 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
2426
2427 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2428 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2429 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2430 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2431 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2432 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2433 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2434 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2435
2436 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2437 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2438 };
2439
2440 const size_t testCount = SK_ARRAY_COUNT(tests);
2441 int index;
2442 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2443 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2444 SkPath path;
2445 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002446 path.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002447 }
2448 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2449 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2450 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2451 }
2452 if (tests[testIndex].fClose) {
2453 path.close();
2454 }
2455 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002456 path.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002457 }
2458 REPORTER_ASSERT(reporter,
Mike Reed430470d2019-09-12 17:09:12 -04002459 tests[testIndex].fIsNestedRect == SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002460 if (tests[testIndex].fIsNestedRect) {
2461 SkRect expected[2], computed[2];
2462 SkPathPriv::FirstDirection expectedDirs[2];
Mike Reed30bc5272019-11-22 18:34:02 +00002463 SkPathDirection computedDirs[2];
Yuqian Li3154a532017-09-06 13:33:30 -04002464 SkRect testBounds;
Mike Reed92b33352019-08-24 19:39:13 -04002465 testBounds.setBounds(tests[testIndex].fPoints, tests[testIndex].fPointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002466 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2467 expected[1] = testBounds;
2468 if (rectFirst) {
2469 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2470 } else {
2471 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2472 }
2473 expectedDirs[1] = tests[testIndex].fDirection;
Mike Reed430470d2019-09-12 17:09:12 -04002474 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, computed, computedDirs));
Yuqian Li3154a532017-09-06 13:33:30 -04002475 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2476 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2477 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2478 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2479 }
2480 }
2481
2482 // fail, close then line
2483 SkPath path1;
2484 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002485 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002486 }
2487 path1.moveTo(r1[0].fX, r1[0].fY);
2488 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2489 path1.lineTo(r1[index].fX, r1[index].fY);
2490 }
2491 path1.close();
2492 path1.lineTo(1, 0);
2493 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002494 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002495 }
Mike Reed430470d2019-09-12 17:09:12 -04002496 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002497
2498 // fail, move in the middle
2499 path1.reset();
2500 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002501 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002502 }
2503 path1.moveTo(r1[0].fX, r1[0].fY);
2504 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2505 if (index == 2) {
2506 path1.moveTo(1, .5f);
2507 }
2508 path1.lineTo(r1[index].fX, r1[index].fY);
2509 }
2510 path1.close();
2511 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002512 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002513 }
Mike Reed430470d2019-09-12 17:09:12 -04002514 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002515
2516 // fail, move on the edge
2517 path1.reset();
2518 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002519 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002520 }
2521 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2522 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2523 path1.lineTo(r1[index].fX, r1[index].fY);
2524 }
2525 path1.close();
2526 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002527 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002528 }
Mike Reed430470d2019-09-12 17:09:12 -04002529 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002530
2531 // fail, quad
2532 path1.reset();
2533 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002534 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002535 }
2536 path1.moveTo(r1[0].fX, r1[0].fY);
2537 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2538 if (index == 2) {
2539 path1.quadTo(1, .5f, 1, .5f);
2540 }
2541 path1.lineTo(r1[index].fX, r1[index].fY);
2542 }
2543 path1.close();
2544 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002545 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002546 }
Mike Reed430470d2019-09-12 17:09:12 -04002547 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002548
2549 // fail, cubic
2550 path1.reset();
2551 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002552 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002553 }
2554 path1.moveTo(r1[0].fX, r1[0].fY);
2555 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2556 if (index == 2) {
2557 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2558 }
2559 path1.lineTo(r1[index].fX, r1[index].fY);
2560 }
2561 path1.close();
2562 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002563 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002564 }
Mike Reed430470d2019-09-12 17:09:12 -04002565 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002566
2567 // fail, not nested
2568 path1.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00002569 path1.addRect(1, 1, 3, 3, SkPathDirection::kCW);
2570 path1.addRect(2, 2, 4, 4, SkPathDirection::kCW);
Mike Reed430470d2019-09-12 17:09:12 -04002571 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002572 }
2573
2574 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2575 SkPath path;
2576 path.moveTo(0, 0);
2577 path.lineTo(10, 0);
2578 path.lineTo(10, 10);
2579 path.lineTo(0, 10);
2580 path.lineTo(0, 0);
2581 path.moveTo(1, 1);
2582 path.lineTo(9, 1);
2583 path.lineTo(9, 9);
2584 path.lineTo(1, 9);
2585 path.lineTo(1, 1);
Mike Reed430470d2019-09-12 17:09:12 -04002586 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002587
2588 // pass, stroke rect
2589 SkPath src, dst;
Mike Reed30bc5272019-11-22 18:34:02 +00002590 src.addRect(1, 1, 7, 7, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002591 SkPaint strokePaint;
2592 strokePaint.setStyle(SkPaint::kStroke_Style);
2593 strokePaint.setStrokeWidth(2);
2594 strokePaint.getFillPath(src, &dst);
Mike Reed430470d2019-09-12 17:09:12 -04002595 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(dst, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002596}
2597
2598static void write_and_read_back(skiatest::Reporter* reporter,
2599 const SkPath& p) {
2600 SkWriter32 writer;
2601 writer.writePath(p);
2602 size_t size = writer.bytesWritten();
2603 SkAutoMalloc storage(size);
2604 writer.flatten(storage.get());
2605 SkReader32 reader(storage.get(), size);
2606
2607 SkPath readBack;
2608 REPORTER_ASSERT(reporter, readBack != p);
2609 reader.readPath(&readBack);
2610 REPORTER_ASSERT(reporter, readBack == p);
2611
Mike Reed30bc5272019-11-22 18:34:02 +00002612 REPORTER_ASSERT(reporter, readBack.getConvexityTypeOrUnknown() ==
2613 p.getConvexityTypeOrUnknown());
Yuqian Li3154a532017-09-06 13:33:30 -04002614
2615 SkRect oval0, oval1;
Mike Reed30bc5272019-11-22 18:34:02 +00002616 SkPathDirection dir0, dir1;
Yuqian Li3154a532017-09-06 13:33:30 -04002617 unsigned start0, start1;
2618 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002619 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2620 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002621 REPORTER_ASSERT(reporter, oval0 == oval1);
2622 REPORTER_ASSERT(reporter, dir0 == dir1);
2623 REPORTER_ASSERT(reporter, start0 == start1);
2624 }
2625 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2626 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002627 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2628 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002629 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2630 REPORTER_ASSERT(reporter, dir0 == dir1);
2631 REPORTER_ASSERT(reporter, start0 == start1);
2632 }
2633 const SkRect& origBounds = p.getBounds();
2634 const SkRect& readBackBounds = readBack.getBounds();
2635
2636 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2637}
2638
Yuqian Li3154a532017-09-06 13:33:30 -04002639static void test_flattening(skiatest::Reporter* reporter) {
2640 SkPath p;
2641
2642 static const SkPoint pts[] = {
2643 { 0, 0 },
2644 { SkIntToScalar(10), SkIntToScalar(10) },
2645 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2646 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2647 };
2648 p.moveTo(pts[0]);
2649 p.lineTo(pts[1]);
2650 p.quadTo(pts[2], pts[3]);
2651 p.cubicTo(pts[4], pts[5], pts[6]);
2652
2653 write_and_read_back(reporter, p);
2654
2655 // create a buffer that should be much larger than the path so we don't
2656 // kill our stack if writer goes too far.
2657 char buffer[1024];
2658 size_t size1 = p.writeToMemory(nullptr);
2659 size_t size2 = p.writeToMemory(buffer);
2660 REPORTER_ASSERT(reporter, size1 == size2);
2661
2662 SkPath p2;
2663 size_t size3 = p2.readFromMemory(buffer, 1024);
2664 REPORTER_ASSERT(reporter, size1 == size3);
2665 REPORTER_ASSERT(reporter, p == p2);
2666
2667 size3 = p2.readFromMemory(buffer, 0);
2668 REPORTER_ASSERT(reporter, !size3);
2669
2670 SkPath tooShort;
2671 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2672 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2673
2674 char buffer2[1024];
2675 size3 = p2.writeToMemory(buffer2);
2676 REPORTER_ASSERT(reporter, size1 == size3);
2677 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2678
2679 // test persistence of the oval flag & convexity
2680 {
2681 SkPath oval;
2682 SkRect rect = SkRect::MakeWH(10, 10);
2683 oval.addOval(rect);
2684
2685 write_and_read_back(reporter, oval);
2686 }
Yuqian Li3154a532017-09-06 13:33:30 -04002687}
2688
2689static void test_transform(skiatest::Reporter* reporter) {
2690 SkPath p;
2691
2692#define CONIC_PERSPECTIVE_BUG_FIXED 0
2693 static const SkPoint pts[] = {
2694 { 0, 0 }, // move
2695 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2696 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2697 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2698#if CONIC_PERSPECTIVE_BUG_FIXED
2699 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2700#endif
2701 };
2702 const int kPtCount = SK_ARRAY_COUNT(pts);
2703
2704 p.moveTo(pts[0]);
2705 p.lineTo(pts[1]);
2706 p.quadTo(pts[2], pts[3]);
2707 p.cubicTo(pts[4], pts[5], pts[6]);
2708#if CONIC_PERSPECTIVE_BUG_FIXED
2709 p.conicTo(pts[4], pts[5], 0.5f);
2710#endif
2711 p.close();
2712
2713 {
2714 SkMatrix matrix;
2715 matrix.reset();
2716 SkPath p1;
2717 p.transform(matrix, &p1);
2718 REPORTER_ASSERT(reporter, p == p1);
2719 }
2720
2721
2722 {
2723 SkMatrix matrix;
2724 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2725
2726 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2727
2728 p.transform(matrix, &p1);
2729 SkPoint pts1[kPtCount];
2730 int count = p1.getPoints(pts1, kPtCount);
2731 REPORTER_ASSERT(reporter, kPtCount == count);
2732 for (int i = 0; i < count; ++i) {
2733 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2734 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2735 }
2736 }
2737
2738 {
2739 SkMatrix matrix;
2740 matrix.reset();
2741 matrix.setPerspX(4);
2742
2743 SkPath p1;
2744 p1.moveTo(SkPoint::Make(0, 0));
2745
Mike Reedbb59dfa2019-12-12 14:48:28 -05002746 p.transform(matrix, &p1, SkApplyPerspectiveClip::kNo);
Yuqian Li3154a532017-09-06 13:33:30 -04002747 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
Mike Reedbb59dfa2019-12-12 14:48:28 -05002748 p1.transform(matrix, nullptr, SkApplyPerspectiveClip::kNo);
Yuqian Li3154a532017-09-06 13:33:30 -04002749 SkRect pBounds = p.getBounds();
2750 SkRect p1Bounds = p1.getBounds();
2751 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2752 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2753 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2754 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2755 }
2756
2757 p.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00002758 p.addCircle(0, 0, 1, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002759
2760 {
2761 SkMatrix matrix;
2762 matrix.reset();
2763 SkPath p1;
2764 p1.moveTo(SkPoint::Make(0, 0));
2765
2766 p.transform(matrix, &p1);
2767 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2768 }
2769
2770
2771 {
2772 SkMatrix matrix;
2773 matrix.reset();
2774 matrix.setScaleX(-1);
2775 SkPath p1;
2776 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2777
2778 p.transform(matrix, &p1);
2779 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2780 }
2781
2782 {
2783 SkMatrix matrix;
2784 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2785 SkPath p1;
2786 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2787
2788 p.transform(matrix, &p1);
2789 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2790 }
Brian Osmandbfcd922019-03-07 16:50:30 -05002791
2792 {
2793 SkPath p1;
2794 p1.addRect({ 10, 20, 30, 40 });
2795 SkPath p2;
2796 p2.addRect({ 10, 20, 30, 40 });
2797 uint32_t id1 = p1.getGenerationID();
2798 uint32_t id2 = p2.getGenerationID();
Mike Reed8fda88e2019-09-05 14:14:38 -04002799 REPORTER_ASSERT(reporter, id1 != id2);
Brian Osmandbfcd922019-03-07 16:50:30 -05002800 SkMatrix matrix;
2801 matrix.setScale(2, 2);
2802 p1.transform(matrix, &p2);
Mike Reed8fda88e2019-09-05 14:14:38 -04002803 REPORTER_ASSERT(reporter, id1 == p1.getGenerationID());
2804 REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002805 p1.transform(matrix);
2806 REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002807 }
Yuqian Li3154a532017-09-06 13:33:30 -04002808}
2809
2810static void test_zero_length_paths(skiatest::Reporter* reporter) {
2811 SkPath p;
2812 uint8_t verbs[32];
2813
2814 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2815 const char* testPath;
2816 const size_t numResultPts;
2817 const SkRect resultBound;
2818 const SkPath::Verb* resultVerbs;
2819 const size_t numResultVerbs;
2820 };
2821
2822 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2823 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2824 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2825 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2826 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2827 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2828 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2829 static const SkPath::Verb resultVerbs8[] = {
2830 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2831 };
2832 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2833 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2834 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2835 static const SkPath::Verb resultVerbs12[] = {
2836 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2837 };
2838 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2839 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2840 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2841 static const SkPath::Verb resultVerbs16[] = {
2842 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2843 };
2844 static const struct zeroPathTestData gZeroLengthTests[] = {
2845 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2846 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2847 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2848 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2849 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2850 { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) },
2851 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2852 { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) },
2853 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2854 { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) },
2855 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2856 { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) },
2857 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2858 { "M 1 1 C 1 1 1 1 1 1 M 2 1 C 2 1 2 1 2 1", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs14,
2859 SK_ARRAY_COUNT(resultVerbs14)
2860 },
2861 { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) },
2862 { "M 1 1 C 1 1 1 1 1 1 z M 2 1 C 2 1 2 1 2 1 z", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs16,
2863 SK_ARRAY_COUNT(resultVerbs16)
2864 }
2865 };
2866
2867 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2868 p.reset();
2869 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2870 REPORTER_ASSERT(reporter, valid);
2871 REPORTER_ASSERT(reporter, !p.isEmpty());
2872 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2873 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2874 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2875 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2876 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2877 }
2878 }
2879}
2880
2881struct SegmentInfo {
2882 SkPath fPath;
2883 int fPointCount;
2884};
2885
2886#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2887
2888static void test_segment_masks(skiatest::Reporter* reporter) {
2889 SkPath p, p2;
2890
2891 p.moveTo(0, 0);
2892 p.quadTo(100, 100, 200, 200);
2893 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2894 REPORTER_ASSERT(reporter, !p.isEmpty());
2895 p2 = p;
2896 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2897 p.cubicTo(100, 100, 200, 200, 300, 300);
2898 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2899 REPORTER_ASSERT(reporter, !p.isEmpty());
2900 p2 = p;
2901 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2902
2903 p.reset();
2904 p.moveTo(0, 0);
2905 p.cubicTo(100, 100, 200, 200, 300, 300);
2906 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2907 p2 = p;
2908 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2909
2910 REPORTER_ASSERT(reporter, !p.isEmpty());
2911}
2912
2913static void test_iter(skiatest::Reporter* reporter) {
2914 SkPath p;
2915 SkPoint pts[4];
2916
2917 // Test an iterator with no path
2918 SkPath::Iter noPathIter;
2919 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2920
2921 // Test that setting an empty path works
2922 noPathIter.setPath(p, false);
2923 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2924
2925 // Test that close path makes no difference for an empty path
2926 noPathIter.setPath(p, true);
2927 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2928
2929 // Test an iterator with an initial empty path
2930 SkPath::Iter iter(p, false);
2931 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2932
2933 // Test that close path makes no difference
2934 iter.setPath(p, true);
2935 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2936
2937
2938 struct iterTestData {
2939 const char* testPath;
2940 const bool forceClose;
Yuqian Li3154a532017-09-06 13:33:30 -04002941 const size_t* numResultPtsPerVerb;
2942 const SkPoint* resultPts;
2943 const SkPath::Verb* resultVerbs;
2944 const size_t numResultVerbs;
2945 };
2946
2947 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2948 static const SkPath::Verb resultVerbs2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002949 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2950 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002951 static const SkPath::Verb resultVerbs3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002952 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2953 };
2954 static const size_t resultPtsSizes1[] = { 0 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002955 static const size_t resultPtsSizes2[] = { 1, 2, 1, 1, 0 };
2956 static const size_t resultPtsSizes3[] = { 1, 2, 1, 1, 1, 0 };
Yuqian Li3154a532017-09-06 13:33:30 -04002957 static const SkPoint* resultPts1 = nullptr;
2958 static const SkPoint resultPts2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002959 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2960 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002961 static const SkPoint resultPts3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002962 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2963 };
2964 static const struct iterTestData gIterTests[] = {
Mike Reedba7e9a62019-08-16 13:30:34 -04002965 { "M 1 0", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2966 { "z", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2967 { "z", true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2968 { "M 1 0 L 1 0 M 0 0 z", false, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2969 { "M 1 0 L 1 0 M 0 0 z", true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }
Yuqian Li3154a532017-09-06 13:33:30 -04002970 };
2971
2972 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2973 p.reset();
2974 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2975 REPORTER_ASSERT(reporter, valid);
2976 iter.setPath(p, gIterTests[i].forceClose);
2977 int j = 0, l = 0;
2978 do {
Mike Reedba7e9a62019-08-16 13:30:34 -04002979 REPORTER_ASSERT(reporter, iter.next(pts) == gIterTests[i].resultVerbs[j]);
Yuqian Li3154a532017-09-06 13:33:30 -04002980 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2981 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2982 }
2983 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2984 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2985 }
2986
2987 p.reset();
2988 iter.setPath(p, false);
2989 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2990 p.lineTo(1, 1);
2991 p.close();
2992 iter.setPath(p, false);
2993 REPORTER_ASSERT(reporter, iter.isClosedContour());
2994 p.reset();
2995 iter.setPath(p, true);
2996 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2997 p.lineTo(1, 1);
2998 iter.setPath(p, true);
2999 REPORTER_ASSERT(reporter, iter.isClosedContour());
3000 p.moveTo(0, 0);
3001 p.lineTo(2, 2);
3002 iter.setPath(p, false);
3003 REPORTER_ASSERT(reporter, !iter.isClosedContour());
3004
3005 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
3006 // check to see if the result is correct.
3007 for (int setNaN = 0; setNaN < 4; ++setNaN) {
3008 p.reset();
3009 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
3010 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
3011 iter.setPath(p, true);
Mike Reedba7e9a62019-08-16 13:30:34 -04003012 iter.next(pts);
3013 iter.next(pts);
3014 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003015 }
3016
3017 p.reset();
3018 p.quadTo(0, 0, 0, 0);
3019 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003020 iter.next(pts);
3021 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003022
3023 p.reset();
3024 p.conicTo(0, 0, 0, 0, 0.5f);
3025 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003026 iter.next(pts);
3027 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003028
3029 p.reset();
3030 p.cubicTo(0, 0, 0, 0, 0, 0);
3031 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003032 iter.next(pts);
3033 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003034
3035 p.moveTo(1, 1); // add a trailing moveto
3036 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003037 iter.next(pts);
3038 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003039
3040 // The GM degeneratesegments.cpp test is more extensive
3041
3042 // Test out mixed degenerate and non-degenerate geometry with Conics
3043 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3044 SkRect r = SkRect::MakeWH(100, 100);
3045 SkRRect rr;
3046 rr.setRectRadii(r, radii);
3047 p.reset();
3048 p.addRRect(rr);
3049 iter.setPath(p, false);
3050 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3051 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
Mike Reedba7e9a62019-08-16 13:30:34 -04003052 return;
Yuqian Li3154a532017-09-06 13:33:30 -04003053 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3054 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3055 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
3056}
3057
3058static void test_raw_iter(skiatest::Reporter* reporter) {
3059 SkPath p;
3060 SkPoint pts[4];
3061
3062 // Test an iterator with no path
3063 SkPath::RawIter noPathIter;
3064 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3065 // Test that setting an empty path works
3066 noPathIter.setPath(p);
3067 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3068
3069 // Test an iterator with an initial empty path
3070 SkPath::RawIter iter(p);
3071 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3072
3073 // Test that a move-only path returns the move.
3074 p.moveTo(SK_Scalar1, 0);
3075 iter.setPath(p);
3076 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3077 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3078 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3079 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3080
3081 // No matter how many moves we add, we should get them all back
3082 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3083 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3084 iter.setPath(p);
3085 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3086 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3087 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3088 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3089 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3090 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3091 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3092 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3093 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3094 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3095
3096 // Initial close is never ever stored
3097 p.reset();
3098 p.close();
3099 iter.setPath(p);
3100 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3101
3102 // Move/close sequences
3103 p.reset();
3104 p.close(); // Not stored, no purpose
3105 p.moveTo(SK_Scalar1, 0);
3106 p.close();
3107 p.close(); // Not stored, no purpose
3108 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3109 p.close();
3110 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3111 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3112 p.close();
3113 iter.setPath(p);
3114 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3115 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3116 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3117 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3118 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3119 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3120 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3121 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3122 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3123 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3124 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3125 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3126 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3127 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3128 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3129 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3130
3131 // Generate random paths and verify
3132 SkPoint randomPts[25];
3133 for (int i = 0; i < 5; ++i) {
3134 for (int j = 0; j < 5; ++j) {
3135 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3136 }
3137 }
3138
3139 // Max of 10 segments, max 3 points per segment
3140 SkRandom rand(9876543);
3141 SkPoint expectedPts[31]; // May have leading moveTo
3142 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
3143 SkPath::Verb nextVerb;
3144
3145 for (int i = 0; i < 500; ++i) {
3146 p.reset();
3147 bool lastWasClose = true;
3148 bool haveMoveTo = false;
3149 SkPoint lastMoveToPt = { 0, 0 };
3150 int numPoints = 0;
3151 int numVerbs = (rand.nextU() >> 16) % 10;
3152 int numIterVerbs = 0;
3153 for (int j = 0; j < numVerbs; ++j) {
3154 do {
3155 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3156 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
3157 switch (nextVerb) {
3158 case SkPath::kMove_Verb:
3159 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3160 p.moveTo(expectedPts[numPoints]);
3161 lastMoveToPt = expectedPts[numPoints];
3162 numPoints += 1;
3163 lastWasClose = false;
3164 haveMoveTo = true;
3165 break;
3166 case SkPath::kLine_Verb:
3167 if (!haveMoveTo) {
3168 expectedPts[numPoints++] = lastMoveToPt;
3169 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3170 haveMoveTo = true;
3171 }
3172 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3173 p.lineTo(expectedPts[numPoints]);
3174 numPoints += 1;
3175 lastWasClose = false;
3176 break;
3177 case SkPath::kQuad_Verb:
3178 if (!haveMoveTo) {
3179 expectedPts[numPoints++] = lastMoveToPt;
3180 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3181 haveMoveTo = true;
3182 }
3183 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3184 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3185 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3186 numPoints += 2;
3187 lastWasClose = false;
3188 break;
3189 case SkPath::kConic_Verb:
3190 if (!haveMoveTo) {
3191 expectedPts[numPoints++] = lastMoveToPt;
3192 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3193 haveMoveTo = true;
3194 }
3195 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3196 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3197 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3198 rand.nextUScalar1() * 4);
3199 numPoints += 2;
3200 lastWasClose = false;
3201 break;
3202 case SkPath::kCubic_Verb:
3203 if (!haveMoveTo) {
3204 expectedPts[numPoints++] = lastMoveToPt;
3205 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3206 haveMoveTo = true;
3207 }
3208 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3209 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3210 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3211 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3212 expectedPts[numPoints + 2]);
3213 numPoints += 3;
3214 lastWasClose = false;
3215 break;
3216 case SkPath::kClose_Verb:
3217 p.close();
3218 haveMoveTo = false;
3219 lastWasClose = true;
3220 break;
3221 default:
3222 SkDEBUGFAIL("unexpected verb");
3223 }
3224 expectedVerbs[numIterVerbs++] = nextVerb;
3225 }
3226
3227 iter.setPath(p);
3228 numVerbs = numIterVerbs;
3229 numIterVerbs = 0;
3230 int numIterPts = 0;
3231 SkPoint lastMoveTo;
3232 SkPoint lastPt;
3233 lastMoveTo.set(0, 0);
3234 lastPt.set(0, 0);
3235 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3236 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3237 numIterVerbs++;
3238 switch (nextVerb) {
3239 case SkPath::kMove_Verb:
3240 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3241 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3242 lastPt = lastMoveTo = pts[0];
3243 numIterPts += 1;
3244 break;
3245 case SkPath::kLine_Verb:
3246 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3247 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3248 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3249 lastPt = pts[1];
3250 numIterPts += 1;
3251 break;
3252 case SkPath::kQuad_Verb:
3253 case SkPath::kConic_Verb:
3254 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3255 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3256 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3257 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3258 lastPt = pts[2];
3259 numIterPts += 2;
3260 break;
3261 case SkPath::kCubic_Verb:
3262 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3263 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3264 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3265 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3266 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3267 lastPt = pts[3];
3268 numIterPts += 3;
3269 break;
3270 case SkPath::kClose_Verb:
3271 lastPt = lastMoveTo;
3272 break;
3273 default:
3274 SkDEBUGFAIL("unexpected verb");
3275 }
3276 }
3277 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3278 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3279 }
3280}
3281
3282static void check_for_circle(skiatest::Reporter* reporter,
3283 const SkPath& path,
3284 bool expectedCircle,
3285 SkPathPriv::FirstDirection expectedDir) {
3286 SkRect rect = SkRect::MakeEmpty();
3287 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
Mike Reed30bc5272019-11-22 18:34:02 +00003288 SkPathDirection isOvalDir;
Yuqian Li3154a532017-09-06 13:33:30 -04003289 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003290 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003291 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3292 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3293 SkPath tmpPath;
3294 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3295 REPORTER_ASSERT(reporter, path == tmpPath);
3296 }
3297 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3298}
3299
3300static void test_circle_skew(skiatest::Reporter* reporter,
3301 const SkPath& path,
3302 SkPathPriv::FirstDirection dir) {
3303 SkPath tmp;
3304
3305 SkMatrix m;
3306 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3307 path.transform(m, &tmp);
3308 // this matrix reverses the direction.
3309 if (SkPathPriv::kCCW_FirstDirection == dir) {
3310 dir = SkPathPriv::kCW_FirstDirection;
3311 } else {
3312 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3313 dir = SkPathPriv::kCCW_FirstDirection;
3314 }
3315 check_for_circle(reporter, tmp, false, dir);
3316}
3317
3318static void test_circle_translate(skiatest::Reporter* reporter,
3319 const SkPath& path,
3320 SkPathPriv::FirstDirection dir) {
3321 SkPath tmp;
3322
3323 // translate at small offset
3324 SkMatrix m;
3325 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3326 path.transform(m, &tmp);
3327 check_for_circle(reporter, tmp, true, dir);
3328
3329 tmp.reset();
3330 m.reset();
3331
3332 // translate at a relatively big offset
3333 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3334 path.transform(m, &tmp);
3335 check_for_circle(reporter, tmp, true, dir);
3336}
3337
3338static void test_circle_rotate(skiatest::Reporter* reporter,
3339 const SkPath& path,
3340 SkPathPriv::FirstDirection dir) {
3341 for (int angle = 0; angle < 360; ++angle) {
3342 SkPath tmp;
3343 SkMatrix m;
3344 m.setRotate(SkIntToScalar(angle));
3345 path.transform(m, &tmp);
3346
3347 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3348 // degrees is not an oval anymore, this can be improved. we made this
3349 // for the simplicity of our implementation.
3350 if (angle % 90 == 0) {
3351 check_for_circle(reporter, tmp, true, dir);
3352 } else {
3353 check_for_circle(reporter, tmp, false, dir);
3354 }
3355 }
3356}
3357
3358static void test_circle_mirror_x(skiatest::Reporter* reporter,
3359 const SkPath& path,
3360 SkPathPriv::FirstDirection dir) {
3361 SkPath tmp;
3362 SkMatrix m;
3363 m.reset();
3364 m.setScaleX(-SK_Scalar1);
3365 path.transform(m, &tmp);
3366 if (SkPathPriv::kCW_FirstDirection == dir) {
3367 dir = SkPathPriv::kCCW_FirstDirection;
3368 } else {
3369 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3370 dir = SkPathPriv::kCW_FirstDirection;
3371 }
3372 check_for_circle(reporter, tmp, true, dir);
3373}
3374
3375static void test_circle_mirror_y(skiatest::Reporter* reporter,
3376 const SkPath& path,
3377 SkPathPriv::FirstDirection dir) {
3378 SkPath tmp;
3379 SkMatrix m;
3380 m.reset();
3381 m.setScaleY(-SK_Scalar1);
3382 path.transform(m, &tmp);
3383
3384 if (SkPathPriv::kCW_FirstDirection == dir) {
3385 dir = SkPathPriv::kCCW_FirstDirection;
3386 } else {
3387 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3388 dir = SkPathPriv::kCW_FirstDirection;
3389 }
3390
3391 check_for_circle(reporter, tmp, true, dir);
3392}
3393
3394static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3395 const SkPath& path,
3396 SkPathPriv::FirstDirection dir) {
3397 SkPath tmp;
3398 SkMatrix m;
3399 m.reset();
3400 m.setScaleX(-SK_Scalar1);
3401 m.setScaleY(-SK_Scalar1);
3402 path.transform(m, &tmp);
3403
3404 check_for_circle(reporter, tmp, true, dir);
3405}
3406
3407static void test_circle_with_direction(skiatest::Reporter* reporter,
Mike Reed30bc5272019-11-22 18:34:02 +00003408 SkPathDirection inDir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003409 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3410 SkPath path;
3411
3412 // circle at origin
3413 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3414
3415 check_for_circle(reporter, path, true, dir);
3416 test_circle_rotate(reporter, path, dir);
3417 test_circle_translate(reporter, path, dir);
3418 test_circle_skew(reporter, path, dir);
3419 test_circle_mirror_x(reporter, path, dir);
3420 test_circle_mirror_y(reporter, path, dir);
3421 test_circle_mirror_xy(reporter, path, dir);
3422
3423 // circle at an offset at (10, 10)
3424 path.reset();
3425 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3426 SkIntToScalar(20), inDir);
3427
3428 check_for_circle(reporter, path, true, dir);
3429 test_circle_rotate(reporter, path, dir);
3430 test_circle_translate(reporter, path, dir);
3431 test_circle_skew(reporter, path, dir);
3432 test_circle_mirror_x(reporter, path, dir);
3433 test_circle_mirror_y(reporter, path, dir);
3434 test_circle_mirror_xy(reporter, path, dir);
3435
3436 // Try different starting points for the contour.
3437 for (unsigned start = 0; start < 4; ++start) {
3438 path.reset();
3439 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3440 test_circle_rotate(reporter, path, dir);
3441 test_circle_translate(reporter, path, dir);
3442 test_circle_skew(reporter, path, dir);
3443 test_circle_mirror_x(reporter, path, dir);
3444 test_circle_mirror_y(reporter, path, dir);
3445 test_circle_mirror_xy(reporter, path, dir);
3446 }
3447}
3448
3449static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3450 SkPath path;
3451 SkPath circle;
3452 SkPath rect;
3453 SkPath empty;
3454
Mike Reed30bc5272019-11-22 18:34:02 +00003455 const SkPathDirection kCircleDir = SkPathDirection::kCW;
3456 const SkPathDirection kCircleDirOpposite = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003457
3458 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3459 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
Mike Reed30bc5272019-11-22 18:34:02 +00003460 SkIntToScalar(20), SkIntToScalar(20), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003461
3462 SkMatrix translate;
3463 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3464
3465 // Although all the path concatenation related operations leave
3466 // the path a circle, most mark it as a non-circle for simplicity
3467
3468 // empty + circle (translate)
3469 path = empty;
3470 path.addPath(circle, translate);
3471 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3472
3473 // circle + empty (translate)
3474 path = circle;
3475 path.addPath(empty, translate);
3476
3477 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3478
3479 // test reverseAddPath
3480 path = circle;
3481 path.reverseAddPath(rect);
3482 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3483}
3484
3485static void test_circle(skiatest::Reporter* reporter) {
Mike Reed30bc5272019-11-22 18:34:02 +00003486 test_circle_with_direction(reporter, SkPathDirection::kCW);
3487 test_circle_with_direction(reporter, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003488
3489 // multiple addCircle()
3490 SkPath path;
Mike Reed30bc5272019-11-22 18:34:02 +00003491 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
3492 path.addCircle(0, 0, SkIntToScalar(20), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003493 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3494
3495 // some extra lineTo() would make isOval() fail
3496 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003497 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003498 path.lineTo(0, 0);
3499 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3500
3501 // not back to the original point
3502 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003503 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003504 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3505 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3506
3507 test_circle_with_add_paths(reporter);
3508
3509 // test negative radius
3510 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003511 path.addCircle(0, 0, -1, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003512 REPORTER_ASSERT(reporter, path.isEmpty());
3513}
3514
3515static void test_oval(skiatest::Reporter* reporter) {
3516 SkRect rect;
3517 SkMatrix m;
3518 SkPath path;
3519 unsigned start = 0;
Mike Reed30bc5272019-11-22 18:34:02 +00003520 SkPathDirection dir = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003521
3522 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3523 path.addOval(rect);
3524
3525 // Defaults to dir = CW and start = 1
3526 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3527
3528 m.setRotate(SkIntToScalar(90));
3529 SkPath tmp;
3530 path.transform(m, &tmp);
3531 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3532 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003533 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003534 REPORTER_ASSERT(reporter, 2 == start);
Mike Reed30bc5272019-11-22 18:34:02 +00003535 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003536
3537 m.reset();
3538 m.setRotate(SkIntToScalar(30));
3539 tmp.reset();
3540 path.transform(m, &tmp);
3541 // an oval rotated 30 degrees is not an oval anymore.
3542 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3543
3544 // since empty path being transformed.
3545 path.reset();
3546 tmp.reset();
3547 m.reset();
3548 path.transform(m, &tmp);
3549 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3550
3551 // empty path is not an oval
3552 tmp.reset();
3553 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3554
3555 // only has moveTo()s
3556 tmp.reset();
3557 tmp.moveTo(0, 0);
3558 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3559 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3560
3561 // mimic WebKit's calling convention,
3562 // call moveTo() first and then call addOval()
3563 path.reset();
3564 path.moveTo(0, 0);
3565 path.addOval(rect);
3566 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3567
3568 // copy path
3569 path.reset();
3570 tmp.reset();
3571 tmp.addOval(rect);
3572 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003573 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Mike Reed30bc5272019-11-22 18:34:02 +00003574 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003575 REPORTER_ASSERT(reporter, 1 == start);
3576}
3577
3578static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3579 SkPath empty;
3580
3581 REPORTER_ASSERT(reporter, p.isEmpty());
3582 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3583 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3584 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3585 REPORTER_ASSERT(reporter, p.isConvex());
Mike Reedcf0e3c62019-12-03 16:26:15 -05003586 REPORTER_ASSERT(reporter, p.getFillType() == SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04003587 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3588 REPORTER_ASSERT(reporter, p == empty);
3589 REPORTER_ASSERT(reporter, !(p != empty));
3590}
3591
3592static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
Mike Reed30bc5272019-11-22 18:34:02 +00003593 SkPathDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003594 REPORTER_ASSERT(reporter, path->isConvex());
3595 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
Mike Reed30bc5272019-11-22 18:34:02 +00003596 path->setConvexityType(SkPathConvexityType::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04003597 REPORTER_ASSERT(reporter, path->isConvex());
3598 path->reset();
3599}
3600
3601static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
Mike Reed30bc5272019-11-22 18:34:02 +00003602 SkPathDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003603 REPORTER_ASSERT(reporter, path->isConvex());
3604 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
Mike Reed30bc5272019-11-22 18:34:02 +00003605 path->setConvexityType(SkPathConvexityType::kUnknown);
3606 REPORTER_ASSERT(reporter, path->getConvexityType() == SkPathConvexityType::kConcave);
Yuqian Li3154a532017-09-06 13:33:30 -04003607 path->reset();
3608}
3609
3610static void test_rrect(skiatest::Reporter* reporter) {
3611 SkPath p;
3612 SkRRect rr;
3613 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3614 SkRect r = {10, 20, 30, 40};
3615 rr.setRectRadii(r, radii);
3616 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003617 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3618 p.addRRect(rr, SkPathDirection::kCCW);
3619 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003620 p.addRoundRect(r, &radii[0].fX);
Mike Reed30bc5272019-11-22 18:34:02 +00003621 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3622 p.addRoundRect(r, &radii[0].fX, SkPathDirection::kCCW);
3623 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003624 p.addRoundRect(r, radii[1].fX, radii[1].fY);
Mike Reed30bc5272019-11-22 18:34:02 +00003625 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3626 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPathDirection::kCCW);
3627 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003628 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3629 SkVector save = radii[i];
3630 radii[i].set(0, 0);
3631 rr.setRectRadii(r, radii);
3632 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003633 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003634 radii[i] = save;
3635 }
3636 p.addRoundRect(r, 0, 0);
3637 SkRect returnedRect;
3638 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3639 REPORTER_ASSERT(reporter, returnedRect == r);
Mike Reed30bc5272019-11-22 18:34:02 +00003640 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003641 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3642 rr.setRectRadii(r, zeroRadii);
3643 p.addRRect(rr);
3644 bool closed;
Mike Reed30bc5272019-11-22 18:34:02 +00003645 SkPathDirection dir;
Yuqian Li3154a532017-09-06 13:33:30 -04003646 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3647 REPORTER_ASSERT(reporter, closed);
Mike Reed30bc5272019-11-22 18:34:02 +00003648 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
3649 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3650 p.addRRect(rr, SkPathDirection::kCW);
3651 p.addRRect(rr, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003652 REPORTER_ASSERT(reporter, !p.isConvex());
3653 p.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003654 p.addRRect(rr, SkPathDirection::kCCW);
3655 p.addRRect(rr, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003656 REPORTER_ASSERT(reporter, !p.isConvex());
3657 p.reset();
3658 SkRect emptyR = {10, 20, 10, 30};
3659 rr.setRectRadii(emptyR, radii);
3660 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003661 // The round rect is "empty" in that it has no fill area. However,
3662 // the path isn't "empty" in that it should have verbs and points.
3663 REPORTER_ASSERT(reporter, !p.isEmpty());
3664 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003665 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3666 rr.setRectRadii(largeR, radii);
3667 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003668 test_rrect_convexity_is_unknown(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003669
3670 // we check for non-finites
3671 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3672 rr.setRectRadii(infR, radii);
3673 REPORTER_ASSERT(reporter, rr.isEmpty());
3674
Brian Osman4be31e22019-08-20 15:35:26 -04003675 // We consider any path with very small (numerically unstable) edges to be concave.
Yuqian Li3154a532017-09-06 13:33:30 -04003676 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3677 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
Mike Reed30bc5272019-11-22 18:34:02 +00003678 test_rrect_convexity_is_unknown(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003679}
3680
3681static void test_arc(skiatest::Reporter* reporter) {
3682 SkPath p;
3683 SkRect emptyOval = {10, 20, 30, 20};
3684 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3685 p.addArc(emptyOval, 1, 2);
3686 REPORTER_ASSERT(reporter, p.isEmpty());
3687 p.reset();
3688 SkRect oval = {10, 20, 30, 40};
3689 p.addArc(oval, 1, 0);
3690 REPORTER_ASSERT(reporter, p.isEmpty());
3691 p.reset();
3692 SkPath cwOval;
3693 cwOval.addOval(oval);
3694 p.addArc(oval, 0, 360);
3695 REPORTER_ASSERT(reporter, p == cwOval);
3696 p.reset();
3697 SkPath ccwOval;
Mike Reed30bc5272019-11-22 18:34:02 +00003698 ccwOval.addOval(oval, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003699 p.addArc(oval, 0, -360);
3700 REPORTER_ASSERT(reporter, p == ccwOval);
3701 p.reset();
3702 p.addArc(oval, 1, 180);
Cary Clarkc9b7c722018-12-12 14:50:23 -05003703 // diagonal colinear points make arc convex
3704 // TODO: one way to keep it concave would be to introduce interpolated on curve points
3705 // between control points and computing the on curve point at scan conversion time
Mike Reed30bc5272019-11-22 18:34:02 +00003706 REPORTER_ASSERT(reporter, p.getConvexityType() == SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04003707 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
Mike Reed30bc5272019-11-22 18:34:02 +00003708 p.setConvexityType(SkPathConvexityType::kUnknown);
3709 REPORTER_ASSERT(reporter, p.getConvexityType() == SkPathConvexityType::kConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04003710}
3711
3712static inline SkScalar oval_start_index_to_angle(unsigned start) {
3713 switch (start) {
3714 case 0:
3715 return 270.f;
3716 case 1:
3717 return 0.f;
3718 case 2:
3719 return 90.f;
3720 case 3:
3721 return 180.f;
3722 default:
3723 return -1.f;
3724 }
3725}
3726
3727static inline SkScalar canonical_start_angle(float angle) {
3728 while (angle < 0.f) {
3729 angle += 360.f;
3730 }
3731 while (angle >= 360.f) {
3732 angle -= 360.f;
3733 }
3734 return angle;
3735}
3736
3737static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3738 const SkPath& path) {
3739 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00003740 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003741 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003742 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003743 REPORTER_ASSERT(reporter, isOval);
3744 SkPath recreatedPath;
3745 recreatedPath.addOval(r, d, s);
3746 REPORTER_ASSERT(reporter, path == recreatedPath);
3747 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
Mike Reed30bc5272019-11-22 18:34:02 +00003748 REPORTER_ASSERT(reporter, (SkPathDirection::kCW == d) == (sweep > 0.f));
Yuqian Li3154a532017-09-06 13:33:30 -04003749}
3750
3751static void test_arc_ovals(skiatest::Reporter* reporter) {
3752 SkRect oval = SkRect::MakeWH(10, 20);
3753 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3754 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3755 SkPath path;
3756 path.addArc(oval, start, sweep);
3757 // SkPath's interfaces for inserting and extracting ovals only allow contours
3758 // to start at multiples of 90 degrees.
3759 if (std::fmod(start, 90.f) == 0) {
3760 check_oval_arc(reporter, start, sweep, path);
3761 } else {
3762 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3763 }
3764 }
3765 // Test start angles that are nearly at valid oval start angles.
3766 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3767 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3768 SkPath path;
3769 path.addArc(oval, start + delta, sweep);
3770 check_oval_arc(reporter, start, sweep, path);
3771 }
3772 }
3773 }
3774}
3775
3776static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3777 SkScalar x0, SkScalar y0) {
3778 SkPoint pts[4];
3779 SkPath::Verb v = iter->next(pts);
3780 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3781 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3782 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3783}
3784
3785static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3786 SkScalar x1, SkScalar y1) {
3787 SkPoint pts[4];
3788 SkPath::Verb v = iter->next(pts);
3789 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3790 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3791 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3792}
3793
3794static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3795 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3796 SkPoint pts[4];
3797 SkPath::Verb v = iter->next(pts);
3798 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3799 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3800 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3801 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3802 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3803}
3804
3805static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3806 SkPoint pts[4];
3807 SkPath::Verb v = iter->next(pts);
3808 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3809}
3810
3811static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3812 check_done(reporter, p, iter);
3813 p->reset();
3814}
3815
3816static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3817 SkScalar x0, SkScalar y0) {
3818 SkPath::RawIter iter(*p);
3819 check_move(reporter, &iter, x0, y0);
3820 check_done_and_reset(reporter, p, &iter);
3821}
3822
3823static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3824 SkScalar x1, SkScalar y1) {
3825 SkPath::RawIter iter(*p);
3826 check_move(reporter, &iter, 0, 0);
3827 check_line(reporter, &iter, x1, y1);
3828 check_done_and_reset(reporter, p, &iter);
3829}
3830
3831static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3832 SkScalar x1, SkScalar y1) {
3833 SkPath::RawIter iter(*p);
3834 check_move(reporter, &iter, 0, 0);
3835 check_line(reporter, &iter, x1, y1);
3836 check_done(reporter, p, &iter);
3837}
3838
3839static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3840 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3841 SkPath::RawIter iter(*p);
3842 check_move(reporter, &iter, 0, 0);
3843 check_line(reporter, &iter, x1, y1);
3844 check_line(reporter, &iter, x2, y2);
3845 check_done_and_reset(reporter, p, &iter);
3846}
3847
3848static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3849 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3850 SkPath::RawIter iter(*p);
3851 check_move(reporter, &iter, 0, 0);
3852 check_quad(reporter, &iter, x1, y1, x2, y2);
3853 check_done_and_reset(reporter, p, &iter);
3854}
3855
3856static bool nearly_equal(const SkRect& a, const SkRect& b) {
3857 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3858 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3859 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3860 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3861}
3862
3863static void test_arcTo(skiatest::Reporter* reporter) {
3864 SkPath p;
3865 p.arcTo(0, 0, 1, 2, 1);
3866 check_path_is_line_and_reset(reporter, &p, 0, 0);
3867 p.arcTo(1, 2, 1, 2, 1);
3868 check_path_is_line_and_reset(reporter, &p, 1, 2);
3869 p.arcTo(1, 2, 3, 4, 0);
3870 check_path_is_line_and_reset(reporter, &p, 1, 2);
3871 p.arcTo(1, 2, 0, 0, 1);
3872 check_path_is_line_and_reset(reporter, &p, 1, 2);
3873 p.arcTo(1, 0, 1, 1, 1);
3874 SkPoint pt;
3875 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3876 p.reset();
3877 p.arcTo(1, 0, 1, -1, 1);
3878 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3879 p.reset();
3880 SkRect oval = {1, 2, 3, 4};
3881 p.arcTo(oval, 0, 0, true);
3882 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3883 p.arcTo(oval, 0, 0, false);
3884 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3885 p.arcTo(oval, 360, 0, true);
3886 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3887 p.arcTo(oval, 360, 0, false);
3888 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3889
3890 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3891 p.arcTo(oval, 0, sweep, false);
3892 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3893 sweep += delta;
3894 delta /= 2;
3895 }
3896 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3897 p.arcTo(oval, 0, sweep, false);
3898 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3899 sweep -= delta;
3900 delta /= 2;
3901 }
3902 SkRect noOvalWidth = {1, 2, 0, 3};
3903 p.reset();
3904 p.arcTo(noOvalWidth, 0, 360, false);
3905 REPORTER_ASSERT(reporter, p.isEmpty());
3906
3907 SkRect noOvalHeight = {1, 2, 3, 1};
3908 p.reset();
3909 p.arcTo(noOvalHeight, 0, 360, false);
3910 REPORTER_ASSERT(reporter, p.isEmpty());
3911}
3912
3913static void test_addPath(skiatest::Reporter* reporter) {
3914 SkPath p, q;
3915 p.lineTo(1, 2);
3916 q.moveTo(4, 4);
3917 q.lineTo(7, 8);
3918 q.conicTo(8, 7, 6, 5, 0.5f);
3919 q.quadTo(6, 7, 8, 6);
3920 q.cubicTo(5, 6, 7, 8, 7, 5);
3921 q.close();
3922 p.addPath(q, -4, -4);
3923 SkRect expected = {0, 0, 4, 4};
3924 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3925 p.reset();
3926 p.reverseAddPath(q);
3927 SkRect reverseExpected = {4, 4, 8, 8};
3928 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3929}
3930
3931static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3932 SkPath p, q;
3933 if (explicitMoveTo) {
3934 p.moveTo(1, 1);
3935 }
3936 p.lineTo(1, 2);
3937 if (explicitMoveTo) {
3938 q.moveTo(2, 1);
3939 }
3940 q.lineTo(2, 2);
3941 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3942 uint8_t verbs[4];
3943 int verbcount = p.getVerbs(verbs, 4);
3944 REPORTER_ASSERT(reporter, verbcount == 4);
3945 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3946 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3947 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3948 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3949}
3950
3951static void test_extendClosedPath(skiatest::Reporter* reporter) {
3952 SkPath p, q;
3953 p.moveTo(1, 1);
3954 p.lineTo(1, 2);
3955 p.lineTo(2, 2);
3956 p.close();
3957 q.moveTo(2, 1);
3958 q.lineTo(2, 3);
3959 p.addPath(q, SkPath::kExtend_AddPathMode);
3960 uint8_t verbs[7];
3961 int verbcount = p.getVerbs(verbs, 7);
3962 REPORTER_ASSERT(reporter, verbcount == 7);
3963 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3964 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3965 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3966 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3967 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3968 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3969 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3970
3971 SkPoint pt;
3972 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3973 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3974 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3975}
3976
3977static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3978 SkPath p, q, r;
3979 // case 1: dst is empty
3980 p.moveTo(2, 1);
3981 p.lineTo(2, 3);
3982 q.addPath(p, mode);
3983 REPORTER_ASSERT(reporter, q == p);
3984 // case 2: src is empty
3985 p.addPath(r, mode);
3986 REPORTER_ASSERT(reporter, q == p);
3987 // case 3: src and dst are empty
3988 q.reset();
3989 q.addPath(r, mode);
3990 REPORTER_ASSERT(reporter, q.isEmpty());
3991}
3992
3993static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3994 SkPath p;
3995 p.conicTo(1, 2, 3, 4, -1);
3996 check_path_is_line_and_reset(reporter, &p, 3, 4);
3997 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3998 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3999 p.conicTo(1, 2, 3, 4, 1);
4000 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
4001}
4002
4003static void test_get_point(skiatest::Reporter* reporter) {
4004 SkPath p;
4005 SkPoint pt = p.getPoint(0);
4006 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
4007 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
4008 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
4009 p.setLastPt(10, 10);
4010 pt = p.getPoint(0);
4011 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
4012 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
4013 p.rMoveTo(10, 10);
4014 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4015}
4016
4017static void test_contains(skiatest::Reporter* reporter) {
4018 SkPath p;
4019 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4020 p.conicTo(SkBits2Float(0xdfdaa221), SkBits2Float(0x5eaac338), SkBits2Float(0x60342f13), SkBits2Float(0xdf0cbb58), SkBits2Float(0x3f3504f3)); // -3.15084e+19f, 6.15237e+18f, 5.19345e+19f, -1.01408e+19f, 0.707107f
4021 p.conicTo(SkBits2Float(0x60ead799), SkBits2Float(0xdfb76c24), SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8), SkBits2Float(0x3f3504f4)); // 1.35377e+20f, -2.6434e+19f, 8.96947e+19f, -1.75139e+19f, 0.707107f
4022 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4023 p.conicTo(SkBits2Float(0x6018b296), SkBits2Float(0xdeee870d), SkBits2Float(0xe008cd8e), SkBits2Float(0x5ed5b2db), SkBits2Float(0x3f3504f3)); // 4.40121e+19f, -8.59386e+18f, -3.94308e+19f, 7.69931e+18f, 0.707107f
4024 p.conicTo(SkBits2Float(0xe0d526d9), SkBits2Float(0x5fa67b31), SkBits2Float(0xe085e7b2), SkBits2Float(0x5f512c01), SkBits2Float(0x3f3504f3)); // -1.22874e+20f, 2.39925e+19f, -7.7191e+19f, 1.50724e+19f, 0.707107f
4025 // this may return true or false, depending on the platform's numerics, but it should not crash
4026 (void) p.contains(-77.2027664f, 15.3066053f);
4027
4028 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004029 p.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004030 REPORTER_ASSERT(reporter, p.contains(0, 0));
Mike Reed7d34dc72019-11-26 12:17:17 -05004031 p.setFillType(SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004032 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4033 p.moveTo(4, 4);
4034 p.lineTo(6, 8);
4035 p.lineTo(8, 4);
4036 // test on edge
4037 REPORTER_ASSERT(reporter, p.contains(6, 4));
4038 REPORTER_ASSERT(reporter, p.contains(5, 6));
4039 REPORTER_ASSERT(reporter, p.contains(7, 6));
4040 // test quick reject
4041 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4042 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4043 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4044 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4045 // test various crossings in x
4046 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4047 REPORTER_ASSERT(reporter, p.contains(6, 7));
4048 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4049 p.reset();
4050 p.moveTo(4, 4);
4051 p.lineTo(8, 6);
4052 p.lineTo(4, 8);
4053 // test on edge
4054 REPORTER_ASSERT(reporter, p.contains(4, 6));
4055 REPORTER_ASSERT(reporter, p.contains(6, 5));
4056 REPORTER_ASSERT(reporter, p.contains(6, 7));
4057 // test various crossings in y
4058 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4059 REPORTER_ASSERT(reporter, p.contains(7, 6));
4060 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4061 p.reset();
4062 p.moveTo(4, 4);
4063 p.lineTo(8, 4);
4064 p.lineTo(8, 8);
4065 p.lineTo(4, 8);
4066 // test on vertices
4067 REPORTER_ASSERT(reporter, p.contains(4, 4));
4068 REPORTER_ASSERT(reporter, p.contains(8, 4));
4069 REPORTER_ASSERT(reporter, p.contains(8, 8));
4070 REPORTER_ASSERT(reporter, p.contains(4, 8));
4071 p.reset();
4072 p.moveTo(4, 4);
4073 p.lineTo(6, 8);
4074 p.lineTo(2, 8);
4075 // test on edge
4076 REPORTER_ASSERT(reporter, p.contains(5, 6));
4077 REPORTER_ASSERT(reporter, p.contains(4, 8));
4078 REPORTER_ASSERT(reporter, p.contains(3, 6));
4079 p.reset();
4080 p.moveTo(4, 4);
4081 p.lineTo(0, 6);
4082 p.lineTo(4, 8);
4083 // test on edge
4084 REPORTER_ASSERT(reporter, p.contains(2, 5));
4085 REPORTER_ASSERT(reporter, p.contains(2, 7));
4086 REPORTER_ASSERT(reporter, p.contains(4, 6));
4087 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4088 p.reset();
4089 p.moveTo(4, 0);
4090 p.lineTo(6, 4);
4091 p.lineTo(2, 4);
4092 p.moveTo(4, 0);
4093 p.lineTo(0, 8);
4094 p.lineTo(8, 8);
4095 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4096 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4097 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4098 REPORTER_ASSERT(reporter, p.contains(4, 4));
4099
4100 // test quads
4101 p.reset();
4102 p.moveTo(4, 4);
4103 p.quadTo(6, 6, 8, 8);
4104 p.quadTo(6, 8, 4, 8);
4105 p.quadTo(4, 6, 4, 4);
4106 REPORTER_ASSERT(reporter, p.contains(5, 6));
4107 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4108 // test quad edge
4109 REPORTER_ASSERT(reporter, p.contains(5, 5));
4110 REPORTER_ASSERT(reporter, p.contains(5, 8));
4111 REPORTER_ASSERT(reporter, p.contains(4, 5));
4112 // test quad endpoints
4113 REPORTER_ASSERT(reporter, p.contains(4, 4));
4114 REPORTER_ASSERT(reporter, p.contains(8, 8));
4115 REPORTER_ASSERT(reporter, p.contains(4, 8));
4116
4117 p.reset();
4118 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4119 p.moveTo(qPts[0]);
4120 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4121 p.quadTo(qPts[index], qPts[index + 1]);
4122 }
4123 REPORTER_ASSERT(reporter, p.contains(5, 6));
4124 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4125 // test quad edge
4126 SkPoint halfway;
4127 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4128 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4129 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4130 }
4131
4132 // test conics
4133 p.reset();
4134 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4135 p.moveTo(kPts[0]);
4136 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4137 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4138 }
4139 REPORTER_ASSERT(reporter, p.contains(5, 6));
4140 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4141 // test conic edge
4142 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4143 SkConic conic(&kPts[index], 0.5f);
4144 halfway = conic.evalAt(0.5f);
4145 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4146 }
4147 // test conic end points
4148 REPORTER_ASSERT(reporter, p.contains(4, 4));
4149 REPORTER_ASSERT(reporter, p.contains(8, 8));
4150 REPORTER_ASSERT(reporter, p.contains(4, 8));
4151
4152 // test cubics
4153 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4154 for (int i = 0; i < 3; ++i) {
4155 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004156 p.setFillType(SkPathFillType::kEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004157 p.moveTo(pts[i].fX, pts[i].fY);
4158 p.cubicTo(pts[i + 1].fX, pts[i + 1].fY, pts[i + 2].fX, pts[i + 2].fY, pts[i + 3].fX, pts[i + 3].fY);
4159 p.cubicTo(pts[i + 4].fX, pts[i + 4].fY, pts[i + 5].fX, pts[i + 5].fY, pts[i + 6].fX, pts[i + 6].fY);
4160 p.close();
4161 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4162 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4163 // test cubic edge
4164 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4165 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4166 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4167 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4168 // test cubic end points
4169 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4170 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4171 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4172 }
4173}
4174
4175class PathRefTest_Private {
4176public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004177 static size_t GetFreeSpace(const SkPathRef& ref) {
Mike Reed8fda88e2019-09-05 14:14:38 -04004178 return (ref.fPoints.reserved() - ref.fPoints.count()) * sizeof(SkPoint)
4179 + (ref.fVerbs.reserved() - ref.fVerbs.count()) * sizeof(uint8_t);
Florin Malita0e0f1a72018-09-11 16:16:19 -04004180 }
4181
Yuqian Li3154a532017-09-06 13:33:30 -04004182 static void TestPathRef(skiatest::Reporter* reporter) {
4183 static const int kRepeatCnt = 10;
4184
4185 sk_sp<SkPathRef> pathRef(new SkPathRef);
4186
4187 SkPathRef::Editor ed(&pathRef);
4188
4189 {
4190 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4191 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4192 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4193 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4194 for (int i = 0; i < kRepeatCnt; ++i) {
4195 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4196 }
4197 ed.resetToSize(0, 0, 0);
4198 }
4199
4200 {
4201 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4202 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4203 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4204 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4205 for (int i = 0; i < kRepeatCnt; ++i) {
4206 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4207 }
4208 ed.resetToSize(0, 0, 0);
4209 }
4210
4211 {
4212 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4213 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4214 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4215 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4216 for (int i = 0; i < kRepeatCnt; ++i) {
4217 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4218 }
4219 ed.resetToSize(0, 0, 0);
4220 }
4221
4222 {
4223 SkScalar* weights = nullptr;
4224 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4225 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4226 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4227 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4228 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4229 REPORTER_ASSERT(reporter, weights);
4230 for (int i = 0; i < kRepeatCnt; ++i) {
4231 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4232 }
4233 ed.resetToSize(0, 0, 0);
4234 }
4235
4236 {
4237 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4238 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4239 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4240 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4241 for (int i = 0; i < kRepeatCnt; ++i) {
4242 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4243 }
4244 ed.resetToSize(0, 0, 0);
4245 }
4246 }
4247};
4248
4249static void test_operatorEqual(skiatest::Reporter* reporter) {
4250 SkPath a;
4251 SkPath b;
4252 REPORTER_ASSERT(reporter, a == a);
4253 REPORTER_ASSERT(reporter, a == b);
Mike Reed7d34dc72019-11-26 12:17:17 -05004254 a.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004255 REPORTER_ASSERT(reporter, a != b);
4256 a.reset();
4257 REPORTER_ASSERT(reporter, a == b);
4258 a.lineTo(1, 1);
4259 REPORTER_ASSERT(reporter, a != b);
4260 a.reset();
4261 REPORTER_ASSERT(reporter, a == b);
4262 a.lineTo(1, 1);
4263 b.lineTo(1, 2);
4264 REPORTER_ASSERT(reporter, a != b);
4265 a.reset();
4266 a.lineTo(1, 2);
4267 REPORTER_ASSERT(reporter, a == b);
4268}
4269
4270static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4271 bool dumpAsHex, const char* str) {
4272 SkDynamicMemoryWStream wStream;
4273 path.dump(&wStream, force, dumpAsHex);
4274 sk_sp<SkData> data = wStream.detachAsData();
4275 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4276 if (strlen(str) > 0) {
4277 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4278 } else {
4279 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4280 }
4281}
4282
4283static void test_dump(skiatest::Reporter* reporter) {
4284 SkPath p;
Mike Reed7d34dc72019-11-26 12:17:17 -05004285 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kWinding);\n");
4286 compare_dump(reporter, p, true, false, "path.setFillType(SkPathFillType::kWinding);\n");
Yuqian Li3154a532017-09-06 13:33:30 -04004287 p.moveTo(1, 2);
4288 p.lineTo(3, 4);
Mike Reed7d34dc72019-11-26 12:17:17 -05004289 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004290 "path.moveTo(1, 2);\n"
4291 "path.lineTo(3, 4);\n");
Mike Reed7d34dc72019-11-26 12:17:17 -05004292 compare_dump(reporter, p, true, false, "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004293 "path.moveTo(1, 2);\n"
4294 "path.lineTo(3, 4);\n"
4295 "path.lineTo(1, 2);\n"
4296 "path.close();\n");
4297 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004298 p.setFillType(SkPathFillType::kEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004299 p.moveTo(1, 2);
4300 p.quadTo(3, 4, 5, 6);
Mike Reed7d34dc72019-11-26 12:17:17 -05004301 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kEvenOdd);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004302 "path.moveTo(1, 2);\n"
4303 "path.quadTo(3, 4, 5, 6);\n");
4304 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004305 p.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004306 p.moveTo(1, 2);
4307 p.conicTo(3, 4, 5, 6, 0.5f);
Mike Reed7d34dc72019-11-26 12:17:17 -05004308 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kInverseWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004309 "path.moveTo(1, 2);\n"
4310 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4311 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004312 p.setFillType(SkPathFillType::kInverseEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004313 p.moveTo(1, 2);
4314 p.cubicTo(3, 4, 5, 6, 7, 8);
Mike Reed7d34dc72019-11-26 12:17:17 -05004315 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kInverseEvenOdd);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004316 "path.moveTo(1, 2);\n"
4317 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4318 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004319 p.setFillType(SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004320 p.moveTo(1, 2);
4321 p.lineTo(3, 4);
4322 compare_dump(reporter, p, false, true,
Mike Reed7d34dc72019-11-26 12:17:17 -05004323 "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004324 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4325 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4326 p.reset();
4327 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4328 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Mike Reed7d34dc72019-11-26 12:17:17 -05004329 compare_dump(reporter, p, false, false, "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004330 "path.moveTo(1, 2);\n"
4331 "path.lineTo(3, 4);\n");
4332}
4333
4334namespace {
4335
4336class ChangeListener : public SkPathRef::GenIDChangeListener {
4337public:
4338 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4339 ~ChangeListener() override {}
4340 void onChange() override {
4341 *fChanged = true;
4342 }
4343private:
4344 bool* fChanged;
4345};
4346
4347}
4348
4349class PathTest_Private {
4350public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004351 static size_t GetFreeSpace(const SkPath& path) {
4352 return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
4353 }
4354
Yuqian Li3154a532017-09-06 13:33:30 -04004355 static void TestPathTo(skiatest::Reporter* reporter) {
4356 SkPath p, q;
4357 p.lineTo(4, 4);
4358 p.reversePathTo(q);
4359 check_path_is_line(reporter, &p, 4, 4);
4360 q.moveTo(-4, -4);
4361 p.reversePathTo(q);
4362 check_path_is_line(reporter, &p, 4, 4);
4363 q.lineTo(7, 8);
4364 q.conicTo(8, 7, 6, 5, 0.5f);
4365 q.quadTo(6, 7, 8, 6);
4366 q.cubicTo(5, 6, 7, 8, 7, 5);
4367 q.close();
4368 p.reversePathTo(q);
4369 SkRect reverseExpected = {-4, -4, 8, 8};
4370 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4371 }
4372
4373 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4374 SkPath p;
4375
4376 bool changed = false;
4377 p.moveTo(0, 0);
4378
4379 // Check that listener is notified on moveTo().
4380
Chris Daltonafa11582018-06-08 12:00:44 -06004381 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004382 REPORTER_ASSERT(reporter, !changed);
4383 p.moveTo(10, 0);
4384 REPORTER_ASSERT(reporter, changed);
4385
4386 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004387 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004388 REPORTER_ASSERT(reporter, !changed);
4389 p.lineTo(20, 0);
4390 REPORTER_ASSERT(reporter, changed);
4391
4392 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004393 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004394 REPORTER_ASSERT(reporter, !changed);
4395 p.reset();
4396 REPORTER_ASSERT(reporter, changed);
4397
4398 p.moveTo(0, 0);
4399
4400 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004401 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004402 REPORTER_ASSERT(reporter, !changed);
4403 p.rewind();
4404 REPORTER_ASSERT(reporter, changed);
4405
Brian Osmandbfcd922019-03-07 16:50:30 -05004406 // Check that listener is notified on transform().
4407 {
4408 SkPath q;
4409 q.moveTo(10, 10);
4410 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4411 REPORTER_ASSERT(reporter, !changed);
4412 SkMatrix matrix;
4413 matrix.setScale(2, 2);
4414 p.transform(matrix, &q);
4415 REPORTER_ASSERT(reporter, changed);
4416 }
4417
Yuqian Li3154a532017-09-06 13:33:30 -04004418 // Check that listener is notified when pathref is deleted.
4419 {
4420 SkPath q;
4421 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004422 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004423 REPORTER_ASSERT(reporter, !changed);
4424 }
4425 // q went out of scope.
4426 REPORTER_ASSERT(reporter, changed);
4427 }
4428};
4429
4430static void test_crbug_629455(skiatest::Reporter* reporter) {
4431 SkPath path;
4432 path.moveTo(0, 0);
4433 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4434 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4435 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4436// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4437 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004438 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004439}
4440
4441static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4442 SkPath path;
4443 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4444 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4445 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4446 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4447 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4448 path.close();
4449
4450 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4451 SkPaint paint;
4452 paint.setAntiAlias(true);
4453 surface->getCanvas()->clipPath(path, true);
4454 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4455}
4456
4457static void test_path_crbugskia6003() {
4458 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4459 SkCanvas* canvas = surface->getCanvas();
4460 SkPaint paint;
4461 paint.setAntiAlias(true);
4462 SkPath path;
4463 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4464 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4465 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4466 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4467 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4468 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4469 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4470 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4471 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4472 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4473 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4474 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4475 path.close();
4476 canvas->clipPath(path, true);
4477 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4478}
4479
4480static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4481 SkPath path;
4482 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4483 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4484 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4485 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4486 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004487 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004488}
4489
Yuqian Li3154a532017-09-06 13:33:30 -04004490static void test_skbug_6947() {
4491 SkPath path;
4492 SkPoint points[] =
4493 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4494 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4495 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4496 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4497 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4498 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4499 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4500 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4501 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4502 {125.119476f, 1.50011659f}};
4503 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4504 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4505 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4506 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4507 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4508 int pointIndex = 0;
4509 for(auto verb : verbs) {
4510 switch (verb) {
4511 case kMove:
4512 path.moveTo(points[pointIndex++]);
4513 break;
4514 case kLine:
4515 path.lineTo(points[pointIndex++]);
4516 break;
4517 case kClose:
4518 default:
4519 path.close();
4520 break;
4521 }
4522 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004523 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004524}
Yuqian Lia81b6262017-09-06 17:10:05 -04004525
4526static void test_skbug_7015() {
4527 SkPath path;
Mike Reed7d34dc72019-11-26 12:17:17 -05004528 path.setFillType(SkPathFillType::kWinding);
Yuqian Lia81b6262017-09-06 17:10:05 -04004529 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4530 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4531 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4532 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4533 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4534 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4535 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004536 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004537}
4538
Yuqian Lic5e4e742017-09-18 14:38:43 -04004539static void test_skbug_7051() {
4540 SkPath path;
4541 path.moveTo(10, 10);
4542 path.cubicTo(10, 20, 10, 30, 30, 30);
4543 path.lineTo(50, 20);
4544 path.lineTo(50, 10);
4545 path.close();
4546 test_draw_AA_path(100, 100, path);
4547}
4548
Ben Wagner2e9f7722019-08-14 15:45:37 -04004549static void test_skbug_7435() {
4550 SkPaint paint;
4551 SkPath path;
Mike Reed7d34dc72019-11-26 12:17:17 -05004552 path.setFillType(SkPathFillType::kWinding);
Ben Wagner2e9f7722019-08-14 15:45:37 -04004553 path.moveTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4554 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4555 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4556 path.lineTo(SkBits2Float(0x7ef348e9), SkBits2Float(0xfef403c6)); // 1.6169e+38f, -1.62176e+38f
4557 path.lineTo(SkBits2Float(0x7ef74c4e), SkBits2Float(0xfef803cb)); // 1.64358e+38f, -1.64834e+38f
4558 path.conicTo(SkBits2Float(0x7ef74f23), SkBits2Float(0xfef8069e), SkBits2Float(0x7ef751f6), SkBits2Float(0xfef803c9), SkBits2Float(0x3f3504f3)); // 1.64365e+38f, -1.64841e+38f, 1.64372e+38f, -1.64834e+38f, 0.707107f
4559 path.conicTo(SkBits2Float(0x7ef754c8), SkBits2Float(0xfef800f5), SkBits2Float(0x7ef751f5), SkBits2Float(0xfef7fe22), SkBits2Float(0x3f353472)); // 1.6438e+38f, -1.64827e+38f, 1.64372e+38f, -1.64819e+38f, 0.707832f
4560 path.lineTo(SkBits2Float(0x7edb57a9), SkBits2Float(0xfedbfe06)); // 1.45778e+38f, -1.4621e+38f
4561 path.lineTo(SkBits2Float(0x7e875976), SkBits2Float(0xfe87fdb3)); // 8.99551e+37f, -9.03815e+37f
4562 path.lineTo(SkBits2Float(0x7ded5c2b), SkBits2Float(0xfdeff59e)); // 3.94382e+37f, -3.98701e+37f
4563 path.lineTo(SkBits2Float(0x7d7a78a7), SkBits2Float(0xfd7fda0f)); // 2.08083e+37f, -2.12553e+37f
4564 path.lineTo(SkBits2Float(0x7d7a6403), SkBits2Float(0xfd7fe461)); // 2.08016e+37f, -2.12587e+37f
4565 path.conicTo(SkBits2Float(0x7d7a4764), SkBits2Float(0xfd7ff2b0), SkBits2Float(0x7d7a55b4), SkBits2Float(0xfd8007a8), SkBits2Float(0x3f3504f3)); // 2.07924e+37f, -2.12633e+37f, 2.0797e+37f, -2.12726e+37f, 0.707107f
4566 path.conicTo(SkBits2Float(0x7d7a5803), SkBits2Float(0xfd8009f7), SkBits2Float(0x7d7a5ba9), SkBits2Float(0xfd800bcc), SkBits2Float(0x3f7cba66)); // 2.07977e+37f, -2.12741e+37f, 2.07989e+37f, -2.12753e+37f, 0.987219f
4567 path.lineTo(SkBits2Float(0x7d8d2067), SkBits2Float(0xfd900bdb)); // 2.34487e+37f, -2.39338e+37f
4568 path.lineTo(SkBits2Float(0x7ddd137a), SkBits2Float(0xfde00c2d)); // 3.67326e+37f, -3.72263e+37f
4569 path.lineTo(SkBits2Float(0x7ddd2a1b), SkBits2Float(0xfddff58e)); // 3.67473e+37f, -3.72116e+37f
4570 path.lineTo(SkBits2Float(0x7c694ae5), SkBits2Float(0xfc7fa67c)); // 4.8453e+36f, -5.30965e+36f
4571 path.lineTo(SkBits2Float(0xfc164a8b), SkBits2Float(0x7c005af5)); // -3.12143e+36f, 2.66584e+36f
4572 path.lineTo(SkBits2Float(0xfc8ae983), SkBits2Float(0x7c802da7)); // -5.77019e+36f, 5.32432e+36f
4573 path.lineTo(SkBits2Float(0xfc8b16d9), SkBits2Float(0x7c80007b)); // -5.77754e+36f, 5.31699e+36f
4574 path.lineTo(SkBits2Float(0xfc8b029c), SkBits2Float(0x7c7f8788)); // -5.77426e+36f, 5.30714e+36f
4575 path.lineTo(SkBits2Float(0xfc8b0290), SkBits2Float(0x7c7f8790)); // -5.77425e+36f, 5.30714e+36f
4576 path.lineTo(SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.77753e+36f, 5.31699e+36f
4577 path.lineTo(SkBits2Float(0xfc8b4409), SkBits2Float(0x7c7fa672)); // -5.78487e+36f, 5.30965e+36f
4578 path.lineTo(SkBits2Float(0x7d7aa2ba), SkBits2Float(0xfd800bd1)); // 2.0822e+37f, -2.12753e+37f
4579 path.lineTo(SkBits2Float(0x7e8757ee), SkBits2Float(0xfe88035b)); // 8.99512e+37f, -9.03962e+37f
4580 path.lineTo(SkBits2Float(0x7ef7552d), SkBits2Float(0xfef803ca)); // 1.64381e+38f, -1.64834e+38f
4581 path.lineTo(SkBits2Float(0x7f0fa653), SkBits2Float(0xff1001f9)); // 1.90943e+38f, -1.91419e+38f
4582 path.lineTo(SkBits2Float(0x7f0fa926), SkBits2Float(0xff0fff24)); // 1.90958e+38f, -1.91404e+38f
4583 path.lineTo(SkBits2Float(0x7f0da75c), SkBits2Float(0xff0dff22)); // 1.8829e+38f, -1.88746e+38f
4584 path.lineTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4585 path.close();
4586 path.moveTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4587 path.lineTo(SkBits2Float(0x7f0da48a), SkBits2Float(0xff0e01f8)); // 1.88275e+38f, -1.88761e+38f
4588 path.lineTo(SkBits2Float(0x7f0fa654), SkBits2Float(0xff1001fa)); // 1.90943e+38f, -1.91419e+38f
4589 path.lineTo(SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90951e+38f, -1.91412e+38f
4590 path.lineTo(SkBits2Float(0x7f0fa927), SkBits2Float(0xff0fff25)); // 1.90958e+38f, -1.91404e+38f
4591 path.lineTo(SkBits2Float(0x7ef75ad5), SkBits2Float(0xfef7fe22)); // 1.64395e+38f, -1.64819e+38f
4592 path.lineTo(SkBits2Float(0x7e875d96), SkBits2Float(0xfe87fdb3)); // 8.99659e+37f, -9.03815e+37f
4593 path.lineTo(SkBits2Float(0x7d7acff6), SkBits2Float(0xfd7fea5b)); // 2.08367e+37f, -2.12606e+37f
4594 path.lineTo(SkBits2Float(0xfc8b0588), SkBits2Float(0x7c8049b7)); // -5.77473e+36f, 5.32887e+36f
4595 path.lineTo(SkBits2Float(0xfc8b2b16), SkBits2Float(0x7c803d32)); // -5.78083e+36f, 5.32684e+36f
4596 path.conicTo(SkBits2Float(0xfc8b395c), SkBits2Float(0x7c803870), SkBits2Float(0xfc8b4405), SkBits2Float(0x7c802dd1), SkBits2Float(0x3f79349d)); // -5.78314e+36f, 5.32607e+36f, -5.78487e+36f, 5.32435e+36f, 0.973459f
4597 path.conicTo(SkBits2Float(0xfc8b715b), SkBits2Float(0x7c8000a5), SkBits2Float(0xfc8b442f), SkBits2Float(0x7c7fa69e), SkBits2Float(0x3f3504f3)); // -5.79223e+36f, 5.31702e+36f, -5.7849e+36f, 5.30966e+36f, 0.707107f
4598 path.lineTo(SkBits2Float(0xfc16ffaa), SkBits2Float(0x7bff4c12)); // -3.13612e+36f, 2.65116e+36f
4599 path.lineTo(SkBits2Float(0x7c6895e0), SkBits2Float(0xfc802dc0)); // 4.83061e+36f, -5.32434e+36f
4600 path.lineTo(SkBits2Float(0x7ddd137b), SkBits2Float(0xfde00c2e)); // 3.67326e+37f, -3.72263e+37f
4601 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4602 path.lineTo(SkBits2Float(0x7ddd2a1c), SkBits2Float(0xfddff58f)); // 3.67473e+37f, -3.72116e+37f
4603 path.lineTo(SkBits2Float(0x7d8d3711), SkBits2Float(0xfd8ff543)); // 2.34634e+37f, -2.39191e+37f
4604 path.lineTo(SkBits2Float(0x7d7a88fe), SkBits2Float(0xfd7fea69)); // 2.08136e+37f, -2.12606e+37f
4605 path.lineTo(SkBits2Float(0x7d7a7254), SkBits2Float(0xfd800080)); // 2.08063e+37f, -2.1268e+37f
4606 path.lineTo(SkBits2Float(0x7d7a80a4), SkBits2Float(0xfd800ed0)); // 2.08109e+37f, -2.12773e+37f
4607 path.lineTo(SkBits2Float(0x7d7a80a8), SkBits2Float(0xfd800ecf)); // 2.08109e+37f, -2.12773e+37f
4608 path.lineTo(SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08063e+37f, -2.1268e+37f
4609 path.lineTo(SkBits2Float(0x7d7a5bb9), SkBits2Float(0xfd800bd0)); // 2.0799e+37f, -2.12753e+37f
4610 path.lineTo(SkBits2Float(0x7ded458b), SkBits2Float(0xfdf00c3e)); // 3.94235e+37f, -3.98848e+37f
4611 path.lineTo(SkBits2Float(0x7e8753ce), SkBits2Float(0xfe88035b)); // 8.99405e+37f, -9.03962e+37f
4612 path.lineTo(SkBits2Float(0x7edb5201), SkBits2Float(0xfedc03ae)); // 1.45763e+38f, -1.46225e+38f
4613 path.lineTo(SkBits2Float(0x7ef74c4d), SkBits2Float(0xfef803ca)); // 1.64358e+38f, -1.64834e+38f
4614 path.lineTo(SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64365e+38f, -1.64827e+38f
4615 path.lineTo(SkBits2Float(0x7ef751f4), SkBits2Float(0xfef7fe21)); // 1.64372e+38f, -1.64819e+38f
4616 path.lineTo(SkBits2Float(0x7ef34e91), SkBits2Float(0xfef3fe1e)); // 1.61705e+38f, -1.62161e+38f
4617 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4618 path.lineTo(SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.48397e+38f, -1.48876e+38f
4619 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4620 path.lineTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4621 path.close();
4622 path.moveTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4623 path.lineTo(SkBits2Float(0xfd6597e5), SkBits2Float(0x7d60177f)); // -1.90739e+37f, 1.86168e+37f
4624 path.lineTo(SkBits2Float(0xfde2cea1), SkBits2Float(0x7de00c2e)); // -3.76848e+37f, 3.72263e+37f
4625 path.lineTo(SkBits2Float(0xfe316511), SkBits2Float(0x7e300657)); // -5.89495e+37f, 5.84943e+37f
4626 path.lineTo(SkBits2Float(0xfe415da1), SkBits2Float(0x7e400666)); // -6.42568e+37f, 6.38112e+37f
4627 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4000be)); // -6.42641e+37f, 6.38039e+37f
4628 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4629 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4630 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e3ff8be)); // -6.42591e+37f, 6.37935e+37f
4631 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e3ff8be)); // -6.42544e+37f, 6.37935e+37f
4632 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4633 path.lineTo(SkBits2Float(0xfe416171), SkBits2Float(0x7e3ffb16)); // -6.42617e+37f, 6.37966e+37f
4634 path.lineTo(SkBits2Float(0xfe016131), SkBits2Float(0x7dfff5ae)); // -4.29938e+37f, 4.25286e+37f
4635 path.lineTo(SkBits2Float(0xfe0155e2), SkBits2Float(0x7e000628)); // -4.29791e+37f, 4.25433e+37f
4636 path.lineTo(SkBits2Float(0xfe0958ea), SkBits2Float(0x7e080630)); // -4.56415e+37f, 4.52018e+37f
4637 path.lineTo(SkBits2Float(0xfe115c92), SkBits2Float(0x7e100638)); // -4.83047e+37f, 4.78603e+37f
4638 path.conicTo(SkBits2Float(0xfe11623c), SkBits2Float(0x7e100bdf), SkBits2Float(0xfe1167e2), SkBits2Float(0x7e100636), SkBits2Float(0x3f3504f3)); // -4.8312e+37f, 4.78676e+37f, -4.83194e+37f, 4.78603e+37f, 0.707107f
4639 path.conicTo(SkBits2Float(0xfe116d87), SkBits2Float(0x7e10008e), SkBits2Float(0xfe1167e2), SkBits2Float(0x7e0ffae8), SkBits2Float(0x3f35240a)); // -4.83267e+37f, 4.78529e+37f, -4.83194e+37f, 4.78456e+37f, 0.707581f
4640 path.lineTo(SkBits2Float(0xfe016b92), SkBits2Float(0x7dfff5af)); // -4.30072e+37f, 4.25286e+37f
4641 path.lineTo(SkBits2Float(0xfdc2d963), SkBits2Float(0x7dbff56e)); // -3.23749e+37f, 3.18946e+37f
4642 path.lineTo(SkBits2Float(0xfd65ae25), SkBits2Float(0x7d5fea3d)); // -1.90811e+37f, 1.86021e+37f
4643 path.lineTo(SkBits2Float(0xfab448de), SkBits2Float(0xf7b50a19)); // -4.68046e+35f, -7.34383e+33f
4644 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4645 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x7800007f)); // -4.60703e+35f, 1.03848e+34f
4646 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x7800007f)); // -4.67194e+35f, 1.03848e+34f
4647 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4648 path.lineTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4649 path.close();
4650 path.moveTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4651 path.lineTo(SkBits2Float(0xfaa7cc52), SkBits2Float(0xf800007f)); // -4.35629e+35f, -1.03848e+34f
4652 path.lineTo(SkBits2Float(0xfd6580e5), SkBits2Float(0x7d60177f)); // -1.90664e+37f, 1.86168e+37f
4653 path.lineTo(SkBits2Float(0xfdc2c2c1), SkBits2Float(0x7dc00c0f)); // -3.23602e+37f, 3.19093e+37f
4654 path.lineTo(SkBits2Float(0xfe016040), SkBits2Float(0x7e000626)); // -4.29925e+37f, 4.25433e+37f
4655 path.lineTo(SkBits2Float(0xfe115c90), SkBits2Float(0x7e100636)); // -4.83047e+37f, 4.78603e+37f
4656 path.lineTo(SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.8312e+37f, 4.78529e+37f
4657 path.lineTo(SkBits2Float(0xfe1167e0), SkBits2Float(0x7e0ffae6)); // -4.83194e+37f, 4.78456e+37f
4658 path.lineTo(SkBits2Float(0xfe096438), SkBits2Float(0x7e07fade)); // -4.56562e+37f, 4.51871e+37f
4659 path.lineTo(SkBits2Float(0xfe016130), SkBits2Float(0x7dfff5ac)); // -4.29938e+37f, 4.25286e+37f
4660 path.lineTo(SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29864e+37f, 4.25359e+37f
4661 path.lineTo(SkBits2Float(0xfe0155e1), SkBits2Float(0x7e000627)); // -4.29791e+37f, 4.25433e+37f
4662 path.lineTo(SkBits2Float(0xfe415879), SkBits2Float(0x7e4008bf)); // -6.42501e+37f, 6.38143e+37f
4663 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e4008bf)); // -6.42591e+37f, 6.38143e+37f
4664 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4665 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4666 path.conicTo(SkBits2Float(0xfe416699), SkBits2Float(0x7e4008bf), SkBits2Float(0xfe4168f1), SkBits2Float(0x7e400668), SkBits2Float(0x3f6c8ed9)); // -6.42684e+37f, 6.38143e+37f, -6.42715e+37f, 6.38113e+37f, 0.924055f
4667 path.conicTo(SkBits2Float(0xfe416e9a), SkBits2Float(0x7e4000c2), SkBits2Float(0xfe4168f3), SkBits2Float(0x7e3ffb17), SkBits2Float(0x3f3504f3)); // -6.42788e+37f, 6.38039e+37f, -6.42715e+37f, 6.37966e+37f, 0.707107f
4668 path.lineTo(SkBits2Float(0xfe317061), SkBits2Float(0x7e2ffb07)); // -5.89642e+37f, 5.84796e+37f
4669 path.lineTo(SkBits2Float(0xfde2e542), SkBits2Float(0x7ddff58e)); // -3.76995e+37f, 3.72116e+37f
4670 path.lineTo(SkBits2Float(0xfd65c525), SkBits2Float(0x7d5fea3d)); // -1.90886e+37f, 1.86021e+37f
4671 path.lineTo(SkBits2Float(0xfab6c8db), SkBits2Float(0xf7b50b4f)); // -4.74536e+35f, -7.34402e+33f
4672 path.lineTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4673 path.close();
4674 path.moveTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4675 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4676 path.quadTo(SkBits2Float(0xfd0593a5), SkBits2Float(0x7d00007f), SkBits2Float(0xfd659785), SkBits2Float(0x7d6000de)); // -1.10971e+37f, 1.0634e+37f, -1.90737e+37f, 1.86095e+37f
4677 path.quadTo(SkBits2Float(0xfda2cdf2), SkBits2Float(0x7da0009f), SkBits2Float(0xfdc2ce12), SkBits2Float(0x7dc000be)); // -2.70505e+37f, 2.6585e+37f, -3.23675e+37f, 3.1902e+37f
4678 path.quadTo(SkBits2Float(0xfde2ce31), SkBits2Float(0x7de000de), SkBits2Float(0xfe0165e9), SkBits2Float(0x7e00007f)); // -3.76845e+37f, 3.72189e+37f, -4.29999e+37f, 4.25359e+37f
4679 path.quadTo(SkBits2Float(0xfe1164b9), SkBits2Float(0x7e10008f), SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.83153e+37f, 4.78529e+37f, -4.8312e+37f, 4.78529e+37f
4680 path.quadTo(SkBits2Float(0xfe116039), SkBits2Float(0x7e10008f), SkBits2Float(0xfe095e91), SkBits2Float(0x7e080087)); // -4.83094e+37f, 4.78529e+37f, -4.56488e+37f, 4.51944e+37f
4681 path.quadTo(SkBits2Float(0xfe015d09), SkBits2Float(0x7e00007f), SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29884e+37f, 4.25359e+37f, -4.29864e+37f, 4.25359e+37f
4682 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4683 path.quadTo(SkBits2Float(0xfe415da9), SkBits2Float(0x7e4000be), SkBits2Float(0xfe415f69), SkBits2Float(0x7e4000be)); // -6.42568e+37f, 6.38039e+37f, -6.42591e+37f, 6.38039e+37f
4684 path.quadTo(SkBits2Float(0xfe416149), SkBits2Float(0x7e4000be), SkBits2Float(0xfe416349), SkBits2Float(0x7e4000be)); // -6.42615e+37f, 6.38039e+37f, -6.42641e+37f, 6.38039e+37f
4685 path.quadTo(SkBits2Float(0xfe416849), SkBits2Float(0x7e4000be), SkBits2Float(0xfe316ab9), SkBits2Float(0x7e3000af)); // -6.42706e+37f, 6.38039e+37f, -5.89569e+37f, 5.84869e+37f
4686 path.quadTo(SkBits2Float(0xfe216d29), SkBits2Float(0x7e20009f), SkBits2Float(0xfde2d9f2), SkBits2Float(0x7de000de)); // -5.36431e+37f, 5.31699e+37f, -3.76921e+37f, 3.72189e+37f
4687 path.quadTo(SkBits2Float(0xfda2d9b2), SkBits2Float(0x7da0009f), SkBits2Float(0xfd65ae85), SkBits2Float(0x7d6000de)); // -2.70582e+37f, 2.6585e+37f, -1.90812e+37f, 1.86095e+37f
4688 path.quadTo(SkBits2Float(0xfd05a9a6), SkBits2Float(0x7d00007f), SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -1.11043e+37f, 1.0634e+37f, -4.67194e+35f, 200
4689 path.close();
4690 path.moveTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4691 path.quadTo(SkBits2Float(0x7f0ba519), SkBits2Float(0xff0c008b), SkBits2Float(0x7f0da5f3), SkBits2Float(0xff0e008d)); // 1.8562e+38f, -1.86095e+38f, 1.88283e+38f, -1.88753e+38f
4692 path.quadTo(SkBits2Float(0x7f0fa6d5), SkBits2Float(0xff10008f), SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90946e+38f, -1.91412e+38f, 1.90951e+38f, -1.91412e+38f
4693 path.quadTo(SkBits2Float(0x7f0faa7d), SkBits2Float(0xff10008f), SkBits2Float(0x7ef75801), SkBits2Float(0xfef800f6)); // 1.90965e+38f, -1.91412e+38f, 1.64388e+38f, -1.64827e+38f
4694 path.quadTo(SkBits2Float(0x7ecf5b09), SkBits2Float(0xfed000ce), SkBits2Float(0x7e875ac2), SkBits2Float(0xfe880087)); // 1.37811e+38f, -1.38242e+38f, 8.99585e+37f, -9.03889e+37f
4695 path.quadTo(SkBits2Float(0x7e0eb505), SkBits2Float(0xfe10008f), SkBits2Float(0x7d7ab958), SkBits2Float(0xfd80007f)); // 4.74226e+37f, -4.78529e+37f, 2.08293e+37f, -2.1268e+37f
4696 path.quadTo(SkBits2Float(0xfc8ac1cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.76374e+36f, 5.31699e+36f, -5.77753e+36f, 5.31699e+36f
4697 path.quadTo(SkBits2Float(0xfc8b36cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc16a51a), SkBits2Float(0x7c00007f)); // -5.78273e+36f, 5.31699e+36f, -3.12877e+36f, 2.6585e+36f
4698 path.quadTo(SkBits2Float(0xfab6e4de), SkBits2Float(0x43480000), SkBits2Float(0x7c68f062), SkBits2Float(0xfc80007f)); // -4.7482e+35f, 200, 4.83795e+36f, -5.31699e+36f
4699 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4700 path.quadTo(SkBits2Float(0x7d9d254b), SkBits2Float(0xfda0009f), SkBits2Float(0x7d8d2bbc), SkBits2Float(0xfd90008f)); // 2.61103e+37f, -2.6585e+37f, 2.3456e+37f, -2.39265e+37f
4701 path.quadTo(SkBits2Float(0x7d7a64d8), SkBits2Float(0xfd80007f), SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08019e+37f, -2.1268e+37f, 2.08063e+37f, -2.1268e+37f
4702 path.quadTo(SkBits2Float(0x7d7a9058), SkBits2Float(0xfd80007f), SkBits2Float(0x7ded50db), SkBits2Float(0xfdf000ee)); // 2.0816e+37f, -2.1268e+37f, 3.94309e+37f, -3.98774e+37f
4703 path.quadTo(SkBits2Float(0x7e2eace5), SkBits2Float(0xfe3000af), SkBits2Float(0x7e8756a2), SkBits2Float(0xfe880087)); // 5.80458e+37f, -5.84869e+37f, 8.99478e+37f, -9.03889e+37f
4704 path.quadTo(SkBits2Float(0x7ebf56d9), SkBits2Float(0xfec000be), SkBits2Float(0x7edb54d5), SkBits2Float(0xfedc00da)); // 1.27167e+38f, -1.27608e+38f, 1.45771e+38f, -1.46217e+38f
4705 path.quadTo(SkBits2Float(0x7ef752e1), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64375e+38f, -1.64827e+38f, 1.64365e+38f, -1.64827e+38f
4706 path.quadTo(SkBits2Float(0x7ef74d71), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef34bbd), SkBits2Float(0xfef400f2)); // 1.64361e+38f, -1.64827e+38f, 1.61698e+38f, -1.62168e+38f
4707 path.quadTo(SkBits2Float(0x7eef4a19), SkBits2Float(0xfef000ee), SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.59035e+38f, -1.5951e+38f, 1.48397e+38f, -1.48876e+38f
4708 path.lineTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4709 path.close();
4710 SkSurface::MakeRasterN32Premul(250, 250, nullptr)->getCanvas()->drawPath(path, paint);
4711}
4712
Yuqian Li3154a532017-09-06 13:33:30 -04004713static void test_interp(skiatest::Reporter* reporter) {
4714 SkPath p1, p2, out;
4715 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4716 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4717 REPORTER_ASSERT(reporter, p1 == out);
4718 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4719 REPORTER_ASSERT(reporter, p1 == out);
4720 p1.moveTo(0, 2);
4721 p1.lineTo(0, 4);
4722 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4723 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4724 p2.moveTo(6, 0);
4725 p2.lineTo(8, 0);
4726 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4727 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4728 REPORTER_ASSERT(reporter, p2 == out);
4729 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4730 REPORTER_ASSERT(reporter, p1 == out);
4731 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4732 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4733 p1.reset();
4734 p1.moveTo(4, 4);
4735 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4736 p2.reset();
4737 p2.moveTo(4, 2);
4738 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4739 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4740 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4741 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4742 p2.reset();
4743 p2.moveTo(4, 2);
4744 p2.conicTo(6, 3, 6, 5, 1);
4745 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4746 p2.reset();
4747 p2.moveTo(4, 4);
4748 p2.conicTo(5, 4, 5, 5, 0.5f);
4749 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4750}
4751
4752DEF_TEST(PathInterp, reporter) {
4753 test_interp(reporter);
4754}
4755
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004756#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004757DEF_TEST(PathBigCubic, reporter) {
4758 SkPath path;
4759 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4760 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4761 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), SkBits2Float(0x00000100), SkBits2Float(0x00000000), SkBits2Float(0x00000100), SkBits2Float(0x00000000)); // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4762 path.moveTo(0, 512);
4763
4764 // this call should not assert
4765 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4766}
4767
4768DEF_TEST(PathContains, reporter) {
4769 test_contains(reporter);
4770}
4771
4772DEF_TEST(Paths, reporter) {
4773 test_fuzz_crbug_647922();
4774 test_fuzz_crbug_643933();
4775 test_sect_with_horizontal_needs_pinning();
4776 test_crbug_629455(reporter);
4777 test_fuzz_crbug_627414(reporter);
4778 test_path_crbug364224();
4779 test_fuzz_crbug_662952(reporter);
4780 test_fuzz_crbug_662730(reporter);
4781 test_fuzz_crbug_662780();
4782 test_mask_overflow();
4783 test_path_crbugskia6003();
4784 test_fuzz_crbug_668907();
Yuqian Li3154a532017-09-06 13:33:30 -04004785 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004786 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004787 test_skbug_7051();
Ben Wagner2e9f7722019-08-14 15:45:37 -04004788 test_skbug_7435();
Yuqian Li3154a532017-09-06 13:33:30 -04004789
4790 SkSize::Make(3, 4);
4791
4792 SkPath p, empty;
4793 SkRect bounds, bounds2;
4794 test_empty(reporter, p);
4795
4796 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4797
4798 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4799 SkPath& self = p;
4800 p = self;
4801
4802 // this triggers a code path in SkPath::swap which is otherwise unexercised
4803 p.swap(self);
4804
Mike Reed92b33352019-08-24 19:39:13 -04004805 bounds.setLTRB(0, 0, SK_Scalar1, SK_Scalar1);
Yuqian Li3154a532017-09-06 13:33:30 -04004806
4807 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4808 check_convex_bounds(reporter, p, bounds);
4809 // we have quads or cubics
4810 REPORTER_ASSERT(reporter,
4811 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4812 REPORTER_ASSERT(reporter, !p.isEmpty());
4813
4814 p.reset();
4815 test_empty(reporter, p);
4816
4817 p.addOval(bounds);
4818 check_convex_bounds(reporter, p, bounds);
4819 REPORTER_ASSERT(reporter, !p.isEmpty());
4820
4821 p.rewind();
4822 test_empty(reporter, p);
4823
4824 p.addRect(bounds);
4825 check_convex_bounds(reporter, p, bounds);
4826 // we have only lines
4827 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4828 REPORTER_ASSERT(reporter, !p.isEmpty());
4829
4830 REPORTER_ASSERT(reporter, p != empty);
4831 REPORTER_ASSERT(reporter, !(p == empty));
4832
4833 // do getPoints and getVerbs return the right result
4834 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4835 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4836 SkPoint pts[4];
4837 int count = p.getPoints(pts, 4);
4838 REPORTER_ASSERT(reporter, count == 4);
4839 uint8_t verbs[6];
4840 verbs[5] = 0xff;
4841 p.getVerbs(verbs, 5);
4842 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4843 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4844 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4845 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4846 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4847 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
Mike Reed92b33352019-08-24 19:39:13 -04004848 bounds2.setBounds(pts, 4);
Yuqian Li3154a532017-09-06 13:33:30 -04004849 REPORTER_ASSERT(reporter, bounds == bounds2);
4850
4851 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4852 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4853 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4854
4855 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4856 bounds2.setEmpty();
4857 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4858 REPORTER_ASSERT(reporter, bounds == bounds2);
4859
4860 // now force p to not be a rect
Mike Reed92b33352019-08-24 19:39:13 -04004861 bounds.setWH(SK_Scalar1/2, SK_Scalar1/2);
Yuqian Li3154a532017-09-06 13:33:30 -04004862 p.addRect(bounds);
4863 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4864
4865 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4866 // path has a trailing moveTo. Please see crbug.com\445368)
4867 {
4868 SkRect r;
4869 p.reset();
4870 p.addRect(bounds);
4871 REPORTER_ASSERT(reporter, p.isRect(&r));
4872 REPORTER_ASSERT(reporter, r == bounds);
4873 // add a moveTo outside of our bounds
4874 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4875 REPORTER_ASSERT(reporter, p.isRect(&r));
4876 REPORTER_ASSERT(reporter, r == bounds);
4877 }
4878
4879 test_operatorEqual(reporter);
4880 test_isLine(reporter);
4881 test_isRect(reporter);
4882 test_is_simple_closed_rect(reporter);
4883 test_isNestedFillRects(reporter);
4884 test_zero_length_paths(reporter);
4885 test_direction(reporter);
4886 test_convexity(reporter);
4887 test_convexity2(reporter);
Cary Clarkc9b7c722018-12-12 14:50:23 -05004888 test_convexity_doubleback(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004889 test_conservativelyContains(reporter);
4890 test_close(reporter);
4891 test_segment_masks(reporter);
4892 test_flattening(reporter);
4893 test_transform(reporter);
4894 test_bounds(reporter);
4895 test_iter(reporter);
4896 test_raw_iter(reporter);
4897 test_circle(reporter);
4898 test_oval(reporter);
4899 test_strokerec(reporter);
4900 test_addPoly(reporter);
4901 test_isfinite(reporter);
4902 test_isfinite_after_transform(reporter);
4903 test_islastcontourclosed(reporter);
4904 test_arb_round_rect_is_convex(reporter);
4905 test_arb_zero_rad_round_rect_is_rect(reporter);
4906 test_addrect(reporter);
4907 test_addrect_isfinite(reporter);
4908 test_tricky_cubic();
4909 test_clipped_cubic();
4910 test_crbug_170666();
4911 test_crbug_493450(reporter);
4912 test_crbug_495894(reporter);
4913 test_crbug_613918();
4914 test_bad_cubic_crbug229478();
4915 test_bad_cubic_crbug234190();
4916 test_gen_id(reporter);
4917 test_path_close_issue1474(reporter);
4918 test_path_to_region(reporter);
4919 test_rrect(reporter);
4920 test_arc(reporter);
4921 test_arc_ovals(reporter);
4922 test_arcTo(reporter);
4923 test_addPath(reporter);
4924 test_addPathMode(reporter, false, false);
4925 test_addPathMode(reporter, true, false);
4926 test_addPathMode(reporter, false, true);
4927 test_addPathMode(reporter, true, true);
4928 test_extendClosedPath(reporter);
4929 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4930 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4931 test_conicTo_special_case(reporter);
4932 test_get_point(reporter);
4933 test_contains(reporter);
4934 PathTest_Private::TestPathTo(reporter);
4935 PathRefTest_Private::TestPathRef(reporter);
4936 PathTest_Private::TestPathrefListeners(reporter);
4937 test_dump(reporter);
4938 test_path_crbug389050(reporter);
4939 test_path_crbugskia2820(reporter);
4940 test_path_crbugskia5995();
4941 test_skbug_3469(reporter);
4942 test_skbug_3239(reporter);
4943 test_bounds_crbug_513799(reporter);
4944 test_fuzz_crbug_638223();
4945}
4946
4947DEF_TEST(conservatively_contains_rect, reporter) {
4948 SkPath path;
4949
4950 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4951 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4952 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4953 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4954 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4955 path.moveTo(0, 0);
4956
4957 // this guy should not assert
4958 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4959}
4960
4961///////////////////////////////////////////////////////////////////////////////////////////////////
4962
4963static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4964 for (int i = 0; i < n; ++i) {
4965 switch (verb) {
4966 case SkPath::kLine_Verb:
4967 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4968 break;
4969 case SkPath::kQuad_Verb:
4970 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4971 rand.nextF()*100, rand.nextF()*100);
4972 break;
4973 case SkPath::kConic_Verb:
4974 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4975 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4976 break;
4977 case SkPath::kCubic_Verb:
4978 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4979 rand.nextF()*100, rand.nextF()*100,
4980 rand.nextF()*100, rand.nextF()*100);
4981 break;
4982 default:
4983 SkASSERT(false);
4984 }
4985 }
4986}
4987
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004988#include "include/pathops/SkPathOps.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004989DEF_TEST(path_tight_bounds, reporter) {
4990 SkRandom rand;
4991
4992 const SkPath::Verb verbs[] = {
4993 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4994 };
4995 for (int i = 0; i < 1000; ++i) {
4996 for (int n = 1; n <= 10; n += 9) {
4997 for (SkPath::Verb verb : verbs) {
4998 SkPath path;
4999 rand_path(&path, rand, verb, n);
5000 SkRect bounds = path.getBounds();
5001 SkRect tight = path.computeTightBounds();
5002 REPORTER_ASSERT(reporter, bounds.contains(tight));
5003
5004 SkRect tight2;
5005 TightBounds(path, &tight2);
5006 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
5007 }
5008 }
5009 }
5010}
5011
5012DEF_TEST(skbug_6450, r) {
5013 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
5014 SkVector rdi[4] = {
5015 { 1.81159976e-09f, 7.58768801e-05f },
5016 { 0.000118725002f, 0.000118725002f },
5017 { 0.000118725002f, 0.000118725002f },
5018 { 0.000118725002f, 0.486297607f }
5019 };
5020 SkRRect irr;
5021 irr.setRectRadii(ri, rdi);
5022 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
5023 SkVector rdo[4] = {
5024 { 0, 0 },
5025 { 0.0103298295f, 0.185887396f },
5026 { 2.52999727e-29f, 169.001938f },
5027 { 195.262741f, 195.161255f }
5028 };
5029 SkRRect orr;
5030 orr.setRectRadii(ro, rdo);
5031 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
5032}
5033
5034DEF_TEST(PathRefSerialization, reporter) {
5035 SkPath path;
5036 const size_t numMoves = 5;
5037 const size_t numConics = 7;
5038 const size_t numPoints = numMoves + 2 * numConics;
5039 const size_t numVerbs = numMoves + numConics;
5040 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
5041 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
5042 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
5043 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
5044
5045 // Verify that path serializes/deserializes properly.
5046 sk_sp<SkData> data = path.serialize();
5047 size_t bytesWritten = data->size();
5048
5049 {
5050 SkPath readBack;
5051 REPORTER_ASSERT(reporter, readBack != path);
5052 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
5053 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
5054 REPORTER_ASSERT(reporter, readBack == path);
5055 }
5056
Yuqian Li3154a532017-09-06 13:33:30 -04005057 // One less byte (rounded down to alignment) than was written will also
5058 // fail to be deserialized.
5059 {
5060 SkPath readBack;
5061 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
5062 REPORTER_ASSERT(reporter, !bytesRead);
5063 }
5064}
Mike Klein1170a552017-09-08 15:00:25 -04005065
5066DEF_TEST(NonFinitePathIteration, reporter) {
5067 SkPath path;
5068 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
5069
5070 int verbs = 0;
5071
5072 SkPath::RawIter iter(path);
5073 SkPoint pts[4];
5074 while (iter.next(pts) != SkPath::kDone_Verb) {
5075 verbs++;
5076 }
5077
5078 REPORTER_ASSERT(reporter, verbs == 0);
5079}
Cary Clark1acd3c72017-12-08 11:37:01 -05005080
Cary Clark1acd3c72017-12-08 11:37:01 -05005081DEF_TEST(AndroidArc, reporter) {
5082 const char* tests[] = {
5083 "M50,0A50,50,0,0 1 100,50 L100,85 A15,15,0,0 1 85,100 L50,100 A50,50,0,0 1 50,0z",
5084 "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
5085 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
5086 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
5087 };
5088 for (auto test : tests) {
5089 SkPath aPath;
5090 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
5091 SkASSERT(aPath.isConvex());
5092 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
5093 SkPath scalePath = aPath;
5094 SkMatrix matrix;
5095 matrix.setScale(scale, scale);
5096 scalePath.transform(matrix);
5097 SkASSERT(scalePath.isConvex());
5098 }
5099 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
5100 SkPath scalePath = aPath;
5101 SkMatrix matrix;
5102 matrix.setScale(scale, scale);
5103 scalePath.transform(matrix);
5104 SkASSERT(scalePath.isConvex());
5105 }
5106 }
5107}
Mike Reed1fe0bcc2018-01-22 16:49:49 -05005108
5109/*
5110 * Try a range of crazy values, just to ensure that we don't assert/crash.
5111 */
5112DEF_TEST(HugeGeometry, reporter) {
5113 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
5114 auto canvas = surf->getCanvas();
5115
5116 const bool aas[] = { false, true };
5117 const SkPaint::Style styles[] = {
5118 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
5119 };
5120 const SkScalar values[] = {
5121 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
5122 SK_ScalarInfinity
5123 };
5124
5125 SkPaint paint;
5126 for (auto x : values) {
5127 SkRect r = { -x, -x, x, x };
5128 for (auto width : values) {
5129 paint.setStrokeWidth(width);
5130 for (auto aa : aas) {
5131 paint.setAntiAlias(aa);
5132 for (auto style : styles) {
5133 paint.setStyle(style);
5134 canvas->drawRect(r, paint);
5135 canvas->drawOval(r, paint);
5136 }
5137 }
5138 }
5139 }
5140
5141}
5142
Mike Reed1c6e7832018-01-30 11:29:36 -05005143// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
5144DEF_TEST(ClipPath_nonfinite, reporter) {
5145 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
5146 SkCanvas* canvas = surf->getCanvas();
5147
5148 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5149 for (bool aa : {false, true}) {
Mike Reed7d34dc72019-11-26 12:17:17 -05005150 for (auto ft : {SkPathFillType::kWinding, SkPathFillType::kInverseWinding}) {
Mike Reed1c6e7832018-01-30 11:29:36 -05005151 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
5152 for (int bits = 1; bits <= 15; ++bits) {
5153 SkPoint p0 = { 0, 0 };
5154 SkPoint p1 = { 0, 0 };
5155 if (bits & 1) p0.fX = -bad;
5156 if (bits & 2) p0.fY = -bad;
5157 if (bits & 4) p1.fX = bad;
5158 if (bits & 8) p1.fY = bad;
5159
5160 SkPath path;
5161 path.moveTo(p0);
5162 path.lineTo(p1);
5163 path.setFillType(ft);
5164 canvas->save();
5165 canvas->clipPath(path, aa);
5166 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
5167 canvas->restore();
5168 }
5169 }
5170 }
5171 }
5172 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5173}
5174
Cary Clark5c715182018-04-09 16:07:11 -04005175// skbug.com/7792
5176DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04005177 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
5178 SkPath path;
5179 for (size_t index = 0; index < count; ++index) {
5180 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
5181 }
Cary Clark31608c02018-04-12 10:29:46 -04005182 if (close) {
5183 path.close();
5184 }
Cary Clark8540e112018-04-11 14:30:27 -04005185 return path;
5186 };
5187 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
5188 SkPath path;
5189 for (size_t index = 0; index < count; ++index) {
5190 switch (verbs[index]) {
5191 case SkPath::kMove_Verb:
5192 path.moveTo(*points++);
5193 break;
5194 case SkPath::kLine_Verb:
5195 path.lineTo(*points++);
5196 break;
5197 case SkPath::kClose_Verb:
5198 path.close();
5199 break;
5200 default:
5201 SkASSERT(0);
5202 }
5203 }
5204 return path;
5205 };
Cary Clarkd4228472018-04-13 07:07:04 -04005206 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04005207 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04005208 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5209 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04005210 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04005211 SkRect compare;
Mike Reed92b33352019-08-24 19:39:13 -04005212 compare.setBounds(&points[1], SK_ARRAY_COUNT(points) - 1);
Cary Clark5c715182018-04-09 16:07:11 -04005213 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005214 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04005215 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
5216 path = makePath(points3, SK_ARRAY_COUNT(points3), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005217 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005218 // isolated from skbug.com/7792#c9
Cary Clark8540e112018-04-11 14:30:27 -04005219 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5220 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005221 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005222 compare.setBounds(&points9[1], SK_ARRAY_COUNT(points9) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005223 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005224 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04005225 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5226 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
5227 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
5228 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04005229 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005230 compare.setBounds(&points11[0], SK_ARRAY_COUNT(points11));
Cary Clark8540e112018-04-11 14:30:27 -04005231 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005232 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04005233 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
5234 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5235 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
5236 SkPath::kLine_Verb, SkPath::kClose_Verb };
5237 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
5238 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
5239 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04005240 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005241 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04005242 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5243 SkPath::kLine_Verb, SkPath::kMove_Verb };
5244 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
5245 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04005246 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005247 compare.setBounds(&points15[0], SK_ARRAY_COUNT(points15) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005248 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005249 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04005250 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
5251 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005252 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005253 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04005254 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5255 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5256 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
5257 SkPath::kLine_Verb, SkPath::kLine_Verb };
5258 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5259 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
5260 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04005261 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005262 // isolated from skbug.com/7792#c23
5263 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5264 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5265 SkPath::kLine_Verb, SkPath::kClose_Verb };
5266 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5267 {75, 150} };
5268 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04005269 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005270 compare.setBounds(&points23[0], SK_ARRAY_COUNT(points23));
Cary Clarkd4228472018-04-13 07:07:04 -04005271 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005272 // isolated from skbug.com/7792#c29
5273 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5274 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5275 SkPath::kClose_Verb };
5276 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5277 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005278 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005279 // isolated from skbug.com/7792#c31
5280 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5281 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5282 SkPath::kClose_Verb };
5283 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5284 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005285 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005286 compare.setBounds(&points31[0], 4);
Cary Clarka7651562018-04-17 09:30:14 -04005287 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005288 // isolated from skbug.com/7792#c36
5289 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5290 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5291 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5292 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005293 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005294 // isolated from skbug.com/7792#c39
5295 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005296 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005297 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5298 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005299 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005300 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005301 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5302 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5303 SkPath::kLine_Verb, SkPath::kClose_Verb };
5304 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5305 {32, 2} };
5306 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5307 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005308 compare.setBounds(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
Cary Clarkb120e922018-04-18 12:25:08 -04005309 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005310 // isolated from skbug.com/7792#c41
5311 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5312 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5313 SkPath::kClose_Verb };
5314 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5315 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5316 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005317 compare.setBounds(&points41[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005318 REPORTER_ASSERT(reporter, rect == compare);
5319 // isolated from skbug.com/7792#c53
5320 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5321 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5322 SkPath::kClose_Verb };
5323 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5324 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5325 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005326 compare.setBounds(&points53[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005327 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005328}
Mike Reedee430912018-05-23 12:12:21 -04005329
Mike Reedc3d8a482018-09-12 10:08:40 -04005330// Be sure we can safely add ourselves
5331DEF_TEST(Path_self_add, reporter) {
5332 // The possible problem is that during path.add() we may have to grow the dst buffers as
5333 // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
5334 // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
5335 // garbage.
5336 //
5337 // The test runs though verious sized src paths, since its not defined publicly what the
5338 // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
5339 // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
5340 // an ASAN error w/o the fix to SkPath::addPath().
5341 //
5342 for (int count = 0; count < 10; ++count) {
5343 SkPath path;
5344 for (int add = 0; add < count; ++add) {
5345 // just add some stuff, so we have something to copy/append in addPath()
5346 path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
5347 }
5348 path.addPath(path, 1, 2);
5349 path.addPath(path, 3, 4);
5350 }
5351}
5352
Mike Kleinc0bd9f92019-04-23 12:05:21 -05005353#include "include/core/SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005354static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5355 // draw in different ways, looking for an assert
5356
5357 {
5358 SkPath path;
5359 path.addPoly(pts, 3, false);
5360 canvas->drawPath(path, SkPaint());
5361 }
5362
5363 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5364 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5365 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5366}
5367
Mike Reedee430912018-05-23 12:12:21 -04005368DEF_TEST(triangle_onehalf, reporter) {
5369 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5370
5371 const SkPoint pts[] = {
5372 { 0.499069244f, 9.63295173f },
5373 { 0.499402374f, 7.88207579f },
5374 { 10.2363272f, 0.49999997f }
5375 };
Mike Reed50595412018-05-30 16:32:33 -04005376 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005377}
Mike Reed50595412018-05-30 16:32:33 -04005378
5379DEF_TEST(triangle_big, reporter) {
5380 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5381
5382 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5383 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5384 // be conservative, so we invoke clipping if we get in this situation.
5385 // This test was added to demonstrate the need for this conservative bounds calc.
5386 // (found by a fuzzer)
5387 const SkPoint pts[] = {
5388 { 0.327190518f, -114.945152f },
5389 { -0.5f, 1.00003874f },
5390 { 0.666425824f, 4304.26172f },
5391 };
5392 draw_triangle(surface->getCanvas(), pts);
5393}
5394
Florin Malita3d413c52018-09-11 14:01:42 -04005395static void add_verbs(SkPath* path, int count) {
5396 path->moveTo(0, 0);
5397 for (int i = 0; i < count; ++i) {
5398 switch (i & 3) {
5399 case 0: path->lineTo(10, 20); break;
5400 case 1: path->quadTo(5, 6, 7, 8); break;
5401 case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
5402 case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
5403 }
5404 }
5405}
5406
5407// Make sure when we call shrinkToFit() that we always shrink (or stay the same)
5408// and that if we call twice, we stay the same.
5409DEF_TEST(Path_shrinkToFit, reporter) {
5410 size_t max_free = 0;
5411 for (int verbs = 0; verbs < 100; ++verbs) {
5412 SkPath unique_path, shared_path;
5413 add_verbs(&unique_path, verbs);
5414 add_verbs(&shared_path, verbs);
5415
5416 const SkPath copy = shared_path;
5417 REPORTER_ASSERT(reporter, shared_path == unique_path);
5418 REPORTER_ASSERT(reporter, shared_path == copy);
5419
5420#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005421 size_t before = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005422#endif
5423 unique_path.shrinkToFit();
5424 shared_path.shrinkToFit();
5425 REPORTER_ASSERT(reporter, shared_path == unique_path);
5426 REPORTER_ASSERT(reporter, shared_path == copy);
5427
5428#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005429 size_t after = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005430 REPORTER_ASSERT(reporter, before >= after);
5431 max_free = std::max(max_free, before - after);
5432
Florin Malita0e0f1a72018-09-11 16:16:19 -04005433 size_t after2 = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005434 REPORTER_ASSERT(reporter, after == after2);
5435#endif
5436 }
5437 if (false) {
5438 SkDebugf("max_free %zu\n", max_free);
5439 }
5440}
Mike Klein9de72162018-09-24 20:30:28 -04005441
5442DEF_TEST(Path_setLastPt, r) {
5443 // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
5444 SkPath p;
5445 p.moveTo(0,0);
5446 p.moveTo(20,01);
5447 p.moveTo(20,10);
5448 p.moveTo(20,61);
5449 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
5450
5451 p.setLastPt(30,01);
5452 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10)); // was {0,0, 20,61}
5453
5454 REPORTER_ASSERT(r, p.isValid());
Mike Klein9de72162018-09-24 20:30:28 -04005455}
Mike Reed6a388002018-10-16 13:13:09 -04005456
5457DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
5458 SkPath path;
5459
5460 path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
5461
5462 // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
5463 SkPath shallowPath = path;
5464
5465 // make sure we don't assert/crash on this.
5466 shallowPath.incReserve(0xffffffff);
5467}
5468
Mike Reed07105bb2018-12-01 14:07:49 -05005469////////////////////////////////////////////////////////////////////////////////////////////////
5470
5471/*
5472 * For speed, we tried to preserve useful/expensive attributes about paths,
5473 * - convexity, isrect, isoval, ...
5474 * Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
5475 * is axis-aligned (e.g. scale+translate)
5476 */
5477
5478struct Xforms {
5479 SkMatrix fIM, fTM, fSM, fRM;
5480
5481 Xforms() {
5482 fIM.reset();
5483 fTM.setTranslate(10, 20);
5484 fSM.setScale(2, 3);
5485 fRM.setRotate(30);
5486 }
5487};
5488
Mike Reed07105bb2018-12-01 14:07:49 -05005489static bool conditional_convex(const SkPath& path, bool is_convex) {
Mike Reed30bc5272019-11-22 18:34:02 +00005490 SkPathConvexityType c = path.getConvexityTypeOrUnknown();
5491 return is_convex ? (c == SkPathConvexityType::kConvex) : (c != SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005492}
Mike Reed07105bb2018-12-01 14:07:49 -05005493
5494// expect axis-aligned shape to survive assignment, identity and scale/translate matrices
5495template <typename ISA>
5496void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
5497 ISA isa_proc) {
5498 REPORTER_ASSERT(reporter, isa_proc(*path));
5499 // force the issue (computing convexity) the first time.
Mike Reed30bc5272019-11-22 18:34:02 +00005500 REPORTER_ASSERT(reporter, path->getConvexityType() == SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005501
5502 SkPath path2;
5503
5504 // a path's isa and convexity should survive assignment
5505 path2 = *path;
5506 REPORTER_ASSERT(reporter, isa_proc(path2));
Mike Reed30bc5272019-11-22 18:34:02 +00005507 REPORTER_ASSERT(reporter, path2.getConvexityTypeOrUnknown() == SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005508
5509 // a path's isa and convexity should identity transform
5510 path->transform(x.fIM, &path2);
5511 path->transform(x.fIM);
5512 REPORTER_ASSERT(reporter, isa_proc(path2));
Mike Reed30bc5272019-11-22 18:34:02 +00005513 REPORTER_ASSERT(reporter, path2.getConvexityTypeOrUnknown() == SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005514 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed30bc5272019-11-22 18:34:02 +00005515 REPORTER_ASSERT(reporter, path->getConvexityTypeOrUnknown() == SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005516
5517 // a path's isa should survive translation, convexity depends on axis alignment
5518 path->transform(x.fTM, &path2);
5519 path->transform(x.fTM);
5520 REPORTER_ASSERT(reporter, isa_proc(path2));
5521 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005522 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5523 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005524
5525 // a path's isa should survive scaling, convexity depends on axis alignment
5526 path->transform(x.fSM, &path2);
5527 path->transform(x.fSM);
5528 REPORTER_ASSERT(reporter, isa_proc(path2));
5529 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005530 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5531 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005532
5533 // For security, post-rotation, we can't assume we're still convex. It might prove to be,
5534 // in fact, still be convex, be we can't have cached that setting, hence the call to
Mike Reed30bc5272019-11-22 18:34:02 +00005535 // getConvexityTypeOrUnknown() instead of getConvexityType().
Mike Reed07105bb2018-12-01 14:07:49 -05005536 path->transform(x.fRM, &path2);
5537 path->transform(x.fRM);
5538 if (isAxisAligned) {
5539 REPORTER_ASSERT(reporter, !isa_proc(path2));
5540 REPORTER_ASSERT(reporter, !isa_proc(*path));
5541 }
Mike Reed30bc5272019-11-22 18:34:02 +00005542 REPORTER_ASSERT(reporter, path2.getConvexityTypeOrUnknown() != SkPathConvexityType::kConvex);
5543 REPORTER_ASSERT(reporter, path->getConvexityTypeOrUnknown() != SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005544}
5545
5546DEF_TEST(Path_survive_transform, r) {
5547 const Xforms x;
5548
5549 SkPath path;
5550 path.addRect({10, 10, 40, 50});
5551 survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
5552
5553 path.reset();
5554 path.addOval({10, 10, 40, 50});
5555 survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
5556
5557 path.reset();
5558 path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
5559 survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
5560
5561 // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
5562 path.reset();
5563 path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
Mike Reed30bc5272019-11-22 18:34:02 +00005564 REPORTER_ASSERT(r, path.getConvexityType() == SkPathConvexityType::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005565 survive(&path, x, false, r, [](const SkPath& p) { return true; });
5566}
Mike Reedfb5f43b2018-12-18 12:17:57 -05005567
5568DEF_TEST(path_last_move_to_index, r) {
5569 // Make sure that copyPath is safe after the call to path.offset().
5570 // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
5571 // set it to path's value inside SkPath::transform()
5572
Mike Reeddf3d2252018-12-20 17:10:27 -05005573 const char text[] = "hello";
5574 constexpr size_t len = sizeof(text) - 1;
5575 SkGlyphID glyphs[len];
Mike Reedfb5f43b2018-12-18 12:17:57 -05005576
Mike Reeddf3d2252018-12-20 17:10:27 -05005577 SkFont font;
Ben Wagner51e15a62019-05-07 15:38:46 -04005578 font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
Mike Reeddf3d2252018-12-20 17:10:27 -05005579
5580 SkPath copyPath;
5581 SkFont().getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
5582 if (src) {
5583 ((SkPath*)ctx)->addPath(*src, mx);
5584 }
5585 }, &copyPath);
Mike Reedfb5f43b2018-12-18 12:17:57 -05005586
Kevin Lubick13486cb2019-01-04 09:49:47 -05005587 SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
Mike Reeddf3d2252018-12-20 17:10:27 -05005588 SkPath path;
Mike Reedfb5f43b2018-12-18 12:17:57 -05005589 path.addRoundRect({10, 10, 110, 110}, radii);
5590 path.offset(0, 5, &(copyPath)); // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
5591
5592 copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
5593}
Mike Reed5f152f02019-08-19 14:40:16 -04005594
5595static void test_edger(skiatest::Reporter* r,
5596 const std::initializer_list<SkPath::Verb>& in,
5597 const std::initializer_list<SkPath::Verb>& expected) {
5598 SkPath path;
5599 SkScalar x = 0, y = 0;
5600 for (auto v : in) {
5601 switch (v) {
5602 case SkPath::kMove_Verb: path.moveTo(x++, y++); break;
5603 case SkPath::kLine_Verb: path.lineTo(x++, y++); break;
5604 case SkPath::kClose_Verb: path.close(); break;
5605 default: SkASSERT(false);
5606 }
5607 }
5608
5609 SkPathEdgeIter iter(path);
5610 for (auto v : expected) {
5611 auto e = iter.next();
5612 REPORTER_ASSERT(r, e);
5613 REPORTER_ASSERT(r, SkPathEdgeIter::EdgeToVerb(e.fEdge) == v);
5614 }
5615 auto e = iter.next();
5616 REPORTER_ASSERT(r, !e);
5617}
5618
5619DEF_TEST(pathedger, r) {
5620 auto M = SkPath::kMove_Verb;
5621 auto L = SkPath::kLine_Verb;
5622 auto C = SkPath::kClose_Verb;
5623
5624 test_edger(r, { M }, {});
5625 test_edger(r, { M, M }, {});
5626 test_edger(r, { M, C }, {});
5627 test_edger(r, { M, M, C }, {});
5628 test_edger(r, { M, L }, { L, L });
5629 test_edger(r, { M, L, C }, { L, L });
5630 test_edger(r, { M, L, L }, { L, L, L });
5631 test_edger(r, { M, L, L, C }, { L, L, L });
5632
5633 test_edger(r, { M, L, L, M, L, L }, { L, L, L, L, L, L });
5634}