blob: ab12551a6c0aec4d83854ed730f9b3c7c718d576 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRRect.h"
12#include "include/core/SkSize.h"
13#include "include/core/SkStream.h"
14#include "include/core/SkStrokeRec.h"
15#include "include/core/SkSurface.h"
Brian Salomon71fe9452020-03-02 16:59:40 -050016#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#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"
Brian Osmanff7bee92020-06-09 16:04:18 -040025#include "src/core/SkReadBuffer.h"
26#include "src/core/SkWriteBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#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
Robert Phillips58adb342020-07-23 09:41:57 -0400244static void test_path_crbugskia2820(skiatest::Reporter* reporter) {
Yuqian Li3154a532017-09-06 13:33:30 -0400245 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
Brian Osman116b33e2020-02-05 13:34:09 -0500795 SkScalar rx = std::min(rect.width(), xIn);
796 SkScalar ry = std::min(rect.height(), yIn);
Yuqian Li3154a532017-09-06 13:33:30 -0400797
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) {
Yuqian Li3154a532017-09-06 13:33:30 -0400975 bool firstTime = true;
976 bool foundClose = false;
Chris Daltonde500372020-05-05 15:06:30 -0600977 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
978 switch (verb) {
979 case SkPathVerb::kMove:
Yuqian Li3154a532017-09-06 13:33:30 -0400980 REPORTER_ASSERT(reporter, firstTime);
981 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
982 srcPts++;
983 firstTime = false;
984 break;
Chris Daltonde500372020-05-05 15:06:30 -0600985 case SkPathVerb::kLine:
Yuqian Li3154a532017-09-06 13:33:30 -0400986 REPORTER_ASSERT(reporter, !firstTime);
987 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
988 srcPts++;
989 break;
Chris Daltonde500372020-05-05 15:06:30 -0600990 case SkPathVerb::kQuad:
Brian Salomon1c80e992018-01-29 09:50:47 -0500991 REPORTER_ASSERT(reporter, false, "unexpected quad verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400992 break;
Chris Daltonde500372020-05-05 15:06:30 -0600993 case SkPathVerb::kConic:
Brian Salomon1c80e992018-01-29 09:50:47 -0500994 REPORTER_ASSERT(reporter, false, "unexpected conic verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400995 break;
Chris Daltonde500372020-05-05 15:06:30 -0600996 case SkPathVerb::kCubic:
Brian Salomon1c80e992018-01-29 09:50:47 -0500997 REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400998 break;
Chris Daltonde500372020-05-05 15:06:30 -0600999 case SkPathVerb::kClose:
Yuqian Li3154a532017-09-06 13:33:30 -04001000 REPORTER_ASSERT(reporter, !firstTime);
1001 REPORTER_ASSERT(reporter, !foundClose);
1002 REPORTER_ASSERT(reporter, expectClose);
1003 foundClose = true;
1004 break;
Yuqian Li3154a532017-09-06 13:33:30 -04001005 }
1006 }
Yuqian Li3154a532017-09-06 13:33:30 -04001007 REPORTER_ASSERT(reporter, foundClose == expectClose);
1008}
1009
1010static void test_addPoly(skiatest::Reporter* reporter) {
1011 SkPoint pts[32];
1012 SkRandom rand;
1013
1014 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1015 pts[i].fX = rand.nextSScalar1();
1016 pts[i].fY = rand.nextSScalar1();
1017 }
1018
1019 for (int doClose = 0; doClose <= 1; ++doClose) {
1020 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1021 SkPath path;
1022 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
1023 test_poly(reporter, path, pts, SkToBool(doClose));
1024 }
1025 }
1026}
1027
1028static void test_strokerec(skiatest::Reporter* reporter) {
1029 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1030 REPORTER_ASSERT(reporter, rec.isFillStyle());
1031
1032 rec.setHairlineStyle();
1033 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
1034
1035 rec.setStrokeStyle(SK_Scalar1, false);
1036 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
1037
1038 rec.setStrokeStyle(SK_Scalar1, true);
1039 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
1040
1041 rec.setStrokeStyle(0, false);
1042 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
1043
1044 rec.setStrokeStyle(0, true);
1045 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1046}
1047
1048// Set this for paths that don't have a consistent direction such as a bowtie.
1049// (cheapComputeDirection is not expected to catch these.)
Mike Kleinf6c85402018-08-13 12:19:28 -04001050// Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
Mike Reed3872c982020-08-29 17:46:51 -04001051const SkPathFirstDirection kDontCheckDir = static_cast<SkPathFirstDirection>(3);
Yuqian Li3154a532017-09-06 13:33:30 -04001052
1053static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04001054 SkPathFirstDirection expected) {
Yuqian Li3154a532017-09-06 13:33:30 -04001055 if (expected == kDontCheckDir) {
1056 return;
1057 }
John Stiles31954bf2020-08-07 17:35:54 -04001058 // We make a copy so that we don't cache the result on the passed in path.
1059 SkPath copy(path); // NOLINT(performance-unnecessary-copy-initialization)
Yuqian Li3154a532017-09-06 13:33:30 -04001060
Mike Reed85f51b22020-08-30 10:32:06 -04001061 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(copy);
1062 if (dir != SkPathFirstDirection::kUnknown) {
Yuqian Li3154a532017-09-06 13:33:30 -04001063 REPORTER_ASSERT(reporter, dir == expected);
Yuqian Li3154a532017-09-06 13:33:30 -04001064 }
1065}
1066
1067static void test_direction(skiatest::Reporter* reporter) {
1068 size_t i;
1069 SkPath path;
Mike Reed85f51b22020-08-30 10:32:06 -04001070 REPORTER_ASSERT(reporter,
1071 SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001072
1073 static const char* gDegen[] = {
1074 "M 10 10",
1075 "M 10 10 M 20 20",
1076 "M 10 10 L 20 20",
1077 "M 10 10 L 10 10 L 10 10",
1078 "M 10 10 Q 10 10 10 10",
1079 "M 10 10 C 10 10 10 10 10 10",
1080 };
1081 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1082 path.reset();
1083 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1084 REPORTER_ASSERT(reporter, valid);
Mike Reed85f51b22020-08-30 10:32:06 -04001085 REPORTER_ASSERT(reporter,
1086 SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001087 }
1088
1089 static const char* gCW[] = {
1090 "M 10 10 L 10 10 Q 20 10 20 20",
1091 "M 10 10 C 20 10 20 20 20 20",
1092 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1093 // rect with top two corners replaced by cubics with identical middle
1094 // control points
1095 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1096 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
1097 };
1098 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1099 path.reset();
1100 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1101 REPORTER_ASSERT(reporter, valid);
Mike Reed3872c982020-08-29 17:46:51 -04001102 check_direction(reporter, path, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001103 }
1104
1105 static const char* gCCW[] = {
1106 "M 10 10 L 10 10 Q 20 10 20 -20",
1107 "M 10 10 C 20 10 20 -20 20 -20",
1108 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1109 // rect with top two corners replaced by cubics with identical middle
1110 // control points
1111 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1112 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
1113 };
1114 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1115 path.reset();
1116 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1117 REPORTER_ASSERT(reporter, valid);
Mike Reed3872c982020-08-29 17:46:51 -04001118 check_direction(reporter, path, SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001119 }
1120
1121 // Test two donuts, each wound a different direction. Only the outer contour
1122 // determines the cheap direction
1123 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001124 path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCW);
1125 path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCCW);
Mike Reed3872c982020-08-29 17:46:51 -04001126 check_direction(reporter, path, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001127
1128 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001129 path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCW);
1130 path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCCW);
Mike Reed3872c982020-08-29 17:46:51 -04001131 check_direction(reporter, path, SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001132
1133 // triangle with one point really far from the origin.
1134 path.reset();
1135 // the first point is roughly 1.05e10, 1.05e10
1136 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1137 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1138 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
Mike Reed3872c982020-08-29 17:46:51 -04001139 check_direction(reporter, path, SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001140
1141 path.reset();
1142 path.conicTo(20, 0, 20, 20, 0.5f);
1143 path.close();
Mike Reed3872c982020-08-29 17:46:51 -04001144 check_direction(reporter, path, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001145
1146 path.reset();
1147 path.lineTo(1, 1e7f);
1148 path.lineTo(1e7f, 2e7f);
1149 path.close();
Mike Reed30bc5272019-11-22 18:34:02 +00001150 REPORTER_ASSERT(reporter, path.isConvex());
Mike Reed3872c982020-08-29 17:46:51 -04001151 check_direction(reporter, path, SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001152}
1153
1154static void add_rect(SkPath* path, const SkRect& r) {
1155 path->moveTo(r.fLeft, r.fTop);
1156 path->lineTo(r.fRight, r.fTop);
1157 path->lineTo(r.fRight, r.fBottom);
1158 path->lineTo(r.fLeft, r.fBottom);
1159 path->close();
1160}
1161
1162static void test_bounds(skiatest::Reporter* reporter) {
1163 static const SkRect rects[] = {
1164 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1165 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1166 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1167 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1168 };
1169
1170 SkPath path0, path1;
1171 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1172 path0.addRect(rects[i]);
1173 add_rect(&path1, rects[i]);
1174 }
1175
1176 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1177}
1178
1179static void stroke_cubic(const SkPoint pts[4]) {
1180 SkPath path;
1181 path.moveTo(pts[0]);
1182 path.cubicTo(pts[1], pts[2], pts[3]);
1183
1184 SkPaint paint;
1185 paint.setStyle(SkPaint::kStroke_Style);
1186 paint.setStrokeWidth(SK_Scalar1 * 2);
1187
1188 SkPath fill;
1189 paint.getFillPath(path, &fill);
1190}
1191
1192// just ensure this can run w/o any SkASSERTS firing in the debug build
1193// we used to assert due to differences in how we determine a degenerate vector
1194// but that was fixed with the introduction of SkPoint::CanNormalize
1195static void stroke_tiny_cubic() {
1196 SkPoint p0[] = {
1197 { 372.0f, 92.0f },
1198 { 372.0f, 92.0f },
1199 { 372.0f, 92.0f },
1200 { 372.0f, 92.0f },
1201 };
1202
1203 stroke_cubic(p0);
1204
1205 SkPoint p1[] = {
1206 { 372.0f, 92.0f },
1207 { 372.0007f, 92.000755f },
1208 { 371.99927f, 92.003922f },
1209 { 371.99826f, 92.003899f },
1210 };
1211
1212 stroke_cubic(p1);
1213}
1214
1215static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1216 for (int i = 0; i < 2; ++i) {
1217 SkPath::Iter iter(path, SkToBool(i));
1218 SkPoint mv;
1219 SkPoint pts[4];
1220 SkPath::Verb v;
1221 int nMT = 0;
1222 int nCL = 0;
1223 mv.set(0, 0);
1224 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1225 switch (v) {
1226 case SkPath::kMove_Verb:
1227 mv = pts[0];
1228 ++nMT;
1229 break;
1230 case SkPath::kClose_Verb:
1231 REPORTER_ASSERT(reporter, mv == pts[0]);
1232 ++nCL;
1233 break;
1234 default:
1235 break;
1236 }
1237 }
1238 // if we force a close on the interator we should have a close
1239 // for every moveTo
1240 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1241 }
1242}
1243
1244static void test_close(skiatest::Reporter* reporter) {
1245 SkPath closePt;
1246 closePt.moveTo(0, 0);
1247 closePt.close();
1248 check_close(reporter, closePt);
1249
1250 SkPath openPt;
1251 openPt.moveTo(0, 0);
1252 check_close(reporter, openPt);
1253
1254 SkPath empty;
1255 check_close(reporter, empty);
1256 empty.close();
1257 check_close(reporter, empty);
1258
1259 SkPath rect;
1260 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1261 check_close(reporter, rect);
1262 rect.close();
1263 check_close(reporter, rect);
1264
1265 SkPath quad;
1266 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1267 check_close(reporter, quad);
1268 quad.close();
1269 check_close(reporter, quad);
1270
1271 SkPath cubic;
1272 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1273 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1274 check_close(reporter, cubic);
1275 cubic.close();
1276 check_close(reporter, cubic);
1277
1278 SkPath line;
1279 line.moveTo(SK_Scalar1, SK_Scalar1);
1280 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1281 check_close(reporter, line);
1282 line.close();
1283 check_close(reporter, line);
1284
1285 SkPath rect2;
1286 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1287 rect2.close();
1288 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1289 check_close(reporter, rect2);
1290 rect2.close();
1291 check_close(reporter, rect2);
1292
1293 SkPath oval3;
1294 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1295 oval3.close();
1296 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1297 check_close(reporter, oval3);
1298 oval3.close();
1299 check_close(reporter, oval3);
1300
1301 SkPath moves;
1302 moves.moveTo(SK_Scalar1, SK_Scalar1);
1303 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1304 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1305 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1306 check_close(reporter, moves);
1307
1308 stroke_tiny_cubic();
1309}
1310
1311static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
Mike Reed6052c0b2020-08-28 11:49:56 -04001312 bool expectedConvexity) {
John Stiles31954bf2020-08-07 17:35:54 -04001313 // We make a copy so that we don't cache the result on the passed in path.
1314 SkPath copy(path); // NOLINT(performance-unnecessary-copy-initialization)
Mike Reed6052c0b2020-08-28 11:49:56 -04001315 bool convexity = copy.isConvex();
1316 REPORTER_ASSERT(reporter, convexity == expectedConvexity);
Yuqian Li3154a532017-09-06 13:33:30 -04001317}
1318
1319static void test_path_crbug389050(skiatest::Reporter* reporter) {
1320 SkPath tinyConvexPolygon;
1321 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1322 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1323 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1324 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1325 tinyConvexPolygon.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001326 tinyConvexPolygon.isConvex();
Mike Reed3872c982020-08-29 17:46:51 -04001327 check_direction(reporter, tinyConvexPolygon, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001328
1329 SkPath platTriangle;
1330 platTriangle.moveTo(0, 0);
1331 platTriangle.lineTo(200, 0);
1332 platTriangle.lineTo(100, 0.04f);
1333 platTriangle.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001334 platTriangle.isConvex();
Mike Reed3872c982020-08-29 17:46:51 -04001335 check_direction(reporter, platTriangle, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001336
1337 platTriangle.reset();
1338 platTriangle.moveTo(0, 0);
1339 platTriangle.lineTo(200, 0);
1340 platTriangle.lineTo(100, 0.03f);
1341 platTriangle.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001342 platTriangle.isConvex();
Mike Reed3872c982020-08-29 17:46:51 -04001343 check_direction(reporter, platTriangle, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001344}
1345
1346static void test_convexity2(skiatest::Reporter* reporter) {
1347 SkPath pt;
1348 pt.moveTo(0, 0);
1349 pt.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001350 check_convexity(reporter, pt, true);
Mike Reed3872c982020-08-29 17:46:51 -04001351 check_direction(reporter, pt, SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001352
1353 SkPath line;
1354 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1355 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1356 line.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001357 check_convexity(reporter, line, true);
Mike Reed3872c982020-08-29 17:46:51 -04001358 check_direction(reporter, line, SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001359
1360 SkPath triLeft;
1361 triLeft.moveTo(0, 0);
1362 triLeft.lineTo(SK_Scalar1, 0);
1363 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1364 triLeft.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001365 check_convexity(reporter, triLeft, true);
Mike Reed3872c982020-08-29 17:46:51 -04001366 check_direction(reporter, triLeft, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001367
1368 SkPath triRight;
1369 triRight.moveTo(0, 0);
1370 triRight.lineTo(-SK_Scalar1, 0);
1371 triRight.lineTo(SK_Scalar1, SK_Scalar1);
1372 triRight.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001373 check_convexity(reporter, triRight, true);
Mike Reed3872c982020-08-29 17:46:51 -04001374 check_direction(reporter, triRight, SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001375
1376 SkPath square;
1377 square.moveTo(0, 0);
1378 square.lineTo(SK_Scalar1, 0);
1379 square.lineTo(SK_Scalar1, SK_Scalar1);
1380 square.lineTo(0, SK_Scalar1);
1381 square.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001382 check_convexity(reporter, square, true);
Mike Reed3872c982020-08-29 17:46:51 -04001383 check_direction(reporter, square, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001384
1385 SkPath redundantSquare;
1386 redundantSquare.moveTo(0, 0);
1387 redundantSquare.lineTo(0, 0);
1388 redundantSquare.lineTo(0, 0);
1389 redundantSquare.lineTo(SK_Scalar1, 0);
1390 redundantSquare.lineTo(SK_Scalar1, 0);
1391 redundantSquare.lineTo(SK_Scalar1, 0);
1392 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1393 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1394 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1395 redundantSquare.lineTo(0, SK_Scalar1);
1396 redundantSquare.lineTo(0, SK_Scalar1);
1397 redundantSquare.lineTo(0, SK_Scalar1);
1398 redundantSquare.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001399 check_convexity(reporter, redundantSquare, true);
Mike Reed3872c982020-08-29 17:46:51 -04001400 check_direction(reporter, redundantSquare, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001401
1402 SkPath bowTie;
1403 bowTie.moveTo(0, 0);
1404 bowTie.lineTo(0, 0);
1405 bowTie.lineTo(0, 0);
1406 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1407 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1408 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1409 bowTie.lineTo(SK_Scalar1, 0);
1410 bowTie.lineTo(SK_Scalar1, 0);
1411 bowTie.lineTo(SK_Scalar1, 0);
1412 bowTie.lineTo(0, SK_Scalar1);
1413 bowTie.lineTo(0, SK_Scalar1);
1414 bowTie.lineTo(0, SK_Scalar1);
1415 bowTie.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001416 check_convexity(reporter, bowTie, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001417 check_direction(reporter, bowTie, kDontCheckDir);
1418
1419 SkPath spiral;
1420 spiral.moveTo(0, 0);
1421 spiral.lineTo(100*SK_Scalar1, 0);
1422 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1423 spiral.lineTo(0, 100*SK_Scalar1);
1424 spiral.lineTo(0, 50*SK_Scalar1);
1425 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1426 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1427 spiral.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001428 check_convexity(reporter, spiral, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001429 check_direction(reporter, spiral, kDontCheckDir);
1430
1431 SkPath dent;
1432 dent.moveTo(0, 0);
1433 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1434 dent.lineTo(0, 100*SK_Scalar1);
1435 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1436 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1437 dent.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001438 check_convexity(reporter, dent, false);
Mike Reed3872c982020-08-29 17:46:51 -04001439 check_direction(reporter, dent, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001440
1441 // https://bug.skia.org/2235
1442 SkPath strokedSin;
1443 for (int i = 0; i < 2000; i++) {
1444 SkScalar x = SkIntToScalar(i) / 2;
1445 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1446 if (0 == i) {
1447 strokedSin.moveTo(x, y);
1448 } else {
1449 strokedSin.lineTo(x, y);
1450 }
1451 }
1452 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1453 stroke.setStrokeStyle(2 * SK_Scalar1);
1454 stroke.applyToPath(&strokedSin, strokedSin);
Mike Reed6052c0b2020-08-28 11:49:56 -04001455 check_convexity(reporter, strokedSin, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001456 check_direction(reporter, strokedSin, kDontCheckDir);
1457
1458 // http://crbug.com/412640
1459 SkPath degenerateConcave;
1460 degenerateConcave.moveTo(148.67912f, 191.875f);
1461 degenerateConcave.lineTo(470.37695f, 7.5f);
1462 degenerateConcave.lineTo(148.67912f, 191.875f);
1463 degenerateConcave.lineTo(41.446522f, 376.25f);
1464 degenerateConcave.lineTo(-55.971577f, 460.0f);
1465 degenerateConcave.lineTo(41.446522f, 376.25f);
Mike Reed6052c0b2020-08-28 11:49:56 -04001466 check_convexity(reporter, degenerateConcave, false);
Mike Reed3872c982020-08-29 17:46:51 -04001467 check_direction(reporter, degenerateConcave, SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001468
1469 // http://crbug.com/433683
1470 SkPath badFirstVector;
1471 badFirstVector.moveTo(501.087708f, 319.610352f);
1472 badFirstVector.lineTo(501.087708f, 319.610352f);
1473 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1474 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1475 badFirstVector.lineTo(301.557678f, 98.044601f);
1476 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1477 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1478 badFirstVector.lineTo(504.912292f, 316.389648f);
1479 badFirstVector.lineTo(501.087708f, 319.610352f);
1480 badFirstVector.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001481 check_convexity(reporter, badFirstVector, false);
Brian Osman4be31e22019-08-20 15:35:26 -04001482
1483 // http://crbug.com/993330
1484 SkPath falseBackEdge;
1485 falseBackEdge.moveTo(-217.83430557928145f, -382.14948768484857f);
1486 falseBackEdge.lineTo(-227.73867866614847f, -399.52485512718323f);
1487 falseBackEdge.cubicTo(-158.3541047666846f, -439.0757140459542f,
1488 -79.8654464485281f, -459.875f,
1489 -1.1368683772161603e-13f, -459.875f);
1490 falseBackEdge.lineTo(-8.08037266162413e-14f, -439.875f);
1491 falseBackEdge.lineTo(-8.526512829121202e-14f, -439.87499999999994f);
1492 falseBackEdge.cubicTo(-76.39209188702645f, -439.87499999999994f,
1493 -151.46727226799754f, -419.98027663161537f,
1494 -217.83430557928145f, -382.14948768484857f);
1495 falseBackEdge.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001496 check_convexity(reporter, falseBackEdge, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001497}
1498
Cary Clarkc9b7c722018-12-12 14:50:23 -05001499static void test_convexity_doubleback(skiatest::Reporter* reporter) {
1500 SkPath doubleback;
1501 doubleback.lineTo(1, 1);
Mike Reed6052c0b2020-08-28 11:49:56 -04001502 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001503 doubleback.lineTo(2, 2);
Mike Reed6052c0b2020-08-28 11:49:56 -04001504 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001505 doubleback.reset();
1506 doubleback.lineTo(1, 0);
Mike Reed6052c0b2020-08-28 11:49:56 -04001507 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001508 doubleback.lineTo(2, 0);
Mike Reed6052c0b2020-08-28 11:49:56 -04001509 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001510 doubleback.lineTo(1, 0);
Mike Reed6052c0b2020-08-28 11:49:56 -04001511 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001512 doubleback.reset();
1513 doubleback.quadTo(1, 1, 2, 2);
Mike Reed6052c0b2020-08-28 11:49:56 -04001514 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001515 doubleback.reset();
1516 doubleback.quadTo(1, 0, 2, 0);
Mike Reed6052c0b2020-08-28 11:49:56 -04001517 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001518 doubleback.quadTo(1, 0, 0, 0);
Mike Reed6052c0b2020-08-28 11:49:56 -04001519 check_convexity(reporter, doubleback, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001520}
1521
Yuqian Li3154a532017-09-06 13:33:30 -04001522static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1523 const SkRect& bounds) {
1524 REPORTER_ASSERT(reporter, p.isConvex());
1525 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1526
1527 SkPath p2(p);
1528 REPORTER_ASSERT(reporter, p2.isConvex());
1529 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1530
1531 SkPath other;
1532 other.swap(p2);
1533 REPORTER_ASSERT(reporter, other.isConvex());
1534 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1535}
1536
1537static void setFromString(SkPath* path, const char str[]) {
1538 bool first = true;
1539 while (str) {
1540 SkScalar x, y;
1541 str = SkParse::FindScalar(str, &x);
1542 if (nullptr == str) {
1543 break;
1544 }
1545 str = SkParse::FindScalar(str, &y);
1546 SkASSERT(str);
1547 if (first) {
1548 path->moveTo(x, y);
1549 first = false;
1550 } else {
1551 path->lineTo(x, y);
1552 }
1553 }
1554}
1555
1556static void test_convexity(skiatest::Reporter* reporter) {
1557 SkPath path;
1558
Mike Reed6052c0b2020-08-28 11:49:56 -04001559 check_convexity(reporter, path, true);
Yuqian Li3154a532017-09-06 13:33:30 -04001560 path.addCircle(0, 0, SkIntToScalar(10));
Mike Reed6052c0b2020-08-28 11:49:56 -04001561 check_convexity(reporter, path, true);
Yuqian Li3154a532017-09-06 13:33:30 -04001562 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
Mike Reed6052c0b2020-08-28 11:49:56 -04001563 check_convexity(reporter, path, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001564
1565 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001566 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCCW);
Mike Reed6052c0b2020-08-28 11:49:56 -04001567 check_convexity(reporter, path, true);
Mike Reed85f51b22020-08-30 10:32:06 -04001568 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001569
1570 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00001571 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCW);
Mike Reed6052c0b2020-08-28 11:49:56 -04001572 check_convexity(reporter, path, true);
Mike Reed85f51b22020-08-30 10:32:06 -04001573 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04001574
1575 path.reset();
Cary Clarkc9b7c722018-12-12 14:50:23 -05001576 path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
Mike Reed6052c0b2020-08-28 11:49:56 -04001577 check_convexity(reporter, path, true);
Yuqian Li3154a532017-09-06 13:33:30 -04001578
1579 static const struct {
Mike Reed3872c982020-08-29 17:46:51 -04001580 const char* fPathStr;
1581 bool fExpectedIsConvex;
1582 SkPathFirstDirection fExpectedDirection;
Yuqian Li3154a532017-09-06 13:33:30 -04001583 } gRec[] = {
Mike Reed3872c982020-08-29 17:46:51 -04001584 { "", true, SkPathFirstDirection::kUnknown },
1585 { "0 0", true, SkPathFirstDirection::kUnknown },
1586 { "0 0 10 10", true, SkPathFirstDirection::kUnknown },
1587 { "0 0 10 10 20 20 0 0 10 10", false, SkPathFirstDirection::kUnknown },
1588 { "0 0 10 10 10 20", true, SkPathFirstDirection::kCW },
1589 { "0 0 10 10 10 0", true, SkPathFirstDirection::kCCW },
Mike Reed6052c0b2020-08-28 11:49:56 -04001590 { "0 0 10 10 10 0 0 10", false, kDontCheckDir },
Mike Reed3872c982020-08-29 17:46:51 -04001591 { "0 0 10 0 0 10 -10 -10", false, SkPathFirstDirection::kCW },
Yuqian Li3154a532017-09-06 13:33:30 -04001592 };
1593
1594 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001595 path.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04001596 setFromString(&path, gRec[i].fPathStr);
Mike Reed6052c0b2020-08-28 11:49:56 -04001597 check_convexity(reporter, path, gRec[i].fExpectedIsConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001598 check_direction(reporter, path, gRec[i].fExpectedDirection);
1599 // check after setting the initial convex and direction
1600 if (kDontCheckDir != gRec[i].fExpectedDirection) {
John Stiles31954bf2020-08-07 17:35:54 -04001601 // We make a copy so that we don't cache the result on the passed in path.
1602 SkPath copy(path); // NOLINT(performance-unnecessary-copy-initialization)
Mike Reed85f51b22020-08-30 10:32:06 -04001603 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(copy);
1604 bool foundDir = dir != SkPathFirstDirection::kUnknown;
Mike Reed3872c982020-08-29 17:46:51 -04001605 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathFirstDirection::kUnknown)
Yuqian Li3154a532017-09-06 13:33:30 -04001606 ^ foundDir);
1607 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
Mike Reed6052c0b2020-08-28 11:49:56 -04001608 check_convexity(reporter, copy, gRec[i].fExpectedIsConvex);
Yuqian Li3154a532017-09-06 13:33:30 -04001609 }
Mike Reed6052c0b2020-08-28 11:49:56 -04001610 REPORTER_ASSERT(reporter, gRec[i].fExpectedIsConvex == path.isConvex());
Yuqian Li3154a532017-09-06 13:33:30 -04001611 check_direction(reporter, path, gRec[i].fExpectedDirection);
1612 }
1613
1614 static const SkPoint nonFinitePts[] = {
1615 { SK_ScalarInfinity, 0 },
1616 { 0, SK_ScalarInfinity },
1617 { SK_ScalarInfinity, SK_ScalarInfinity },
1618 { SK_ScalarNegativeInfinity, 0},
1619 { 0, SK_ScalarNegativeInfinity },
1620 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1621 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1622 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1623 { SK_ScalarNaN, 0 },
1624 { 0, SK_ScalarNaN },
1625 { SK_ScalarNaN, SK_ScalarNaN },
1626 };
1627
1628 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1629
Cary Clarkc9b7c722018-12-12 14:50:23 -05001630 static const SkPoint axisAlignedPts[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04001631 { SK_ScalarMax, 0 },
1632 { 0, SK_ScalarMax },
Yuqian Li3154a532017-09-06 13:33:30 -04001633 { SK_ScalarMin, 0 },
1634 { 0, SK_ScalarMin },
Yuqian Li3154a532017-09-06 13:33:30 -04001635 };
1636
Cary Clarkc9b7c722018-12-12 14:50:23 -05001637 const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
Yuqian Li3154a532017-09-06 13:33:30 -04001638
Cary Clarkc9b7c722018-12-12 14:50:23 -05001639 for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
Yuqian Li3154a532017-09-06 13:33:30 -04001640 int i = (int) (index % nonFinitePtsCount);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001641 int f = (int) (index % axisAlignedPtsCount);
1642 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001643 path.reset();
1644 switch (index % 13) {
1645 case 0: path.lineTo(nonFinitePts[i]); break;
1646 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001647 case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
1648 case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
1649 case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
1650 case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
1651 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
1652 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
1653 case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
1654 case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001655 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001656 case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001657 case 12: path.moveTo(nonFinitePts[i]); break;
1658 }
Mike Reed6052c0b2020-08-28 11:49:56 -04001659 REPORTER_ASSERT(reporter,
Mike Reed3872c982020-08-29 17:46:51 -04001660 SkPathPriv::GetConvexityOrUnknown(path) == SkPathConvexity::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04001661 }
1662
Cary Clarkc9b7c722018-12-12 14:50:23 -05001663 for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
1664 int f = (int) (index % axisAlignedPtsCount);
1665 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001666 path.reset();
1667 int curveSelect = index % 11;
1668 switch (curveSelect) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001669 case 0: path.moveTo(axisAlignedPts[f]); break;
1670 case 1: path.lineTo(axisAlignedPts[f]); break;
1671 case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
1672 case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
1673 case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
1674 case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
1675 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
1676 case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
1677 case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
1678 case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
1679 case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001680 }
Cary Clarkc9b7c722018-12-12 14:50:23 -05001681 if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
Mike Reed6052c0b2020-08-28 11:49:56 -04001682 check_convexity(reporter, path, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001683 } else {
John Stiles31954bf2020-08-07 17:35:54 -04001684 // We make a copy so that we don't cache the result on the passed in path.
1685 SkPath copy(path); // NOLINT(performance-unnecessary-copy-initialization)
Mike Reed6052c0b2020-08-28 11:49:56 -04001686 REPORTER_ASSERT(reporter, !copy.isConvex());
Cary Clarkc9b7c722018-12-12 14:50:23 -05001687 }
Yuqian Li3154a532017-09-06 13:33:30 -04001688 }
1689
Cary Clarkc9b7c722018-12-12 14:50:23 -05001690 static const SkPoint diagonalPts[] = {
1691 { SK_ScalarMax, SK_ScalarMax },
1692 { SK_ScalarMin, SK_ScalarMin },
1693 };
1694
1695 const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
1696
1697 for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
1698 int f = (int) (index % diagonalPtsCount);
1699 int g = (int) ((f + 1) % diagonalPtsCount);
1700 path.reset();
1701 int curveSelect = index % 11;
1702 switch (curveSelect) {
1703 case 0: path.moveTo(diagonalPts[f]); break;
1704 case 1: path.lineTo(diagonalPts[f]); break;
1705 case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
1706 case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
1707 case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
1708 case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
1709 case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
1710 case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
1711 case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
1712 case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
1713 case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
1714 }
1715 if (curveSelect == 0) {
Mike Reed6052c0b2020-08-28 11:49:56 -04001716 check_convexity(reporter, path, true);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001717 } else {
John Stiles31954bf2020-08-07 17:35:54 -04001718 // We make a copy so that we don't cache the result on the passed in path.
1719 SkPath copy(path); // NOLINT(performance-unnecessary-copy-initialization)
Mike Reed6052c0b2020-08-28 11:49:56 -04001720 REPORTER_ASSERT(reporter, !copy.isConvex());
Cary Clarkc9b7c722018-12-12 14:50:23 -05001721 }
1722 }
1723
1724
Yuqian Li3154a532017-09-06 13:33:30 -04001725 path.reset();
1726 path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1727 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
1728 path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
1729 path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
1730 path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
1731 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1732 path.close();
Mike Reed6052c0b2020-08-28 11:49:56 -04001733 check_convexity(reporter, path, false);
Yuqian Li3154a532017-09-06 13:33:30 -04001734
1735}
1736
1737static void test_isLine(skiatest::Reporter* reporter) {
1738 SkPath path;
1739 SkPoint pts[2];
1740 const SkScalar value = SkIntToScalar(5);
1741
1742 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1743
1744 // set some non-zero values
1745 pts[0].set(value, value);
1746 pts[1].set(value, value);
1747 REPORTER_ASSERT(reporter, !path.isLine(pts));
1748 // check that pts was untouched
1749 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1750 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1751
1752 const SkScalar moveX = SkIntToScalar(1);
1753 const SkScalar moveY = SkIntToScalar(2);
1754 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1755
1756 path.moveTo(moveX, moveY);
1757 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1758 REPORTER_ASSERT(reporter, !path.isLine(pts));
1759 // check that pts was untouched
1760 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1761 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1762
1763 const SkScalar lineX = SkIntToScalar(2);
1764 const SkScalar lineY = SkIntToScalar(2);
1765 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1766
1767 path.lineTo(lineX, lineY);
1768 REPORTER_ASSERT(reporter, path.isLine(nullptr));
1769
1770 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1771 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1772 REPORTER_ASSERT(reporter, path.isLine(pts));
1773 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1774 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1775
1776 path.lineTo(0, 0); // too many points/verbs
1777 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1778 REPORTER_ASSERT(reporter, !path.isLine(pts));
1779 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1780 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1781
1782 path.reset();
1783 path.quadTo(1, 1, 2, 2);
1784 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1785}
1786
1787static void test_conservativelyContains(skiatest::Reporter* reporter) {
1788 SkPath path;
1789
1790 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1791 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1792
1793 // A circle that bounds kBaseRect (with a significant amount of slop)
Brian Osman116b33e2020-02-05 13:34:09 -05001794 SkScalar circleR = std::max(kBaseRect.width(), kBaseRect.height());
Yuqian Li3154a532017-09-06 13:33:30 -04001795 circleR *= 1.75f / 2;
1796 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1797
1798 // round-rect radii
1799 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1800
1801 static const struct SUPPRESS_VISIBILITY_WARNING {
1802 SkRect fQueryRect;
1803 bool fInRect;
1804 bool fInCircle;
1805 bool fInRR;
1806 bool fInCubicRR;
1807 } kQueries[] = {
1808 {kBaseRect, true, true, false, false},
1809
1810 // rect well inside of kBaseRect
1811 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1812 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1813 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1814 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1815 true, true, true, true},
1816
1817 // rects with edges off by one from kBaseRect's edges
1818 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1819 kBaseRect.width(), kBaseRect.height() + 1),
1820 false, true, false, false},
1821 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1822 kBaseRect.width() + 1, kBaseRect.height()),
1823 false, true, false, false},
1824 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1825 kBaseRect.width() + 1, kBaseRect.height() + 1),
1826 false, true, false, false},
1827 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1828 kBaseRect.width(), kBaseRect.height()),
1829 false, true, false, false},
1830 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1831 kBaseRect.width(), kBaseRect.height()),
1832 false, true, false, false},
1833 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1834 kBaseRect.width() + 2, kBaseRect.height()),
1835 false, true, false, false},
1836 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1837 kBaseRect.width() + 2, kBaseRect.height()),
1838 false, true, false, false},
1839
1840 // zero-w/h rects at each corner of kBaseRect
1841 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1842 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1843 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1844 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1845
1846 // far away rect
1847 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1848 SkIntToScalar(10), SkIntToScalar(10)),
1849 false, false, false, false},
1850
1851 // very large rect containing kBaseRect
1852 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1853 kBaseRect.fTop - 5 * kBaseRect.height(),
1854 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1855 false, false, false, false},
1856
1857 // skinny rect that spans same y-range as kBaseRect
1858 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1859 SkIntToScalar(1), kBaseRect.height()),
1860 true, true, true, true},
1861
1862 // short rect that spans same x-range as kBaseRect
1863 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1864 true, true, true, true},
1865
1866 // skinny rect that spans slightly larger y-range than kBaseRect
1867 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1868 SkIntToScalar(1), kBaseRect.height() + 1),
1869 false, true, false, false},
1870
1871 // short rect that spans slightly larger x-range than kBaseRect
1872 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1873 kBaseRect.width() + 1, SkScalar(1)),
1874 false, true, false, false},
1875 };
1876
1877 for (int inv = 0; inv < 4; ++inv) {
1878 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1879 SkRect qRect = kQueries[q].fQueryRect;
1880 if (inv & 0x1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001881 using std::swap;
1882 swap(qRect.fLeft, qRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04001883 }
1884 if (inv & 0x2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001885 using std::swap;
1886 swap(qRect.fTop, qRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04001887 }
1888 for (int d = 0; d < 2; ++d) {
Mike Reed30bc5272019-11-22 18:34:02 +00001889 SkPathDirection dir = d ? SkPathDirection::kCCW : SkPathDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04001890 path.reset();
1891 path.addRect(kBaseRect, dir);
1892 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1893 path.conservativelyContainsRect(qRect));
1894
1895 path.reset();
1896 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1897 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1898 path.conservativelyContainsRect(qRect));
1899
1900 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00001901 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
Yuqian Li3154a532017-09-06 13:33:30 -04001902 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1903 path.conservativelyContainsRect(qRect));
1904
1905 path.reset();
1906 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1907 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1908 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1909 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1910 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1911 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1912 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1913 path.close();
1914 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1915 path.conservativelyContainsRect(qRect));
1916
1917 }
1918 // Slightly non-convex shape, shouldn't contain any rects.
1919 path.reset();
1920 path.moveTo(0, 0);
1921 path.lineTo(SkIntToScalar(50), 0.05f);
1922 path.lineTo(SkIntToScalar(100), 0);
1923 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1924 path.lineTo(0, SkIntToScalar(100));
1925 path.close();
1926 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1927 }
1928 }
1929
1930 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1931 path.reset();
1932 path.moveTo(0, 0);
1933 path.lineTo(SkIntToScalar(100), 0);
1934 path.lineTo(0, SkIntToScalar(100));
1935
1936 // inside, on along top edge
1937 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1938 SkIntToScalar(10),
1939 SkIntToScalar(10))));
1940 // above
1941 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1942 SkRect::MakeXYWH(SkIntToScalar(50),
1943 SkIntToScalar(-10),
1944 SkIntToScalar(10),
1945 SkIntToScalar(10))));
1946 // to the left
1947 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1948 SkIntToScalar(5),
1949 SkIntToScalar(5),
1950 SkIntToScalar(5))));
1951
1952 // outside the diagonal edge
1953 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1954 SkIntToScalar(200),
1955 SkIntToScalar(20),
1956 SkIntToScalar(5))));
1957
1958
1959 // Test that multiple move commands do not cause asserts.
Yuqian Li3154a532017-09-06 13:33:30 -04001960 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
Yuqian Li3154a532017-09-06 13:33:30 -04001961 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1962 SkIntToScalar(10),
1963 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001964
1965 // Same as above path and first test but with an extra moveTo.
1966 path.reset();
1967 path.moveTo(100, 100);
1968 path.moveTo(0, 0);
1969 path.lineTo(SkIntToScalar(100), 0);
1970 path.lineTo(0, SkIntToScalar(100));
Michael Ludwig48f10652021-04-22 09:34:45 -04001971 // Convexity logic treats a path as filled and closed, so that multiple (non-trailing) moveTos
1972 // have no effect on convexity
1973 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(
Brian Osman205a1262017-09-18 13:13:48 +00001974 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1975 SkIntToScalar(10),
1976 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001977
1978 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
1979 // following the non-empty sub-path. Verifies that this does not trigger assertions.
1980 path.reset();
1981 path.moveTo(0, 0);
1982 path.lineTo(SkIntToScalar(100), 0);
1983 path.lineTo(0, SkIntToScalar(100));
1984 path.moveTo(100, 100);
1985
1986 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1987 SkIntToScalar(10),
1988 SkIntToScalar(10))));
1989
1990 // Test that multiple move commands do not cause asserts and that the function
1991 // is not confused by the multiple moves.
1992 path.reset();
1993 path.moveTo(0, 0);
1994 path.lineTo(SkIntToScalar(100), 0);
1995 path.lineTo(0, SkIntToScalar(100));
1996 path.moveTo(0, SkIntToScalar(200));
1997 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1998 path.lineTo(0, SkIntToScalar(300));
1999
2000 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2001 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2002 SkIntToScalar(10),
2003 SkIntToScalar(10))));
2004
2005 path.reset();
2006 path.lineTo(100, 100);
2007 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
2008
2009 // An empty path should not contain any rectangle. It's questionable whether an empty path
2010 // contains an empty rectangle. However, since it is a conservative test it is ok to
2011 // return false.
2012 path.reset();
2013 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2014 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2015}
2016
2017static void test_isRect_open_close(skiatest::Reporter* reporter) {
2018 SkPath path;
2019 bool isClosed;
2020
2021 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
2022 path.close();
2023
2024 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
2025 REPORTER_ASSERT(reporter, isClosed);
2026}
2027
2028// Simple isRect test is inline TestPath, below.
2029// test_isRect provides more extensive testing.
2030static void test_isRect(skiatest::Reporter* reporter) {
2031 test_isRect_open_close(reporter);
2032
2033 // passing tests (all moveTo / lineTo...
2034 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2035 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2036 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2037 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2038 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
2039 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2040 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2041 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2042 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2043 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
2044 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
2045 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
2046 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
2047 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
2048 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
2049
2050 // failing tests
2051 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2052 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2053 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2054 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2055 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2056 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2057 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2058 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2059 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2060 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2061 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
2062
2063 // no close, but we should detect them as fillably the same as a rect
2064 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2065 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2066 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2067
2068 // like c2, but we double-back on ourselves
2069 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2070 // like c2, but we overshoot the start point
2071 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2072 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2073
2074 struct IsRectTest {
2075 SkPoint *fPoints;
2076 int fPointCount;
2077 bool fClose;
2078 bool fIsRect;
2079 } tests[] = {
2080 { r1, SK_ARRAY_COUNT(r1), true, true },
2081 { r2, SK_ARRAY_COUNT(r2), true, true },
2082 { r3, SK_ARRAY_COUNT(r3), true, true },
2083 { r4, SK_ARRAY_COUNT(r4), true, true },
2084 { r5, SK_ARRAY_COUNT(r5), true, true },
2085 { r6, SK_ARRAY_COUNT(r6), true, true },
2086 { r7, SK_ARRAY_COUNT(r7), true, true },
2087 { r8, SK_ARRAY_COUNT(r8), true, true },
2088 { r9, SK_ARRAY_COUNT(r9), true, true },
2089 { ra, SK_ARRAY_COUNT(ra), true, true },
2090 { rb, SK_ARRAY_COUNT(rb), true, true },
2091 { rc, SK_ARRAY_COUNT(rc), true, true },
2092 { rd, SK_ARRAY_COUNT(rd), true, true },
2093 { re, SK_ARRAY_COUNT(re), true, true },
2094 { rf, SK_ARRAY_COUNT(rf), true, true },
2095
2096 { f1, SK_ARRAY_COUNT(f1), true, false },
2097 { f2, SK_ARRAY_COUNT(f2), true, false },
2098 { f3, SK_ARRAY_COUNT(f3), true, false },
2099 { f4, SK_ARRAY_COUNT(f4), true, false },
2100 { f5, SK_ARRAY_COUNT(f5), true, false },
2101 { f6, SK_ARRAY_COUNT(f6), true, false },
2102 { f7, SK_ARRAY_COUNT(f7), true, false },
2103 { f8, SK_ARRAY_COUNT(f8), true, false },
2104 { f9, SK_ARRAY_COUNT(f9), true, false },
2105 { fa, SK_ARRAY_COUNT(fa), true, false },
2106 { fb, SK_ARRAY_COUNT(fb), true, false },
2107
2108 { c1, SK_ARRAY_COUNT(c1), false, true },
2109 { c2, SK_ARRAY_COUNT(c2), false, true },
2110 { c3, SK_ARRAY_COUNT(c3), false, true },
2111
2112 { d1, SK_ARRAY_COUNT(d1), false, false },
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002113 { d2, SK_ARRAY_COUNT(d2), false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002114 { d3, SK_ARRAY_COUNT(d3), false, false },
2115 };
2116
2117 const size_t testCount = SK_ARRAY_COUNT(tests);
2118 int index;
2119 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2120 SkPath path;
2121 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2122 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2123 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2124 }
2125 if (tests[testIndex].fClose) {
2126 path.close();
2127 }
2128 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2129
2130 if (tests[testIndex].fIsRect) {
2131 SkRect computed, expected;
2132 bool isClosed;
Mike Reed30bc5272019-11-22 18:34:02 +00002133 SkPathDirection direction;
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002134 int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
Mike Reed92b33352019-08-24 19:39:13 -04002135 expected.setBounds(tests[testIndex].fPoints, pointCount);
Mike Reed85f51b22020-08-30 10:32:06 -04002136 SkPathFirstDirection cheapDirection = SkPathPriv::ComputeFirstDirection(path);
2137 REPORTER_ASSERT(reporter, cheapDirection != SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04002138 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2139 REPORTER_ASSERT(reporter, expected == computed);
2140 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2141 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2142 } else {
2143 SkRect computed;
Mike Reed92b33352019-08-24 19:39:13 -04002144 computed.setLTRB(123, 456, 789, 1011);
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002145 for (auto c : {true, false})
Mike Reed30bc5272019-11-22 18:34:02 +00002146 for (auto d : {SkPathDirection::kCW, SkPathDirection::kCCW}) {
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002147 bool isClosed = c;
Mike Reed30bc5272019-11-22 18:34:02 +00002148 SkPathDirection direction = d;
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002149 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2150 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2151 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2152 REPORTER_ASSERT(reporter, isClosed == c);
2153 REPORTER_ASSERT(reporter, direction == d);
2154 }
Yuqian Li3154a532017-09-06 13:33:30 -04002155 }
2156 }
2157
2158 // fail, close then line
2159 SkPath path1;
2160 path1.moveTo(r1[0].fX, r1[0].fY);
2161 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2162 path1.lineTo(r1[index].fX, r1[index].fY);
2163 }
2164 path1.close();
2165 path1.lineTo(1, 0);
2166 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2167
2168 // fail, move in the middle
2169 path1.reset();
2170 path1.moveTo(r1[0].fX, r1[0].fY);
2171 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2172 if (index == 2) {
2173 path1.moveTo(1, .5f);
2174 }
2175 path1.lineTo(r1[index].fX, r1[index].fY);
2176 }
2177 path1.close();
2178 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2179
2180 // fail, move on the edge
2181 path1.reset();
2182 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2183 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2184 path1.lineTo(r1[index].fX, r1[index].fY);
2185 }
2186 path1.close();
2187 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2188
2189 // fail, quad
2190 path1.reset();
2191 path1.moveTo(r1[0].fX, r1[0].fY);
2192 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2193 if (index == 2) {
2194 path1.quadTo(1, .5f, 1, .5f);
2195 }
2196 path1.lineTo(r1[index].fX, r1[index].fY);
2197 }
2198 path1.close();
2199 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2200
2201 // fail, cubic
2202 path1.reset();
2203 path1.moveTo(r1[0].fX, r1[0].fY);
2204 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2205 if (index == 2) {
2206 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2207 }
2208 path1.lineTo(r1[index].fX, r1[index].fY);
2209 }
2210 path1.close();
2211 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2212}
2213
Chris Dalton34c50522020-09-17 15:45:20 -06002214static void check_simple_rect(skiatest::Reporter* reporter, const SkPath& path, bool isClosed,
2215 const SkRect& rect, SkPathDirection dir, unsigned start) {
Yuqian Li3154a532017-09-06 13:33:30 -04002216 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00002217 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002218 unsigned s = ~0U;
2219
Chris Dalton34c50522020-09-17 15:45:20 -06002220 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleRect(path, false, &r, &d, &s) == isClosed);
2221 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleRect(path, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002222 REPORTER_ASSERT(reporter, r == rect);
2223 REPORTER_ASSERT(reporter, d == dir);
2224 REPORTER_ASSERT(reporter, s == start);
2225}
2226
Chris Dalton34c50522020-09-17 15:45:20 -06002227static void test_is_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002228 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002229 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00002230 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002231 unsigned s = ~0U;
2232
2233 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2234 const SkRect emptyRect = SkRect::MakeEmpty();
Yuqian Li3154a532017-09-06 13:33:30 -04002235 for (int start = 0; start < 4; ++start) {
Mike Reed30bc5272019-11-22 18:34:02 +00002236 for (auto dir : {SkPathDirection::kCCW, SkPathDirection::kCW}) {
Yuqian Li3154a532017-09-06 13:33:30 -04002237 SkPath path;
2238 path.addRect(testRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002239 check_simple_rect(reporter, path, true, testRect, dir, start);
Yuqian Li3154a532017-09-06 13:33:30 -04002240 path.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002241 check_simple_rect(reporter, path, true, testRect, dir, start);
Yuqian Li3154a532017-09-06 13:33:30 -04002242 SkPath path2 = path;
2243 path2.lineTo(10, 10);
Chris Dalton34c50522020-09-17 15:45:20 -06002244 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2245 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002246 path2 = path;
2247 path2.moveTo(10, 10);
Chris Dalton34c50522020-09-17 15:45:20 -06002248 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2249 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002250 path2 = path;
2251 path2.addRect(testRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002252 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2253 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002254 // Make the path by hand, manually closing it.
2255 path2.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04002256 SkPoint firstPt = {0.f, 0.f};
Chris Daltonde500372020-05-05 15:06:30 -06002257 for (auto [v, verbPts, w] : SkPathPriv::Iterate(path)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002258 switch(v) {
Chris Daltonde500372020-05-05 15:06:30 -06002259 case SkPathVerb::kMove:
Yuqian Li3154a532017-09-06 13:33:30 -04002260 firstPt = verbPts[0];
2261 path2.moveTo(verbPts[0]);
2262 break;
Chris Daltonde500372020-05-05 15:06:30 -06002263 case SkPathVerb::kLine:
Yuqian Li3154a532017-09-06 13:33:30 -04002264 path2.lineTo(verbPts[1]);
2265 break;
2266 default:
2267 break;
2268 }
2269 }
2270 // We haven't closed it yet...
Chris Dalton34c50522020-09-17 15:45:20 -06002271 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2272 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002273 // ... now we do and test again.
2274 path2.lineTo(firstPt);
Chris Dalton34c50522020-09-17 15:45:20 -06002275 check_simple_rect(reporter, path2, false, testRect, dir, start);
Yuqian Li3154a532017-09-06 13:33:30 -04002276 // A redundant close shouldn't cause a failure.
2277 path2.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002278 check_simple_rect(reporter, path2, true, testRect, dir, start);
Yuqian Li3154a532017-09-06 13:33:30 -04002279 // Degenerate point and line rects are not allowed
2280 path2.reset();
2281 path2.addRect(emptyRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002282 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2283 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002284 SkRect degenRect = testRect;
2285 degenRect.fLeft = degenRect.fRight;
2286 path2.reset();
2287 path2.addRect(degenRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002288 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2289 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002290 degenRect = testRect;
2291 degenRect.fTop = degenRect.fBottom;
2292 path2.reset();
2293 path2.addRect(degenRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002294 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2295 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
Yuqian Li3154a532017-09-06 13:33:30 -04002296 // An inverted rect makes a rect path, but changes the winding dir and start point.
Mike Reed30bc5272019-11-22 18:34:02 +00002297 SkPathDirection swapDir = (dir == SkPathDirection::kCW)
2298 ? SkPathDirection::kCCW
2299 : SkPathDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002300 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2301 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2302 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002303 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002304 path2.reset();
2305 path2.addRect(swapRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002306 check_simple_rect(reporter, path2, true, testRect, swapDir, kXSwapStarts[start]);
Yuqian Li3154a532017-09-06 13:33:30 -04002307 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002308 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002309 path2.reset();
2310 path2.addRect(swapRect, dir, start);
Chris Dalton34c50522020-09-17 15:45:20 -06002311 check_simple_rect(reporter, path2, true, testRect, swapDir, kYSwapStarts[start]);
Yuqian Li3154a532017-09-06 13:33:30 -04002312 }
2313 }
2314 // down, up, left, close
John Stilesfa088a62021-08-12 23:01:41 -04002315 SkPath path;
Yuqian Li3154a532017-09-06 13:33:30 -04002316 path.moveTo(1, 1);
2317 path.lineTo(1, 2);
2318 path.lineTo(1, 1);
2319 path.lineTo(0, 1);
2320 SkRect rect;
Mike Reed30bc5272019-11-22 18:34:02 +00002321 SkPathDirection dir;
Yuqian Li3154a532017-09-06 13:33:30 -04002322 unsigned start;
2323 path.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002324 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2325 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04002326 // right, left, up, close
2327 path.reset();
2328 path.moveTo(1, 1);
2329 path.lineTo(2, 1);
2330 path.lineTo(1, 1);
2331 path.lineTo(1, 0);
2332 path.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002333 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2334 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04002335 // parallelogram with horizontal edges
2336 path.reset();
2337 path.moveTo(1, 0);
2338 path.lineTo(3, 0);
2339 path.lineTo(2, 1);
2340 path.lineTo(0, 1);
2341 path.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002342 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2343 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04002344 // parallelogram with vertical edges
2345 path.reset();
2346 path.moveTo(0, 1);
2347 path.lineTo(0, 3);
2348 path.lineTo(1, 2);
2349 path.lineTo(1, 0);
2350 path.close();
Chris Dalton34c50522020-09-17 15:45:20 -06002351 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2352 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04002353
2354}
2355
2356static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2357 // passing tests (all moveTo / lineTo...
2358 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2359 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2360 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2361 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2362 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2363 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2364 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2365 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2366 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2367 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2368 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2369 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2370 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2371 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2372
2373 // failing tests
2374 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2375 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2376 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2377 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2378 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2379 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2380 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2381 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2382
2383 // success, no close is OK
2384 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2385 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2386
2387 struct IsNestedRectTest {
2388 SkPoint *fPoints;
2389 int fPointCount;
Mike Reed3872c982020-08-29 17:46:51 -04002390 SkPathFirstDirection fDirection;
Yuqian Li3154a532017-09-06 13:33:30 -04002391 bool fClose;
2392 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2393 } tests[] = {
Mike Reed3872c982020-08-29 17:46:51 -04002394 { r1, SK_ARRAY_COUNT(r1), SkPathFirstDirection::kCW , true, true },
2395 { r2, SK_ARRAY_COUNT(r2), SkPathFirstDirection::kCW , true, true },
2396 { r3, SK_ARRAY_COUNT(r3), SkPathFirstDirection::kCW , true, true },
2397 { r4, SK_ARRAY_COUNT(r4), SkPathFirstDirection::kCW , true, true },
2398 { r5, SK_ARRAY_COUNT(r5), SkPathFirstDirection::kCCW, true, true },
2399 { r6, SK_ARRAY_COUNT(r6), SkPathFirstDirection::kCCW, true, true },
2400 { r7, SK_ARRAY_COUNT(r7), SkPathFirstDirection::kCCW, true, true },
2401 { r8, SK_ARRAY_COUNT(r8), SkPathFirstDirection::kCCW, true, true },
2402 { r9, SK_ARRAY_COUNT(r9), SkPathFirstDirection::kCCW, true, true },
2403 { ra, SK_ARRAY_COUNT(ra), SkPathFirstDirection::kCCW, true, true },
2404 { rb, SK_ARRAY_COUNT(rb), SkPathFirstDirection::kCW, true, true },
2405 { rc, SK_ARRAY_COUNT(rc), SkPathFirstDirection::kCW, true, true },
2406 { rd, SK_ARRAY_COUNT(rd), SkPathFirstDirection::kCCW, true, true },
2407 { re, SK_ARRAY_COUNT(re), SkPathFirstDirection::kCW, true, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002408
Mike Reed3872c982020-08-29 17:46:51 -04002409 { f1, SK_ARRAY_COUNT(f1), SkPathFirstDirection::kUnknown, true, false },
2410 { f2, SK_ARRAY_COUNT(f2), SkPathFirstDirection::kUnknown, true, false },
2411 { f3, SK_ARRAY_COUNT(f3), SkPathFirstDirection::kUnknown, true, false },
2412 { f4, SK_ARRAY_COUNT(f4), SkPathFirstDirection::kUnknown, true, false },
2413 { f5, SK_ARRAY_COUNT(f5), SkPathFirstDirection::kUnknown, true, false },
2414 { f6, SK_ARRAY_COUNT(f6), SkPathFirstDirection::kUnknown, true, false },
2415 { f7, SK_ARRAY_COUNT(f7), SkPathFirstDirection::kUnknown, true, false },
2416 { f8, SK_ARRAY_COUNT(f8), SkPathFirstDirection::kUnknown, true, false },
Yuqian Li3154a532017-09-06 13:33:30 -04002417
Mike Reed3872c982020-08-29 17:46:51 -04002418 { c1, SK_ARRAY_COUNT(c1), SkPathFirstDirection::kCW, false, true },
2419 { c2, SK_ARRAY_COUNT(c2), SkPathFirstDirection::kCW, false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002420 };
2421
2422 const size_t testCount = SK_ARRAY_COUNT(tests);
2423 int index;
2424 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2425 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2426 SkPath path;
2427 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002428 path.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002429 }
2430 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2431 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2432 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2433 }
2434 if (tests[testIndex].fClose) {
2435 path.close();
2436 }
2437 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002438 path.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002439 }
2440 REPORTER_ASSERT(reporter,
Mike Reed430470d2019-09-12 17:09:12 -04002441 tests[testIndex].fIsNestedRect == SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002442 if (tests[testIndex].fIsNestedRect) {
2443 SkRect expected[2], computed[2];
Mike Reed3872c982020-08-29 17:46:51 -04002444 SkPathFirstDirection expectedDirs[2];
Mike Reed30bc5272019-11-22 18:34:02 +00002445 SkPathDirection computedDirs[2];
Yuqian Li3154a532017-09-06 13:33:30 -04002446 SkRect testBounds;
Mike Reed92b33352019-08-24 19:39:13 -04002447 testBounds.setBounds(tests[testIndex].fPoints, tests[testIndex].fPointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002448 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2449 expected[1] = testBounds;
2450 if (rectFirst) {
Mike Reed3872c982020-08-29 17:46:51 -04002451 expectedDirs[0] = SkPathFirstDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002452 } else {
Mike Reed3872c982020-08-29 17:46:51 -04002453 expectedDirs[0] = SkPathFirstDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04002454 }
2455 expectedDirs[1] = tests[testIndex].fDirection;
Mike Reed430470d2019-09-12 17:09:12 -04002456 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, computed, computedDirs));
Yuqian Li3154a532017-09-06 13:33:30 -04002457 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2458 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2459 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2460 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2461 }
2462 }
2463
2464 // fail, close then line
2465 SkPath path1;
2466 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002467 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002468 }
2469 path1.moveTo(r1[0].fX, r1[0].fY);
2470 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2471 path1.lineTo(r1[index].fX, r1[index].fY);
2472 }
2473 path1.close();
2474 path1.lineTo(1, 0);
2475 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002476 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002477 }
Mike Reed430470d2019-09-12 17:09:12 -04002478 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002479
2480 // fail, move in the middle
2481 path1.reset();
2482 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002483 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002484 }
2485 path1.moveTo(r1[0].fX, r1[0].fY);
2486 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2487 if (index == 2) {
2488 path1.moveTo(1, .5f);
2489 }
2490 path1.lineTo(r1[index].fX, r1[index].fY);
2491 }
2492 path1.close();
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 on the edge
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 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2504 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2505 path1.lineTo(r1[index].fX, r1[index].fY);
2506 }
2507 path1.close();
2508 if (!rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002509 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002510 }
Mike Reed430470d2019-09-12 17:09:12 -04002511 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002512
2513 // fail, quad
2514 path1.reset();
2515 if (rectFirst) {
Mike Reed30bc5272019-11-22 18:34:02 +00002516 path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002517 }
2518 path1.moveTo(r1[0].fX, r1[0].fY);
2519 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2520 if (index == 2) {
2521 path1.quadTo(1, .5f, 1, .5f);
2522 }
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, cubic
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.cubicTo(1, .5f, 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, not nested
2550 path1.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00002551 path1.addRect(1, 1, 3, 3, SkPathDirection::kCW);
2552 path1.addRect(2, 2, 4, 4, SkPathDirection::kCW);
Mike Reed430470d2019-09-12 17:09:12 -04002553 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002554 }
2555
2556 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2557 SkPath path;
2558 path.moveTo(0, 0);
2559 path.lineTo(10, 0);
2560 path.lineTo(10, 10);
2561 path.lineTo(0, 10);
2562 path.lineTo(0, 0);
2563 path.moveTo(1, 1);
2564 path.lineTo(9, 1);
2565 path.lineTo(9, 9);
2566 path.lineTo(1, 9);
2567 path.lineTo(1, 1);
Mike Reed430470d2019-09-12 17:09:12 -04002568 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002569
2570 // pass, stroke rect
2571 SkPath src, dst;
Mike Reed30bc5272019-11-22 18:34:02 +00002572 src.addRect(1, 1, 7, 7, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002573 SkPaint strokePaint;
2574 strokePaint.setStyle(SkPaint::kStroke_Style);
2575 strokePaint.setStrokeWidth(2);
2576 strokePaint.getFillPath(src, &dst);
Mike Reed430470d2019-09-12 17:09:12 -04002577 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(dst, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002578}
2579
2580static void write_and_read_back(skiatest::Reporter* reporter,
2581 const SkPath& p) {
Brian Osmanff7bee92020-06-09 16:04:18 -04002582 SkBinaryWriteBuffer writer;
Yuqian Li3154a532017-09-06 13:33:30 -04002583 writer.writePath(p);
2584 size_t size = writer.bytesWritten();
2585 SkAutoMalloc storage(size);
Brian Osmanff7bee92020-06-09 16:04:18 -04002586 writer.writeToMemory(storage.get());
2587 SkReadBuffer reader(storage.get(), size);
Yuqian Li3154a532017-09-06 13:33:30 -04002588
2589 SkPath readBack;
2590 REPORTER_ASSERT(reporter, readBack != p);
2591 reader.readPath(&readBack);
2592 REPORTER_ASSERT(reporter, readBack == p);
2593
Mike Reed3872c982020-08-29 17:46:51 -04002594 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(readBack) ==
2595 SkPathPriv::GetConvexityOrUnknown(p));
Yuqian Li3154a532017-09-06 13:33:30 -04002596
2597 SkRect oval0, oval1;
Mike Reed30bc5272019-11-22 18:34:02 +00002598 SkPathDirection dir0, dir1;
Yuqian Li3154a532017-09-06 13:33:30 -04002599 unsigned start0, start1;
2600 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002601 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2602 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002603 REPORTER_ASSERT(reporter, oval0 == oval1);
2604 REPORTER_ASSERT(reporter, dir0 == dir1);
2605 REPORTER_ASSERT(reporter, start0 == start1);
2606 }
2607 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2608 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002609 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2610 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002611 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2612 REPORTER_ASSERT(reporter, dir0 == dir1);
2613 REPORTER_ASSERT(reporter, start0 == start1);
2614 }
2615 const SkRect& origBounds = p.getBounds();
2616 const SkRect& readBackBounds = readBack.getBounds();
2617
2618 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2619}
2620
Yuqian Li3154a532017-09-06 13:33:30 -04002621static void test_flattening(skiatest::Reporter* reporter) {
2622 SkPath p;
2623
2624 static const SkPoint pts[] = {
2625 { 0, 0 },
2626 { SkIntToScalar(10), SkIntToScalar(10) },
2627 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2628 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2629 };
2630 p.moveTo(pts[0]);
2631 p.lineTo(pts[1]);
2632 p.quadTo(pts[2], pts[3]);
2633 p.cubicTo(pts[4], pts[5], pts[6]);
2634
2635 write_and_read_back(reporter, p);
2636
2637 // create a buffer that should be much larger than the path so we don't
2638 // kill our stack if writer goes too far.
2639 char buffer[1024];
2640 size_t size1 = p.writeToMemory(nullptr);
2641 size_t size2 = p.writeToMemory(buffer);
2642 REPORTER_ASSERT(reporter, size1 == size2);
2643
2644 SkPath p2;
2645 size_t size3 = p2.readFromMemory(buffer, 1024);
2646 REPORTER_ASSERT(reporter, size1 == size3);
2647 REPORTER_ASSERT(reporter, p == p2);
2648
2649 size3 = p2.readFromMemory(buffer, 0);
2650 REPORTER_ASSERT(reporter, !size3);
2651
2652 SkPath tooShort;
2653 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2654 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2655
2656 char buffer2[1024];
2657 size3 = p2.writeToMemory(buffer2);
2658 REPORTER_ASSERT(reporter, size1 == size3);
2659 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2660
2661 // test persistence of the oval flag & convexity
2662 {
2663 SkPath oval;
2664 SkRect rect = SkRect::MakeWH(10, 10);
2665 oval.addOval(rect);
2666
2667 write_and_read_back(reporter, oval);
2668 }
Yuqian Li3154a532017-09-06 13:33:30 -04002669}
2670
2671static void test_transform(skiatest::Reporter* reporter) {
2672 SkPath p;
2673
2674#define CONIC_PERSPECTIVE_BUG_FIXED 0
2675 static const SkPoint pts[] = {
2676 { 0, 0 }, // move
2677 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2678 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2679 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2680#if CONIC_PERSPECTIVE_BUG_FIXED
2681 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2682#endif
2683 };
2684 const int kPtCount = SK_ARRAY_COUNT(pts);
2685
2686 p.moveTo(pts[0]);
2687 p.lineTo(pts[1]);
2688 p.quadTo(pts[2], pts[3]);
2689 p.cubicTo(pts[4], pts[5], pts[6]);
2690#if CONIC_PERSPECTIVE_BUG_FIXED
2691 p.conicTo(pts[4], pts[5], 0.5f);
2692#endif
2693 p.close();
2694
2695 {
2696 SkMatrix matrix;
2697 matrix.reset();
2698 SkPath p1;
2699 p.transform(matrix, &p1);
2700 REPORTER_ASSERT(reporter, p == p1);
2701 }
2702
2703
2704 {
2705 SkMatrix matrix;
2706 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2707
2708 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2709
2710 p.transform(matrix, &p1);
2711 SkPoint pts1[kPtCount];
2712 int count = p1.getPoints(pts1, kPtCount);
2713 REPORTER_ASSERT(reporter, kPtCount == count);
2714 for (int i = 0; i < count; ++i) {
2715 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2716 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2717 }
2718 }
2719
2720 {
2721 SkMatrix matrix;
2722 matrix.reset();
2723 matrix.setPerspX(4);
2724
2725 SkPath p1;
2726 p1.moveTo(SkPoint::Make(0, 0));
2727
Mike Reedbb59dfa2019-12-12 14:48:28 -05002728 p.transform(matrix, &p1, SkApplyPerspectiveClip::kNo);
Yuqian Li3154a532017-09-06 13:33:30 -04002729 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
Mike Reedbb59dfa2019-12-12 14:48:28 -05002730 p1.transform(matrix, nullptr, SkApplyPerspectiveClip::kNo);
Yuqian Li3154a532017-09-06 13:33:30 -04002731 SkRect pBounds = p.getBounds();
2732 SkRect p1Bounds = p1.getBounds();
2733 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2734 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2735 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2736 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2737 }
2738
2739 p.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00002740 p.addCircle(0, 0, 1, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002741
2742 {
2743 SkMatrix matrix;
2744 matrix.reset();
2745 SkPath p1;
2746 p1.moveTo(SkPoint::Make(0, 0));
2747
2748 p.transform(matrix, &p1);
Mike Reed85f51b22020-08-30 10:32:06 -04002749 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002750 }
2751
2752
2753 {
2754 SkMatrix matrix;
2755 matrix.reset();
2756 matrix.setScaleX(-1);
2757 SkPath p1;
2758 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2759
2760 p.transform(matrix, &p1);
Mike Reed85f51b22020-08-30 10:32:06 -04002761 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04002762 }
2763
2764 {
2765 SkMatrix matrix;
2766 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2767 SkPath p1;
2768 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2769
2770 p.transform(matrix, &p1);
Mike Reed85f51b22020-08-30 10:32:06 -04002771 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kUnknown);
Yuqian Li3154a532017-09-06 13:33:30 -04002772 }
Brian Osmandbfcd922019-03-07 16:50:30 -05002773
2774 {
2775 SkPath p1;
2776 p1.addRect({ 10, 20, 30, 40 });
2777 SkPath p2;
2778 p2.addRect({ 10, 20, 30, 40 });
2779 uint32_t id1 = p1.getGenerationID();
2780 uint32_t id2 = p2.getGenerationID();
Mike Reed8fda88e2019-09-05 14:14:38 -04002781 REPORTER_ASSERT(reporter, id1 != id2);
Brian Osmandbfcd922019-03-07 16:50:30 -05002782 SkMatrix matrix;
2783 matrix.setScale(2, 2);
2784 p1.transform(matrix, &p2);
Mike Reed8fda88e2019-09-05 14:14:38 -04002785 REPORTER_ASSERT(reporter, id1 == p1.getGenerationID());
2786 REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002787 p1.transform(matrix);
2788 REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002789 }
Yuqian Li3154a532017-09-06 13:33:30 -04002790}
2791
2792static void test_zero_length_paths(skiatest::Reporter* reporter) {
2793 SkPath p;
2794 uint8_t verbs[32];
2795
2796 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2797 const char* testPath;
2798 const size_t numResultPts;
2799 const SkRect resultBound;
2800 const SkPath::Verb* resultVerbs;
2801 const size_t numResultVerbs;
2802 };
2803
2804 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2805 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2806 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2807 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2808 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2809 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2810 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2811 static const SkPath::Verb resultVerbs8[] = {
2812 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2813 };
2814 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2815 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2816 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2817 static const SkPath::Verb resultVerbs12[] = {
2818 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2819 };
2820 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2821 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2822 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2823 static const SkPath::Verb resultVerbs16[] = {
2824 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2825 };
2826 static const struct zeroPathTestData gZeroLengthTests[] = {
2827 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2828 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2829 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2830 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2831 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2832 { "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) },
2833 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2834 { "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) },
2835 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2836 { "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) },
2837 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2838 { "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) },
2839 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2840 { "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,
2841 SK_ARRAY_COUNT(resultVerbs14)
2842 },
2843 { "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) },
2844 { "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,
2845 SK_ARRAY_COUNT(resultVerbs16)
2846 }
2847 };
2848
2849 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2850 p.reset();
2851 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2852 REPORTER_ASSERT(reporter, valid);
2853 REPORTER_ASSERT(reporter, !p.isEmpty());
2854 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2855 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2856 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2857 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2858 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2859 }
2860 }
2861}
2862
2863struct SegmentInfo {
2864 SkPath fPath;
2865 int fPointCount;
2866};
2867
2868#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2869
2870static void test_segment_masks(skiatest::Reporter* reporter) {
2871 SkPath p, p2;
2872
2873 p.moveTo(0, 0);
2874 p.quadTo(100, 100, 200, 200);
2875 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2876 REPORTER_ASSERT(reporter, !p.isEmpty());
2877 p2 = p;
2878 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2879 p.cubicTo(100, 100, 200, 200, 300, 300);
2880 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2881 REPORTER_ASSERT(reporter, !p.isEmpty());
2882 p2 = p;
2883 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2884
2885 p.reset();
2886 p.moveTo(0, 0);
2887 p.cubicTo(100, 100, 200, 200, 300, 300);
2888 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2889 p2 = p;
2890 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2891
2892 REPORTER_ASSERT(reporter, !p.isEmpty());
2893}
2894
2895static void test_iter(skiatest::Reporter* reporter) {
2896 SkPath p;
2897 SkPoint pts[4];
2898
2899 // Test an iterator with no path
2900 SkPath::Iter noPathIter;
2901 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2902
2903 // Test that setting an empty path works
2904 noPathIter.setPath(p, false);
2905 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2906
2907 // Test that close path makes no difference for an empty path
2908 noPathIter.setPath(p, true);
2909 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2910
2911 // Test an iterator with an initial empty path
2912 SkPath::Iter iter(p, false);
2913 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2914
2915 // Test that close path makes no difference
2916 iter.setPath(p, true);
2917 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2918
2919
2920 struct iterTestData {
2921 const char* testPath;
2922 const bool forceClose;
Yuqian Li3154a532017-09-06 13:33:30 -04002923 const size_t* numResultPtsPerVerb;
2924 const SkPoint* resultPts;
2925 const SkPath::Verb* resultVerbs;
2926 const size_t numResultVerbs;
2927 };
2928
2929 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2930 static const SkPath::Verb resultVerbs2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002931 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2932 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002933 static const SkPath::Verb resultVerbs3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002934 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2935 };
2936 static const size_t resultPtsSizes1[] = { 0 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002937 static const size_t resultPtsSizes2[] = { 1, 2, 1, 1, 0 };
2938 static const size_t resultPtsSizes3[] = { 1, 2, 1, 1, 1, 0 };
Yuqian Li3154a532017-09-06 13:33:30 -04002939 static const SkPoint* resultPts1 = nullptr;
2940 static const SkPoint resultPts2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002941 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2942 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002943 static const SkPoint resultPts3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002944 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2945 };
2946 static const struct iterTestData gIterTests[] = {
Mike Reedba7e9a62019-08-16 13:30:34 -04002947 { "M 1 0", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2948 { "z", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2949 { "z", true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2950 { "M 1 0 L 1 0 M 0 0 z", false, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2951 { "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 -04002952 };
2953
2954 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2955 p.reset();
2956 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2957 REPORTER_ASSERT(reporter, valid);
2958 iter.setPath(p, gIterTests[i].forceClose);
2959 int j = 0, l = 0;
2960 do {
Mike Reedba7e9a62019-08-16 13:30:34 -04002961 REPORTER_ASSERT(reporter, iter.next(pts) == gIterTests[i].resultVerbs[j]);
Yuqian Li3154a532017-09-06 13:33:30 -04002962 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2963 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2964 }
2965 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2966 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2967 }
2968
2969 p.reset();
2970 iter.setPath(p, false);
2971 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2972 p.lineTo(1, 1);
2973 p.close();
2974 iter.setPath(p, false);
2975 REPORTER_ASSERT(reporter, iter.isClosedContour());
2976 p.reset();
2977 iter.setPath(p, true);
2978 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2979 p.lineTo(1, 1);
2980 iter.setPath(p, true);
2981 REPORTER_ASSERT(reporter, iter.isClosedContour());
2982 p.moveTo(0, 0);
2983 p.lineTo(2, 2);
2984 iter.setPath(p, false);
2985 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2986
2987 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2988 // check to see if the result is correct.
2989 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2990 p.reset();
2991 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2992 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2993 iter.setPath(p, true);
Mike Reedba7e9a62019-08-16 13:30:34 -04002994 iter.next(pts);
2995 iter.next(pts);
2996 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04002997 }
2998
2999 p.reset();
3000 p.quadTo(0, 0, 0, 0);
3001 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003002 iter.next(pts);
3003 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003004
3005 p.reset();
3006 p.conicTo(0, 0, 0, 0, 0.5f);
3007 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003008 iter.next(pts);
3009 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003010
3011 p.reset();
3012 p.cubicTo(0, 0, 0, 0, 0, 0);
3013 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003014 iter.next(pts);
3015 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003016
3017 p.moveTo(1, 1); // add a trailing moveto
3018 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003019 iter.next(pts);
3020 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003021
3022 // The GM degeneratesegments.cpp test is more extensive
3023
3024 // Test out mixed degenerate and non-degenerate geometry with Conics
3025 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3026 SkRect r = SkRect::MakeWH(100, 100);
3027 SkRRect rr;
3028 rr.setRectRadii(r, radii);
3029 p.reset();
3030 p.addRRect(rr);
3031 iter.setPath(p, false);
3032 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3033 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
Mike Reedba7e9a62019-08-16 13:30:34 -04003034 return;
Yuqian Li3154a532017-09-06 13:33:30 -04003035 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3036 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3037 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
3038}
3039
Chris Daltonde500372020-05-05 15:06:30 -06003040static void test_range_iter(skiatest::Reporter* reporter) {
3041 SkPath path;
Yuqian Li3154a532017-09-06 13:33:30 -04003042
3043 // Test an iterator with an initial empty path
Chris Daltonde500372020-05-05 15:06:30 -06003044 SkPathPriv::Iterate iterate(path);
3045 REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
Yuqian Li3154a532017-09-06 13:33:30 -04003046
3047 // Test that a move-only path returns the move.
Chris Daltonde500372020-05-05 15:06:30 -06003048 path.moveTo(SK_Scalar1, 0);
3049 iterate = SkPathPriv::Iterate(path);
3050 SkPathPriv::RangeIter iter = iterate.begin();
3051 {
3052 auto [verb, pts, w] = *iter++;
3053 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3054 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3055 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3056 }
3057 REPORTER_ASSERT(reporter, iter == iterate.end());
Yuqian Li3154a532017-09-06 13:33:30 -04003058
3059 // No matter how many moves we add, we should get them all back
Chris Daltonde500372020-05-05 15:06:30 -06003060 path.moveTo(SK_Scalar1*2, SK_Scalar1);
3061 path.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3062 iterate = SkPathPriv::Iterate(path);
3063 iter = iterate.begin();
3064 {
3065 auto [verb, pts, w] = *iter++;
3066 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3067 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3068 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3069 }
3070 {
3071 auto [verb, pts, w] = *iter++;
3072 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3073 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3074 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3075 }
3076 {
3077 auto [verb, pts, w] = *iter++;
3078 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3079 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3080 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3081 }
3082 REPORTER_ASSERT(reporter, iter == iterate.end());
Yuqian Li3154a532017-09-06 13:33:30 -04003083
3084 // Initial close is never ever stored
Chris Daltonde500372020-05-05 15:06:30 -06003085 path.reset();
3086 path.close();
3087 iterate = SkPathPriv::Iterate(path);
3088 REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
Yuqian Li3154a532017-09-06 13:33:30 -04003089
3090 // Move/close sequences
Chris Daltonde500372020-05-05 15:06:30 -06003091 path.reset();
3092 path.close(); // Not stored, no purpose
3093 path.moveTo(SK_Scalar1, 0);
3094 path.close();
3095 path.close(); // Not stored, no purpose
3096 path.moveTo(SK_Scalar1*2, SK_Scalar1);
3097 path.close();
3098 path.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3099 path.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3100 path.close();
3101 iterate = SkPathPriv::Iterate(path);
3102 iter = iterate.begin();
3103 {
3104 auto [verb, pts, w] = *iter++;
3105 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3106 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3107 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3108 }
3109 {
3110 auto [verb, pts, w] = *iter++;
3111 REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3112 }
3113 {
3114 auto [verb, pts, w] = *iter++;
3115 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3116 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3117 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3118 }
3119 {
3120 auto [verb, pts, w] = *iter++;
3121 REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3122 }
3123 {
3124 auto [verb, pts, w] = *iter++;
3125 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3126 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3127 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3128 }
3129 {
3130 auto [verb, pts, w] = *iter++;
3131 REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3132 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3133 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3134 }
3135 {
3136 auto [verb, pts, w] = *iter++;
3137 REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3138 }
3139 REPORTER_ASSERT(reporter, iter == iterate.end());
Yuqian Li3154a532017-09-06 13:33:30 -04003140
3141 // Generate random paths and verify
3142 SkPoint randomPts[25];
3143 for (int i = 0; i < 5; ++i) {
3144 for (int j = 0; j < 5; ++j) {
3145 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3146 }
3147 }
3148
3149 // Max of 10 segments, max 3 points per segment
3150 SkRandom rand(9876543);
Chris Daltonde500372020-05-05 15:06:30 -06003151 SkPoint expectedPts[31]; // May have leading moveTo
3152 SkPathVerb expectedVerbs[22]; // May have leading moveTo
3153 SkPathVerb nextVerb;
Yuqian Li3154a532017-09-06 13:33:30 -04003154
3155 for (int i = 0; i < 500; ++i) {
Chris Daltonde500372020-05-05 15:06:30 -06003156 path.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003157 bool lastWasClose = true;
3158 bool haveMoveTo = false;
3159 SkPoint lastMoveToPt = { 0, 0 };
3160 int numPoints = 0;
3161 int numVerbs = (rand.nextU() >> 16) % 10;
3162 int numIterVerbs = 0;
3163 for (int j = 0; j < numVerbs; ++j) {
3164 do {
Chris Daltonde500372020-05-05 15:06:30 -06003165 nextVerb = static_cast<SkPathVerb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3166 } while (lastWasClose && nextVerb == SkPathVerb::kClose);
Yuqian Li3154a532017-09-06 13:33:30 -04003167 switch (nextVerb) {
Chris Daltonde500372020-05-05 15:06:30 -06003168 case SkPathVerb::kMove:
Yuqian Li3154a532017-09-06 13:33:30 -04003169 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
Chris Daltonde500372020-05-05 15:06:30 -06003170 path.moveTo(expectedPts[numPoints]);
Yuqian Li3154a532017-09-06 13:33:30 -04003171 lastMoveToPt = expectedPts[numPoints];
3172 numPoints += 1;
3173 lastWasClose = false;
3174 haveMoveTo = true;
3175 break;
Chris Daltonde500372020-05-05 15:06:30 -06003176 case SkPathVerb::kLine:
Yuqian Li3154a532017-09-06 13:33:30 -04003177 if (!haveMoveTo) {
3178 expectedPts[numPoints++] = lastMoveToPt;
Chris Daltonde500372020-05-05 15:06:30 -06003179 expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
Yuqian Li3154a532017-09-06 13:33:30 -04003180 haveMoveTo = true;
3181 }
3182 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
Chris Daltonde500372020-05-05 15:06:30 -06003183 path.lineTo(expectedPts[numPoints]);
Yuqian Li3154a532017-09-06 13:33:30 -04003184 numPoints += 1;
3185 lastWasClose = false;
3186 break;
Chris Daltonde500372020-05-05 15:06:30 -06003187 case SkPathVerb::kQuad:
Yuqian Li3154a532017-09-06 13:33:30 -04003188 if (!haveMoveTo) {
3189 expectedPts[numPoints++] = lastMoveToPt;
Chris Daltonde500372020-05-05 15:06:30 -06003190 expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
Yuqian Li3154a532017-09-06 13:33:30 -04003191 haveMoveTo = true;
3192 }
3193 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3194 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
Chris Daltonde500372020-05-05 15:06:30 -06003195 path.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
Yuqian Li3154a532017-09-06 13:33:30 -04003196 numPoints += 2;
3197 lastWasClose = false;
3198 break;
Chris Daltonde500372020-05-05 15:06:30 -06003199 case SkPathVerb::kConic:
Yuqian Li3154a532017-09-06 13:33:30 -04003200 if (!haveMoveTo) {
3201 expectedPts[numPoints++] = lastMoveToPt;
Chris Daltonde500372020-05-05 15:06:30 -06003202 expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
Yuqian Li3154a532017-09-06 13:33:30 -04003203 haveMoveTo = true;
3204 }
3205 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3206 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
Chris Daltonde500372020-05-05 15:06:30 -06003207 path.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3208 rand.nextUScalar1() * 4);
Yuqian Li3154a532017-09-06 13:33:30 -04003209 numPoints += 2;
3210 lastWasClose = false;
3211 break;
Chris Daltonde500372020-05-05 15:06:30 -06003212 case SkPathVerb::kCubic:
Yuqian Li3154a532017-09-06 13:33:30 -04003213 if (!haveMoveTo) {
3214 expectedPts[numPoints++] = lastMoveToPt;
Chris Daltonde500372020-05-05 15:06:30 -06003215 expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
Yuqian Li3154a532017-09-06 13:33:30 -04003216 haveMoveTo = true;
3217 }
3218 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3219 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3220 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
Chris Daltonde500372020-05-05 15:06:30 -06003221 path.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3222 expectedPts[numPoints + 2]);
Yuqian Li3154a532017-09-06 13:33:30 -04003223 numPoints += 3;
3224 lastWasClose = false;
3225 break;
Chris Daltonde500372020-05-05 15:06:30 -06003226 case SkPathVerb::kClose:
3227 path.close();
Yuqian Li3154a532017-09-06 13:33:30 -04003228 haveMoveTo = false;
3229 lastWasClose = true;
3230 break;
3231 default:
3232 SkDEBUGFAIL("unexpected verb");
3233 }
3234 expectedVerbs[numIterVerbs++] = nextVerb;
3235 }
3236
Yuqian Li3154a532017-09-06 13:33:30 -04003237 numVerbs = numIterVerbs;
3238 numIterVerbs = 0;
3239 int numIterPts = 0;
3240 SkPoint lastMoveTo;
3241 SkPoint lastPt;
3242 lastMoveTo.set(0, 0);
3243 lastPt.set(0, 0);
Brian Salomon3dbd1592021-08-17 09:08:17 -04003244 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
3245 REPORTER_ASSERT(reporter, verb == expectedVerbs[numIterVerbs]);
Yuqian Li3154a532017-09-06 13:33:30 -04003246 numIterVerbs++;
Brian Salomon3dbd1592021-08-17 09:08:17 -04003247 switch (verb) {
Chris Daltonde500372020-05-05 15:06:30 -06003248 case SkPathVerb::kMove:
Yuqian Li3154a532017-09-06 13:33:30 -04003249 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3250 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3251 lastPt = lastMoveTo = pts[0];
3252 numIterPts += 1;
3253 break;
Chris Daltonde500372020-05-05 15:06:30 -06003254 case SkPathVerb::kLine:
Yuqian Li3154a532017-09-06 13:33:30 -04003255 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3256 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3257 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3258 lastPt = pts[1];
3259 numIterPts += 1;
3260 break;
Chris Daltonde500372020-05-05 15:06:30 -06003261 case SkPathVerb::kQuad:
3262 case SkPathVerb::kConic:
Yuqian Li3154a532017-09-06 13:33:30 -04003263 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3264 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3265 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3266 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3267 lastPt = pts[2];
3268 numIterPts += 2;
3269 break;
Chris Daltonde500372020-05-05 15:06:30 -06003270 case SkPathVerb::kCubic:
Yuqian Li3154a532017-09-06 13:33:30 -04003271 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3272 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3273 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3274 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3275 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3276 lastPt = pts[3];
3277 numIterPts += 3;
3278 break;
Chris Daltonde500372020-05-05 15:06:30 -06003279 case SkPathVerb::kClose:
Yuqian Li3154a532017-09-06 13:33:30 -04003280 lastPt = lastMoveTo;
3281 break;
3282 default:
3283 SkDEBUGFAIL("unexpected verb");
3284 }
3285 }
3286 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3287 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3288 }
3289}
3290
3291static void check_for_circle(skiatest::Reporter* reporter,
3292 const SkPath& path,
3293 bool expectedCircle,
Mike Reed3872c982020-08-29 17:46:51 -04003294 SkPathFirstDirection expectedDir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003295 SkRect rect = SkRect::MakeEmpty();
3296 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
Mike Reed30bc5272019-11-22 18:34:02 +00003297 SkPathDirection isOvalDir;
Yuqian Li3154a532017-09-06 13:33:30 -04003298 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003299 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003300 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3301 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3302 SkPath tmpPath;
3303 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3304 REPORTER_ASSERT(reporter, path == tmpPath);
3305 }
Mike Reed85f51b22020-08-30 10:32:06 -04003306 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == expectedDir);
Yuqian Li3154a532017-09-06 13:33:30 -04003307}
3308
3309static void test_circle_skew(skiatest::Reporter* reporter,
3310 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003311 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003312 SkPath tmp;
3313
3314 SkMatrix m;
3315 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3316 path.transform(m, &tmp);
3317 // this matrix reverses the direction.
Mike Reed3872c982020-08-29 17:46:51 -04003318 if (SkPathFirstDirection::kCCW == dir) {
3319 dir = SkPathFirstDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003320 } else {
Mike Reed3872c982020-08-29 17:46:51 -04003321 REPORTER_ASSERT(reporter, SkPathFirstDirection::kCW == dir);
3322 dir = SkPathFirstDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003323 }
3324 check_for_circle(reporter, tmp, false, dir);
3325}
3326
3327static void test_circle_translate(skiatest::Reporter* reporter,
3328 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003329 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003330 SkPath tmp;
3331
3332 // translate at small offset
3333 SkMatrix m;
3334 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3335 path.transform(m, &tmp);
3336 check_for_circle(reporter, tmp, true, dir);
3337
3338 tmp.reset();
3339 m.reset();
3340
3341 // translate at a relatively big offset
3342 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3343 path.transform(m, &tmp);
3344 check_for_circle(reporter, tmp, true, dir);
3345}
3346
3347static void test_circle_rotate(skiatest::Reporter* reporter,
3348 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003349 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003350 for (int angle = 0; angle < 360; ++angle) {
3351 SkPath tmp;
3352 SkMatrix m;
3353 m.setRotate(SkIntToScalar(angle));
3354 path.transform(m, &tmp);
3355
3356 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3357 // degrees is not an oval anymore, this can be improved. we made this
3358 // for the simplicity of our implementation.
3359 if (angle % 90 == 0) {
3360 check_for_circle(reporter, tmp, true, dir);
3361 } else {
3362 check_for_circle(reporter, tmp, false, dir);
3363 }
3364 }
3365}
3366
3367static void test_circle_mirror_x(skiatest::Reporter* reporter,
3368 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003369 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003370 SkPath tmp;
3371 SkMatrix m;
3372 m.reset();
3373 m.setScaleX(-SK_Scalar1);
3374 path.transform(m, &tmp);
Mike Reed3872c982020-08-29 17:46:51 -04003375 if (SkPathFirstDirection::kCW == dir) {
3376 dir = SkPathFirstDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003377 } else {
Mike Reed3872c982020-08-29 17:46:51 -04003378 REPORTER_ASSERT(reporter, SkPathFirstDirection::kCCW == dir);
3379 dir = SkPathFirstDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003380 }
3381 check_for_circle(reporter, tmp, true, dir);
3382}
3383
3384static void test_circle_mirror_y(skiatest::Reporter* reporter,
3385 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003386 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003387 SkPath tmp;
3388 SkMatrix m;
3389 m.reset();
3390 m.setScaleY(-SK_Scalar1);
3391 path.transform(m, &tmp);
3392
Mike Reed3872c982020-08-29 17:46:51 -04003393 if (SkPathFirstDirection::kCW == dir) {
3394 dir = SkPathFirstDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003395 } else {
Mike Reed3872c982020-08-29 17:46:51 -04003396 REPORTER_ASSERT(reporter, SkPathFirstDirection::kCCW == dir);
3397 dir = SkPathFirstDirection::kCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003398 }
3399
3400 check_for_circle(reporter, tmp, true, dir);
3401}
3402
3403static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3404 const SkPath& path,
Mike Reed3872c982020-08-29 17:46:51 -04003405 SkPathFirstDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003406 SkPath tmp;
3407 SkMatrix m;
3408 m.reset();
3409 m.setScaleX(-SK_Scalar1);
3410 m.setScaleY(-SK_Scalar1);
3411 path.transform(m, &tmp);
3412
3413 check_for_circle(reporter, tmp, true, dir);
3414}
3415
3416static void test_circle_with_direction(skiatest::Reporter* reporter,
Mike Reed30bc5272019-11-22 18:34:02 +00003417 SkPathDirection inDir) {
Mike Reed3872c982020-08-29 17:46:51 -04003418 const SkPathFirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
Yuqian Li3154a532017-09-06 13:33:30 -04003419 SkPath path;
3420
3421 // circle at origin
3422 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3423
3424 check_for_circle(reporter, path, true, dir);
3425 test_circle_rotate(reporter, path, dir);
3426 test_circle_translate(reporter, path, dir);
3427 test_circle_skew(reporter, path, dir);
3428 test_circle_mirror_x(reporter, path, dir);
3429 test_circle_mirror_y(reporter, path, dir);
3430 test_circle_mirror_xy(reporter, path, dir);
3431
3432 // circle at an offset at (10, 10)
3433 path.reset();
3434 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3435 SkIntToScalar(20), inDir);
3436
3437 check_for_circle(reporter, path, true, dir);
3438 test_circle_rotate(reporter, path, dir);
3439 test_circle_translate(reporter, path, dir);
3440 test_circle_skew(reporter, path, dir);
3441 test_circle_mirror_x(reporter, path, dir);
3442 test_circle_mirror_y(reporter, path, dir);
3443 test_circle_mirror_xy(reporter, path, dir);
3444
3445 // Try different starting points for the contour.
3446 for (unsigned start = 0; start < 4; ++start) {
3447 path.reset();
3448 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3449 test_circle_rotate(reporter, path, dir);
3450 test_circle_translate(reporter, path, dir);
3451 test_circle_skew(reporter, path, dir);
3452 test_circle_mirror_x(reporter, path, dir);
3453 test_circle_mirror_y(reporter, path, dir);
3454 test_circle_mirror_xy(reporter, path, dir);
3455 }
3456}
3457
3458static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3459 SkPath path;
3460 SkPath circle;
3461 SkPath rect;
3462 SkPath empty;
3463
Mike Reed30bc5272019-11-22 18:34:02 +00003464 const SkPathDirection kCircleDir = SkPathDirection::kCW;
3465 const SkPathDirection kCircleDirOpposite = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003466
3467 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3468 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
Mike Reed30bc5272019-11-22 18:34:02 +00003469 SkIntToScalar(20), SkIntToScalar(20), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003470
3471 SkMatrix translate;
3472 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3473
3474 // Although all the path concatenation related operations leave
3475 // the path a circle, most mark it as a non-circle for simplicity
3476
3477 // empty + circle (translate)
3478 path = empty;
3479 path.addPath(circle, translate);
3480 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3481
3482 // circle + empty (translate)
3483 path = circle;
3484 path.addPath(empty, translate);
3485
3486 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3487
3488 // test reverseAddPath
3489 path = circle;
3490 path.reverseAddPath(rect);
3491 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3492}
3493
3494static void test_circle(skiatest::Reporter* reporter) {
Mike Reed30bc5272019-11-22 18:34:02 +00003495 test_circle_with_direction(reporter, SkPathDirection::kCW);
3496 test_circle_with_direction(reporter, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003497
3498 // multiple addCircle()
3499 SkPath path;
Mike Reed30bc5272019-11-22 18:34:02 +00003500 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
3501 path.addCircle(0, 0, SkIntToScalar(20), SkPathDirection::kCW);
Mike Reed3872c982020-08-29 17:46:51 -04003502 check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003503
3504 // some extra lineTo() would make isOval() fail
3505 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003506 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003507 path.lineTo(0, 0);
Mike Reed3872c982020-08-29 17:46:51 -04003508 check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003509
3510 // not back to the original point
3511 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003512 path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003513 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
Mike Reed3872c982020-08-29 17:46:51 -04003514 check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003515
3516 test_circle_with_add_paths(reporter);
3517
3518 // test negative radius
3519 path.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003520 path.addCircle(0, 0, -1, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003521 REPORTER_ASSERT(reporter, path.isEmpty());
3522}
3523
3524static void test_oval(skiatest::Reporter* reporter) {
3525 SkRect rect;
3526 SkMatrix m;
3527 SkPath path;
3528 unsigned start = 0;
Mike Reed30bc5272019-11-22 18:34:02 +00003529 SkPathDirection dir = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003530
3531 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3532 path.addOval(rect);
3533
3534 // Defaults to dir = CW and start = 1
3535 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3536
3537 m.setRotate(SkIntToScalar(90));
3538 SkPath tmp;
3539 path.transform(m, &tmp);
3540 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3541 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003542 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003543 REPORTER_ASSERT(reporter, 2 == start);
Mike Reed30bc5272019-11-22 18:34:02 +00003544 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003545
3546 m.reset();
3547 m.setRotate(SkIntToScalar(30));
3548 tmp.reset();
3549 path.transform(m, &tmp);
3550 // an oval rotated 30 degrees is not an oval anymore.
3551 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3552
3553 // since empty path being transformed.
3554 path.reset();
3555 tmp.reset();
3556 m.reset();
3557 path.transform(m, &tmp);
3558 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3559
3560 // empty path is not an oval
3561 tmp.reset();
3562 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3563
3564 // only has moveTo()s
3565 tmp.reset();
3566 tmp.moveTo(0, 0);
3567 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3568 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3569
3570 // mimic WebKit's calling convention,
3571 // call moveTo() first and then call addOval()
3572 path.reset();
3573 path.moveTo(0, 0);
3574 path.addOval(rect);
3575 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3576
3577 // copy path
3578 path.reset();
3579 tmp.reset();
3580 tmp.addOval(rect);
3581 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003582 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Mike Reed30bc5272019-11-22 18:34:02 +00003583 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003584 REPORTER_ASSERT(reporter, 1 == start);
3585}
3586
3587static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3588 SkPath empty;
3589
3590 REPORTER_ASSERT(reporter, p.isEmpty());
3591 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3592 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3593 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3594 REPORTER_ASSERT(reporter, p.isConvex());
Mike Reedcf0e3c62019-12-03 16:26:15 -05003595 REPORTER_ASSERT(reporter, p.getFillType() == SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04003596 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3597 REPORTER_ASSERT(reporter, p == empty);
3598 REPORTER_ASSERT(reporter, !(p != empty));
3599}
3600
3601static void test_rrect_is_convex(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());
Mike Reed85f51b22020-08-30 10:32:06 -04003604 REPORTER_ASSERT(reporter,
3605 SkPathPriv::ComputeFirstDirection(*path) == SkPathPriv::AsFirstDirection(dir));
Mike Reed6052c0b2020-08-28 11:49:56 -04003606 SkPathPriv::ForceComputeConvexity(*path);
Yuqian Li3154a532017-09-06 13:33:30 -04003607 REPORTER_ASSERT(reporter, path->isConvex());
3608 path->reset();
3609}
3610
3611static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
Mike Reed30bc5272019-11-22 18:34:02 +00003612 SkPathDirection dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003613 REPORTER_ASSERT(reporter, path->isConvex());
Mike Reed85f51b22020-08-30 10:32:06 -04003614 REPORTER_ASSERT(reporter,
3615 SkPathPriv::ComputeFirstDirection(*path) == SkPathPriv::AsFirstDirection(dir));
Mike Reed6052c0b2020-08-28 11:49:56 -04003616 SkPathPriv::ForceComputeConvexity(*path);
3617 REPORTER_ASSERT(reporter, !path->isConvex());
Yuqian Li3154a532017-09-06 13:33:30 -04003618 path->reset();
3619}
3620
3621static void test_rrect(skiatest::Reporter* reporter) {
3622 SkPath p;
3623 SkRRect rr;
3624 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3625 SkRect r = {10, 20, 30, 40};
3626 rr.setRectRadii(r, radii);
3627 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003628 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3629 p.addRRect(rr, SkPathDirection::kCCW);
3630 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003631 p.addRoundRect(r, &radii[0].fX);
Mike Reed30bc5272019-11-22 18:34:02 +00003632 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3633 p.addRoundRect(r, &radii[0].fX, SkPathDirection::kCCW);
3634 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003635 p.addRoundRect(r, radii[1].fX, radii[1].fY);
Mike Reed30bc5272019-11-22 18:34:02 +00003636 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3637 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPathDirection::kCCW);
3638 test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003639 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3640 SkVector save = radii[i];
3641 radii[i].set(0, 0);
3642 rr.setRectRadii(r, radii);
3643 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003644 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003645 radii[i] = save;
3646 }
3647 p.addRoundRect(r, 0, 0);
3648 SkRect returnedRect;
3649 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3650 REPORTER_ASSERT(reporter, returnedRect == r);
Mike Reed30bc5272019-11-22 18:34:02 +00003651 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003652 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3653 rr.setRectRadii(r, zeroRadii);
3654 p.addRRect(rr);
3655 bool closed;
Mike Reed30bc5272019-11-22 18:34:02 +00003656 SkPathDirection dir;
Yuqian Li3154a532017-09-06 13:33:30 -04003657 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3658 REPORTER_ASSERT(reporter, closed);
Mike Reed30bc5272019-11-22 18:34:02 +00003659 REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
3660 test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3661 p.addRRect(rr, SkPathDirection::kCW);
3662 p.addRRect(rr, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003663 REPORTER_ASSERT(reporter, !p.isConvex());
3664 p.reset();
Mike Reed30bc5272019-11-22 18:34:02 +00003665 p.addRRect(rr, SkPathDirection::kCCW);
3666 p.addRRect(rr, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003667 REPORTER_ASSERT(reporter, !p.isConvex());
3668 p.reset();
3669 SkRect emptyR = {10, 20, 10, 30};
3670 rr.setRectRadii(emptyR, radii);
3671 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003672 // The round rect is "empty" in that it has no fill area. However,
3673 // the path isn't "empty" in that it should have verbs and points.
3674 REPORTER_ASSERT(reporter, !p.isEmpty());
3675 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003676 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3677 rr.setRectRadii(largeR, radii);
3678 p.addRRect(rr);
Mike Reed30bc5272019-11-22 18:34:02 +00003679 test_rrect_convexity_is_unknown(reporter, &p, SkPathDirection::kCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003680
3681 // we check for non-finites
3682 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3683 rr.setRectRadii(infR, radii);
3684 REPORTER_ASSERT(reporter, rr.isEmpty());
Yuqian Li3154a532017-09-06 13:33:30 -04003685}
3686
3687static void test_arc(skiatest::Reporter* reporter) {
3688 SkPath p;
3689 SkRect emptyOval = {10, 20, 30, 20};
3690 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3691 p.addArc(emptyOval, 1, 2);
3692 REPORTER_ASSERT(reporter, p.isEmpty());
3693 p.reset();
3694 SkRect oval = {10, 20, 30, 40};
3695 p.addArc(oval, 1, 0);
3696 REPORTER_ASSERT(reporter, p.isEmpty());
3697 p.reset();
3698 SkPath cwOval;
3699 cwOval.addOval(oval);
3700 p.addArc(oval, 0, 360);
3701 REPORTER_ASSERT(reporter, p == cwOval);
3702 p.reset();
3703 SkPath ccwOval;
Mike Reed30bc5272019-11-22 18:34:02 +00003704 ccwOval.addOval(oval, SkPathDirection::kCCW);
Yuqian Li3154a532017-09-06 13:33:30 -04003705 p.addArc(oval, 0, -360);
3706 REPORTER_ASSERT(reporter, p == ccwOval);
3707 p.reset();
3708 p.addArc(oval, 1, 180);
Cary Clarkc9b7c722018-12-12 14:50:23 -05003709 // diagonal colinear points make arc convex
3710 // TODO: one way to keep it concave would be to introduce interpolated on curve points
3711 // between control points and computing the on curve point at scan conversion time
Mike Reed6052c0b2020-08-28 11:49:56 -04003712 REPORTER_ASSERT(reporter, p.isConvex());
Mike Reed85f51b22020-08-30 10:32:06 -04003713 REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p) == SkPathFirstDirection::kCW);
Mike Reed6052c0b2020-08-28 11:49:56 -04003714 SkPathPriv::ForceComputeConvexity(p);
3715 REPORTER_ASSERT(reporter, p.isConvex());
Yuqian Li3154a532017-09-06 13:33:30 -04003716}
3717
3718static inline SkScalar oval_start_index_to_angle(unsigned start) {
3719 switch (start) {
3720 case 0:
3721 return 270.f;
3722 case 1:
3723 return 0.f;
3724 case 2:
3725 return 90.f;
3726 case 3:
3727 return 180.f;
3728 default:
3729 return -1.f;
3730 }
3731}
3732
3733static inline SkScalar canonical_start_angle(float angle) {
3734 while (angle < 0.f) {
3735 angle += 360.f;
3736 }
3737 while (angle >= 360.f) {
3738 angle -= 360.f;
3739 }
3740 return angle;
3741}
3742
3743static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3744 const SkPath& path) {
3745 SkRect r = SkRect::MakeEmpty();
Mike Reed30bc5272019-11-22 18:34:02 +00003746 SkPathDirection d = SkPathDirection::kCCW;
Yuqian Li3154a532017-09-06 13:33:30 -04003747 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003748 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003749 REPORTER_ASSERT(reporter, isOval);
3750 SkPath recreatedPath;
3751 recreatedPath.addOval(r, d, s);
3752 REPORTER_ASSERT(reporter, path == recreatedPath);
3753 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
Mike Reed30bc5272019-11-22 18:34:02 +00003754 REPORTER_ASSERT(reporter, (SkPathDirection::kCW == d) == (sweep > 0.f));
Yuqian Li3154a532017-09-06 13:33:30 -04003755}
3756
3757static void test_arc_ovals(skiatest::Reporter* reporter) {
3758 SkRect oval = SkRect::MakeWH(10, 20);
3759 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3760 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3761 SkPath path;
3762 path.addArc(oval, start, sweep);
3763 // SkPath's interfaces for inserting and extracting ovals only allow contours
3764 // to start at multiples of 90 degrees.
3765 if (std::fmod(start, 90.f) == 0) {
3766 check_oval_arc(reporter, start, sweep, path);
3767 } else {
3768 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3769 }
3770 }
3771 // Test start angles that are nearly at valid oval start angles.
3772 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3773 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3774 SkPath path;
3775 path.addArc(oval, start + delta, sweep);
3776 check_oval_arc(reporter, start, sweep, path);
3777 }
3778 }
3779 }
3780}
3781
Chris Daltonde500372020-05-05 15:06:30 -06003782static void check_move(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
Yuqian Li3154a532017-09-06 13:33:30 -04003783 SkScalar x0, SkScalar y0) {
Chris Daltonde500372020-05-05 15:06:30 -06003784 auto [v, pts, w] = *(*iter)++;
3785 REPORTER_ASSERT(reporter, v == SkPathVerb::kMove);
Yuqian Li3154a532017-09-06 13:33:30 -04003786 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3787 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3788}
3789
Chris Daltonde500372020-05-05 15:06:30 -06003790static void check_line(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
Yuqian Li3154a532017-09-06 13:33:30 -04003791 SkScalar x1, SkScalar y1) {
Chris Daltonde500372020-05-05 15:06:30 -06003792 auto [v, pts, w] = *(*iter)++;
3793 REPORTER_ASSERT(reporter, v == SkPathVerb::kLine);
Yuqian Li3154a532017-09-06 13:33:30 -04003794 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3795 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3796}
3797
Chris Daltonde500372020-05-05 15:06:30 -06003798static void check_quad(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
Yuqian Li3154a532017-09-06 13:33:30 -04003799 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
Chris Daltonde500372020-05-05 15:06:30 -06003800 auto [v, pts, w] = *(*iter)++;
3801 REPORTER_ASSERT(reporter, v == SkPathVerb::kQuad);
Yuqian Li3154a532017-09-06 13:33:30 -04003802 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3803 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3804 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3805 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3806}
3807
William Hesse3c246112021-08-05 18:27:06 +02003808static void check_close(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter) {
3809 auto [v, pts, w] = *(*iter)++;
3810 REPORTER_ASSERT(reporter, v == SkPathVerb::kClose);
3811}
3812
Chris Daltonde500372020-05-05 15:06:30 -06003813static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPathPriv::RangeIter* iter) {
3814 REPORTER_ASSERT(reporter, *iter == SkPathPriv::Iterate(*p).end());
Yuqian Li3154a532017-09-06 13:33:30 -04003815}
3816
Chris Daltonde500372020-05-05 15:06:30 -06003817static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p,
3818 SkPathPriv::RangeIter* iter) {
Yuqian Li3154a532017-09-06 13:33:30 -04003819 check_done(reporter, p, iter);
3820 p->reset();
3821}
3822
3823static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3824 SkScalar x0, SkScalar y0) {
Chris Daltonde500372020-05-05 15:06:30 -06003825 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
Yuqian Li3154a532017-09-06 13:33:30 -04003826 check_move(reporter, &iter, x0, y0);
3827 check_done_and_reset(reporter, p, &iter);
3828}
3829
3830static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3831 SkScalar x1, SkScalar y1) {
Chris Daltonde500372020-05-05 15:06:30 -06003832 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
Yuqian Li3154a532017-09-06 13:33:30 -04003833 check_move(reporter, &iter, 0, 0);
3834 check_line(reporter, &iter, x1, y1);
3835 check_done_and_reset(reporter, p, &iter);
3836}
3837
3838static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3839 SkScalar x1, SkScalar y1) {
Chris Daltonde500372020-05-05 15:06:30 -06003840 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
Yuqian Li3154a532017-09-06 13:33:30 -04003841 check_move(reporter, &iter, 0, 0);
3842 check_line(reporter, &iter, x1, y1);
3843 check_done(reporter, p, &iter);
3844}
3845
3846static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3847 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
Chris Daltonde500372020-05-05 15:06:30 -06003848 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
Yuqian Li3154a532017-09-06 13:33:30 -04003849 check_move(reporter, &iter, 0, 0);
3850 check_line(reporter, &iter, x1, y1);
3851 check_line(reporter, &iter, x2, y2);
3852 check_done_and_reset(reporter, p, &iter);
3853}
3854
3855static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3856 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
Chris Daltonde500372020-05-05 15:06:30 -06003857 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
Yuqian Li3154a532017-09-06 13:33:30 -04003858 check_move(reporter, &iter, 0, 0);
3859 check_quad(reporter, &iter, x1, y1, x2, y2);
3860 check_done_and_reset(reporter, p, &iter);
3861}
3862
3863static bool nearly_equal(const SkRect& a, const SkRect& b) {
3864 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3865 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3866 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3867 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3868}
3869
William Hesse3c246112021-08-05 18:27:06 +02003870static void test_rMoveTo(skiatest::Reporter* reporter) {
3871 SkPath p;
3872 p.moveTo(10, 11);
3873 p.lineTo(20, 21);
3874 p.close();
3875 p.rMoveTo(30, 31);
3876 SkPathPriv::RangeIter iter = SkPathPriv::Iterate(p).begin();
3877 check_move(reporter, &iter, 10, 11);
3878 check_line(reporter, &iter, 20, 21);
3879 check_close(reporter, &iter);
3880 check_move(reporter, &iter, 10 + 30, 11 + 31);
3881 check_done_and_reset(reporter, &p, &iter);
3882
3883 p.moveTo(10, 11);
3884 p.lineTo(20, 21);
3885 p.rMoveTo(30, 31);
3886 iter = SkPathPriv::Iterate(p).begin();
3887 check_move(reporter, &iter, 10, 11);
3888 check_line(reporter, &iter, 20, 21);
3889 check_move(reporter, &iter, 20 + 30, 21 + 31);
3890 check_done_and_reset(reporter, &p, &iter);
3891
3892 p.rMoveTo(30, 31);
3893 iter = SkPathPriv::Iterate(p).begin();
3894 check_move(reporter, &iter, 30, 31);
3895 check_done_and_reset(reporter, &p, &iter);
3896}
3897
Yuqian Li3154a532017-09-06 13:33:30 -04003898static void test_arcTo(skiatest::Reporter* reporter) {
3899 SkPath p;
3900 p.arcTo(0, 0, 1, 2, 1);
3901 check_path_is_line_and_reset(reporter, &p, 0, 0);
3902 p.arcTo(1, 2, 1, 2, 1);
3903 check_path_is_line_and_reset(reporter, &p, 1, 2);
3904 p.arcTo(1, 2, 3, 4, 0);
3905 check_path_is_line_and_reset(reporter, &p, 1, 2);
3906 p.arcTo(1, 2, 0, 0, 1);
3907 check_path_is_line_and_reset(reporter, &p, 1, 2);
3908 p.arcTo(1, 0, 1, 1, 1);
3909 SkPoint pt;
3910 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3911 p.reset();
3912 p.arcTo(1, 0, 1, -1, 1);
3913 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3914 p.reset();
3915 SkRect oval = {1, 2, 3, 4};
3916 p.arcTo(oval, 0, 0, true);
3917 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3918 p.arcTo(oval, 0, 0, false);
3919 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3920 p.arcTo(oval, 360, 0, true);
3921 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3922 p.arcTo(oval, 360, 0, false);
3923 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3924
3925 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3926 p.arcTo(oval, 0, sweep, false);
3927 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3928 sweep += delta;
3929 delta /= 2;
3930 }
3931 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3932 p.arcTo(oval, 0, sweep, false);
3933 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3934 sweep -= delta;
3935 delta /= 2;
3936 }
3937 SkRect noOvalWidth = {1, 2, 0, 3};
3938 p.reset();
3939 p.arcTo(noOvalWidth, 0, 360, false);
3940 REPORTER_ASSERT(reporter, p.isEmpty());
3941
3942 SkRect noOvalHeight = {1, 2, 3, 1};
3943 p.reset();
3944 p.arcTo(noOvalHeight, 0, 360, false);
3945 REPORTER_ASSERT(reporter, p.isEmpty());
Ian Prest05676f72020-02-13 10:46:56 -08003946
3947#ifndef SK_LEGACY_PATH_ARCTO_ENDPOINT
3948 // Inspired by http://code.google.com/p/chromium/issues/detail?id=1001768
3949 {
3950 p.reset();
3951 p.moveTo(216, 216);
3952 p.arcTo(216, 108, 0, SkPath::ArcSize::kLarge_ArcSize, SkPathDirection::kCW, 216, 0);
3953 p.arcTo(270, 135, 0, SkPath::ArcSize::kLarge_ArcSize, SkPathDirection::kCCW, 216, 216);
3954
3955 // The 'arcTo' call should end up exactly at the starting location.
3956 int n = p.countPoints();
3957 REPORTER_ASSERT(reporter, p.getPoint(0) == p.getPoint(n - 1));
3958 }
3959#endif
Yuqian Li3154a532017-09-06 13:33:30 -04003960}
3961
3962static void test_addPath(skiatest::Reporter* reporter) {
3963 SkPath p, q;
3964 p.lineTo(1, 2);
3965 q.moveTo(4, 4);
3966 q.lineTo(7, 8);
3967 q.conicTo(8, 7, 6, 5, 0.5f);
3968 q.quadTo(6, 7, 8, 6);
3969 q.cubicTo(5, 6, 7, 8, 7, 5);
3970 q.close();
3971 p.addPath(q, -4, -4);
3972 SkRect expected = {0, 0, 4, 4};
3973 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3974 p.reset();
3975 p.reverseAddPath(q);
3976 SkRect reverseExpected = {4, 4, 8, 8};
3977 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3978}
3979
3980static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3981 SkPath p, q;
3982 if (explicitMoveTo) {
3983 p.moveTo(1, 1);
3984 }
3985 p.lineTo(1, 2);
3986 if (explicitMoveTo) {
3987 q.moveTo(2, 1);
3988 }
3989 q.lineTo(2, 2);
3990 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3991 uint8_t verbs[4];
3992 int verbcount = p.getVerbs(verbs, 4);
3993 REPORTER_ASSERT(reporter, verbcount == 4);
3994 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3995 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3996 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3997 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3998}
3999
4000static void test_extendClosedPath(skiatest::Reporter* reporter) {
4001 SkPath p, q;
4002 p.moveTo(1, 1);
4003 p.lineTo(1, 2);
4004 p.lineTo(2, 2);
4005 p.close();
4006 q.moveTo(2, 1);
4007 q.lineTo(2, 3);
4008 p.addPath(q, SkPath::kExtend_AddPathMode);
4009 uint8_t verbs[7];
4010 int verbcount = p.getVerbs(verbs, 7);
4011 REPORTER_ASSERT(reporter, verbcount == 7);
4012 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
4013 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
4014 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
4015 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
4016 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
4017 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
4018 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
4019
4020 SkPoint pt;
4021 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
4022 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
4023 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
4024}
4025
4026static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
4027 SkPath p, q, r;
4028 // case 1: dst is empty
4029 p.moveTo(2, 1);
4030 p.lineTo(2, 3);
4031 q.addPath(p, mode);
4032 REPORTER_ASSERT(reporter, q == p);
4033 // case 2: src is empty
4034 p.addPath(r, mode);
4035 REPORTER_ASSERT(reporter, q == p);
4036 // case 3: src and dst are empty
4037 q.reset();
4038 q.addPath(r, mode);
4039 REPORTER_ASSERT(reporter, q.isEmpty());
4040}
4041
4042static void test_conicTo_special_case(skiatest::Reporter* reporter) {
4043 SkPath p;
4044 p.conicTo(1, 2, 3, 4, -1);
4045 check_path_is_line_and_reset(reporter, &p, 3, 4);
4046 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
4047 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
4048 p.conicTo(1, 2, 3, 4, 1);
4049 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
4050}
4051
4052static void test_get_point(skiatest::Reporter* reporter) {
4053 SkPath p;
4054 SkPoint pt = p.getPoint(0);
4055 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
4056 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
4057 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
4058 p.setLastPt(10, 10);
4059 pt = p.getPoint(0);
4060 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
4061 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
4062 p.rMoveTo(10, 10);
4063 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4064}
4065
4066static void test_contains(skiatest::Reporter* reporter) {
4067 SkPath p;
4068 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4069 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
4070 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
4071 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4072 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
4073 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
4074 // this may return true or false, depending on the platform's numerics, but it should not crash
4075 (void) p.contains(-77.2027664f, 15.3066053f);
4076
4077 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004078 p.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004079 REPORTER_ASSERT(reporter, p.contains(0, 0));
Mike Reed7d34dc72019-11-26 12:17:17 -05004080 p.setFillType(SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004081 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4082 p.moveTo(4, 4);
4083 p.lineTo(6, 8);
4084 p.lineTo(8, 4);
4085 // test on edge
4086 REPORTER_ASSERT(reporter, p.contains(6, 4));
4087 REPORTER_ASSERT(reporter, p.contains(5, 6));
4088 REPORTER_ASSERT(reporter, p.contains(7, 6));
4089 // test quick reject
4090 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4091 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4092 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4093 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4094 // test various crossings in x
4095 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4096 REPORTER_ASSERT(reporter, p.contains(6, 7));
4097 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4098 p.reset();
4099 p.moveTo(4, 4);
4100 p.lineTo(8, 6);
4101 p.lineTo(4, 8);
4102 // test on edge
4103 REPORTER_ASSERT(reporter, p.contains(4, 6));
4104 REPORTER_ASSERT(reporter, p.contains(6, 5));
4105 REPORTER_ASSERT(reporter, p.contains(6, 7));
4106 // test various crossings in y
4107 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4108 REPORTER_ASSERT(reporter, p.contains(7, 6));
4109 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4110 p.reset();
4111 p.moveTo(4, 4);
4112 p.lineTo(8, 4);
4113 p.lineTo(8, 8);
4114 p.lineTo(4, 8);
4115 // test on vertices
4116 REPORTER_ASSERT(reporter, p.contains(4, 4));
4117 REPORTER_ASSERT(reporter, p.contains(8, 4));
4118 REPORTER_ASSERT(reporter, p.contains(8, 8));
4119 REPORTER_ASSERT(reporter, p.contains(4, 8));
4120 p.reset();
4121 p.moveTo(4, 4);
4122 p.lineTo(6, 8);
4123 p.lineTo(2, 8);
4124 // test on edge
4125 REPORTER_ASSERT(reporter, p.contains(5, 6));
4126 REPORTER_ASSERT(reporter, p.contains(4, 8));
4127 REPORTER_ASSERT(reporter, p.contains(3, 6));
4128 p.reset();
4129 p.moveTo(4, 4);
4130 p.lineTo(0, 6);
4131 p.lineTo(4, 8);
4132 // test on edge
4133 REPORTER_ASSERT(reporter, p.contains(2, 5));
4134 REPORTER_ASSERT(reporter, p.contains(2, 7));
4135 REPORTER_ASSERT(reporter, p.contains(4, 6));
4136 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4137 p.reset();
4138 p.moveTo(4, 0);
4139 p.lineTo(6, 4);
4140 p.lineTo(2, 4);
4141 p.moveTo(4, 0);
4142 p.lineTo(0, 8);
4143 p.lineTo(8, 8);
4144 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4145 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4146 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4147 REPORTER_ASSERT(reporter, p.contains(4, 4));
4148
4149 // test quads
4150 p.reset();
4151 p.moveTo(4, 4);
4152 p.quadTo(6, 6, 8, 8);
4153 p.quadTo(6, 8, 4, 8);
4154 p.quadTo(4, 6, 4, 4);
4155 REPORTER_ASSERT(reporter, p.contains(5, 6));
4156 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4157 // test quad edge
4158 REPORTER_ASSERT(reporter, p.contains(5, 5));
4159 REPORTER_ASSERT(reporter, p.contains(5, 8));
4160 REPORTER_ASSERT(reporter, p.contains(4, 5));
4161 // test quad endpoints
4162 REPORTER_ASSERT(reporter, p.contains(4, 4));
4163 REPORTER_ASSERT(reporter, p.contains(8, 8));
4164 REPORTER_ASSERT(reporter, p.contains(4, 8));
4165
4166 p.reset();
4167 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4168 p.moveTo(qPts[0]);
4169 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4170 p.quadTo(qPts[index], qPts[index + 1]);
4171 }
4172 REPORTER_ASSERT(reporter, p.contains(5, 6));
4173 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4174 // test quad edge
4175 SkPoint halfway;
4176 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4177 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4178 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4179 }
4180
4181 // test conics
4182 p.reset();
4183 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4184 p.moveTo(kPts[0]);
4185 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4186 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4187 }
4188 REPORTER_ASSERT(reporter, p.contains(5, 6));
4189 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4190 // test conic edge
4191 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4192 SkConic conic(&kPts[index], 0.5f);
4193 halfway = conic.evalAt(0.5f);
4194 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4195 }
4196 // test conic end points
4197 REPORTER_ASSERT(reporter, p.contains(4, 4));
4198 REPORTER_ASSERT(reporter, p.contains(8, 8));
4199 REPORTER_ASSERT(reporter, p.contains(4, 8));
4200
4201 // test cubics
4202 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4203 for (int i = 0; i < 3; ++i) {
4204 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004205 p.setFillType(SkPathFillType::kEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004206 p.moveTo(pts[i].fX, pts[i].fY);
4207 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);
4208 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);
4209 p.close();
4210 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4211 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4212 // test cubic edge
4213 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4214 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4215 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4216 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4217 // test cubic end points
4218 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4219 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4220 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4221 }
4222}
4223
4224class PathRefTest_Private {
4225public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004226 static size_t GetFreeSpace(const SkPathRef& ref) {
Mike Reed8fda88e2019-09-05 14:14:38 -04004227 return (ref.fPoints.reserved() - ref.fPoints.count()) * sizeof(SkPoint)
4228 + (ref.fVerbs.reserved() - ref.fVerbs.count()) * sizeof(uint8_t);
Florin Malita0e0f1a72018-09-11 16:16:19 -04004229 }
4230
Yuqian Li3154a532017-09-06 13:33:30 -04004231 static void TestPathRef(skiatest::Reporter* reporter) {
4232 static const int kRepeatCnt = 10;
4233
4234 sk_sp<SkPathRef> pathRef(new SkPathRef);
4235
4236 SkPathRef::Editor ed(&pathRef);
4237
4238 {
4239 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4240 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4241 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4242 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4243 for (int i = 0; i < kRepeatCnt; ++i) {
4244 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4245 }
4246 ed.resetToSize(0, 0, 0);
4247 }
4248
4249 {
4250 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4251 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4252 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4253 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4254 for (int i = 0; i < kRepeatCnt; ++i) {
4255 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4256 }
4257 ed.resetToSize(0, 0, 0);
4258 }
4259
4260 {
4261 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4262 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4263 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4264 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4265 for (int i = 0; i < kRepeatCnt; ++i) {
4266 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4267 }
4268 ed.resetToSize(0, 0, 0);
4269 }
4270
4271 {
4272 SkScalar* weights = nullptr;
4273 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4274 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4275 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4276 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4277 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4278 REPORTER_ASSERT(reporter, weights);
4279 for (int i = 0; i < kRepeatCnt; ++i) {
4280 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4281 }
4282 ed.resetToSize(0, 0, 0);
4283 }
4284
4285 {
4286 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4287 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4288 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4289 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4290 for (int i = 0; i < kRepeatCnt; ++i) {
4291 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4292 }
4293 ed.resetToSize(0, 0, 0);
4294 }
4295 }
4296};
4297
4298static void test_operatorEqual(skiatest::Reporter* reporter) {
4299 SkPath a;
4300 SkPath b;
4301 REPORTER_ASSERT(reporter, a == a);
4302 REPORTER_ASSERT(reporter, a == b);
Mike Reed7d34dc72019-11-26 12:17:17 -05004303 a.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004304 REPORTER_ASSERT(reporter, a != b);
4305 a.reset();
4306 REPORTER_ASSERT(reporter, a == b);
4307 a.lineTo(1, 1);
4308 REPORTER_ASSERT(reporter, a != b);
4309 a.reset();
4310 REPORTER_ASSERT(reporter, a == b);
4311 a.lineTo(1, 1);
4312 b.lineTo(1, 2);
4313 REPORTER_ASSERT(reporter, a != b);
4314 a.reset();
4315 a.lineTo(1, 2);
4316 REPORTER_ASSERT(reporter, a == b);
4317}
4318
Mike Reededb22ec2020-12-06 14:08:41 -05004319static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool dumpAsHex,
4320 const char* str) {
Yuqian Li3154a532017-09-06 13:33:30 -04004321 SkDynamicMemoryWStream wStream;
Mike Reededb22ec2020-12-06 14:08:41 -05004322 path.dump(&wStream, dumpAsHex);
Yuqian Li3154a532017-09-06 13:33:30 -04004323 sk_sp<SkData> data = wStream.detachAsData();
4324 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4325 if (strlen(str) > 0) {
4326 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4327 } else {
4328 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4329 }
4330}
4331
4332static void test_dump(skiatest::Reporter* reporter) {
4333 SkPath p;
Mike Reededb22ec2020-12-06 14:08:41 -05004334 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n");
Yuqian Li3154a532017-09-06 13:33:30 -04004335 p.moveTo(1, 2);
4336 p.lineTo(3, 4);
Mike Reededb22ec2020-12-06 14:08:41 -05004337 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004338 "path.moveTo(1, 2);\n"
4339 "path.lineTo(3, 4);\n");
Yuqian Li3154a532017-09-06 13:33:30 -04004340 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004341 p.setFillType(SkPathFillType::kEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004342 p.moveTo(1, 2);
4343 p.quadTo(3, 4, 5, 6);
Mike Reededb22ec2020-12-06 14:08:41 -05004344 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kEvenOdd);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004345 "path.moveTo(1, 2);\n"
4346 "path.quadTo(3, 4, 5, 6);\n");
4347 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004348 p.setFillType(SkPathFillType::kInverseWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004349 p.moveTo(1, 2);
4350 p.conicTo(3, 4, 5, 6, 0.5f);
Mike Reededb22ec2020-12-06 14:08:41 -05004351 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kInverseWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004352 "path.moveTo(1, 2);\n"
4353 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4354 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004355 p.setFillType(SkPathFillType::kInverseEvenOdd);
Yuqian Li3154a532017-09-06 13:33:30 -04004356 p.moveTo(1, 2);
4357 p.cubicTo(3, 4, 5, 6, 7, 8);
Mike Reededb22ec2020-12-06 14:08:41 -05004358 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kInverseEvenOdd);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004359 "path.moveTo(1, 2);\n"
4360 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4361 p.reset();
Mike Reed7d34dc72019-11-26 12:17:17 -05004362 p.setFillType(SkPathFillType::kWinding);
Yuqian Li3154a532017-09-06 13:33:30 -04004363 p.moveTo(1, 2);
4364 p.lineTo(3, 4);
Mike Reededb22ec2020-12-06 14:08:41 -05004365 compare_dump(reporter, p, true,
Mike Reed7d34dc72019-11-26 12:17:17 -05004366 "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004367 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4368 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4369 p.reset();
4370 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4371 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Mike Reededb22ec2020-12-06 14:08:41 -05004372 compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n"
Yuqian Li3154a532017-09-06 13:33:30 -04004373 "path.moveTo(1, 2);\n"
4374 "path.lineTo(3, 4);\n");
4375}
4376
4377namespace {
4378
Brian Salomon99a813c2020-03-02 12:50:47 -05004379class ChangeListener : public SkIDChangeListener {
Yuqian Li3154a532017-09-06 13:33:30 -04004380public:
4381 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4382 ~ChangeListener() override {}
Brian Salomon99a813c2020-03-02 12:50:47 -05004383 void changed() override { *fChanged = true; }
4384
Yuqian Li3154a532017-09-06 13:33:30 -04004385private:
4386 bool* fChanged;
4387};
4388
John Stilesa6841be2020-08-06 14:11:56 -04004389} // namespace
Yuqian Li3154a532017-09-06 13:33:30 -04004390
4391class PathTest_Private {
4392public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004393 static size_t GetFreeSpace(const SkPath& path) {
4394 return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
4395 }
4396
Yuqian Li3154a532017-09-06 13:33:30 -04004397 static void TestPathTo(skiatest::Reporter* reporter) {
4398 SkPath p, q;
4399 p.lineTo(4, 4);
4400 p.reversePathTo(q);
4401 check_path_is_line(reporter, &p, 4, 4);
4402 q.moveTo(-4, -4);
4403 p.reversePathTo(q);
4404 check_path_is_line(reporter, &p, 4, 4);
4405 q.lineTo(7, 8);
4406 q.conicTo(8, 7, 6, 5, 0.5f);
4407 q.quadTo(6, 7, 8, 6);
4408 q.cubicTo(5, 6, 7, 8, 7, 5);
4409 q.close();
4410 p.reversePathTo(q);
4411 SkRect reverseExpected = {-4, -4, 8, 8};
4412 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4413 }
4414
4415 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4416 SkPath p;
4417
4418 bool changed = false;
4419 p.moveTo(0, 0);
4420
4421 // Check that listener is notified on moveTo().
4422
Chris Daltonafa11582018-06-08 12:00:44 -06004423 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004424 REPORTER_ASSERT(reporter, !changed);
4425 p.moveTo(10, 0);
4426 REPORTER_ASSERT(reporter, changed);
4427
4428 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004429 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004430 REPORTER_ASSERT(reporter, !changed);
4431 p.lineTo(20, 0);
4432 REPORTER_ASSERT(reporter, changed);
4433
4434 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004435 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004436 REPORTER_ASSERT(reporter, !changed);
4437 p.reset();
4438 REPORTER_ASSERT(reporter, changed);
4439
4440 p.moveTo(0, 0);
4441
4442 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004443 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004444 REPORTER_ASSERT(reporter, !changed);
4445 p.rewind();
4446 REPORTER_ASSERT(reporter, changed);
4447
Brian Osmandbfcd922019-03-07 16:50:30 -05004448 // Check that listener is notified on transform().
4449 {
4450 SkPath q;
4451 q.moveTo(10, 10);
4452 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4453 REPORTER_ASSERT(reporter, !changed);
4454 SkMatrix matrix;
4455 matrix.setScale(2, 2);
4456 p.transform(matrix, &q);
4457 REPORTER_ASSERT(reporter, changed);
4458 }
4459
Yuqian Li3154a532017-09-06 13:33:30 -04004460 // Check that listener is notified when pathref is deleted.
4461 {
4462 SkPath q;
4463 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004464 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004465 REPORTER_ASSERT(reporter, !changed);
4466 }
4467 // q went out of scope.
4468 REPORTER_ASSERT(reporter, changed);
4469 }
4470};
4471
4472static void test_crbug_629455(skiatest::Reporter* reporter) {
4473 SkPath path;
4474 path.moveTo(0, 0);
4475 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4476 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4477 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4478// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4479 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004480 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004481}
4482
4483static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4484 SkPath path;
4485 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4486 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4487 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4488 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4489 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4490 path.close();
4491
4492 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4493 SkPaint paint;
4494 paint.setAntiAlias(true);
4495 surface->getCanvas()->clipPath(path, true);
4496 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4497}
4498
4499static void test_path_crbugskia6003() {
4500 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4501 SkCanvas* canvas = surface->getCanvas();
4502 SkPaint paint;
4503 paint.setAntiAlias(true);
4504 SkPath path;
4505 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4506 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4507 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4508 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4509 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4510 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4511 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4512 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4513 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4514 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4515 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4516 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4517 path.close();
4518 canvas->clipPath(path, true);
4519 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4520}
4521
4522static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4523 SkPath path;
4524 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4525 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4526 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4527 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4528 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004529 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004530}
4531
Yuqian Li3154a532017-09-06 13:33:30 -04004532static void test_skbug_6947() {
4533 SkPath path;
4534 SkPoint points[] =
4535 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4536 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4537 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4538 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4539 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4540 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4541 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4542 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4543 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4544 {125.119476f, 1.50011659f}};
4545 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4546 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4547 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4548 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4549 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4550 int pointIndex = 0;
4551 for(auto verb : verbs) {
4552 switch (verb) {
4553 case kMove:
4554 path.moveTo(points[pointIndex++]);
4555 break;
4556 case kLine:
4557 path.lineTo(points[pointIndex++]);
4558 break;
4559 case kClose:
4560 default:
4561 path.close();
4562 break;
4563 }
4564 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004565 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004566}
Yuqian Lia81b6262017-09-06 17:10:05 -04004567
4568static void test_skbug_7015() {
4569 SkPath path;
Mike Reed7d34dc72019-11-26 12:17:17 -05004570 path.setFillType(SkPathFillType::kWinding);
Yuqian Lia81b6262017-09-06 17:10:05 -04004571 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4572 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4573 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4574 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4575 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4576 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4577 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004578 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004579}
4580
Yuqian Lic5e4e742017-09-18 14:38:43 -04004581static void test_skbug_7051() {
4582 SkPath path;
4583 path.moveTo(10, 10);
4584 path.cubicTo(10, 20, 10, 30, 30, 30);
4585 path.lineTo(50, 20);
4586 path.lineTo(50, 10);
4587 path.close();
4588 test_draw_AA_path(100, 100, path);
4589}
4590
Ben Wagner2e9f7722019-08-14 15:45:37 -04004591static void test_skbug_7435() {
4592 SkPaint paint;
4593 SkPath path;
Mike Reed7d34dc72019-11-26 12:17:17 -05004594 path.setFillType(SkPathFillType::kWinding);
Ben Wagner2e9f7722019-08-14 15:45:37 -04004595 path.moveTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4596 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4597 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4598 path.lineTo(SkBits2Float(0x7ef348e9), SkBits2Float(0xfef403c6)); // 1.6169e+38f, -1.62176e+38f
4599 path.lineTo(SkBits2Float(0x7ef74c4e), SkBits2Float(0xfef803cb)); // 1.64358e+38f, -1.64834e+38f
4600 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
4601 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
4602 path.lineTo(SkBits2Float(0x7edb57a9), SkBits2Float(0xfedbfe06)); // 1.45778e+38f, -1.4621e+38f
4603 path.lineTo(SkBits2Float(0x7e875976), SkBits2Float(0xfe87fdb3)); // 8.99551e+37f, -9.03815e+37f
4604 path.lineTo(SkBits2Float(0x7ded5c2b), SkBits2Float(0xfdeff59e)); // 3.94382e+37f, -3.98701e+37f
4605 path.lineTo(SkBits2Float(0x7d7a78a7), SkBits2Float(0xfd7fda0f)); // 2.08083e+37f, -2.12553e+37f
4606 path.lineTo(SkBits2Float(0x7d7a6403), SkBits2Float(0xfd7fe461)); // 2.08016e+37f, -2.12587e+37f
4607 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
4608 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
4609 path.lineTo(SkBits2Float(0x7d8d2067), SkBits2Float(0xfd900bdb)); // 2.34487e+37f, -2.39338e+37f
4610 path.lineTo(SkBits2Float(0x7ddd137a), SkBits2Float(0xfde00c2d)); // 3.67326e+37f, -3.72263e+37f
4611 path.lineTo(SkBits2Float(0x7ddd2a1b), SkBits2Float(0xfddff58e)); // 3.67473e+37f, -3.72116e+37f
4612 path.lineTo(SkBits2Float(0x7c694ae5), SkBits2Float(0xfc7fa67c)); // 4.8453e+36f, -5.30965e+36f
4613 path.lineTo(SkBits2Float(0xfc164a8b), SkBits2Float(0x7c005af5)); // -3.12143e+36f, 2.66584e+36f
4614 path.lineTo(SkBits2Float(0xfc8ae983), SkBits2Float(0x7c802da7)); // -5.77019e+36f, 5.32432e+36f
4615 path.lineTo(SkBits2Float(0xfc8b16d9), SkBits2Float(0x7c80007b)); // -5.77754e+36f, 5.31699e+36f
4616 path.lineTo(SkBits2Float(0xfc8b029c), SkBits2Float(0x7c7f8788)); // -5.77426e+36f, 5.30714e+36f
4617 path.lineTo(SkBits2Float(0xfc8b0290), SkBits2Float(0x7c7f8790)); // -5.77425e+36f, 5.30714e+36f
4618 path.lineTo(SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.77753e+36f, 5.31699e+36f
4619 path.lineTo(SkBits2Float(0xfc8b4409), SkBits2Float(0x7c7fa672)); // -5.78487e+36f, 5.30965e+36f
4620 path.lineTo(SkBits2Float(0x7d7aa2ba), SkBits2Float(0xfd800bd1)); // 2.0822e+37f, -2.12753e+37f
4621 path.lineTo(SkBits2Float(0x7e8757ee), SkBits2Float(0xfe88035b)); // 8.99512e+37f, -9.03962e+37f
4622 path.lineTo(SkBits2Float(0x7ef7552d), SkBits2Float(0xfef803ca)); // 1.64381e+38f, -1.64834e+38f
4623 path.lineTo(SkBits2Float(0x7f0fa653), SkBits2Float(0xff1001f9)); // 1.90943e+38f, -1.91419e+38f
4624 path.lineTo(SkBits2Float(0x7f0fa926), SkBits2Float(0xff0fff24)); // 1.90958e+38f, -1.91404e+38f
4625 path.lineTo(SkBits2Float(0x7f0da75c), SkBits2Float(0xff0dff22)); // 1.8829e+38f, -1.88746e+38f
4626 path.lineTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4627 path.close();
4628 path.moveTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4629 path.lineTo(SkBits2Float(0x7f0da48a), SkBits2Float(0xff0e01f8)); // 1.88275e+38f, -1.88761e+38f
4630 path.lineTo(SkBits2Float(0x7f0fa654), SkBits2Float(0xff1001fa)); // 1.90943e+38f, -1.91419e+38f
4631 path.lineTo(SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90951e+38f, -1.91412e+38f
4632 path.lineTo(SkBits2Float(0x7f0fa927), SkBits2Float(0xff0fff25)); // 1.90958e+38f, -1.91404e+38f
4633 path.lineTo(SkBits2Float(0x7ef75ad5), SkBits2Float(0xfef7fe22)); // 1.64395e+38f, -1.64819e+38f
4634 path.lineTo(SkBits2Float(0x7e875d96), SkBits2Float(0xfe87fdb3)); // 8.99659e+37f, -9.03815e+37f
4635 path.lineTo(SkBits2Float(0x7d7acff6), SkBits2Float(0xfd7fea5b)); // 2.08367e+37f, -2.12606e+37f
4636 path.lineTo(SkBits2Float(0xfc8b0588), SkBits2Float(0x7c8049b7)); // -5.77473e+36f, 5.32887e+36f
4637 path.lineTo(SkBits2Float(0xfc8b2b16), SkBits2Float(0x7c803d32)); // -5.78083e+36f, 5.32684e+36f
4638 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
4639 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
4640 path.lineTo(SkBits2Float(0xfc16ffaa), SkBits2Float(0x7bff4c12)); // -3.13612e+36f, 2.65116e+36f
4641 path.lineTo(SkBits2Float(0x7c6895e0), SkBits2Float(0xfc802dc0)); // 4.83061e+36f, -5.32434e+36f
4642 path.lineTo(SkBits2Float(0x7ddd137b), SkBits2Float(0xfde00c2e)); // 3.67326e+37f, -3.72263e+37f
4643 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4644 path.lineTo(SkBits2Float(0x7ddd2a1c), SkBits2Float(0xfddff58f)); // 3.67473e+37f, -3.72116e+37f
4645 path.lineTo(SkBits2Float(0x7d8d3711), SkBits2Float(0xfd8ff543)); // 2.34634e+37f, -2.39191e+37f
4646 path.lineTo(SkBits2Float(0x7d7a88fe), SkBits2Float(0xfd7fea69)); // 2.08136e+37f, -2.12606e+37f
4647 path.lineTo(SkBits2Float(0x7d7a7254), SkBits2Float(0xfd800080)); // 2.08063e+37f, -2.1268e+37f
4648 path.lineTo(SkBits2Float(0x7d7a80a4), SkBits2Float(0xfd800ed0)); // 2.08109e+37f, -2.12773e+37f
4649 path.lineTo(SkBits2Float(0x7d7a80a8), SkBits2Float(0xfd800ecf)); // 2.08109e+37f, -2.12773e+37f
4650 path.lineTo(SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08063e+37f, -2.1268e+37f
4651 path.lineTo(SkBits2Float(0x7d7a5bb9), SkBits2Float(0xfd800bd0)); // 2.0799e+37f, -2.12753e+37f
4652 path.lineTo(SkBits2Float(0x7ded458b), SkBits2Float(0xfdf00c3e)); // 3.94235e+37f, -3.98848e+37f
4653 path.lineTo(SkBits2Float(0x7e8753ce), SkBits2Float(0xfe88035b)); // 8.99405e+37f, -9.03962e+37f
4654 path.lineTo(SkBits2Float(0x7edb5201), SkBits2Float(0xfedc03ae)); // 1.45763e+38f, -1.46225e+38f
4655 path.lineTo(SkBits2Float(0x7ef74c4d), SkBits2Float(0xfef803ca)); // 1.64358e+38f, -1.64834e+38f
4656 path.lineTo(SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64365e+38f, -1.64827e+38f
4657 path.lineTo(SkBits2Float(0x7ef751f4), SkBits2Float(0xfef7fe21)); // 1.64372e+38f, -1.64819e+38f
4658 path.lineTo(SkBits2Float(0x7ef34e91), SkBits2Float(0xfef3fe1e)); // 1.61705e+38f, -1.62161e+38f
4659 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4660 path.lineTo(SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.48397e+38f, -1.48876e+38f
4661 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4662 path.lineTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4663 path.close();
4664 path.moveTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4665 path.lineTo(SkBits2Float(0xfd6597e5), SkBits2Float(0x7d60177f)); // -1.90739e+37f, 1.86168e+37f
4666 path.lineTo(SkBits2Float(0xfde2cea1), SkBits2Float(0x7de00c2e)); // -3.76848e+37f, 3.72263e+37f
4667 path.lineTo(SkBits2Float(0xfe316511), SkBits2Float(0x7e300657)); // -5.89495e+37f, 5.84943e+37f
4668 path.lineTo(SkBits2Float(0xfe415da1), SkBits2Float(0x7e400666)); // -6.42568e+37f, 6.38112e+37f
4669 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4000be)); // -6.42641e+37f, 6.38039e+37f
4670 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4671 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4672 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e3ff8be)); // -6.42591e+37f, 6.37935e+37f
4673 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e3ff8be)); // -6.42544e+37f, 6.37935e+37f
4674 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4675 path.lineTo(SkBits2Float(0xfe416171), SkBits2Float(0x7e3ffb16)); // -6.42617e+37f, 6.37966e+37f
4676 path.lineTo(SkBits2Float(0xfe016131), SkBits2Float(0x7dfff5ae)); // -4.29938e+37f, 4.25286e+37f
4677 path.lineTo(SkBits2Float(0xfe0155e2), SkBits2Float(0x7e000628)); // -4.29791e+37f, 4.25433e+37f
4678 path.lineTo(SkBits2Float(0xfe0958ea), SkBits2Float(0x7e080630)); // -4.56415e+37f, 4.52018e+37f
4679 path.lineTo(SkBits2Float(0xfe115c92), SkBits2Float(0x7e100638)); // -4.83047e+37f, 4.78603e+37f
4680 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
4681 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
4682 path.lineTo(SkBits2Float(0xfe016b92), SkBits2Float(0x7dfff5af)); // -4.30072e+37f, 4.25286e+37f
4683 path.lineTo(SkBits2Float(0xfdc2d963), SkBits2Float(0x7dbff56e)); // -3.23749e+37f, 3.18946e+37f
4684 path.lineTo(SkBits2Float(0xfd65ae25), SkBits2Float(0x7d5fea3d)); // -1.90811e+37f, 1.86021e+37f
4685 path.lineTo(SkBits2Float(0xfab448de), SkBits2Float(0xf7b50a19)); // -4.68046e+35f, -7.34383e+33f
4686 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4687 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x7800007f)); // -4.60703e+35f, 1.03848e+34f
4688 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x7800007f)); // -4.67194e+35f, 1.03848e+34f
4689 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4690 path.lineTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4691 path.close();
4692 path.moveTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4693 path.lineTo(SkBits2Float(0xfaa7cc52), SkBits2Float(0xf800007f)); // -4.35629e+35f, -1.03848e+34f
4694 path.lineTo(SkBits2Float(0xfd6580e5), SkBits2Float(0x7d60177f)); // -1.90664e+37f, 1.86168e+37f
4695 path.lineTo(SkBits2Float(0xfdc2c2c1), SkBits2Float(0x7dc00c0f)); // -3.23602e+37f, 3.19093e+37f
4696 path.lineTo(SkBits2Float(0xfe016040), SkBits2Float(0x7e000626)); // -4.29925e+37f, 4.25433e+37f
4697 path.lineTo(SkBits2Float(0xfe115c90), SkBits2Float(0x7e100636)); // -4.83047e+37f, 4.78603e+37f
4698 path.lineTo(SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.8312e+37f, 4.78529e+37f
4699 path.lineTo(SkBits2Float(0xfe1167e0), SkBits2Float(0x7e0ffae6)); // -4.83194e+37f, 4.78456e+37f
4700 path.lineTo(SkBits2Float(0xfe096438), SkBits2Float(0x7e07fade)); // -4.56562e+37f, 4.51871e+37f
4701 path.lineTo(SkBits2Float(0xfe016130), SkBits2Float(0x7dfff5ac)); // -4.29938e+37f, 4.25286e+37f
4702 path.lineTo(SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29864e+37f, 4.25359e+37f
4703 path.lineTo(SkBits2Float(0xfe0155e1), SkBits2Float(0x7e000627)); // -4.29791e+37f, 4.25433e+37f
4704 path.lineTo(SkBits2Float(0xfe415879), SkBits2Float(0x7e4008bf)); // -6.42501e+37f, 6.38143e+37f
4705 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e4008bf)); // -6.42591e+37f, 6.38143e+37f
4706 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4707 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4708 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
4709 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
4710 path.lineTo(SkBits2Float(0xfe317061), SkBits2Float(0x7e2ffb07)); // -5.89642e+37f, 5.84796e+37f
4711 path.lineTo(SkBits2Float(0xfde2e542), SkBits2Float(0x7ddff58e)); // -3.76995e+37f, 3.72116e+37f
4712 path.lineTo(SkBits2Float(0xfd65c525), SkBits2Float(0x7d5fea3d)); // -1.90886e+37f, 1.86021e+37f
4713 path.lineTo(SkBits2Float(0xfab6c8db), SkBits2Float(0xf7b50b4f)); // -4.74536e+35f, -7.34402e+33f
4714 path.lineTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4715 path.close();
4716 path.moveTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4717 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4718 path.quadTo(SkBits2Float(0xfd0593a5), SkBits2Float(0x7d00007f), SkBits2Float(0xfd659785), SkBits2Float(0x7d6000de)); // -1.10971e+37f, 1.0634e+37f, -1.90737e+37f, 1.86095e+37f
4719 path.quadTo(SkBits2Float(0xfda2cdf2), SkBits2Float(0x7da0009f), SkBits2Float(0xfdc2ce12), SkBits2Float(0x7dc000be)); // -2.70505e+37f, 2.6585e+37f, -3.23675e+37f, 3.1902e+37f
4720 path.quadTo(SkBits2Float(0xfde2ce31), SkBits2Float(0x7de000de), SkBits2Float(0xfe0165e9), SkBits2Float(0x7e00007f)); // -3.76845e+37f, 3.72189e+37f, -4.29999e+37f, 4.25359e+37f
4721 path.quadTo(SkBits2Float(0xfe1164b9), SkBits2Float(0x7e10008f), SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.83153e+37f, 4.78529e+37f, -4.8312e+37f, 4.78529e+37f
4722 path.quadTo(SkBits2Float(0xfe116039), SkBits2Float(0x7e10008f), SkBits2Float(0xfe095e91), SkBits2Float(0x7e080087)); // -4.83094e+37f, 4.78529e+37f, -4.56488e+37f, 4.51944e+37f
4723 path.quadTo(SkBits2Float(0xfe015d09), SkBits2Float(0x7e00007f), SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29884e+37f, 4.25359e+37f, -4.29864e+37f, 4.25359e+37f
4724 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4725 path.quadTo(SkBits2Float(0xfe415da9), SkBits2Float(0x7e4000be), SkBits2Float(0xfe415f69), SkBits2Float(0x7e4000be)); // -6.42568e+37f, 6.38039e+37f, -6.42591e+37f, 6.38039e+37f
4726 path.quadTo(SkBits2Float(0xfe416149), SkBits2Float(0x7e4000be), SkBits2Float(0xfe416349), SkBits2Float(0x7e4000be)); // -6.42615e+37f, 6.38039e+37f, -6.42641e+37f, 6.38039e+37f
4727 path.quadTo(SkBits2Float(0xfe416849), SkBits2Float(0x7e4000be), SkBits2Float(0xfe316ab9), SkBits2Float(0x7e3000af)); // -6.42706e+37f, 6.38039e+37f, -5.89569e+37f, 5.84869e+37f
4728 path.quadTo(SkBits2Float(0xfe216d29), SkBits2Float(0x7e20009f), SkBits2Float(0xfde2d9f2), SkBits2Float(0x7de000de)); // -5.36431e+37f, 5.31699e+37f, -3.76921e+37f, 3.72189e+37f
4729 path.quadTo(SkBits2Float(0xfda2d9b2), SkBits2Float(0x7da0009f), SkBits2Float(0xfd65ae85), SkBits2Float(0x7d6000de)); // -2.70582e+37f, 2.6585e+37f, -1.90812e+37f, 1.86095e+37f
4730 path.quadTo(SkBits2Float(0xfd05a9a6), SkBits2Float(0x7d00007f), SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -1.11043e+37f, 1.0634e+37f, -4.67194e+35f, 200
4731 path.close();
4732 path.moveTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4733 path.quadTo(SkBits2Float(0x7f0ba519), SkBits2Float(0xff0c008b), SkBits2Float(0x7f0da5f3), SkBits2Float(0xff0e008d)); // 1.8562e+38f, -1.86095e+38f, 1.88283e+38f, -1.88753e+38f
4734 path.quadTo(SkBits2Float(0x7f0fa6d5), SkBits2Float(0xff10008f), SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90946e+38f, -1.91412e+38f, 1.90951e+38f, -1.91412e+38f
4735 path.quadTo(SkBits2Float(0x7f0faa7d), SkBits2Float(0xff10008f), SkBits2Float(0x7ef75801), SkBits2Float(0xfef800f6)); // 1.90965e+38f, -1.91412e+38f, 1.64388e+38f, -1.64827e+38f
4736 path.quadTo(SkBits2Float(0x7ecf5b09), SkBits2Float(0xfed000ce), SkBits2Float(0x7e875ac2), SkBits2Float(0xfe880087)); // 1.37811e+38f, -1.38242e+38f, 8.99585e+37f, -9.03889e+37f
4737 path.quadTo(SkBits2Float(0x7e0eb505), SkBits2Float(0xfe10008f), SkBits2Float(0x7d7ab958), SkBits2Float(0xfd80007f)); // 4.74226e+37f, -4.78529e+37f, 2.08293e+37f, -2.1268e+37f
4738 path.quadTo(SkBits2Float(0xfc8ac1cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.76374e+36f, 5.31699e+36f, -5.77753e+36f, 5.31699e+36f
4739 path.quadTo(SkBits2Float(0xfc8b36cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc16a51a), SkBits2Float(0x7c00007f)); // -5.78273e+36f, 5.31699e+36f, -3.12877e+36f, 2.6585e+36f
4740 path.quadTo(SkBits2Float(0xfab6e4de), SkBits2Float(0x43480000), SkBits2Float(0x7c68f062), SkBits2Float(0xfc80007f)); // -4.7482e+35f, 200, 4.83795e+36f, -5.31699e+36f
4741 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4742 path.quadTo(SkBits2Float(0x7d9d254b), SkBits2Float(0xfda0009f), SkBits2Float(0x7d8d2bbc), SkBits2Float(0xfd90008f)); // 2.61103e+37f, -2.6585e+37f, 2.3456e+37f, -2.39265e+37f
4743 path.quadTo(SkBits2Float(0x7d7a64d8), SkBits2Float(0xfd80007f), SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08019e+37f, -2.1268e+37f, 2.08063e+37f, -2.1268e+37f
4744 path.quadTo(SkBits2Float(0x7d7a9058), SkBits2Float(0xfd80007f), SkBits2Float(0x7ded50db), SkBits2Float(0xfdf000ee)); // 2.0816e+37f, -2.1268e+37f, 3.94309e+37f, -3.98774e+37f
4745 path.quadTo(SkBits2Float(0x7e2eace5), SkBits2Float(0xfe3000af), SkBits2Float(0x7e8756a2), SkBits2Float(0xfe880087)); // 5.80458e+37f, -5.84869e+37f, 8.99478e+37f, -9.03889e+37f
4746 path.quadTo(SkBits2Float(0x7ebf56d9), SkBits2Float(0xfec000be), SkBits2Float(0x7edb54d5), SkBits2Float(0xfedc00da)); // 1.27167e+38f, -1.27608e+38f, 1.45771e+38f, -1.46217e+38f
4747 path.quadTo(SkBits2Float(0x7ef752e1), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64375e+38f, -1.64827e+38f, 1.64365e+38f, -1.64827e+38f
4748 path.quadTo(SkBits2Float(0x7ef74d71), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef34bbd), SkBits2Float(0xfef400f2)); // 1.64361e+38f, -1.64827e+38f, 1.61698e+38f, -1.62168e+38f
4749 path.quadTo(SkBits2Float(0x7eef4a19), SkBits2Float(0xfef000ee), SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.59035e+38f, -1.5951e+38f, 1.48397e+38f, -1.48876e+38f
4750 path.lineTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4751 path.close();
4752 SkSurface::MakeRasterN32Premul(250, 250, nullptr)->getCanvas()->drawPath(path, paint);
4753}
4754
Yuqian Li3154a532017-09-06 13:33:30 -04004755static void test_interp(skiatest::Reporter* reporter) {
4756 SkPath p1, p2, out;
4757 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4758 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4759 REPORTER_ASSERT(reporter, p1 == out);
4760 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4761 REPORTER_ASSERT(reporter, p1 == out);
4762 p1.moveTo(0, 2);
4763 p1.lineTo(0, 4);
4764 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4765 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4766 p2.moveTo(6, 0);
4767 p2.lineTo(8, 0);
4768 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4769 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4770 REPORTER_ASSERT(reporter, p2 == out);
4771 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4772 REPORTER_ASSERT(reporter, p1 == out);
4773 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4774 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4775 p1.reset();
4776 p1.moveTo(4, 4);
4777 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4778 p2.reset();
4779 p2.moveTo(4, 2);
4780 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4781 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4782 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4783 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4784 p2.reset();
4785 p2.moveTo(4, 2);
4786 p2.conicTo(6, 3, 6, 5, 1);
4787 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4788 p2.reset();
4789 p2.moveTo(4, 4);
4790 p2.conicTo(5, 4, 5, 5, 0.5f);
4791 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4792}
4793
4794DEF_TEST(PathInterp, reporter) {
4795 test_interp(reporter);
4796}
4797
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004798#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004799DEF_TEST(PathBigCubic, reporter) {
4800 SkPath path;
4801 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4802 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4803 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
4804 path.moveTo(0, 512);
4805
4806 // this call should not assert
4807 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4808}
4809
4810DEF_TEST(PathContains, reporter) {
4811 test_contains(reporter);
4812}
4813
4814DEF_TEST(Paths, reporter) {
4815 test_fuzz_crbug_647922();
4816 test_fuzz_crbug_643933();
4817 test_sect_with_horizontal_needs_pinning();
4818 test_crbug_629455(reporter);
4819 test_fuzz_crbug_627414(reporter);
4820 test_path_crbug364224();
4821 test_fuzz_crbug_662952(reporter);
4822 test_fuzz_crbug_662730(reporter);
4823 test_fuzz_crbug_662780();
4824 test_mask_overflow();
4825 test_path_crbugskia6003();
4826 test_fuzz_crbug_668907();
Yuqian Li3154a532017-09-06 13:33:30 -04004827 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004828 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004829 test_skbug_7051();
Ben Wagner2e9f7722019-08-14 15:45:37 -04004830 test_skbug_7435();
Yuqian Li3154a532017-09-06 13:33:30 -04004831
4832 SkSize::Make(3, 4);
4833
4834 SkPath p, empty;
4835 SkRect bounds, bounds2;
4836 test_empty(reporter, p);
4837
4838 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4839
4840 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4841 SkPath& self = p;
4842 p = self;
4843
4844 // this triggers a code path in SkPath::swap which is otherwise unexercised
4845 p.swap(self);
4846
Mike Reed92b33352019-08-24 19:39:13 -04004847 bounds.setLTRB(0, 0, SK_Scalar1, SK_Scalar1);
Yuqian Li3154a532017-09-06 13:33:30 -04004848
4849 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4850 check_convex_bounds(reporter, p, bounds);
4851 // we have quads or cubics
4852 REPORTER_ASSERT(reporter,
4853 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4854 REPORTER_ASSERT(reporter, !p.isEmpty());
4855
4856 p.reset();
4857 test_empty(reporter, p);
4858
4859 p.addOval(bounds);
4860 check_convex_bounds(reporter, p, bounds);
4861 REPORTER_ASSERT(reporter, !p.isEmpty());
4862
4863 p.rewind();
4864 test_empty(reporter, p);
4865
4866 p.addRect(bounds);
4867 check_convex_bounds(reporter, p, bounds);
4868 // we have only lines
4869 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4870 REPORTER_ASSERT(reporter, !p.isEmpty());
4871
4872 REPORTER_ASSERT(reporter, p != empty);
4873 REPORTER_ASSERT(reporter, !(p == empty));
4874
4875 // do getPoints and getVerbs return the right result
4876 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4877 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4878 SkPoint pts[4];
4879 int count = p.getPoints(pts, 4);
4880 REPORTER_ASSERT(reporter, count == 4);
4881 uint8_t verbs[6];
4882 verbs[5] = 0xff;
4883 p.getVerbs(verbs, 5);
4884 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4885 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4886 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4887 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4888 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4889 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
Mike Reed92b33352019-08-24 19:39:13 -04004890 bounds2.setBounds(pts, 4);
Yuqian Li3154a532017-09-06 13:33:30 -04004891 REPORTER_ASSERT(reporter, bounds == bounds2);
4892
4893 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4894 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4895 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4896
4897 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4898 bounds2.setEmpty();
4899 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4900 REPORTER_ASSERT(reporter, bounds == bounds2);
4901
4902 // now force p to not be a rect
Mike Reed92b33352019-08-24 19:39:13 -04004903 bounds.setWH(SK_Scalar1/2, SK_Scalar1/2);
Yuqian Li3154a532017-09-06 13:33:30 -04004904 p.addRect(bounds);
4905 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4906
4907 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4908 // path has a trailing moveTo. Please see crbug.com\445368)
4909 {
4910 SkRect r;
4911 p.reset();
4912 p.addRect(bounds);
4913 REPORTER_ASSERT(reporter, p.isRect(&r));
4914 REPORTER_ASSERT(reporter, r == bounds);
4915 // add a moveTo outside of our bounds
4916 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4917 REPORTER_ASSERT(reporter, p.isRect(&r));
4918 REPORTER_ASSERT(reporter, r == bounds);
4919 }
4920
4921 test_operatorEqual(reporter);
4922 test_isLine(reporter);
4923 test_isRect(reporter);
Chris Dalton34c50522020-09-17 15:45:20 -06004924 test_is_closed_rect(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004925 test_isNestedFillRects(reporter);
4926 test_zero_length_paths(reporter);
4927 test_direction(reporter);
4928 test_convexity(reporter);
4929 test_convexity2(reporter);
Cary Clarkc9b7c722018-12-12 14:50:23 -05004930 test_convexity_doubleback(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004931 test_conservativelyContains(reporter);
4932 test_close(reporter);
4933 test_segment_masks(reporter);
4934 test_flattening(reporter);
4935 test_transform(reporter);
4936 test_bounds(reporter);
4937 test_iter(reporter);
Chris Daltonde500372020-05-05 15:06:30 -06004938 test_range_iter(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004939 test_circle(reporter);
4940 test_oval(reporter);
4941 test_strokerec(reporter);
4942 test_addPoly(reporter);
4943 test_isfinite(reporter);
4944 test_isfinite_after_transform(reporter);
4945 test_islastcontourclosed(reporter);
4946 test_arb_round_rect_is_convex(reporter);
4947 test_arb_zero_rad_round_rect_is_rect(reporter);
4948 test_addrect(reporter);
4949 test_addrect_isfinite(reporter);
4950 test_tricky_cubic();
4951 test_clipped_cubic();
4952 test_crbug_170666();
4953 test_crbug_493450(reporter);
4954 test_crbug_495894(reporter);
4955 test_crbug_613918();
4956 test_bad_cubic_crbug229478();
4957 test_bad_cubic_crbug234190();
4958 test_gen_id(reporter);
4959 test_path_close_issue1474(reporter);
4960 test_path_to_region(reporter);
4961 test_rrect(reporter);
William Hesse3c246112021-08-05 18:27:06 +02004962 test_rMoveTo(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004963 test_arc(reporter);
4964 test_arc_ovals(reporter);
4965 test_arcTo(reporter);
4966 test_addPath(reporter);
4967 test_addPathMode(reporter, false, false);
4968 test_addPathMode(reporter, true, false);
4969 test_addPathMode(reporter, false, true);
4970 test_addPathMode(reporter, true, true);
4971 test_extendClosedPath(reporter);
4972 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4973 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4974 test_conicTo_special_case(reporter);
4975 test_get_point(reporter);
4976 test_contains(reporter);
4977 PathTest_Private::TestPathTo(reporter);
4978 PathRefTest_Private::TestPathRef(reporter);
4979 PathTest_Private::TestPathrefListeners(reporter);
4980 test_dump(reporter);
4981 test_path_crbug389050(reporter);
4982 test_path_crbugskia2820(reporter);
4983 test_path_crbugskia5995();
4984 test_skbug_3469(reporter);
4985 test_skbug_3239(reporter);
4986 test_bounds_crbug_513799(reporter);
4987 test_fuzz_crbug_638223();
4988}
4989
4990DEF_TEST(conservatively_contains_rect, reporter) {
4991 SkPath path;
4992
4993 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4994 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4995 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4996 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4997 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4998 path.moveTo(0, 0);
4999
Leon Scroggins IIIa4c80982020-07-28 10:20:58 -04005000 // this should not assert
Yuqian Li3154a532017-09-06 13:33:30 -04005001 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
5002}
5003
5004///////////////////////////////////////////////////////////////////////////////////////////////////
5005
5006static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
5007 for (int i = 0; i < n; ++i) {
5008 switch (verb) {
5009 case SkPath::kLine_Verb:
5010 path->lineTo(rand.nextF()*100, rand.nextF()*100);
5011 break;
5012 case SkPath::kQuad_Verb:
5013 path->quadTo(rand.nextF()*100, rand.nextF()*100,
5014 rand.nextF()*100, rand.nextF()*100);
5015 break;
5016 case SkPath::kConic_Verb:
5017 path->conicTo(rand.nextF()*100, rand.nextF()*100,
5018 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
5019 break;
5020 case SkPath::kCubic_Verb:
5021 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
5022 rand.nextF()*100, rand.nextF()*100,
5023 rand.nextF()*100, rand.nextF()*100);
5024 break;
5025 default:
5026 SkASSERT(false);
5027 }
5028 }
5029}
5030
Mike Kleinc0bd9f92019-04-23 12:05:21 -05005031#include "include/pathops/SkPathOps.h"
Yuqian Li3154a532017-09-06 13:33:30 -04005032DEF_TEST(path_tight_bounds, reporter) {
5033 SkRandom rand;
5034
5035 const SkPath::Verb verbs[] = {
5036 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
5037 };
5038 for (int i = 0; i < 1000; ++i) {
5039 for (int n = 1; n <= 10; n += 9) {
5040 for (SkPath::Verb verb : verbs) {
5041 SkPath path;
5042 rand_path(&path, rand, verb, n);
5043 SkRect bounds = path.getBounds();
5044 SkRect tight = path.computeTightBounds();
5045 REPORTER_ASSERT(reporter, bounds.contains(tight));
5046
5047 SkRect tight2;
5048 TightBounds(path, &tight2);
5049 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
5050 }
5051 }
5052 }
5053}
5054
5055DEF_TEST(skbug_6450, r) {
5056 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
5057 SkVector rdi[4] = {
5058 { 1.81159976e-09f, 7.58768801e-05f },
5059 { 0.000118725002f, 0.000118725002f },
5060 { 0.000118725002f, 0.000118725002f },
5061 { 0.000118725002f, 0.486297607f }
5062 };
5063 SkRRect irr;
5064 irr.setRectRadii(ri, rdi);
5065 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
5066 SkVector rdo[4] = {
5067 { 0, 0 },
5068 { 0.0103298295f, 0.185887396f },
5069 { 2.52999727e-29f, 169.001938f },
5070 { 195.262741f, 195.161255f }
5071 };
5072 SkRRect orr;
5073 orr.setRectRadii(ro, rdo);
5074 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
5075}
5076
5077DEF_TEST(PathRefSerialization, reporter) {
5078 SkPath path;
5079 const size_t numMoves = 5;
5080 const size_t numConics = 7;
5081 const size_t numPoints = numMoves + 2 * numConics;
5082 const size_t numVerbs = numMoves + numConics;
5083 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
5084 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
5085 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
5086 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
5087
5088 // Verify that path serializes/deserializes properly.
5089 sk_sp<SkData> data = path.serialize();
5090 size_t bytesWritten = data->size();
5091
5092 {
5093 SkPath readBack;
5094 REPORTER_ASSERT(reporter, readBack != path);
5095 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
5096 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
5097 REPORTER_ASSERT(reporter, readBack == path);
5098 }
5099
Yuqian Li3154a532017-09-06 13:33:30 -04005100 // One less byte (rounded down to alignment) than was written will also
5101 // fail to be deserialized.
5102 {
5103 SkPath readBack;
5104 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
5105 REPORTER_ASSERT(reporter, !bytesRead);
5106 }
5107}
Mike Klein1170a552017-09-08 15:00:25 -04005108
5109DEF_TEST(NonFinitePathIteration, reporter) {
5110 SkPath path;
5111 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
Chris Daltonde500372020-05-05 15:06:30 -06005112 SkPathPriv::Iterate iterate(path);
5113 REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
Mike Klein1170a552017-09-08 15:00:25 -04005114}
Cary Clark1acd3c72017-12-08 11:37:01 -05005115
Cary Clark1acd3c72017-12-08 11:37:01 -05005116DEF_TEST(AndroidArc, reporter) {
5117 const char* tests[] = {
Brian Salomon7a492f72020-08-21 12:39:33 -04005118 "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",
5119 ("M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
5120 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z"),
5121 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
Cary Clark1acd3c72017-12-08 11:37:01 -05005122 };
5123 for (auto test : tests) {
5124 SkPath aPath;
5125 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
5126 SkASSERT(aPath.isConvex());
5127 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
5128 SkPath scalePath = aPath;
5129 SkMatrix matrix;
5130 matrix.setScale(scale, scale);
5131 scalePath.transform(matrix);
5132 SkASSERT(scalePath.isConvex());
5133 }
5134 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
5135 SkPath scalePath = aPath;
5136 SkMatrix matrix;
5137 matrix.setScale(scale, scale);
5138 scalePath.transform(matrix);
5139 SkASSERT(scalePath.isConvex());
5140 }
5141 }
5142}
Mike Reed1fe0bcc2018-01-22 16:49:49 -05005143
5144/*
5145 * Try a range of crazy values, just to ensure that we don't assert/crash.
5146 */
5147DEF_TEST(HugeGeometry, reporter) {
5148 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
5149 auto canvas = surf->getCanvas();
5150
5151 const bool aas[] = { false, true };
5152 const SkPaint::Style styles[] = {
5153 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
5154 };
5155 const SkScalar values[] = {
5156 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
5157 SK_ScalarInfinity
5158 };
5159
5160 SkPaint paint;
5161 for (auto x : values) {
5162 SkRect r = { -x, -x, x, x };
5163 for (auto width : values) {
5164 paint.setStrokeWidth(width);
5165 for (auto aa : aas) {
5166 paint.setAntiAlias(aa);
5167 for (auto style : styles) {
5168 paint.setStyle(style);
5169 canvas->drawRect(r, paint);
5170 canvas->drawOval(r, paint);
5171 }
5172 }
5173 }
5174 }
5175
5176}
5177
Mike Reed1c6e7832018-01-30 11:29:36 -05005178// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
5179DEF_TEST(ClipPath_nonfinite, reporter) {
5180 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
5181 SkCanvas* canvas = surf->getCanvas();
5182
5183 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5184 for (bool aa : {false, true}) {
Mike Reed7d34dc72019-11-26 12:17:17 -05005185 for (auto ft : {SkPathFillType::kWinding, SkPathFillType::kInverseWinding}) {
Mike Reed1c6e7832018-01-30 11:29:36 -05005186 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
5187 for (int bits = 1; bits <= 15; ++bits) {
5188 SkPoint p0 = { 0, 0 };
5189 SkPoint p1 = { 0, 0 };
5190 if (bits & 1) p0.fX = -bad;
5191 if (bits & 2) p0.fY = -bad;
5192 if (bits & 4) p1.fX = bad;
5193 if (bits & 8) p1.fY = bad;
5194
5195 SkPath path;
5196 path.moveTo(p0);
5197 path.lineTo(p1);
5198 path.setFillType(ft);
5199 canvas->save();
5200 canvas->clipPath(path, aa);
5201 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
5202 canvas->restore();
5203 }
5204 }
5205 }
5206 }
5207 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5208}
5209
Cary Clark5c715182018-04-09 16:07:11 -04005210// skbug.com/7792
5211DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04005212 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
5213 SkPath path;
5214 for (size_t index = 0; index < count; ++index) {
5215 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
5216 }
Cary Clark31608c02018-04-12 10:29:46 -04005217 if (close) {
5218 path.close();
5219 }
Cary Clark8540e112018-04-11 14:30:27 -04005220 return path;
5221 };
5222 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
5223 SkPath path;
5224 for (size_t index = 0; index < count; ++index) {
5225 switch (verbs[index]) {
5226 case SkPath::kMove_Verb:
5227 path.moveTo(*points++);
5228 break;
5229 case SkPath::kLine_Verb:
5230 path.lineTo(*points++);
5231 break;
5232 case SkPath::kClose_Verb:
5233 path.close();
5234 break;
5235 default:
5236 SkASSERT(0);
5237 }
5238 }
5239 return path;
5240 };
Cary Clarkd4228472018-04-13 07:07:04 -04005241 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04005242 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04005243 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5244 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04005245 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04005246 SkRect compare;
Mike Reed92b33352019-08-24 19:39:13 -04005247 compare.setBounds(&points[1], SK_ARRAY_COUNT(points) - 1);
Cary Clark5c715182018-04-09 16:07:11 -04005248 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005249 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04005250 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
5251 path = makePath(points3, SK_ARRAY_COUNT(points3), 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#c9
Cary Clark8540e112018-04-11 14:30:27 -04005254 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5255 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005256 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005257 compare.setBounds(&points9[1], SK_ARRAY_COUNT(points9) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005258 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005259 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04005260 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5261 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
5262 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
5263 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04005264 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005265 compare.setBounds(&points11[0], SK_ARRAY_COUNT(points11));
Cary Clark8540e112018-04-11 14:30:27 -04005266 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005267 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04005268 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
5269 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5270 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
5271 SkPath::kLine_Verb, SkPath::kClose_Verb };
5272 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
5273 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
5274 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04005275 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005276 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04005277 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5278 SkPath::kLine_Verb, SkPath::kMove_Verb };
5279 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
5280 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04005281 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005282 compare.setBounds(&points15[0], SK_ARRAY_COUNT(points15) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005283 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005284 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04005285 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
5286 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005287 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005288 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04005289 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5290 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5291 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
5292 SkPath::kLine_Verb, SkPath::kLine_Verb };
5293 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5294 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
5295 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04005296 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005297 // isolated from skbug.com/7792#c23
5298 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5299 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5300 SkPath::kLine_Verb, SkPath::kClose_Verb };
5301 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5302 {75, 150} };
5303 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04005304 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005305 compare.setBounds(&points23[0], SK_ARRAY_COUNT(points23));
Cary Clarkd4228472018-04-13 07:07:04 -04005306 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005307 // isolated from skbug.com/7792#c29
5308 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5309 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5310 SkPath::kClose_Verb };
5311 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5312 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005313 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005314 // isolated from skbug.com/7792#c31
5315 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5316 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5317 SkPath::kClose_Verb };
5318 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5319 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005320 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005321 compare.setBounds(&points31[0], 4);
Cary Clarka7651562018-04-17 09:30:14 -04005322 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005323 // isolated from skbug.com/7792#c36
5324 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5325 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5326 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5327 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005328 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005329 // isolated from skbug.com/7792#c39
5330 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005331 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005332 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5333 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005334 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005335 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005336 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5337 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5338 SkPath::kLine_Verb, SkPath::kClose_Verb };
5339 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5340 {32, 2} };
5341 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5342 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005343 compare.setBounds(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
Cary Clarkb120e922018-04-18 12:25:08 -04005344 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005345 // isolated from skbug.com/7792#c41
5346 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5347 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5348 SkPath::kClose_Verb };
5349 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5350 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5351 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005352 compare.setBounds(&points41[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005353 REPORTER_ASSERT(reporter, rect == compare);
5354 // isolated from skbug.com/7792#c53
5355 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5356 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5357 SkPath::kClose_Verb };
5358 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5359 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5360 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005361 compare.setBounds(&points53[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005362 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005363}
Mike Reedee430912018-05-23 12:12:21 -04005364
Mike Reedc3d8a482018-09-12 10:08:40 -04005365// Be sure we can safely add ourselves
5366DEF_TEST(Path_self_add, reporter) {
5367 // The possible problem is that during path.add() we may have to grow the dst buffers as
5368 // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
5369 // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
5370 // garbage.
5371 //
5372 // The test runs though verious sized src paths, since its not defined publicly what the
5373 // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
5374 // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
5375 // an ASAN error w/o the fix to SkPath::addPath().
5376 //
5377 for (int count = 0; count < 10; ++count) {
5378 SkPath path;
5379 for (int add = 0; add < count; ++add) {
5380 // just add some stuff, so we have something to copy/append in addPath()
5381 path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
5382 }
5383 path.addPath(path, 1, 2);
5384 path.addPath(path, 3, 4);
5385 }
5386}
5387
Mike Kleinc0bd9f92019-04-23 12:05:21 -05005388#include "include/core/SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005389static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5390 // draw in different ways, looking for an assert
5391
5392 {
5393 SkPath path;
5394 path.addPoly(pts, 3, false);
5395 canvas->drawPath(path, SkPaint());
5396 }
5397
5398 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5399 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5400 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5401}
5402
Mike Reedee430912018-05-23 12:12:21 -04005403DEF_TEST(triangle_onehalf, reporter) {
5404 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5405
5406 const SkPoint pts[] = {
5407 { 0.499069244f, 9.63295173f },
5408 { 0.499402374f, 7.88207579f },
5409 { 10.2363272f, 0.49999997f }
5410 };
Mike Reed50595412018-05-30 16:32:33 -04005411 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005412}
Mike Reed50595412018-05-30 16:32:33 -04005413
5414DEF_TEST(triangle_big, reporter) {
5415 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5416
5417 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5418 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5419 // be conservative, so we invoke clipping if we get in this situation.
5420 // This test was added to demonstrate the need for this conservative bounds calc.
5421 // (found by a fuzzer)
5422 const SkPoint pts[] = {
5423 { 0.327190518f, -114.945152f },
5424 { -0.5f, 1.00003874f },
5425 { 0.666425824f, 4304.26172f },
5426 };
5427 draw_triangle(surface->getCanvas(), pts);
5428}
5429
Florin Malita3d413c52018-09-11 14:01:42 -04005430static void add_verbs(SkPath* path, int count) {
5431 path->moveTo(0, 0);
5432 for (int i = 0; i < count; ++i) {
5433 switch (i & 3) {
5434 case 0: path->lineTo(10, 20); break;
5435 case 1: path->quadTo(5, 6, 7, 8); break;
5436 case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
5437 case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
5438 }
5439 }
5440}
5441
5442// Make sure when we call shrinkToFit() that we always shrink (or stay the same)
5443// and that if we call twice, we stay the same.
5444DEF_TEST(Path_shrinkToFit, reporter) {
5445 size_t max_free = 0;
5446 for (int verbs = 0; verbs < 100; ++verbs) {
5447 SkPath unique_path, shared_path;
5448 add_verbs(&unique_path, verbs);
5449 add_verbs(&shared_path, verbs);
5450
5451 const SkPath copy = shared_path;
Mike Reedc924a882020-10-13 21:22:47 -04005452
Florin Malita3d413c52018-09-11 14:01:42 -04005453 REPORTER_ASSERT(reporter, shared_path == unique_path);
5454 REPORTER_ASSERT(reporter, shared_path == copy);
5455
Mike Reedc924a882020-10-13 21:22:47 -04005456 uint32_t uID = unique_path.getGenerationID();
5457 uint32_t sID = shared_path.getGenerationID();
5458 uint32_t cID = copy.getGenerationID();
5459 REPORTER_ASSERT(reporter, sID == cID);
5460
Florin Malita3d413c52018-09-11 14:01:42 -04005461#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005462 size_t before = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005463#endif
Mike Reedf1f1e7d2020-10-15 14:52:54 -04005464 SkPathPriv::ShrinkToFit(&unique_path);
5465 SkPathPriv::ShrinkToFit(&shared_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005466 REPORTER_ASSERT(reporter, shared_path == unique_path);
5467 REPORTER_ASSERT(reporter, shared_path == copy);
5468
Mike Reedc924a882020-10-13 21:22:47 -04005469 // since the unique_path is "unique", it's genID need not have changed even though
5470 // unique_path has changed (been shrunk)
5471 REPORTER_ASSERT(reporter, uID == unique_path.getGenerationID());
5472 // since the copy has not been changed, its ID should be the same
5473 REPORTER_ASSERT(reporter, cID == copy.getGenerationID());
5474 // but since shared_path has changed, and was not uniquely owned, it's gen ID needs to have
5475 // changed, breaking the "sharing" -- this is done defensively in case there were any
5476 // outstanding Iterators active on copy, which could have been invalidated during
5477 // shrinkToFit.
5478 REPORTER_ASSERT(reporter, sID != shared_path.getGenerationID());
5479
Florin Malita3d413c52018-09-11 14:01:42 -04005480#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005481 size_t after = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005482 REPORTER_ASSERT(reporter, before >= after);
5483 max_free = std::max(max_free, before - after);
5484
Florin Malita0e0f1a72018-09-11 16:16:19 -04005485 size_t after2 = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005486 REPORTER_ASSERT(reporter, after == after2);
5487#endif
5488 }
5489 if (false) {
5490 SkDebugf("max_free %zu\n", max_free);
5491 }
5492}
Mike Klein9de72162018-09-24 20:30:28 -04005493
5494DEF_TEST(Path_setLastPt, r) {
5495 // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
5496 SkPath p;
5497 p.moveTo(0,0);
5498 p.moveTo(20,01);
5499 p.moveTo(20,10);
5500 p.moveTo(20,61);
5501 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
5502
5503 p.setLastPt(30,01);
5504 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10)); // was {0,0, 20,61}
5505
5506 REPORTER_ASSERT(r, p.isValid());
Mike Klein9de72162018-09-24 20:30:28 -04005507}
Mike Reed6a388002018-10-16 13:13:09 -04005508
5509DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
5510 SkPath path;
5511
5512 path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
5513
5514 // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
5515 SkPath shallowPath = path;
5516
5517 // make sure we don't assert/crash on this.
5518 shallowPath.incReserve(0xffffffff);
5519}
5520
Mike Reed07105bb2018-12-01 14:07:49 -05005521////////////////////////////////////////////////////////////////////////////////////////////////
5522
5523/*
5524 * For speed, we tried to preserve useful/expensive attributes about paths,
5525 * - convexity, isrect, isoval, ...
5526 * Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
5527 * is axis-aligned (e.g. scale+translate)
5528 */
5529
5530struct Xforms {
5531 SkMatrix fIM, fTM, fSM, fRM;
5532
5533 Xforms() {
5534 fIM.reset();
5535 fTM.setTranslate(10, 20);
5536 fSM.setScale(2, 3);
5537 fRM.setRotate(30);
5538 }
5539};
5540
Mike Reed07105bb2018-12-01 14:07:49 -05005541static bool conditional_convex(const SkPath& path, bool is_convex) {
Mike Reed3872c982020-08-29 17:46:51 -04005542 SkPathConvexity c = SkPathPriv::GetConvexityOrUnknown(path);
5543 return is_convex ? (c == SkPathConvexity::kConvex) : (c != SkPathConvexity::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005544}
Mike Reed07105bb2018-12-01 14:07:49 -05005545
5546// expect axis-aligned shape to survive assignment, identity and scale/translate matrices
5547template <typename ISA>
5548void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
5549 ISA isa_proc) {
5550 REPORTER_ASSERT(reporter, isa_proc(*path));
5551 // force the issue (computing convexity) the first time.
Mike Reed6052c0b2020-08-28 11:49:56 -04005552 REPORTER_ASSERT(reporter, path->isConvex());
Mike Reed07105bb2018-12-01 14:07:49 -05005553
5554 SkPath path2;
5555
5556 // a path's isa and convexity should survive assignment
5557 path2 = *path;
5558 REPORTER_ASSERT(reporter, isa_proc(path2));
Mike Reed3872c982020-08-29 17:46:51 -04005559 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) == SkPathConvexity::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005560
5561 // a path's isa and convexity should identity transform
5562 path->transform(x.fIM, &path2);
5563 path->transform(x.fIM);
5564 REPORTER_ASSERT(reporter, isa_proc(path2));
Mike Reed3872c982020-08-29 17:46:51 -04005565 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) == SkPathConvexity::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005566 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed3872c982020-08-29 17:46:51 -04005567 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(*path) == SkPathConvexity::kConvex);
Mike Reed07105bb2018-12-01 14:07:49 -05005568
5569 // a path's isa should survive translation, convexity depends on axis alignment
5570 path->transform(x.fTM, &path2);
5571 path->transform(x.fTM);
5572 REPORTER_ASSERT(reporter, isa_proc(path2));
5573 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005574 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5575 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005576
5577 // a path's isa should survive scaling, convexity depends on axis alignment
5578 path->transform(x.fSM, &path2);
5579 path->transform(x.fSM);
5580 REPORTER_ASSERT(reporter, isa_proc(path2));
5581 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005582 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5583 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005584
5585 // For security, post-rotation, we can't assume we're still convex. It might prove to be,
5586 // in fact, still be convex, be we can't have cached that setting, hence the call to
Mike Reed3872c982020-08-29 17:46:51 -04005587 // getConvexityOrUnknown() instead of getConvexity().
Mike Reed07105bb2018-12-01 14:07:49 -05005588 path->transform(x.fRM, &path2);
5589 path->transform(x.fRM);
Mike Reed3872c982020-08-29 17:46:51 -04005590 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) != SkPathConvexity::kConvex);
5591 REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(*path) != SkPathConvexity::kConvex);
Mike Reed375721d2020-08-07 16:00:06 -04005592
Mike Reed07105bb2018-12-01 14:07:49 -05005593 if (isAxisAligned) {
5594 REPORTER_ASSERT(reporter, !isa_proc(path2));
5595 REPORTER_ASSERT(reporter, !isa_proc(*path));
5596 }
Mike Reed07105bb2018-12-01 14:07:49 -05005597}
5598
5599DEF_TEST(Path_survive_transform, r) {
5600 const Xforms x;
5601
5602 SkPath path;
5603 path.addRect({10, 10, 40, 50});
5604 survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
5605
5606 path.reset();
5607 path.addOval({10, 10, 40, 50});
5608 survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
5609
5610 path.reset();
5611 path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
5612 survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
5613
5614 // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
5615 path.reset();
5616 path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
Mike Reed6052c0b2020-08-28 11:49:56 -04005617 REPORTER_ASSERT(r, path.isConvex());
Mike Reed07105bb2018-12-01 14:07:49 -05005618 survive(&path, x, false, r, [](const SkPath& p) { return true; });
5619}
Mike Reedfb5f43b2018-12-18 12:17:57 -05005620
5621DEF_TEST(path_last_move_to_index, r) {
5622 // Make sure that copyPath is safe after the call to path.offset().
5623 // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
5624 // set it to path's value inside SkPath::transform()
5625
Mike Reeddf3d2252018-12-20 17:10:27 -05005626 const char text[] = "hello";
5627 constexpr size_t len = sizeof(text) - 1;
5628 SkGlyphID glyphs[len];
Mike Reedfb5f43b2018-12-18 12:17:57 -05005629
Mike Reeddf3d2252018-12-20 17:10:27 -05005630 SkFont font;
Ben Wagner51e15a62019-05-07 15:38:46 -04005631 font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
Mike Reeddf3d2252018-12-20 17:10:27 -05005632
5633 SkPath copyPath;
5634 SkFont().getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
5635 if (src) {
5636 ((SkPath*)ctx)->addPath(*src, mx);
5637 }
5638 }, &copyPath);
Mike Reedfb5f43b2018-12-18 12:17:57 -05005639
Kevin Lubick13486cb2019-01-04 09:49:47 -05005640 SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
Mike Reeddf3d2252018-12-20 17:10:27 -05005641 SkPath path;
Mike Reedfb5f43b2018-12-18 12:17:57 -05005642 path.addRoundRect({10, 10, 110, 110}, radii);
5643 path.offset(0, 5, &(copyPath)); // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
5644
5645 copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
5646}
Mike Reed5f152f02019-08-19 14:40:16 -04005647
5648static void test_edger(skiatest::Reporter* r,
5649 const std::initializer_list<SkPath::Verb>& in,
5650 const std::initializer_list<SkPath::Verb>& expected) {
5651 SkPath path;
5652 SkScalar x = 0, y = 0;
5653 for (auto v : in) {
5654 switch (v) {
5655 case SkPath::kMove_Verb: path.moveTo(x++, y++); break;
5656 case SkPath::kLine_Verb: path.lineTo(x++, y++); break;
5657 case SkPath::kClose_Verb: path.close(); break;
5658 default: SkASSERT(false);
5659 }
5660 }
5661
5662 SkPathEdgeIter iter(path);
5663 for (auto v : expected) {
5664 auto e = iter.next();
5665 REPORTER_ASSERT(r, e);
5666 REPORTER_ASSERT(r, SkPathEdgeIter::EdgeToVerb(e.fEdge) == v);
5667 }
Mike Reed375721d2020-08-07 16:00:06 -04005668 REPORTER_ASSERT(r, !iter.next());
Mike Reed5f152f02019-08-19 14:40:16 -04005669}
5670
Mike Reedb21c1f22020-10-31 10:31:55 -04005671static void assert_points(skiatest::Reporter* reporter,
5672 const SkPath& path, const std::initializer_list<SkPoint>& list) {
5673 const SkPoint* expected = list.begin();
5674 SkPath::RawIter iter(path);
5675 for (size_t i = 0;;) {
5676 SkPoint pts[4];
5677 switch (iter.next(pts)) {
5678 case SkPath::kDone_Verb:
5679 REPORTER_ASSERT(reporter, i == list.size());
5680 return;
5681 case SkPath::kMove_Verb:
5682 REPORTER_ASSERT(reporter, pts[0] == expected[i]);
5683 i++;
5684 break;
5685 case SkPath::kLine_Verb:
5686 REPORTER_ASSERT(reporter, pts[1] == expected[i]);
5687 i++;
5688 break;
5689 case SkPath::kClose_Verb: break;
5690 default: SkASSERT(false);
5691 }
5692 }
5693}
5694
5695static void test_addRect_and_trailing_lineTo(skiatest::Reporter* reporter) {
5696 SkPath path;
5697 const SkRect r = {1, 2, 3, 4};
5698 // build our default p-array clockwise
5699 const SkPoint p[] = {
5700 {r.fLeft, r.fTop}, {r.fRight, r.fTop},
5701 {r.fRight, r.fBottom}, {r.fLeft, r.fBottom},
5702 };
5703
5704 for (auto dir : {SkPathDirection::kCW, SkPathDirection::kCCW}) {
5705 int increment = dir == SkPathDirection::kCW ? 1 : 3;
5706 for (int i = 0; i < 4; ++i) {
5707 path.reset();
5708 path.addRect(r, dir, i);
5709
5710 // check that we return the 4 ponts in the expected order
5711 SkPoint e[4];
5712 for (int j = 0; j < 4; ++j) {
5713 int index = (i + j*increment) % 4;
5714 e[j] = p[index];
5715 }
5716 assert_points(reporter, path, {
5717 e[0], e[1], e[2], e[3]
5718 });
5719
5720 // check that the new line begins where the rect began
5721 path.lineTo(7,8);
5722 assert_points(reporter, path, {
5723 e[0], e[1], e[2], e[3],
5724 e[0], {7,8},
5725 });
5726 }
5727 }
5728
5729 // now add a moveTo before the rect, just to be sure we don't always look at
5730 // the "first" point in the path when we handle the trailing lineTo
5731 path.reset();
5732 path.moveTo(7, 8);
5733 path.addRect(r, SkPathDirection::kCW, 2);
5734 path.lineTo(5, 6);
5735
5736 assert_points(reporter, path, {
5737 {7,8}, // initial moveTo
5738 p[2], p[3], p[0], p[1], // rect
5739 p[2], {5, 6}, // trailing line
5740 });
5741}
5742
Mike Reed282f8f42020-11-10 15:34:10 -05005743/*
5744 * SkPath allows the caller to "skip" calling moveTo for contours. If lineTo (or a curve) is
5745 * called on an empty path, a 'moveTo(0,0)' will automatically be injected. If the path is
5746 * not empty, but its last contour has been "closed", then it will inject a moveTo corresponding
5747 * to where the last contour itself started (i.e. its moveTo).
5748 *
5749 * This test exercises this in a particular case:
5750 * path.moveTo(...) <-- needed to show the bug
5751 * path.moveTo....close()
5752 * // at this point, the path's verbs are: M M ... C
5753 *
5754 * path.lineTo(...)
5755 * // after lineTo, the path's verbs are: M M ... C M L
5756 */
5757static void test_addPath_and_injected_moveTo(skiatest::Reporter* reporter) {
5758 /*
5759 * Given a path, and the expected last-point and last-move-to in it,
5760 * assert that, after a lineTo(), that the injected moveTo corresponds
5761 * to the expected value.
5762 */
5763 auto test_before_after_lineto = [reporter](SkPath& path,
5764 SkPoint expectedLastPt,
5765 SkPoint expectedMoveTo) {
5766 SkPoint p = path.getPoint(path.countPoints() - 1);
5767 REPORTER_ASSERT(reporter, p == expectedLastPt);
5768
5769 const SkPoint newLineTo = {1234, 5678};
5770 path.lineTo(newLineTo);
5771
5772 p = path.getPoint(path.countPoints() - 2);
5773 REPORTER_ASSERT(reporter, p == expectedMoveTo); // this was injected by lineTo()
5774
5775 p = path.getPoint(path.countPoints() - 1);
5776 REPORTER_ASSERT(reporter, p == newLineTo);
5777 };
5778
5779 SkPath path1;
5780 SkPath path2;
5781
5782 path1.moveTo(230, 230); // Needed to show the bug: a moveTo before the addRect
5783
5784 // add a rect, but the shape doesn't really matter
5785 path1.moveTo(20,30).lineTo(40,30).lineTo(40,50).lineTo(20,50).close();
5786
5787 path2.addPath(path1); // this must correctly update its "last-move-to" so that when
5788 // lineTo is called, it will inject the correct moveTo.
5789
5790 // at this point, path1 and path2 should be the same...
5791
5792 test_before_after_lineto(path1, {20,50}, {20,30});
5793 test_before_after_lineto(path2, {20,50}, {20,30});
5794}
5795
Mike Reed5f152f02019-08-19 14:40:16 -04005796DEF_TEST(pathedger, r) {
5797 auto M = SkPath::kMove_Verb;
5798 auto L = SkPath::kLine_Verb;
5799 auto C = SkPath::kClose_Verb;
5800
5801 test_edger(r, { M }, {});
5802 test_edger(r, { M, M }, {});
5803 test_edger(r, { M, C }, {});
5804 test_edger(r, { M, M, C }, {});
5805 test_edger(r, { M, L }, { L, L });
5806 test_edger(r, { M, L, C }, { L, L });
5807 test_edger(r, { M, L, L }, { L, L, L });
5808 test_edger(r, { M, L, L, C }, { L, L, L });
5809
5810 test_edger(r, { M, L, L, M, L, L }, { L, L, L, L, L, L });
Mike Reed282f8f42020-11-10 15:34:10 -05005811
5812 test_addRect_and_trailing_lineTo(r);
5813 test_addPath_and_injected_moveTo(r);
Mike Reed5f152f02019-08-19 14:40:16 -04005814}
Mike Reed0dd0ed62020-12-09 13:49:17 -05005815
5816DEF_TEST(path_addpath_crbug_1153516, r) {
5817 // When we add a path to another path, we need to sniff out in case the argument ended
5818 // with a kClose, in which case we need to fiddle with our lastMoveIndex (as ::close() does)
5819 SkPath p1, p2;
5820 p1.addRect({143,226,200,241});
5821 p1.addPath(p1);
5822 p1.lineTo(262,513); // this should not assert
5823}
Mike Reed543266a2021-01-30 17:57:39 -05005824
5825DEF_TEST(path_convexity_scale_way_down, r) {
5826 SkPath path = SkPathBuilder().moveTo(0,0).lineTo(1, 0)
5827 .lineTo(1,1).lineTo(0,1)
5828 .detach();
5829
5830 REPORTER_ASSERT(r, path.isConvex());
5831 SkPath path2;
5832 const SkScalar scale = 1e-8f;
5833 path.transform(SkMatrix::Scale(scale, scale), &path2);
5834 SkPathPriv::ForceComputeConvexity(path2);
5835 REPORTER_ASSERT(r, path2.isConvex());
5836}
Michael Ludwig48f10652021-04-22 09:34:45 -04005837
5838// crbug.com/1187385
5839DEF_TEST(path_moveto_addrect, r) {
5840 // Test both an empty and non-empty rect passed to SkPath::addRect
5841 SkRect rects[] = {{207.0f, 237.0f, 300.0f, 237.0f},
5842 {207.0f, 237.0f, 300.0f, 267.0f}};
5843
5844 for (SkRect rect: rects) {
5845 for (int numExtraMoveTos : {0, 1, 2, 3}) {
5846 SkPath path;
5847 // Convexity and contains functions treat the path as a simple fill, so consecutive
5848 // moveTos are collapsed together.
5849 for (int i = 0; i < numExtraMoveTos; ++i) {
5850 path.moveTo(i, i);
5851 }
5852 path.addRect(rect);
5853
5854 REPORTER_ASSERT(r, (numExtraMoveTos + 1) == SkPathPriv::LeadingMoveToCount(path));
5855
5856 // addRect should mark the path as known convex automatically (i.e. it wasn't set
5857 // to unknown after edits)
5858 SkPathConvexity origConvexity = SkPathPriv::GetConvexityOrUnknown(path);
5859 REPORTER_ASSERT(r, origConvexity == SkPathConvexity::kConvex);
5860
5861 // but it should also agree with the regular convexity computation
5862 SkPathPriv::ForceComputeConvexity(path);
5863 REPORTER_ASSERT(r, path.isConvex());
5864
5865 SkRect query = rect.makeInset(10.f, 0.f);
5866 REPORTER_ASSERT(r, path.conservativelyContainsRect(query));
5867 }
5868 }
5869}
Michael Ludwig9f73b042021-06-21 17:04:40 -04005870
5871// crbug.com/1220754
5872DEF_TEST(path_moveto_twopass_convexity, r) {
5873 // There had been a bug when the last moveTo index > 0, the calculated point count was incorrect
5874 // and the BySign convexity pass would not evaluate the entire path, effectively only using the
5875 // winding rule for determining convexity.
5876 SkPath path;
5877 path.setFillType(SkPathFillType::kWinding);
5878 path.moveTo(3.25f, 115.5f);
5879 path.conicTo(9.98099e+17f, 2.83874e+15f, 1.75098e-30f, 1.75097e-30f, 1.05385e+18f);
5880 path.conicTo(9.96938e+17f, 6.3804e+19f, 9.96934e+17f, 1.75096e-30f, 1.75096e-30f);
5881 path.quadTo(1.28886e+10f, 9.9647e+17f, 9.98101e+17f, 2.61006e+15f);
5882 REPORTER_ASSERT(r, !path.isConvex());
5883
5884 SkPath pathWithExtraMoveTo;
5885 pathWithExtraMoveTo.setFillType(SkPathFillType::kWinding);
5886 pathWithExtraMoveTo.moveTo(5.90043e-39f, 1.34525e-43f);
5887 pathWithExtraMoveTo.addPath(path);
5888 REPORTER_ASSERT(r, !pathWithExtraMoveTo.isConvex());
5889}