xref: /aosp_15_r20/external/skia/tests/PathTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 
8 #include "include/core/SkBlendMode.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPath.h"
18 #include "include/core/SkPathBuilder.h"
19 #include "include/core/SkPathTypes.h"
20 #include "include/core/SkPathUtils.h"
21 #include "include/core/SkPoint.h"
22 #include "include/core/SkRRect.h"
23 #include "include/core/SkRect.h"
24 #include "include/core/SkRefCnt.h"
25 #include "include/core/SkRegion.h"
26 #include "include/core/SkScalar.h"
27 #include "include/core/SkSize.h"
28 #include "include/core/SkStream.h"
29 #include "include/core/SkStrokeRec.h"
30 #include "include/core/SkSurface.h"
31 #include "include/core/SkTypes.h"
32 #include "include/core/SkVertices.h"
33 #include "include/pathops/SkPathOps.h"
34 #include "include/private/SkIDChangeListener.h"
35 #include "include/private/SkPathRef.h"
36 #include "include/private/base/SkFloatingPoint.h"
37 #include "include/private/base/SkMalloc.h"
38 #include "include/private/base/SkTo.h"
39 #include "include/utils/SkNullCanvas.h"
40 #include "include/utils/SkParse.h"
41 #include "include/utils/SkParsePath.h"
42 #include "src/base/SkAutoMalloc.h"
43 #include "src/base/SkFloatBits.h"
44 #include "src/base/SkRandom.h"
45 #include "src/core/SkGeometry.h"
46 #include "src/core/SkPathEnums.h"
47 #include "src/core/SkPathPriv.h"
48 #include "src/core/SkReadBuffer.h"
49 #include "src/core/SkWriteBuffer.h"
50 #include "tests/Test.h"
51 #include "tools/fonts/FontToolUtils.h"
52 
53 #include <algorithm>
54 #include <cfloat>
55 #include <cmath>
56 #include <cstdint>
57 #include <cstring>
58 #include <initializer_list>
59 #include <memory>
60 #include <vector>
61 
set_radii(SkVector radii[4],int index,float rad)62 static void set_radii(SkVector radii[4], int index, float rad) {
63     sk_bzero(radii, sizeof(SkVector) * 4);
64     radii[index].set(rad, rad);
65 }
66 
test_add_rrect(skiatest::Reporter * reporter,const SkRect & bounds,const SkVector radii[4])67 static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
68                            const SkVector radii[4]) {
69     SkRRect rrect;
70     rrect.setRectRadii(bounds, radii);
71     REPORTER_ASSERT(reporter, bounds == rrect.rect());
72 
73     SkPath path;
74     // this line should not assert in the debug build (from validate)
75     path.addRRect(rrect);
76     REPORTER_ASSERT(reporter, bounds == path.getBounds());
77 }
78 
test_skbug_3469(skiatest::Reporter * reporter)79 static void test_skbug_3469(skiatest::Reporter* reporter) {
80     SkPath path;
81     path.moveTo(20, 20);
82     path.quadTo(20, 50, 80, 50);
83     path.quadTo(20, 50, 20, 80);
84     REPORTER_ASSERT(reporter, !path.isConvex());
85 }
86 
test_skbug_3239(skiatest::Reporter * reporter)87 static void test_skbug_3239(skiatest::Reporter* reporter) {
88     const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
89     const float max = SkBits2Float(0x4b7f1c1d); /*  16718877.000000 */
90     const float big = SkBits2Float(0x4b7f1bd7); /*  16718807.000000 */
91 
92     const float rad = 33436320;
93 
94     const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
95     const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
96 
97     SkVector radii[4];
98     for (int i = 0; i < 4; ++i) {
99         set_radii(radii, i, rad);
100         test_add_rrect(reporter, rectx, radii);
101         test_add_rrect(reporter, recty, radii);
102     }
103 }
104 
make_path_crbug364224(SkPath * path)105 static void make_path_crbug364224(SkPath* path) {
106     path->reset();
107     path->moveTo(3.747501373f, 2.724499941f);
108     path->lineTo(3.747501373f, 3.75f);
109     path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
110     path->lineTo(0.7475013733f, 4.0f);
111     path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
112     path->lineTo(0.4975013733f, 1.0f);
113     path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
114     path->lineTo(3.497501373f, 0.75f);
115     path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
116     path->lineTo(3.715001345f, 0.5512499809f);
117     path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
118     path->lineTo(0.7475013733f, 0.4999999702f);
119     path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
120     path->lineTo(0.2475013733f, 3.75f);
121     path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
122     path->lineTo(3.497501373f, 4.25f);
123     path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
124     path->lineTo(3.997501373f, 2.474750042f);
125     path->lineTo(3.747501373f, 2.724499941f);
126     path->close();
127 }
128 
make_path_crbug364224_simplified(SkPath * path)129 static void make_path_crbug364224_simplified(SkPath* path) {
130     path->moveTo(3.747501373f, 2.724499941f);
131     path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
132     path->close();
133 }
134 
test_sect_with_horizontal_needs_pinning()135 static void test_sect_with_horizontal_needs_pinning() {
136     // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
137     // intersection.
138     SkPath path;
139     path.reset();
140     path.moveTo(-540000, -720000);
141     path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
142     path.lineTo(1, 1);
143 
144     // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
145     SkPaint paint;
146     SkSurfaces::Raster(SkImageInfo::MakeN32Premul(10, 10))->getCanvas()->drawPath(path, paint);
147 }
148 
test_iterative_intersect_line()149 static void test_iterative_intersect_line() {
150     // crbug.com/1320467
151     // SkLineClipper::IntersectLine used to clip against the horizontal segment. Then, if it still
152     // needed clipping, would clip against the vertical segment, but start over from the un-clipped
153     // endpoints. With that version, this draw would trigger an assert.
154     // With the fix (iteratively clipping the intermediate results after the first operation),
155     // this shouldn't assert:
156     SkPath path;
157     path.moveTo(-478.805145f, 153.862549f);
158     path.lineTo(6.27216804e+19f, 6.27216804e+19f);
159     path.lineTo(-666.754272f, 155.086304f);
160     path.close();
161 
162     SkPaint paint;
163     paint.setStyle(SkPaint::kStroke_Style);
164     SkSurfaces::Raster(SkImageInfo::MakeN32Premul(256, 256))->getCanvas()->drawPath(path, paint);
165 }
166 
test_path_crbug364224()167 static void test_path_crbug364224() {
168     SkPath path;
169     SkPaint paint;
170     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(84, 88)));
171     SkCanvas* canvas = surface->getCanvas();
172 
173     make_path_crbug364224_simplified(&path);
174     canvas->drawPath(path, paint);
175 
176     make_path_crbug364224(&path);
177     canvas->drawPath(path, paint);
178 }
179 
test_draw_AA_path(int width,int height,const SkPath & path)180 static void test_draw_AA_path(int width, int height, const SkPath& path) {
181     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(width, height)));
182     SkCanvas* canvas = surface->getCanvas();
183     SkPaint paint;
184     paint.setAntiAlias(true);
185     canvas->drawPath(path, paint);
186 }
187 
188 // this is a unit test instead of a GM because it doesn't draw anything
test_fuzz_crbug_638223()189 static void test_fuzz_crbug_638223() {
190     SkPath path;
191     path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01));  // 50474, 161.113f
192     path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
193         SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
194         SkBits2Float(0x6bf9abea));  // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
195     test_draw_AA_path(250, 250, path);
196 }
197 
test_fuzz_crbug_643933()198 static void test_fuzz_crbug_643933() {
199     SkPath path;
200     path.moveTo(0, 0);
201     path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff),  // 2.93943e-39f, 14.125f
202             SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
203             SkBits2Float(0x721aee0c));  // 3.0687e+30f
204     test_draw_AA_path(250, 250, path);
205     path.reset();
206     path.moveTo(0, 0);
207     path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff),  // 4.58981e-41f, 14.625f
208         SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04),  // 510.269f, 29.8643f
209         SkBits2Float(0x5d06eff8));  // 6.07704e+17f
210     test_draw_AA_path(250, 250, path);
211 }
212 
test_fuzz_crbug_647922()213 static void test_fuzz_crbug_647922() {
214     SkPath path;
215     path.moveTo(0, 0);
216     path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff),  // 2.05276e-41f, 50.125f
217             SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9),  // 139406, 547.639f
218             SkBits2Float(0x4d1ade0f));  // 1.6239e+08f
219     test_draw_AA_path(250, 250, path);
220 }
221 
test_fuzz_crbug_662780()222 static void test_fuzz_crbug_662780() {
223     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(250, 250)));
224     SkCanvas* canvas = surface->getCanvas();
225     SkPaint paint;
226     paint.setAntiAlias(true);
227     SkPath path;
228     path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000));  // 8, 158
229     path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000));  // 8, 120
230     // 8, 8, 8.00002f, 8, 0.707107f
231     path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
232             SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
233     path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000));  // 308, 8
234     // 308, 8, 308, 8, 0.707107f
235     path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
236             SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
237     path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000));  // 308, 158
238     // 308, 158, 308, 158, 0.707107f
239     path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
240             SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
241     path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000));  // 8, 158
242     // 8, 158, 8, 158, 0.707107f
243     path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
244             SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
245     path.close();
246     canvas->clipPath(path, true);
247     canvas->drawRect(SkRect::MakeWH(250, 250), paint);
248 }
249 
test_mask_overflow()250 static void test_mask_overflow() {
251     SkPath path;
252     path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000));  // 453, 341
253     path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000));  // 444.75f, 341
254     // 440.47f, 341, 437, 344.47f, 437, 348.75f
255     path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
256             SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
257             SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
258     path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000));  // 437, 355
259     path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000));  // 453, 355
260     path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000));  // 453, 341
261     test_draw_AA_path(500, 500, path);
262 }
263 
test_fuzz_crbug_668907()264 static void test_fuzz_crbug_668907() {
265     SkPath path;
266     path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540));  // 11341.8f, 0.00196679f
267     path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
268             SkBits2Float(0x414100ff));  // 12.0626f, -12.0784f, 12.0784f, 12.0627f
269     path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540));  // 11341.8f, 0.00196679f
270     path.close();
271     test_draw_AA_path(400, 500, path);
272 }
273 
274 /**
275  * In debug mode, this path was causing an assertion to fail in
276  * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
277  */
make_path_crbugskia2820(SkPath * path,skiatest::Reporter * reporter)278 static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
279     SkPoint orig, p1, p2, p3;
280     orig = SkPoint::Make(1.f, 1.f);
281     p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
282     p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
283     p3 = SkPoint::Make(2.f, 2.f);
284 
285     path->reset();
286     path->moveTo(orig);
287     path->cubicTo(p1, p2, p3);
288     path->close();
289 }
290 
test_path_crbugskia2820(skiatest::Reporter * reporter)291 static void test_path_crbugskia2820(skiatest::Reporter* reporter) {
292     SkPath path;
293     make_path_crbugskia2820(&path, reporter);
294 
295     SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
296     stroke.setStrokeStyle(2 * SK_Scalar1);
297     stroke.applyToPath(&path, path);
298 }
299 
test_path_crbugskia5995()300 static void test_path_crbugskia5995() {
301     SkPath path;
302     path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030));  // 2.75294f, 0.172059f
303     path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
304             SkBits2Float(0x00000000));  // 26.7735f, 6.40969e-10f, 8.07426f, 0
305     path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000));  // 0, 0
306     test_draw_AA_path(500, 500, path);
307 }
308 
make_path0(SkPath * path)309 static void make_path0(SkPath* path) {
310     // from  *  https://code.google.com/p/skia/issues/detail?id=1706
311 
312     path->moveTo(146.939f, 1012.84f);
313     path->lineTo(181.747f, 1009.18f);
314     path->lineTo(182.165f, 1013.16f);
315     path->lineTo(147.357f, 1016.82f);
316     path->lineTo(146.939f, 1012.84f);
317     path->close();
318 }
319 
make_path1(SkPath * path)320 static void make_path1(SkPath* path) {
321     path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
322 }
323 
324 typedef void (*PathProc)(SkPath*);
325 
326 /*
327  *  Regression test: we used to crash (overwrite internal storage) during
328  *  construction of the region when the path was INVERSE. That is now fixed,
329  *  so test these regions (which used to assert/crash).
330  *
331  *  https://code.google.com/p/skia/issues/detail?id=1706
332  */
test_path_to_region(skiatest::Reporter * reporter)333 static void test_path_to_region(skiatest::Reporter* reporter) {
334     PathProc procs[] = {
335         make_path0,
336         make_path1,
337     };
338 
339     SkRegion clip;
340     clip.setRect({0, 0, 1255, 1925});
341 
342     for (size_t i = 0; i < std::size(procs); ++i) {
343         SkPath path;
344         procs[i](&path);
345 
346         SkRegion rgn;
347         rgn.setPath(path, clip);
348         path.toggleInverseFillType();
349         rgn.setPath(path, clip);
350     }
351 }
352 
353 #ifdef SK_BUILD_FOR_WIN
354     #define SUPPRESS_VISIBILITY_WARNING
355 #else
356     #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
357 #endif
358 
test_path_close_issue1474(skiatest::Reporter * reporter)359 static void test_path_close_issue1474(skiatest::Reporter* reporter) {
360     // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
361     // are relative to the point we close to, not relative to the point we close from.
362     SkPath path;
363     SkPoint last;
364 
365     // Test rLineTo().
366     path.rLineTo(0, 100);
367     path.rLineTo(100, 0);
368     path.close();          // Returns us back to 0,0.
369     path.rLineTo(50, 50);  // This should go to 50,50.
370 
371     path.getLastPt(&last);
372     REPORTER_ASSERT(reporter, 50 == last.fX);
373     REPORTER_ASSERT(reporter, 50 == last.fY);
374 
375     // Test rQuadTo().
376     path.rewind();
377     path.rLineTo(0, 100);
378     path.rLineTo(100, 0);
379     path.close();
380     path.rQuadTo(50, 50, 75, 75);
381 
382     path.getLastPt(&last);
383     REPORTER_ASSERT(reporter, 75 == last.fX);
384     REPORTER_ASSERT(reporter, 75 == last.fY);
385 
386     // Test rConicTo().
387     path.rewind();
388     path.rLineTo(0, 100);
389     path.rLineTo(100, 0);
390     path.close();
391     path.rConicTo(50, 50, 85, 85, 2);
392 
393     path.getLastPt(&last);
394     REPORTER_ASSERT(reporter, 85 == last.fX);
395     REPORTER_ASSERT(reporter, 85 == last.fY);
396 
397     // Test rCubicTo().
398     path.rewind();
399     path.rLineTo(0, 100);
400     path.rLineTo(100, 0);
401     path.close();
402     path.rCubicTo(50, 50, 85, 85, 95, 95);
403 
404     path.getLastPt(&last);
405     REPORTER_ASSERT(reporter, 95 == last.fX);
406     REPORTER_ASSERT(reporter, 95 == last.fY);
407 }
408 
test_gen_id(skiatest::Reporter * reporter)409 static void test_gen_id(skiatest::Reporter* reporter) {
410     SkPath a, b;
411     REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
412 
413     a.moveTo(0, 0);
414     const uint32_t z = a.getGenerationID();
415     REPORTER_ASSERT(reporter, z != b.getGenerationID());
416 
417     a.reset();
418     REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
419 
420     a.moveTo(1, 1);
421     const uint32_t y = a.getGenerationID();
422     REPORTER_ASSERT(reporter, z != y);
423 
424     b.moveTo(2, 2);
425     const uint32_t x = b.getGenerationID();
426     REPORTER_ASSERT(reporter, x != y && x != z);
427 
428     a.swap(b);
429     REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
430 
431     b = a;
432     REPORTER_ASSERT(reporter, b.getGenerationID() == x);
433 
434     SkPath c(a);
435     REPORTER_ASSERT(reporter, c.getGenerationID() == x);
436 
437     c.lineTo(3, 3);
438     const uint32_t w = c.getGenerationID();
439     REPORTER_ASSERT(reporter, b.getGenerationID() == x);
440     REPORTER_ASSERT(reporter, a.getGenerationID() == x);
441     REPORTER_ASSERT(reporter, w != x);
442 
443 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
444     static bool kExpectGenIDToIgnoreFill = false;
445 #else
446     static bool kExpectGenIDToIgnoreFill = true;
447 #endif
448 
449     c.toggleInverseFillType();
450     const uint32_t v = c.getGenerationID();
451     REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
452 
453     c.rewind();
454     REPORTER_ASSERT(reporter, v != c.getGenerationID());
455 }
456 
457 // This used to assert in the debug build, as the edges did not all line-up.
test_bad_cubic_crbug234190()458 static void test_bad_cubic_crbug234190() {
459     SkPath path;
460     path.moveTo(13.8509f, 3.16858f);
461     path.cubicTo(-2.35893e+08f, -4.21044e+08f,
462                  -2.38991e+08f, -4.26573e+08f,
463                  -2.41016e+08f, -4.30188e+08f);
464     test_draw_AA_path(84, 88, path);
465 }
466 
test_bad_cubic_crbug229478()467 static void test_bad_cubic_crbug229478() {
468     const SkPoint pts[] = {
469         { 4595.91064f,    -11596.9873f },
470         { 4597.2168f,    -11595.9414f },
471         { 4598.52344f,    -11594.8955f },
472         { 4599.83008f,    -11593.8496f },
473     };
474 
475     SkPath path;
476     path.moveTo(pts[0]);
477     path.cubicTo(pts[1], pts[2], pts[3]);
478 
479     SkPaint paint;
480     paint.setStyle(SkPaint::kStroke_Style);
481     paint.setStrokeWidth(20);
482 
483     SkPath dst;
484     // Before the fix, this would infinite-recurse, and run out of stack
485     // because we would keep trying to subdivide a degenerate cubic segment.
486     skpathutils::FillPathWithPaint(path, paint, &dst, nullptr);
487 }
488 
build_path_170666(SkPath & path)489 static void build_path_170666(SkPath& path) {
490     path.moveTo(17.9459f, 21.6344f);
491     path.lineTo(139.545f, -47.8105f);
492     path.lineTo(139.545f, -47.8105f);
493     path.lineTo(131.07f, -47.3888f);
494     path.lineTo(131.07f, -47.3888f);
495     path.lineTo(122.586f, -46.9532f);
496     path.lineTo(122.586f, -46.9532f);
497     path.lineTo(18076.6f, 31390.9f);
498     path.lineTo(18076.6f, 31390.9f);
499     path.lineTo(18085.1f, 31390.5f);
500     path.lineTo(18085.1f, 31390.5f);
501     path.lineTo(18076.6f, 31390.9f);
502     path.lineTo(18076.6f, 31390.9f);
503     path.lineTo(17955, 31460.3f);
504     path.lineTo(17955, 31460.3f);
505     path.lineTo(17963.5f, 31459.9f);
506     path.lineTo(17963.5f, 31459.9f);
507     path.lineTo(17971.9f, 31459.5f);
508     path.lineTo(17971.9f, 31459.5f);
509     path.lineTo(17.9551f, 21.6205f);
510     path.lineTo(17.9551f, 21.6205f);
511     path.lineTo(9.47091f, 22.0561f);
512     path.lineTo(9.47091f, 22.0561f);
513     path.lineTo(17.9459f, 21.6344f);
514     path.lineTo(17.9459f, 21.6344f);
515     path.close();path.moveTo(0.995934f, 22.4779f);
516     path.lineTo(0.986725f, 22.4918f);
517     path.lineTo(0.986725f, 22.4918f);
518     path.lineTo(17955, 31460.4f);
519     path.lineTo(17955, 31460.4f);
520     path.lineTo(17971.9f, 31459.5f);
521     path.lineTo(17971.9f, 31459.5f);
522     path.lineTo(18093.6f, 31390.1f);
523     path.lineTo(18093.6f, 31390.1f);
524     path.lineTo(18093.6f, 31390);
525     path.lineTo(18093.6f, 31390);
526     path.lineTo(139.555f, -47.8244f);
527     path.lineTo(139.555f, -47.8244f);
528     path.lineTo(122.595f, -46.9671f);
529     path.lineTo(122.595f, -46.9671f);
530     path.lineTo(0.995934f, 22.4779f);
531     path.lineTo(0.995934f, 22.4779f);
532     path.close();
533     path.moveTo(5.43941f, 25.5223f);
534     path.lineTo(798267, -28871.1f);
535     path.lineTo(798267, -28871.1f);
536     path.lineTo(3.12512e+06f, -113102);
537     path.lineTo(3.12512e+06f, -113102);
538     path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
539     path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
540     path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
541     path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
542     path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
543     path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
544     path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
545     path.lineTo(2.78271e+08f, -1.00733e+07f);
546     path.lineTo(2.78271e+08f, -1.00733e+07f);
547     path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
548     path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
549     path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
550     path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
551     path.lineTo(2.77473e+08f, -1.00444e+07f);
552     path.lineTo(2.77473e+08f, -1.00444e+07f);
553     path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
554     path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
555     path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
556     path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
557     path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
558     path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
559     path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
560     path.lineTo(798284, -28872);
561     path.lineTo(798284, -28872);
562     path.lineTo(22.4044f, 24.6677f);
563     path.lineTo(22.4044f, 24.6677f);
564     path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
565     path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
566     path.close();
567 }
568 
build_path_simple_170666(SkPath & path)569 static void build_path_simple_170666(SkPath& path) {
570     path.moveTo(126.677f, 24.1591f);
571     path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
572 }
573 
574 // This used to assert in the SK_DEBUG build, as the clip step would fail with
575 // too-few interations in our cubic-line intersection code. That code now runs
576 // 24 interations (instead of 16).
test_crbug_170666()577 static void test_crbug_170666() {
578     SkPath path;
579     build_path_simple_170666(path);
580     test_draw_AA_path(1000, 1000, path);
581 
582     build_path_170666(path);
583     test_draw_AA_path(1000, 1000, path);
584 }
585 
586 
test_tiny_path_convexity(skiatest::Reporter * reporter,const char * pathBug,SkScalar tx,SkScalar ty,SkScalar scale)587 static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
588         SkScalar tx, SkScalar ty, SkScalar scale) {
589     SkPath smallPath;
590     SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
591     bool smallConvex = smallPath.isConvex();
592     SkPath largePath;
593     SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
594     SkMatrix matrix;
595     matrix.reset();
596     matrix.preTranslate(100, 100);
597     matrix.preScale(scale, scale);
598     largePath.transform(matrix);
599     bool largeConvex = largePath.isConvex();
600     REPORTER_ASSERT(reporter, smallConvex == largeConvex);
601 }
602 
test_crbug_493450(skiatest::Reporter * reporter)603 static void test_crbug_493450(skiatest::Reporter* reporter) {
604     const char reducedCase[] =
605         "M0,0"
606         "L0.0002, 0"
607         "L0.0002, 0.0002"
608         "L0.0001, 0.0001"
609         "L0,0.0002"
610         "Z";
611     test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
612     const char originalFiddleData[] =
613         "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
614         "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
615         "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
616         "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
617         "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
618     test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
619             826357.3384828606f);
620 }
621 
test_crbug_495894(skiatest::Reporter * reporter)622 static void test_crbug_495894(skiatest::Reporter* reporter) {
623     const char originalFiddleData[] =
624         "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
625         "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
626         "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
627         "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
628         "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
629         "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
630         "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
631         "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
632         "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
633         "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
634         "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
635         "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
636         "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
637         "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
638         "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
639         "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
640         "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
641         "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
642         "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
643         "L-0.33993994441916414,-0.11288906492739594Z";
644     test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
645             65536);
646 }
647 
test_crbug_613918()648 static void test_crbug_613918() {
649     SkPath path;
650     path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
651     path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
652     path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
653 
654     // This call could lead to an assert or uninitialized read due to a failure
655     // to check the return value from SkCubicClipper::ChopMonoAtY.
656     path.contains(-1.84817e-08f, 1.15465e-08f);
657 }
658 
test_addrect(skiatest::Reporter * reporter)659 static void test_addrect(skiatest::Reporter* reporter) {
660     SkPath path;
661     path.lineTo(0, 0);
662     path.addRect(SkRect::MakeWH(50, 100));
663     REPORTER_ASSERT(reporter, path.isRect(nullptr));
664 
665     path.reset();
666     path.lineTo(FLT_EPSILON, FLT_EPSILON);
667     path.addRect(SkRect::MakeWH(50, 100));
668     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
669 
670     path.reset();
671     path.quadTo(0, 0, 0, 0);
672     path.addRect(SkRect::MakeWH(50, 100));
673     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
674 
675     path.reset();
676     path.conicTo(0, 0, 0, 0, 0.5f);
677     path.addRect(SkRect::MakeWH(50, 100));
678     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
679 
680     path.reset();
681     path.cubicTo(0, 0, 0, 0, 0, 0);
682     path.addRect(SkRect::MakeWH(50, 100));
683     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
684 }
685 
686 // Make sure we stay non-finite once we get there (unless we reset or rewind).
test_addrect_isfinite(skiatest::Reporter * reporter)687 static void test_addrect_isfinite(skiatest::Reporter* reporter) {
688     SkPath path;
689 
690     path.addRect(SkRect::MakeWH(50, 100));
691     REPORTER_ASSERT(reporter, path.isFinite());
692 
693     path.moveTo(0, 0);
694     path.lineTo(SK_ScalarInfinity, 42);
695     REPORTER_ASSERT(reporter, !path.isFinite());
696 
697     path.addRect(SkRect::MakeWH(50, 100));
698     REPORTER_ASSERT(reporter, !path.isFinite());
699 
700     path.reset();
701     REPORTER_ASSERT(reporter, path.isFinite());
702 
703     path.addRect(SkRect::MakeWH(50, 100));
704     REPORTER_ASSERT(reporter, path.isFinite());
705 }
706 
build_big_path(SkPath * path,bool reducedCase)707 static void build_big_path(SkPath* path, bool reducedCase) {
708     if (reducedCase) {
709         path->moveTo(577330, 1971.72f);
710         path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
711     } else {
712         path->moveTo(60.1631f, 7.70567f);
713         path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
714         path->lineTo(577379, 1977.77f);
715         path->quadTo(577364, 1979.57f, 577325, 1980.26f);
716         path->quadTo(577286, 1980.95f, 577245, 1980.13f);
717         path->quadTo(577205, 1979.3f, 577187, 1977.45f);
718         path->quadTo(577168, 1975.6f, 577183, 1973.8f);
719         path->quadTo(577198, 1972, 577238, 1971.31f);
720         path->quadTo(577277, 1970.62f, 577317, 1971.45f);
721         path->quadTo(577330, 1971.72f, 577341, 1972.11f);
722         path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
723         path->moveTo(306.718f, -32.912f);
724         path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
725     }
726 }
727 
test_clipped_cubic()728 static void test_clipped_cubic() {
729     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(640, 480)));
730 
731     // This path used to assert, because our cubic-chopping code incorrectly
732     // moved control points after the chop. This test should be run in SK_DEBUG
733     // mode to ensure that we no long assert.
734     SkPath path;
735     for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
736         build_big_path(&path, SkToBool(doReducedCase));
737 
738         SkPaint paint;
739         for (int doAA = 0; doAA <= 1; ++doAA) {
740             paint.setAntiAlias(SkToBool(doAA));
741             surface->getCanvas()->drawPath(path, paint);
742         }
743     }
744 }
745 
dump_if_ne(skiatest::Reporter * reporter,const SkRect & expected,const SkRect & bounds)746 static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
747     if (expected != bounds) {
748         ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
749                bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
750                expected.left(), expected.top(), expected.right(), expected.bottom());
751     }
752 }
753 
test_bounds_crbug_513799(skiatest::Reporter * reporter)754 static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
755     SkPath path;
756 #if 0
757     // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
758     // rewritten them to avoid this (compiler-bug?).
759     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
760 
761     path.moveTo(-5, -8);
762     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
763 
764     path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
765     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
766 
767     path.moveTo(1, 2);
768     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
769 #else
770     dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
771 
772     path.moveTo(-5, -8);    // should set the bounds
773     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
774 
775     path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
776     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
777 
778     path.moveTo(1, 2);  // don't expect this to have changed the bounds
779     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
780 #endif
781 }
782 
test_fuzz_crbug_627414(skiatest::Reporter * reporter)783 static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
784     SkPath path;
785     path.moveTo(0, 0);
786     path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
787     test_draw_AA_path(100, 100, path);
788 }
789 
790 // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
791 // which triggered an assert, from a tricky cubic. This test replicates that
792 // example, so we can ensure that we handle it (in SkEdge.cpp), and don't
793 // assert in the SK_DEBUG build.
test_tricky_cubic()794 static void test_tricky_cubic() {
795     const SkPoint pts[] = {
796         { SkDoubleToScalar(18.8943768),    SkDoubleToScalar(129.121277) },
797         { SkDoubleToScalar(18.8937435),    SkDoubleToScalar(129.121689) },
798         { SkDoubleToScalar(18.8950119),    SkDoubleToScalar(129.120422) },
799         { SkDoubleToScalar(18.5030727),    SkDoubleToScalar(129.13121)  },
800     };
801 
802     SkPath path;
803     path.moveTo(pts[0]);
804     path.cubicTo(pts[1], pts[2], pts[3]);
805     test_draw_AA_path(19, 130, path);
806 }
807 
808 // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
809 //
test_isfinite_after_transform(skiatest::Reporter * reporter)810 static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
811     SkPath path;
812     path.quadTo(157, 366, 286, 208);
813     path.arcTo(37, 442, 315, 163, 957494590897113.0f);
814 
815     SkMatrix matrix;
816     matrix.setScale(1000*1000, 1000*1000);
817 
818     // Be sure that path::transform correctly updates isFinite and the bounds
819     // if the transformation overflows. The previous bug was that isFinite was
820     // set to true in this case, but the bounds were not set to empty (which
821     // they should be).
822     while (path.isFinite()) {
823         REPORTER_ASSERT(reporter, path.getBounds().isFinite());
824         REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
825         path.transform(matrix);
826     }
827     REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
828 
829     matrix.setTranslate(SK_Scalar1, SK_Scalar1);
830     path.transform(matrix);
831     // we need to still be non-finite
832     REPORTER_ASSERT(reporter, !path.isFinite());
833     REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
834 }
835 
add_corner_arc(SkPath * path,const SkRect & rect,SkScalar xIn,SkScalar yIn,int startAngle)836 static void add_corner_arc(SkPath* path, const SkRect& rect,
837                            SkScalar xIn, SkScalar yIn,
838                            int startAngle)
839 {
840 
841     SkScalar rx = std::min(rect.width(), xIn);
842     SkScalar ry = std::min(rect.height(), yIn);
843 
844     SkRect arcRect;
845     arcRect.setLTRB(-rx, -ry, rx, ry);
846     switch (startAngle) {
847     case 0:
848         arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
849         break;
850     case 90:
851         arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
852         break;
853     case 180:
854         arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
855         break;
856     case 270:
857         arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
858         break;
859     default:
860         break;
861     }
862 
863     path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
864 }
865 
make_arb_round_rect(SkPath * path,const SkRect & r,SkScalar xCorner,SkScalar yCorner)866 static void make_arb_round_rect(SkPath* path, const SkRect& r,
867                                 SkScalar xCorner, SkScalar yCorner) {
868     // we are lazy here and use the same x & y for each corner
869     add_corner_arc(path, r, xCorner, yCorner, 270);
870     add_corner_arc(path, r, xCorner, yCorner, 0);
871     add_corner_arc(path, r, xCorner, yCorner, 90);
872     add_corner_arc(path, r, xCorner, yCorner, 180);
873     path->close();
874 }
875 
876 // Chrome creates its own round rects with each corner possibly being different.
877 // Performance will suffer if they are not convex.
878 // Note: PathBench::ArbRoundRectBench performs almost exactly
879 // the same test (but with drawing)
test_arb_round_rect_is_convex(skiatest::Reporter * reporter)880 static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
881     SkRandom rand;
882     SkRect r;
883 
884     for (int i = 0; i < 5000; ++i) {
885 
886         SkScalar size = rand.nextUScalar1() * 30;
887         if (size < SK_Scalar1) {
888             continue;
889         }
890         r.fLeft = rand.nextUScalar1() * 300;
891         r.fTop =  rand.nextUScalar1() * 300;
892         r.fRight =  r.fLeft + 2 * size;
893         r.fBottom = r.fTop + 2 * size;
894 
895         SkPath temp;
896 
897         make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
898 
899         REPORTER_ASSERT(reporter, temp.isConvex());
900     }
901 }
902 
903 // Chrome will sometimes create a 0 radius round rect. The degenerate
904 // quads prevent the path from being converted to a rect
905 // Note: PathBench::ArbRoundRectBench performs almost exactly
906 // the same test (but with drawing)
test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter * reporter)907 static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
908     SkRandom rand;
909     SkRect r;
910 
911     for (int i = 0; i < 5000; ++i) {
912 
913         SkScalar size = rand.nextUScalar1() * 30;
914         if (size < SK_Scalar1) {
915             continue;
916         }
917         r.fLeft = rand.nextUScalar1() * 300;
918         r.fTop =  rand.nextUScalar1() * 300;
919         r.fRight =  r.fLeft + 2 * size;
920         r.fBottom = r.fTop + 2 * size;
921 
922         SkPath temp;
923 
924         make_arb_round_rect(&temp, r, 0, 0);
925 
926         SkRect result;
927         REPORTER_ASSERT(reporter, temp.isRect(&result));
928         REPORTER_ASSERT(reporter, r == result);
929     }
930 }
931 
test_rect_isfinite(skiatest::Reporter * reporter)932 static void test_rect_isfinite(skiatest::Reporter* reporter) {
933     const SkScalar inf = SK_ScalarInfinity;
934     const SkScalar negInf = SK_ScalarNegativeInfinity;
935     const SkScalar nan = SK_ScalarNaN;
936 
937     SkRect r;
938     r.setEmpty();
939     REPORTER_ASSERT(reporter, r.isFinite());
940     r.setLTRB(0, 0, inf, negInf);
941     REPORTER_ASSERT(reporter, !r.isFinite());
942     r.setLTRB(0, 0, nan, 0);
943     REPORTER_ASSERT(reporter, !r.isFinite());
944 
945     SkPoint pts[] = {
946         { 0, 0 },
947         { SK_Scalar1, 0 },
948         { 0, SK_Scalar1 },
949     };
950 
951     bool isFine = r.setBoundsCheck(pts, 3);
952     REPORTER_ASSERT(reporter, isFine);
953     REPORTER_ASSERT(reporter, !r.isEmpty());
954 
955     pts[1].set(inf, 0);
956     isFine = r.setBoundsCheck(pts, 3);
957     REPORTER_ASSERT(reporter, !isFine);
958     REPORTER_ASSERT(reporter, r.isEmpty());
959 
960     pts[1].set(nan, 0);
961     isFine = r.setBoundsCheck(pts, 3);
962     REPORTER_ASSERT(reporter, !isFine);
963     REPORTER_ASSERT(reporter, r.isEmpty());
964 }
965 
test_path_isfinite(skiatest::Reporter * reporter)966 static void test_path_isfinite(skiatest::Reporter* reporter) {
967     const SkScalar inf = SK_ScalarInfinity;
968     const SkScalar negInf = SK_ScalarNegativeInfinity;
969     const SkScalar nan = SK_ScalarNaN;
970 
971     SkPath path;
972     REPORTER_ASSERT(reporter, path.isFinite());
973 
974     path.reset();
975     REPORTER_ASSERT(reporter, path.isFinite());
976 
977     path.reset();
978     path.moveTo(SK_Scalar1, 0);
979     REPORTER_ASSERT(reporter, path.isFinite());
980 
981     path.reset();
982     path.moveTo(inf, negInf);
983     REPORTER_ASSERT(reporter, !path.isFinite());
984 
985     path.reset();
986     path.moveTo(nan, 0);
987     REPORTER_ASSERT(reporter, !path.isFinite());
988 }
989 
test_isfinite(skiatest::Reporter * reporter)990 static void test_isfinite(skiatest::Reporter* reporter) {
991     test_rect_isfinite(reporter);
992     test_path_isfinite(reporter);
993 }
994 
test_islastcontourclosed(skiatest::Reporter * reporter)995 static void test_islastcontourclosed(skiatest::Reporter* reporter) {
996     SkPath path;
997     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
998     path.moveTo(0, 0);
999     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
1000     path.close();
1001     REPORTER_ASSERT(reporter, path.isLastContourClosed());
1002     path.lineTo(100, 100);
1003     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
1004     path.moveTo(200, 200);
1005     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
1006     path.close();
1007     REPORTER_ASSERT(reporter, path.isLastContourClosed());
1008     path.moveTo(0, 0);
1009     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
1010 }
1011 
1012 // assert that we always
1013 //  start with a moveTo
1014 //  only have 1 moveTo
1015 //  only have Lines after that
1016 //  end with a single close
1017 //  only have (at most) 1 close
1018 //
test_poly(skiatest::Reporter * reporter,const SkPath & path,const SkPoint srcPts[],bool expectClose)1019 static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
1020                       const SkPoint srcPts[], bool expectClose) {
1021     bool firstTime = true;
1022     bool foundClose = false;
1023     for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
1024         switch (verb) {
1025             case SkPathVerb::kMove:
1026                 REPORTER_ASSERT(reporter, firstTime);
1027                 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
1028                 srcPts++;
1029                 firstTime = false;
1030                 break;
1031             case SkPathVerb::kLine:
1032                 REPORTER_ASSERT(reporter, !firstTime);
1033                 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
1034                 srcPts++;
1035                 break;
1036             case SkPathVerb::kQuad:
1037                 REPORTER_ASSERT(reporter, false, "unexpected quad verb");
1038                 break;
1039             case SkPathVerb::kConic:
1040                 REPORTER_ASSERT(reporter, false, "unexpected conic verb");
1041                 break;
1042             case SkPathVerb::kCubic:
1043                 REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
1044                 break;
1045             case SkPathVerb::kClose:
1046                 REPORTER_ASSERT(reporter, !firstTime);
1047                 REPORTER_ASSERT(reporter, !foundClose);
1048                 REPORTER_ASSERT(reporter, expectClose);
1049                 foundClose = true;
1050                 break;
1051         }
1052     }
1053     REPORTER_ASSERT(reporter, foundClose == expectClose);
1054 }
1055 
test_addPoly(skiatest::Reporter * reporter)1056 static void test_addPoly(skiatest::Reporter* reporter) {
1057     SkPoint pts[32];
1058     SkRandom rand;
1059 
1060     for (size_t i = 0; i < std::size(pts); ++i) {
1061         pts[i].fX = rand.nextSScalar1();
1062         pts[i].fY = rand.nextSScalar1();
1063     }
1064 
1065     for (int doClose = 0; doClose <= 1; ++doClose) {
1066         for (size_t count = 1; count <= std::size(pts); ++count) {
1067             SkPath path;
1068             path.addPoly(pts, SkToInt(count), SkToBool(doClose));
1069             test_poly(reporter, path, pts, SkToBool(doClose));
1070         }
1071     }
1072 }
1073 
test_strokerec(skiatest::Reporter * reporter)1074 static void test_strokerec(skiatest::Reporter* reporter) {
1075     SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1076     REPORTER_ASSERT(reporter, rec.isFillStyle());
1077 
1078     rec.setHairlineStyle();
1079     REPORTER_ASSERT(reporter, rec.isHairlineStyle());
1080 
1081     rec.setStrokeStyle(SK_Scalar1, false);
1082     REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
1083 
1084     rec.setStrokeStyle(SK_Scalar1, true);
1085     REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
1086 
1087     rec.setStrokeStyle(0, false);
1088     REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
1089 
1090     rec.setStrokeStyle(0, true);
1091     REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1092 }
1093 
1094 // Set this for paths that don't have a consistent direction such as a bowtie.
1095 // (cheapComputeDirection is not expected to catch these.)
1096 // Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
1097 const SkPathFirstDirection kDontCheckDir = static_cast<SkPathFirstDirection>(3);
1098 
check_direction(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection expected)1099 static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
1100                             SkPathFirstDirection expected) {
1101     if (expected == kDontCheckDir) {
1102         return;
1103     }
1104     // We make a copy so that we don't cache the result on the passed in path.
1105     SkPath copy(path);  // NOLINT(performance-unnecessary-copy-initialization)
1106 
1107     SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(copy);
1108     if (dir != SkPathFirstDirection::kUnknown) {
1109         REPORTER_ASSERT(reporter, dir == expected);
1110     }
1111 }
1112 
test_direction(skiatest::Reporter * reporter)1113 static void test_direction(skiatest::Reporter* reporter) {
1114     size_t i;
1115     SkPath path;
1116     REPORTER_ASSERT(reporter,
1117                     SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kUnknown);
1118 
1119     static const char* gDegen[] = {
1120         "M 10 10",
1121         "M 10 10 M 20 20",
1122         "M 10 10 L 20 20",
1123         "M 10 10 L 10 10 L 10 10",
1124         "M 10 10 Q 10 10 10 10",
1125         "M 10 10 C 10 10 10 10 10 10",
1126     };
1127     for (i = 0; i < std::size(gDegen); ++i) {
1128         path.reset();
1129         bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1130         REPORTER_ASSERT(reporter, valid);
1131         REPORTER_ASSERT(reporter,
1132                         SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kUnknown);
1133     }
1134 
1135     static const char* gCW[] = {
1136         "M 10 10 L 10 10 Q 20 10 20 20",
1137         "M 10 10 C 20 10 20 20 20 20",
1138         "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1139         // rect with top two corners replaced by cubics with identical middle
1140         // control points
1141         "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1142         "M 20 10 L 0 10 Q 10 10 20 0",  // left, degenerate serif
1143     };
1144     for (i = 0; i < std::size(gCW); ++i) {
1145         path.reset();
1146         bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1147         REPORTER_ASSERT(reporter, valid);
1148         check_direction(reporter, path, SkPathFirstDirection::kCW);
1149     }
1150 
1151     static const char* gCCW[] = {
1152         "M 10 10 L 10 10 Q 20 10 20 -20",
1153         "M 10 10 C 20 10 20 -20 20 -20",
1154         "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1155         // rect with top two corners replaced by cubics with identical middle
1156         // control points
1157         "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1158         "M 10 10 L 30 10 Q 20 10 10 0",  // right, degenerate serif
1159     };
1160     for (i = 0; i < std::size(gCCW); ++i) {
1161         path.reset();
1162         bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1163         REPORTER_ASSERT(reporter, valid);
1164         check_direction(reporter, path, SkPathFirstDirection::kCCW);
1165     }
1166 
1167     // Test two donuts, each wound a different direction. Only the outer contour
1168     // determines the cheap direction
1169     path.reset();
1170     path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCW);
1171     path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCCW);
1172     check_direction(reporter, path, SkPathFirstDirection::kCW);
1173 
1174     path.reset();
1175     path.addCircle(0, 0, SkIntToScalar(1), SkPathDirection::kCW);
1176     path.addCircle(0, 0, SkIntToScalar(2), SkPathDirection::kCCW);
1177     check_direction(reporter, path, SkPathFirstDirection::kCCW);
1178 
1179     // triangle with one point really far from the origin.
1180     path.reset();
1181     // the first point is roughly 1.05e10, 1.05e10
1182     path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1183     path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1184     path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1185     check_direction(reporter, path, SkPathFirstDirection::kCCW);
1186 
1187     path.reset();
1188     path.conicTo(20, 0, 20, 20, 0.5f);
1189     path.close();
1190     check_direction(reporter, path, SkPathFirstDirection::kCW);
1191 
1192     path.reset();
1193     path.lineTo(1, 1e7f);
1194     path.lineTo(1e7f, 2e7f);
1195     path.close();
1196     REPORTER_ASSERT(reporter, path.isConvex());
1197     check_direction(reporter, path, SkPathFirstDirection::kCCW);
1198 }
1199 
add_rect(SkPath * path,const SkRect & r)1200 static void add_rect(SkPath* path, const SkRect& r) {
1201     path->moveTo(r.fLeft, r.fTop);
1202     path->lineTo(r.fRight, r.fTop);
1203     path->lineTo(r.fRight, r.fBottom);
1204     path->lineTo(r.fLeft, r.fBottom);
1205     path->close();
1206 }
1207 
test_bounds(skiatest::Reporter * reporter)1208 static void test_bounds(skiatest::Reporter* reporter) {
1209     static const SkRect rects[] = {
1210         { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1211         { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1212         { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1213         { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1214     };
1215 
1216     SkPath path0, path1;
1217     for (size_t i = 0; i < std::size(rects); ++i) {
1218         path0.addRect(rects[i]);
1219         add_rect(&path1, rects[i]);
1220     }
1221 
1222     REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1223 }
1224 
stroke_cubic(const SkPoint pts[4])1225 static void stroke_cubic(const SkPoint pts[4]) {
1226     SkPath path;
1227     path.moveTo(pts[0]);
1228     path.cubicTo(pts[1], pts[2], pts[3]);
1229 
1230     SkPaint paint;
1231     paint.setStyle(SkPaint::kStroke_Style);
1232     paint.setStrokeWidth(SK_Scalar1 * 2);
1233 
1234     SkPath fill;
1235     skpathutils::FillPathWithPaint(path, paint, &fill);
1236 }
1237 
1238 // just ensure this can run w/o any SkASSERTS firing in the debug build
1239 // we used to assert due to differences in how we determine a degenerate vector
1240 // but that was fixed with the introduction of SkPoint::CanNormalize
stroke_tiny_cubic()1241 static void stroke_tiny_cubic() {
1242     SkPoint p0[] = {
1243         { 372.0f,   92.0f },
1244         { 372.0f,   92.0f },
1245         { 372.0f,   92.0f },
1246         { 372.0f,   92.0f },
1247     };
1248 
1249     stroke_cubic(p0);
1250 
1251     SkPoint p1[] = {
1252         { 372.0f,       92.0f },
1253         { 372.0007f,    92.000755f },
1254         { 371.99927f,   92.003922f },
1255         { 371.99826f,   92.003899f },
1256     };
1257 
1258     stroke_cubic(p1);
1259 }
1260 
check_close(skiatest::Reporter * reporter,const SkPath & path)1261 static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1262     for (int i = 0; i < 2; ++i) {
1263         SkPath::Iter iter(path, SkToBool(i));
1264         SkPoint mv;
1265         SkPoint pts[4];
1266         SkPath::Verb v;
1267         int nMT = 0;
1268         int nCL = 0;
1269         mv.set(0, 0);
1270         while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1271             switch (v) {
1272                 case SkPath::kMove_Verb:
1273                     mv = pts[0];
1274                     ++nMT;
1275                     break;
1276                 case SkPath::kClose_Verb:
1277                     REPORTER_ASSERT(reporter, mv == pts[0]);
1278                     ++nCL;
1279                     break;
1280                 default:
1281                     break;
1282             }
1283         }
1284         // if we force a close on the interator we should have a close
1285         // for every moveTo
1286         REPORTER_ASSERT(reporter, !i || nMT == nCL);
1287     }
1288 }
1289 
test_close(skiatest::Reporter * reporter)1290 static void test_close(skiatest::Reporter* reporter) {
1291     SkPath closePt;
1292     closePt.moveTo(0, 0);
1293     closePt.close();
1294     check_close(reporter, closePt);
1295 
1296     SkPath openPt;
1297     openPt.moveTo(0, 0);
1298     check_close(reporter, openPt);
1299 
1300     SkPath empty;
1301     check_close(reporter, empty);
1302     empty.close();
1303     check_close(reporter, empty);
1304 
1305     SkPath rect;
1306     rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1307     check_close(reporter, rect);
1308     rect.close();
1309     check_close(reporter, rect);
1310 
1311     SkPath quad;
1312     quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1313     check_close(reporter, quad);
1314     quad.close();
1315     check_close(reporter, quad);
1316 
1317     SkPath cubic;
1318     quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1319                  10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1320     check_close(reporter, cubic);
1321     cubic.close();
1322     check_close(reporter, cubic);
1323 
1324     SkPath line;
1325     line.moveTo(SK_Scalar1, SK_Scalar1);
1326     line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1327     check_close(reporter, line);
1328     line.close();
1329     check_close(reporter, line);
1330 
1331     SkPath rect2;
1332     rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1333     rect2.close();
1334     rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1335     check_close(reporter, rect2);
1336     rect2.close();
1337     check_close(reporter, rect2);
1338 
1339     SkPath oval3;
1340     oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1341     oval3.close();
1342     oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1343     check_close(reporter, oval3);
1344     oval3.close();
1345     check_close(reporter, oval3);
1346 
1347     SkPath moves;
1348     moves.moveTo(SK_Scalar1, SK_Scalar1);
1349     moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1350     moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1351     moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1352     check_close(reporter, moves);
1353 
1354     stroke_tiny_cubic();
1355 }
1356 
check_convexity(skiatest::Reporter * reporter,const SkPath & path,bool expectedConvexity)1357 static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1358                             bool expectedConvexity) {
1359     // We make a copy so that we don't cache the result on the passed in path.
1360     SkPath copy(path);  // NOLINT(performance-unnecessary-copy-initialization)
1361     bool convexity = copy.isConvex();
1362     REPORTER_ASSERT(reporter, convexity == expectedConvexity);
1363 }
1364 
test_path_crbug389050(skiatest::Reporter * reporter)1365 static void test_path_crbug389050(skiatest::Reporter* reporter) {
1366     SkPath  tinyConvexPolygon;
1367     tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1368     tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1369     tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1370     tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1371     tinyConvexPolygon.close();
1372     tinyConvexPolygon.isConvex();
1373     check_direction(reporter, tinyConvexPolygon, SkPathFirstDirection::kCW);
1374 
1375     SkPath  platTriangle;
1376     platTriangle.moveTo(0, 0);
1377     platTriangle.lineTo(200, 0);
1378     platTriangle.lineTo(100, 0.04f);
1379     platTriangle.close();
1380     platTriangle.isConvex();
1381     check_direction(reporter, platTriangle, SkPathFirstDirection::kCW);
1382 
1383     platTriangle.reset();
1384     platTriangle.moveTo(0, 0);
1385     platTriangle.lineTo(200, 0);
1386     platTriangle.lineTo(100, 0.03f);
1387     platTriangle.close();
1388     platTriangle.isConvex();
1389     check_direction(reporter, platTriangle, SkPathFirstDirection::kCW);
1390 }
1391 
test_convexity2(skiatest::Reporter * reporter)1392 static void test_convexity2(skiatest::Reporter* reporter) {
1393     SkPath pt;
1394     pt.moveTo(0, 0);
1395     pt.close();
1396     check_convexity(reporter, pt, true);
1397     check_direction(reporter, pt, SkPathFirstDirection::kUnknown);
1398 
1399     SkPath line;
1400     line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1401     line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1402     line.close();
1403     check_convexity(reporter, line, true);
1404     check_direction(reporter, line, SkPathFirstDirection::kUnknown);
1405 
1406     SkPath triLeft;
1407     triLeft.moveTo(0, 0);
1408     triLeft.lineTo(SK_Scalar1, 0);
1409     triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1410     triLeft.close();
1411     check_convexity(reporter, triLeft, true);
1412     check_direction(reporter, triLeft, SkPathFirstDirection::kCW);
1413 
1414     SkPath triRight;
1415     triRight.moveTo(0, 0);
1416     triRight.lineTo(-SK_Scalar1, 0);
1417     triRight.lineTo(SK_Scalar1, SK_Scalar1);
1418     triRight.close();
1419     check_convexity(reporter, triRight, true);
1420     check_direction(reporter, triRight, SkPathFirstDirection::kCCW);
1421 
1422     SkPath square;
1423     square.moveTo(0, 0);
1424     square.lineTo(SK_Scalar1, 0);
1425     square.lineTo(SK_Scalar1, SK_Scalar1);
1426     square.lineTo(0, SK_Scalar1);
1427     square.close();
1428     check_convexity(reporter, square, true);
1429     check_direction(reporter, square, SkPathFirstDirection::kCW);
1430 
1431     SkPath redundantSquare;
1432     redundantSquare.moveTo(0, 0);
1433     redundantSquare.lineTo(0, 0);
1434     redundantSquare.lineTo(0, 0);
1435     redundantSquare.lineTo(SK_Scalar1, 0);
1436     redundantSquare.lineTo(SK_Scalar1, 0);
1437     redundantSquare.lineTo(SK_Scalar1, 0);
1438     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1439     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1440     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1441     redundantSquare.lineTo(0, SK_Scalar1);
1442     redundantSquare.lineTo(0, SK_Scalar1);
1443     redundantSquare.lineTo(0, SK_Scalar1);
1444     redundantSquare.close();
1445     check_convexity(reporter, redundantSquare, true);
1446     check_direction(reporter, redundantSquare, SkPathFirstDirection::kCW);
1447 
1448     SkPath bowTie;
1449     bowTie.moveTo(0, 0);
1450     bowTie.lineTo(0, 0);
1451     bowTie.lineTo(0, 0);
1452     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1453     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1454     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1455     bowTie.lineTo(SK_Scalar1, 0);
1456     bowTie.lineTo(SK_Scalar1, 0);
1457     bowTie.lineTo(SK_Scalar1, 0);
1458     bowTie.lineTo(0, SK_Scalar1);
1459     bowTie.lineTo(0, SK_Scalar1);
1460     bowTie.lineTo(0, SK_Scalar1);
1461     bowTie.close();
1462     check_convexity(reporter, bowTie, false);
1463     check_direction(reporter, bowTie, kDontCheckDir);
1464 
1465     SkPath spiral;
1466     spiral.moveTo(0, 0);
1467     spiral.lineTo(100*SK_Scalar1, 0);
1468     spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1469     spiral.lineTo(0, 100*SK_Scalar1);
1470     spiral.lineTo(0, 50*SK_Scalar1);
1471     spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1472     spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1473     spiral.close();
1474     check_convexity(reporter, spiral, false);
1475     check_direction(reporter, spiral, kDontCheckDir);
1476 
1477     SkPath dent;
1478     dent.moveTo(0, 0);
1479     dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1480     dent.lineTo(0, 100*SK_Scalar1);
1481     dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1482     dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1483     dent.close();
1484     check_convexity(reporter, dent, false);
1485     check_direction(reporter, dent, SkPathFirstDirection::kCW);
1486 
1487     // https://bug.skia.org/2235
1488     SkPath strokedSin;
1489     for (int i = 0; i < 2000; i++) {
1490         SkScalar x = SkIntToScalar(i) / 2;
1491         SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1492         if (0 == i) {
1493             strokedSin.moveTo(x, y);
1494         } else {
1495             strokedSin.lineTo(x, y);
1496         }
1497     }
1498     SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1499     stroke.setStrokeStyle(2 * SK_Scalar1);
1500     stroke.applyToPath(&strokedSin, strokedSin);
1501     check_convexity(reporter, strokedSin, false);
1502     check_direction(reporter, strokedSin, kDontCheckDir);
1503 
1504     // http://crbug.com/412640
1505     SkPath degenerateConcave;
1506     degenerateConcave.moveTo(148.67912f, 191.875f);
1507     degenerateConcave.lineTo(470.37695f, 7.5f);
1508     degenerateConcave.lineTo(148.67912f, 191.875f);
1509     degenerateConcave.lineTo(41.446522f, 376.25f);
1510     degenerateConcave.lineTo(-55.971577f, 460.0f);
1511     degenerateConcave.lineTo(41.446522f, 376.25f);
1512     check_convexity(reporter, degenerateConcave, false);
1513     check_direction(reporter, degenerateConcave, SkPathFirstDirection::kUnknown);
1514 
1515     // http://crbug.com/433683
1516     SkPath badFirstVector;
1517     badFirstVector.moveTo(501.087708f, 319.610352f);
1518     badFirstVector.lineTo(501.087708f, 319.610352f);
1519     badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1520     badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1521     badFirstVector.lineTo(301.557678f, 98.044601f);
1522     badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1523     badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1524     badFirstVector.lineTo(504.912292f, 316.389648f);
1525     badFirstVector.lineTo(501.087708f, 319.610352f);
1526     badFirstVector.close();
1527     check_convexity(reporter, badFirstVector, false);
1528 
1529     // http://crbug.com/993330
1530     SkPath falseBackEdge;
1531     falseBackEdge.moveTo(-217.83430557928145f,      -382.14948768484857f);
1532     falseBackEdge.lineTo(-227.73867866614847f,      -399.52485512718323f);
1533     falseBackEdge.cubicTo(-158.3541047666846f,      -439.0757140459542f,
1534                           -79.8654464485281f,       -459.875f,
1535                           -1.1368683772161603e-13f, -459.875f);
1536     falseBackEdge.lineTo(-8.08037266162413e-14f,    -439.875f);
1537     falseBackEdge.lineTo(-8.526512829121202e-14f,   -439.87499999999994f);
1538     falseBackEdge.cubicTo(-76.39209188702645f,      -439.87499999999994f,
1539                           -151.46727226799754f,     -419.98027663161537f,
1540                           -217.83430557928145f,     -382.14948768484857f);
1541     falseBackEdge.close();
1542     check_convexity(reporter, falseBackEdge, false);
1543 }
1544 
test_convexity_doubleback(skiatest::Reporter * reporter)1545 static void test_convexity_doubleback(skiatest::Reporter* reporter) {
1546     SkPath doubleback;
1547     doubleback.lineTo(1, 1);
1548     check_convexity(reporter, doubleback, true);
1549     doubleback.lineTo(2, 2);
1550     check_convexity(reporter, doubleback, true);
1551     doubleback.reset();
1552     doubleback.lineTo(1, 0);
1553     check_convexity(reporter, doubleback, true);
1554     doubleback.lineTo(2, 0);
1555     check_convexity(reporter, doubleback, true);
1556     doubleback.lineTo(1, 0);
1557     check_convexity(reporter, doubleback, true);
1558     doubleback.reset();
1559     doubleback.quadTo(1, 1, 2, 2);
1560     check_convexity(reporter, doubleback, true);
1561     doubleback.reset();
1562     doubleback.quadTo(1, 0, 2, 0);
1563     check_convexity(reporter, doubleback, true);
1564     doubleback.quadTo(1, 0, 0, 0);
1565     check_convexity(reporter, doubleback, true);
1566 
1567     doubleback.reset();
1568     doubleback.lineTo(1, 0);
1569     doubleback.lineTo(1, 0);
1570     doubleback.lineTo(1, 1);
1571     doubleback.lineTo(1, 1);
1572     doubleback.lineTo(1, 0);
1573     check_convexity(reporter, doubleback, false);
1574 
1575     doubleback.reset();
1576     doubleback.lineTo(-1, 0);
1577     doubleback.lineTo(-1, 1);
1578     doubleback.lineTo(-1, 0);
1579     check_convexity(reporter, doubleback, false);
1580 
1581     for (int i = 0; i < 4; ++i) {
1582         doubleback.reset();
1583         doubleback.moveTo(0, 0);
1584         if (i == 0) {
1585             doubleback.lineTo(-1, -1);
1586             doubleback.lineTo(0, 0);
1587         }
1588         doubleback.lineTo(0, 1);
1589         if (i == 1) {
1590             doubleback.lineTo(0, 2);
1591             doubleback.lineTo(0, 1);
1592         }
1593         doubleback.lineTo(1, 1);
1594         if (i == 2) {
1595             doubleback.lineTo(2, 2);
1596             doubleback.lineTo(1, 1);
1597         }
1598         doubleback.lineTo(0, 0);
1599         if (i == 3) {
1600             doubleback.lineTo(-1, -1);
1601             doubleback.lineTo(0, 0);
1602         }
1603         check_convexity(reporter, doubleback, false);
1604     }
1605 }
1606 
check_convex_bounds(skiatest::Reporter * reporter,const SkPath & p,const SkRect & bounds)1607 static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1608                                 const SkRect& bounds) {
1609     REPORTER_ASSERT(reporter, p.isConvex());
1610     REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1611 
1612     SkPath p2(p);
1613     REPORTER_ASSERT(reporter, p2.isConvex());
1614     REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1615 
1616     SkPath other;
1617     other.swap(p2);
1618     REPORTER_ASSERT(reporter, other.isConvex());
1619     REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1620 }
1621 
setFromString(SkPath * path,const char str[])1622 static void setFromString(SkPath* path, const char str[]) {
1623     bool first = true;
1624     while (str) {
1625         SkScalar x, y;
1626         str = SkParse::FindScalar(str, &x);
1627         if (nullptr == str) {
1628             break;
1629         }
1630         str = SkParse::FindScalar(str, &y);
1631         SkASSERT(str);
1632         if (first) {
1633             path->moveTo(x, y);
1634             first = false;
1635         } else {
1636             path->lineTo(x, y);
1637         }
1638     }
1639 }
1640 
test_convexity(skiatest::Reporter * reporter)1641 static void test_convexity(skiatest::Reporter* reporter) {
1642     SkPath path;
1643 
1644     check_convexity(reporter, path, true);
1645     path.addCircle(0, 0, SkIntToScalar(10));
1646     check_convexity(reporter, path, true);
1647     path.addCircle(0, 0, SkIntToScalar(10));   // 2nd circle
1648     check_convexity(reporter, path, false);
1649 
1650     path.reset();
1651     path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCCW);
1652     check_convexity(reporter, path, true);
1653     REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kCCW);
1654 
1655     path.reset();
1656     path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPathDirection::kCW);
1657     check_convexity(reporter, path, true);
1658     REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == SkPathFirstDirection::kCW);
1659 
1660     path.reset();
1661     path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
1662     check_convexity(reporter, path, true);
1663 
1664     static const struct {
1665         const char*           fPathStr;
1666         bool                  fExpectedIsConvex;
1667         SkPathFirstDirection  fExpectedDirection;
1668     } gRec[] = {
1669         { "", true, SkPathFirstDirection::kUnknown },
1670         { "0 0", true, SkPathFirstDirection::kUnknown },
1671         { "0 0 10 10", true, SkPathFirstDirection::kUnknown },
1672         { "0 0 10 10 20 20 0 0 10 10", false, SkPathFirstDirection::kUnknown },
1673         { "0 0 10 10 10 20", true, SkPathFirstDirection::kCW },
1674         { "0 0 10 10 10 0", true, SkPathFirstDirection::kCCW },
1675         { "0 0 10 10 10 0 0 10", false, kDontCheckDir },
1676         { "0 0 10 0 0 10 -10 -10", false, SkPathFirstDirection::kCW },
1677     };
1678 
1679     for (size_t i = 0; i < std::size(gRec); ++i) {
1680         path.reset();
1681         setFromString(&path, gRec[i].fPathStr);
1682         check_convexity(reporter, path, gRec[i].fExpectedIsConvex);
1683         check_direction(reporter, path, gRec[i].fExpectedDirection);
1684         // check after setting the initial convex and direction
1685         if (kDontCheckDir != gRec[i].fExpectedDirection) {
1686             // We make a copy so that we don't cache the result on the passed in path.
1687             SkPath copy(path);  // NOLINT(performance-unnecessary-copy-initialization)
1688             SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(copy);
1689             bool foundDir = dir != SkPathFirstDirection::kUnknown;
1690             REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathFirstDirection::kUnknown)
1691                     ^ foundDir);
1692             REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1693             check_convexity(reporter, copy, gRec[i].fExpectedIsConvex);
1694         }
1695         REPORTER_ASSERT(reporter, gRec[i].fExpectedIsConvex == path.isConvex());
1696         check_direction(reporter, path, gRec[i].fExpectedDirection);
1697     }
1698 
1699     static const SkPoint nonFinitePts[] = {
1700         { SK_ScalarInfinity, 0 },
1701         { 0, SK_ScalarInfinity },
1702         { SK_ScalarInfinity, SK_ScalarInfinity },
1703         { SK_ScalarNegativeInfinity, 0},
1704         { 0, SK_ScalarNegativeInfinity },
1705         { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1706         { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1707         { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1708         { SK_ScalarNaN, 0 },
1709         { 0, SK_ScalarNaN },
1710         { SK_ScalarNaN, SK_ScalarNaN },
1711     };
1712 
1713     const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1714 
1715     static const SkPoint axisAlignedPts[] = {
1716         { SK_ScalarMax, 0 },
1717         { 0, SK_ScalarMax },
1718         { SK_ScalarMin, 0 },
1719         { 0, SK_ScalarMin },
1720     };
1721 
1722     const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
1723 
1724     for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
1725         int i = (int) (index % nonFinitePtsCount);
1726         int f = (int) (index % axisAlignedPtsCount);
1727         int g = (int) ((f + 1) % axisAlignedPtsCount);
1728         path.reset();
1729         switch (index % 13) {
1730             case 0: path.lineTo(nonFinitePts[i]); break;
1731             case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1732             case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
1733             case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
1734             case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
1735             case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
1736             case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
1737             case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
1738             case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
1739             case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
1740             case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1741             case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
1742             case 12: path.moveTo(nonFinitePts[i]); break;
1743         }
1744         REPORTER_ASSERT(reporter,
1745                     SkPathPriv::GetConvexityOrUnknown(path) == SkPathConvexity::kUnknown);
1746     }
1747 
1748     for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
1749         int f = (int) (index % axisAlignedPtsCount);
1750         int g = (int) ((f + 1) % axisAlignedPtsCount);
1751         path.reset();
1752         int curveSelect = index % 11;
1753         switch (curveSelect) {
1754             case 0: path.moveTo(axisAlignedPts[f]); break;
1755             case 1: path.lineTo(axisAlignedPts[f]); break;
1756             case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
1757             case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
1758             case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
1759             case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
1760             case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
1761             case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
1762             case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
1763             case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
1764             case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
1765         }
1766         if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
1767             check_convexity(reporter, path, true);
1768         } else {
1769             // We make a copy so that we don't cache the result on the passed in path.
1770             SkPath copy(path);  // NOLINT(performance-unnecessary-copy-initialization)
1771             REPORTER_ASSERT(reporter, !copy.isConvex());
1772         }
1773     }
1774 
1775     static const SkPoint diagonalPts[] = {
1776         { SK_ScalarMax, SK_ScalarMax },
1777         { SK_ScalarMin, SK_ScalarMin },
1778     };
1779 
1780     const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
1781 
1782     for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
1783         int f = (int) (index % diagonalPtsCount);
1784         int g = (int) ((f + 1) % diagonalPtsCount);
1785         path.reset();
1786         int curveSelect = index % 11;
1787         switch (curveSelect) {
1788             case 0: path.moveTo(diagonalPts[f]); break;
1789             case 1: path.lineTo(diagonalPts[f]); break;
1790             case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
1791             case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
1792             case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
1793             case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
1794             case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
1795             case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
1796             case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
1797             case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
1798             case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
1799         }
1800         if (curveSelect == 0) {
1801             check_convexity(reporter, path, true);
1802         } else {
1803             // We make a copy so that we don't cache the result on the passed in path.
1804             SkPath copy(path);  // NOLINT(performance-unnecessary-copy-initialization)
1805             REPORTER_ASSERT(reporter, !copy.isConvex());
1806         }
1807     }
1808 
1809 
1810     path.reset();
1811     path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d));  // -0.284072f, -0.0622362f
1812     path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38));  // -0.284072f, -0.0622351f
1813     path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7));  // -0.28407f, -0.0622307f
1814     path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886));  // -0.284067f, -0.0622182f
1815     path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9));  // -0.284084f, -0.0622269f
1816     path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d));  // -0.284072f, -0.0622362f
1817     path.close();
1818     check_convexity(reporter, path, false);
1819 
1820 }
1821 
test_isLine(skiatest::Reporter * reporter)1822 static void test_isLine(skiatest::Reporter* reporter) {
1823     SkPath path;
1824     SkPoint pts[2];
1825     const SkScalar value = SkIntToScalar(5);
1826 
1827     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1828 
1829     // set some non-zero values
1830     pts[0].set(value, value);
1831     pts[1].set(value, value);
1832     REPORTER_ASSERT(reporter, !path.isLine(pts));
1833     // check that pts was untouched
1834     REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1835     REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1836 
1837     const SkScalar moveX = SkIntToScalar(1);
1838     const SkScalar moveY = SkIntToScalar(2);
1839     REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1840 
1841     path.moveTo(moveX, moveY);
1842     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1843     REPORTER_ASSERT(reporter, !path.isLine(pts));
1844     // check that pts was untouched
1845     REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1846     REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1847 
1848     const SkScalar lineX = SkIntToScalar(2);
1849     const SkScalar lineY = SkIntToScalar(2);
1850     REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1851 
1852     path.lineTo(lineX, lineY);
1853     REPORTER_ASSERT(reporter, path.isLine(nullptr));
1854 
1855     REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1856     REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1857     REPORTER_ASSERT(reporter, path.isLine(pts));
1858     REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1859     REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1860 
1861     path.lineTo(0, 0);  // too many points/verbs
1862     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1863     REPORTER_ASSERT(reporter, !path.isLine(pts));
1864     REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1865     REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1866 
1867     path.reset();
1868     path.quadTo(1, 1, 2, 2);
1869     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1870 }
1871 
test_conservativelyContains(skiatest::Reporter * reporter)1872 static void test_conservativelyContains(skiatest::Reporter* reporter) {
1873     SkPath path;
1874 
1875     // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1876     static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1877 
1878     // A circle that bounds kBaseRect (with a significant amount of slop)
1879     SkScalar circleR = std::max(kBaseRect.width(), kBaseRect.height());
1880     circleR *= 1.75f / 2;
1881     static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1882 
1883     // round-rect radii
1884     static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1885 
1886     static const struct SUPPRESS_VISIBILITY_WARNING {
1887         SkRect fQueryRect;
1888         bool   fInRect;
1889         bool   fInCircle;
1890         bool   fInRR;
1891         bool   fInCubicRR;
1892     } kQueries[] = {
1893         {kBaseRect, true, true, false, false},
1894 
1895         // rect well inside of kBaseRect
1896         {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1897                           kBaseRect.fTop + 0.25f*kBaseRect.height(),
1898                           kBaseRect.fRight - 0.25f*kBaseRect.width(),
1899                           kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1900                           true, true, true, true},
1901 
1902         // rects with edges off by one from kBaseRect's edges
1903         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1904                           kBaseRect.width(), kBaseRect.height() + 1),
1905          false, true, false, false},
1906         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1907                           kBaseRect.width() + 1, kBaseRect.height()),
1908          false, true, false, false},
1909         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1910                           kBaseRect.width() + 1, kBaseRect.height() + 1),
1911          false, true, false, false},
1912         {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1913                           kBaseRect.width(), kBaseRect.height()),
1914          false, true, false, false},
1915         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1916                           kBaseRect.width(), kBaseRect.height()),
1917          false, true, false, false},
1918         {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1919                           kBaseRect.width() + 2, kBaseRect.height()),
1920          false, true, false, false},
1921         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1922                           kBaseRect.width() + 2, kBaseRect.height()),
1923          false, true, false, false},
1924 
1925         // zero-w/h rects at each corner of kBaseRect
1926         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1927         {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1928         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1929         {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1930 
1931         // far away rect
1932         {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1933                           SkIntToScalar(10), SkIntToScalar(10)),
1934          false, false, false, false},
1935 
1936         // very large rect containing kBaseRect
1937         {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1938                           kBaseRect.fTop - 5 * kBaseRect.height(),
1939                           11 * kBaseRect.width(), 11 * kBaseRect.height()),
1940          false, false, false, false},
1941 
1942         // skinny rect that spans same y-range as kBaseRect
1943         {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1944                           SkIntToScalar(1), kBaseRect.height()),
1945          true, true, true, true},
1946 
1947         // short rect that spans same x-range as kBaseRect
1948         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1949          true, true, true, true},
1950 
1951         // skinny rect that spans slightly larger y-range than kBaseRect
1952         {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1953                           SkIntToScalar(1), kBaseRect.height() + 1),
1954          false, true, false, false},
1955 
1956         // short rect that spans slightly larger x-range than kBaseRect
1957         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1958                           kBaseRect.width() + 1, SkScalar(1)),
1959          false, true, false, false},
1960     };
1961 
1962     for (int inv = 0; inv < 4; ++inv) {
1963         for (size_t q = 0; q < std::size(kQueries); ++q) {
1964             SkRect qRect = kQueries[q].fQueryRect;
1965             if (inv & 0x1) {
1966                 using std::swap;
1967                 swap(qRect.fLeft, qRect.fRight);
1968             }
1969             if (inv & 0x2) {
1970                 using std::swap;
1971                 swap(qRect.fTop, qRect.fBottom);
1972             }
1973             for (int d = 0; d < 2; ++d) {
1974                 SkPathDirection dir = d ? SkPathDirection::kCCW : SkPathDirection::kCW;
1975                 path.reset();
1976                 path.addRect(kBaseRect, dir);
1977                 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1978                                           path.conservativelyContainsRect(qRect));
1979 
1980                 path.reset();
1981                 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1982                 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1983                                           path.conservativelyContainsRect(qRect));
1984 
1985                 path.reset();
1986                 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1987                 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1988                                           path.conservativelyContainsRect(qRect));
1989 
1990                 path.reset();
1991                 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1992                 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1993                              kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1994                              kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1995                 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1996                 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1997                 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1998                 path.close();
1999                 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
2000                                           path.conservativelyContainsRect(qRect));
2001 
2002             }
2003             // Slightly non-convex shape, shouldn't contain any rects.
2004             path.reset();
2005             path.moveTo(0, 0);
2006             path.lineTo(SkIntToScalar(50), 0.05f);
2007             path.lineTo(SkIntToScalar(100), 0);
2008             path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
2009             path.lineTo(0, SkIntToScalar(100));
2010             path.close();
2011             REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
2012         }
2013     }
2014 
2015     // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
2016     path.reset();
2017     path.moveTo(0, 0);
2018     path.lineTo(SkIntToScalar(100), 0);
2019     path.lineTo(0, SkIntToScalar(100));
2020 
2021     // inside, on along top edge
2022     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
2023                                                                                SkIntToScalar(10),
2024                                                                                SkIntToScalar(10))));
2025     // above
2026     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2027         SkRect::MakeXYWH(SkIntToScalar(50),
2028                          SkIntToScalar(-10),
2029                          SkIntToScalar(10),
2030                          SkIntToScalar(10))));
2031     // to the left
2032     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
2033                                                                                 SkIntToScalar(5),
2034                                                                                 SkIntToScalar(5),
2035                                                                                 SkIntToScalar(5))));
2036 
2037     // outside the diagonal edge
2038     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
2039                                                                                 SkIntToScalar(200),
2040                                                                                 SkIntToScalar(20),
2041                                                                                 SkIntToScalar(5))));
2042 
2043 
2044     // Test that multiple move commands do not cause asserts.
2045     path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
2046     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
2047                                                                                SkIntToScalar(10),
2048                                                                                SkIntToScalar(10))));
2049 
2050     // Same as above path and first test but with an extra moveTo.
2051     path.reset();
2052     path.moveTo(100, 100);
2053     path.moveTo(0, 0);
2054     path.lineTo(SkIntToScalar(100), 0);
2055     path.lineTo(0, SkIntToScalar(100));
2056     // Convexity logic treats a path as filled and closed, so that multiple (non-trailing) moveTos
2057     // have no effect on convexity
2058     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(
2059         SkRect::MakeXYWH(SkIntToScalar(50), 0,
2060                          SkIntToScalar(10),
2061                          SkIntToScalar(10))));
2062 
2063     // Same as above path and first test but with the extra moveTo making a degenerate sub-path
2064     // following the non-empty sub-path. Verifies that this does not trigger assertions.
2065     path.reset();
2066     path.moveTo(0, 0);
2067     path.lineTo(SkIntToScalar(100), 0);
2068     path.lineTo(0, SkIntToScalar(100));
2069     path.moveTo(100, 100);
2070 
2071     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
2072                                                                                SkIntToScalar(10),
2073                                                                                SkIntToScalar(10))));
2074 
2075     // Test that multiple move commands do not cause asserts and that the function
2076     // is not confused by the multiple moves.
2077     path.reset();
2078     path.moveTo(0, 0);
2079     path.lineTo(SkIntToScalar(100), 0);
2080     path.lineTo(0, SkIntToScalar(100));
2081     path.moveTo(0, SkIntToScalar(200));
2082     path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
2083     path.lineTo(0, SkIntToScalar(300));
2084 
2085     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2086                                                             SkRect::MakeXYWH(SkIntToScalar(50), 0,
2087                                                                              SkIntToScalar(10),
2088                                                                              SkIntToScalar(10))));
2089 
2090     path.reset();
2091     path.lineTo(100, 100);
2092     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
2093 
2094     // An empty path should not contain any rectangle. It's questionable whether an empty path
2095     // contains an empty rectangle. However, since it is a conservative test it is ok to
2096     // return false.
2097     path.reset();
2098     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2099     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2100 
2101     path.reset();
2102     path.moveTo(50, 50);
2103     path.cubicTo(0, 0, 100, 0, 50, 50);
2104     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(100, 100)));
2105     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(30, 30)));
2106     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2107     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2108 
2109     path.reset();
2110     path.moveTo(50, 50);
2111     path.quadTo(100, 100, 50, 50);
2112     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2113     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2114 }
2115 
test_isRect_open_close(skiatest::Reporter * reporter)2116 static void test_isRect_open_close(skiatest::Reporter* reporter) {
2117     SkPath path;
2118     bool isClosed;
2119 
2120     path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
2121     path.close();
2122 
2123     REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
2124     REPORTER_ASSERT(reporter, isClosed);
2125 }
2126 
2127 // Simple isRect test is inline TestPath, below.
2128 // test_isRect provides more extensive testing.
test_isRect(skiatest::Reporter * reporter)2129 static void test_isRect(skiatest::Reporter* reporter) {
2130     test_isRect_open_close(reporter);
2131 
2132     // passing tests (all moveTo / lineTo...
2133     SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2134     SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2135     SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2136     SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2137     SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
2138     SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2139     SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2140     SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2141     SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2142     SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
2143     SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
2144     SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
2145     SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
2146     SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
2147     SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
2148 
2149     // failing tests
2150     SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2151     SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2152     SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2153     SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2154     SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2155     SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2156     SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2157     SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2158     SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2159     SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2160     SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
2161 
2162     // no close, but we should detect them as fillably the same as a rect
2163     SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2164     SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2165     SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2166 
2167     // like c2, but we double-back on ourselves
2168     SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2169     // like c2, but we overshoot the start point
2170     SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2171     SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2172 
2173     struct IsRectTest {
2174         SkPoint *fPoints;
2175         int fPointCount;
2176         bool fClose;
2177         bool fIsRect;
2178     } tests[] = {
2179         { r1, std::size(r1), true, true },
2180         { r2, std::size(r2), true, true },
2181         { r3, std::size(r3), true, true },
2182         { r4, std::size(r4), true, true },
2183         { r5, std::size(r5), true, true },
2184         { r6, std::size(r6), true, true },
2185         { r7, std::size(r7), true, true },
2186         { r8, std::size(r8), true, true },
2187         { r9, std::size(r9), true, true },
2188         { ra, std::size(ra), true, true },
2189         { rb, std::size(rb), true, true },
2190         { rc, std::size(rc), true, true },
2191         { rd, std::size(rd), true, true },
2192         { re, std::size(re), true, true },
2193         { rf, std::size(rf), true, true },
2194 
2195         { f1, std::size(f1), true, false },
2196         { f2, std::size(f2), true, false },
2197         { f3, std::size(f3), true, false },
2198         { f4, std::size(f4), true, false },
2199         { f5, std::size(f5), true, false },
2200         { f6, std::size(f6), true, false },
2201         { f7, std::size(f7), true, false },
2202         { f8, std::size(f8), true, false },
2203         { f9, std::size(f9), true, false },
2204         { fa, std::size(fa), true, false },
2205         { fb, std::size(fb), true, false },
2206 
2207         { c1, std::size(c1), false, true },
2208         { c2, std::size(c2), false, true },
2209         { c3, std::size(c3), false, true },
2210 
2211         { d1, std::size(d1), false, false },
2212         { d2, std::size(d2), false, true },
2213         { d3, std::size(d3), false, false },
2214     };
2215 
2216     const size_t testCount = std::size(tests);
2217     int index;
2218     for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2219         SkPath path;
2220         path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2221         for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2222             path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2223         }
2224         if (tests[testIndex].fClose) {
2225             path.close();
2226         }
2227         REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2228 
2229         if (tests[testIndex].fIsRect) {
2230             SkRect computed, expected;
2231             bool isClosed;
2232             SkPathDirection direction;
2233             int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
2234             expected.setBounds(tests[testIndex].fPoints, pointCount);
2235             SkPathFirstDirection cheapDirection = SkPathPriv::ComputeFirstDirection(path);
2236             REPORTER_ASSERT(reporter, cheapDirection != SkPathFirstDirection::kUnknown);
2237             REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2238             REPORTER_ASSERT(reporter, expected == computed);
2239             REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2240             REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2241         } else {
2242             SkRect computed;
2243             computed.setLTRB(123, 456, 789, 1011);
2244             for (auto c : {true, false})
2245             for (auto d : {SkPathDirection::kCW, SkPathDirection::kCCW}) {
2246               bool isClosed = c;
2247               SkPathDirection direction = d;
2248               REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2249               REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2250               REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2251               REPORTER_ASSERT(reporter, isClosed == c);
2252               REPORTER_ASSERT(reporter, direction == d);
2253             }
2254         }
2255     }
2256 
2257     // fail, close then line
2258     SkPath path1;
2259     path1.moveTo(r1[0].fX, r1[0].fY);
2260     for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2261         path1.lineTo(r1[index].fX, r1[index].fY);
2262     }
2263     path1.close();
2264     path1.lineTo(1, 0);
2265     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2266 
2267     // fail, move in the middle
2268     path1.reset();
2269     path1.moveTo(r1[0].fX, r1[0].fY);
2270     for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2271         if (index == 2) {
2272             path1.moveTo(1, .5f);
2273         }
2274         path1.lineTo(r1[index].fX, r1[index].fY);
2275     }
2276     path1.close();
2277     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2278 
2279     // fail, move on the edge
2280     path1.reset();
2281     for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2282         path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2283         path1.lineTo(r1[index].fX, r1[index].fY);
2284     }
2285     path1.close();
2286     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2287 
2288     // fail, quad
2289     path1.reset();
2290     path1.moveTo(r1[0].fX, r1[0].fY);
2291     for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2292         if (index == 2) {
2293             path1.quadTo(1, .5f, 1, .5f);
2294         }
2295         path1.lineTo(r1[index].fX, r1[index].fY);
2296     }
2297     path1.close();
2298     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2299 
2300     // fail, cubic
2301     path1.reset();
2302     path1.moveTo(r1[0].fX, r1[0].fY);
2303     for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2304         if (index == 2) {
2305             path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2306         }
2307         path1.lineTo(r1[index].fX, r1[index].fY);
2308     }
2309     path1.close();
2310     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2311 }
2312 
check_simple_rect(skiatest::Reporter * reporter,const SkPath & path,bool isClosed,const SkRect & rect,SkPathDirection dir,unsigned start)2313 static void check_simple_rect(skiatest::Reporter* reporter, const SkPath& path, bool isClosed,
2314                               const SkRect& rect, SkPathDirection dir, unsigned start) {
2315     SkRect r = SkRect::MakeEmpty();
2316     SkPathDirection d = SkPathDirection::kCCW;
2317     unsigned s = ~0U;
2318 
2319     REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleRect(path, false, &r, &d, &s) == isClosed);
2320     REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleRect(path, true, &r, &d, &s));
2321     REPORTER_ASSERT(reporter, r == rect);
2322     REPORTER_ASSERT(reporter, d == dir);
2323     REPORTER_ASSERT(reporter, s == start);
2324 }
2325 
test_is_closed_rect(skiatest::Reporter * reporter)2326 static void test_is_closed_rect(skiatest::Reporter* reporter) {
2327     using std::swap;
2328     SkRect r = SkRect::MakeEmpty();
2329     SkPathDirection d = SkPathDirection::kCCW;
2330     unsigned s = ~0U;
2331 
2332     const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2333     const SkRect emptyRect = SkRect::MakeEmpty();
2334     for (int start = 0; start < 4; ++start) {
2335         for (auto dir : {SkPathDirection::kCCW, SkPathDirection::kCW}) {
2336             SkPath path;
2337             path.addRect(testRect, dir, start);
2338             check_simple_rect(reporter, path, true, testRect, dir, start);
2339             path.close();
2340             check_simple_rect(reporter, path, true, testRect, dir, start);
2341             SkPath path2 = path;
2342             path2.lineTo(10, 10);
2343             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2344             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2345             path2 = path;
2346             path2.moveTo(10, 10);
2347             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2348             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2349             path2 = path;
2350             path2.addRect(testRect, dir, start);
2351             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2352             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2353             // Make the path by hand, manually closing it.
2354             path2.reset();
2355             SkPoint firstPt = {0.f, 0.f};
2356             for (auto [v, verbPts, w] : SkPathPriv::Iterate(path)) {
2357                 switch(v) {
2358                     case SkPathVerb::kMove:
2359                         firstPt = verbPts[0];
2360                         path2.moveTo(verbPts[0]);
2361                         break;
2362                     case SkPathVerb::kLine:
2363                         path2.lineTo(verbPts[1]);
2364                         break;
2365                     default:
2366                         break;
2367                 }
2368             }
2369             // We haven't closed it yet...
2370             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2371             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2372             // ... now we do and test again.
2373             path2.lineTo(firstPt);
2374             check_simple_rect(reporter, path2, false, testRect, dir, start);
2375             // A redundant close shouldn't cause a failure.
2376             path2.close();
2377             check_simple_rect(reporter, path2, true, testRect, dir, start);
2378             // Degenerate point and line rects are not allowed
2379             path2.reset();
2380             path2.addRect(emptyRect, dir, start);
2381             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2382             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2383             SkRect degenRect = testRect;
2384             degenRect.fLeft = degenRect.fRight;
2385             path2.reset();
2386             path2.addRect(degenRect, dir, start);
2387             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2388             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2389             degenRect = testRect;
2390             degenRect.fTop = degenRect.fBottom;
2391             path2.reset();
2392             path2.addRect(degenRect, dir, start);
2393             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, false, &r, &d, &s));
2394             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path2, true, &r, &d, &s));
2395             // An inverted rect makes a rect path, but changes the winding dir and start point.
2396             SkPathDirection swapDir = (dir == SkPathDirection::kCW)
2397                                             ? SkPathDirection::kCCW
2398                                             : SkPathDirection::kCW;
2399             static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2400             static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2401             SkRect swapRect = testRect;
2402             swap(swapRect.fLeft, swapRect.fRight);
2403             path2.reset();
2404             path2.addRect(swapRect, dir, start);
2405             check_simple_rect(reporter, path2, true, testRect, swapDir, kXSwapStarts[start]);
2406             swapRect = testRect;
2407             swap(swapRect.fTop, swapRect.fBottom);
2408             path2.reset();
2409             path2.addRect(swapRect, dir, start);
2410             check_simple_rect(reporter, path2, true, testRect, swapDir, kYSwapStarts[start]);
2411         }
2412     }
2413     // down, up, left, close
2414     SkPath path;
2415     path.moveTo(1, 1);
2416     path.lineTo(1, 2);
2417     path.lineTo(1, 1);
2418     path.lineTo(0, 1);
2419     SkRect rect;
2420     SkPathDirection  dir;
2421     unsigned start;
2422     path.close();
2423     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2424     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
2425     // right, left, up, close
2426     path.reset();
2427     path.moveTo(1, 1);
2428     path.lineTo(2, 1);
2429     path.lineTo(1, 1);
2430     path.lineTo(1, 0);
2431     path.close();
2432     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2433     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
2434     // parallelogram with horizontal edges
2435     path.reset();
2436     path.moveTo(1, 0);
2437     path.lineTo(3, 0);
2438     path.lineTo(2, 1);
2439     path.lineTo(0, 1);
2440     path.close();
2441     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2442     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
2443     // parallelogram with vertical edges
2444     path.reset();
2445     path.moveTo(0, 1);
2446     path.lineTo(0, 3);
2447     path.lineTo(1, 2);
2448     path.lineTo(1, 0);
2449     path.close();
2450     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, false, &rect, &dir, &start));
2451     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleRect(path, true, &rect, &dir, &start));
2452 
2453 }
2454 
test_isArc(skiatest::Reporter * reporter)2455 static void test_isArc(skiatest::Reporter* reporter) {
2456     SkPath path;
2457     REPORTER_ASSERT(reporter, !path.isArc(nullptr));
2458 
2459     // One circle, one oval:
2460     const SkRect kOvals[] = { SkRect::MakeWH(100, 100), SkRect::MakeWH(100, 200)};
2461 
2462     // Various start and sweep angles. Note that we can't test with more than a full revolution,
2463     // those cases are automatically converted to ovals by SkPath.
2464     const SkScalar kStartAngles[] = { -270, -135, -45, 0, 10, 70, 180, 350 };
2465     const SkScalar kSweepAngles[] = { -350, -190, -90, -5, 5, 89, 180, 270, 350 };
2466 
2467     int mutator = 0;
2468 
2469     for (SkRect oval : kOvals) {
2470         for (SkScalar startAngle : kStartAngles) {
2471             for (SkScalar sweepAngle : kSweepAngles) {
2472                 // For now, isArc only works for arcs where useCenter is false!
2473                 // TODO: When that's fixed, add more tests cases here.
2474                 path.rewind();
2475                 // Include an extra moveTo at the start - this should not interfere with isArc
2476                 path.moveTo(oval.center());
2477                 path.addArc(oval, startAngle, sweepAngle);
2478 
2479                 SkArc arc;
2480                 REPORTER_ASSERT(reporter, path.isArc(&arc));
2481                 REPORTER_ASSERT(reporter,
2482                                 oval == arc.fOval &&
2483                                 startAngle == arc.fStartAngle &&
2484                                 sweepAngle == arc.fSweepAngle &&
2485                                 !arc.isWedge());
2486 
2487                 // Apply some mutation. All of these should cause the path to no longer be an arc:
2488                 switch (mutator) {
2489                     case 0:
2490                         path.addArc(oval, startAngle, sweepAngle);
2491                         break;
2492                     case 1:
2493                         path.lineTo(oval.center());
2494                         break;
2495                     case 2:
2496                         path.lineTo(path.getPoint(0));
2497                         break;
2498                     case 3:
2499                         path.close();
2500                         break;
2501                     case 4:
2502                         path.moveTo(oval.center());
2503                         break;
2504                     default:
2505                         SkUNREACHABLE;
2506                 }
2507                 mutator = (mutator + 1) % 5;
2508                 REPORTER_ASSERT(reporter, !path.isArc(nullptr));
2509             }
2510         }
2511     }
2512 
2513     // Having any non-move verb before the arc should cause isArc to return false:
2514     path.rewind();
2515     path.lineTo(kOvals[0].center());
2516     path.addArc(kOvals[0], kStartAngles[0], kSweepAngles[0]);
2517     REPORTER_ASSERT(reporter, !path.isArc(nullptr));
2518 
2519     // Finally, transforming an arc path by a non-identity should always result in a non-arc path:
2520     // TODO: We could clearly preserve arcs for translation, and for scale/rotation with extra work.
2521     for (SkMatrix m :
2522          {SkMatrix::Translate(10, 10), SkMatrix::RotateDeg(90), SkMatrix::Scale(2, 2)}) {
2523         path.rewind();
2524         path.addArc(kOvals[0], kStartAngles[0], kSweepAngles[0]);
2525         REPORTER_ASSERT(reporter, path.isArc(nullptr));
2526         path.transform(SkMatrix::I());
2527         REPORTER_ASSERT(reporter, path.isArc(nullptr));
2528         path.transform(m);
2529         REPORTER_ASSERT(reporter, !path.isArc(nullptr));
2530     }
2531 }
2532 
test_isNestedFillRects(skiatest::Reporter * reporter)2533 static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2534     // passing tests (all moveTo / lineTo...
2535     SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2536     SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2537     SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2538     SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2539     SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2540     SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2541     SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2542     SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2543     SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2544     SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2545     SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2546     SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2547     SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2548     SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2549 
2550     // failing tests
2551     SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2552     SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2553     SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2554     SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2555     SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2556     SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2557     SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2558     SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2559 
2560     // success, no close is OK
2561     SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2562     SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2563 
2564     struct IsNestedRectTest {
2565         SkPoint *fPoints;
2566         int fPointCount;
2567         SkPathFirstDirection fDirection;
2568         bool fClose;
2569         bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2570     } tests[] = {
2571         { r1, std::size(r1), SkPathFirstDirection::kCW , true, true },
2572         { r2, std::size(r2), SkPathFirstDirection::kCW , true, true },
2573         { r3, std::size(r3), SkPathFirstDirection::kCW , true, true },
2574         { r4, std::size(r4), SkPathFirstDirection::kCW , true, true },
2575         { r5, std::size(r5), SkPathFirstDirection::kCCW, true, true },
2576         { r6, std::size(r6), SkPathFirstDirection::kCCW, true, true },
2577         { r7, std::size(r7), SkPathFirstDirection::kCCW, true, true },
2578         { r8, std::size(r8), SkPathFirstDirection::kCCW, true, true },
2579         { r9, std::size(r9), SkPathFirstDirection::kCCW, true, true },
2580         { ra, std::size(ra), SkPathFirstDirection::kCCW, true, true },
2581         { rb, std::size(rb), SkPathFirstDirection::kCW,  true, true },
2582         { rc, std::size(rc), SkPathFirstDirection::kCW,  true, true },
2583         { rd, std::size(rd), SkPathFirstDirection::kCCW, true, true },
2584         { re, std::size(re), SkPathFirstDirection::kCW,  true, true },
2585 
2586         { f1, std::size(f1), SkPathFirstDirection::kUnknown, true, false },
2587         { f2, std::size(f2), SkPathFirstDirection::kUnknown, true, false },
2588         { f3, std::size(f3), SkPathFirstDirection::kUnknown, true, false },
2589         { f4, std::size(f4), SkPathFirstDirection::kUnknown, true, false },
2590         { f5, std::size(f5), SkPathFirstDirection::kUnknown, true, false },
2591         { f6, std::size(f6), SkPathFirstDirection::kUnknown, true, false },
2592         { f7, std::size(f7), SkPathFirstDirection::kUnknown, true, false },
2593         { f8, std::size(f8), SkPathFirstDirection::kUnknown, true, false },
2594 
2595         { c1, std::size(c1), SkPathFirstDirection::kCW, false, true },
2596         { c2, std::size(c2), SkPathFirstDirection::kCW, false, true },
2597     };
2598 
2599     const size_t testCount = std::size(tests);
2600     int index;
2601     for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2602         for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2603             SkPath path;
2604             if (rectFirst) {
2605                 path.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2606             }
2607             path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2608             for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2609                 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2610             }
2611             if (tests[testIndex].fClose) {
2612                 path.close();
2613             }
2614             if (!rectFirst) {
2615                 path.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2616             }
2617             REPORTER_ASSERT(reporter,
2618                             tests[testIndex].fIsNestedRect == SkPathPriv::IsNestedFillRects(path, nullptr));
2619             if (tests[testIndex].fIsNestedRect) {
2620                 SkRect expected[2], computed[2];
2621                 SkPathFirstDirection expectedDirs[2];
2622                 SkPathDirection computedDirs[2];
2623                 SkRect testBounds;
2624                 testBounds.setBounds(tests[testIndex].fPoints, tests[testIndex].fPointCount);
2625                 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2626                 expected[1] = testBounds;
2627                 if (rectFirst) {
2628                     expectedDirs[0] = SkPathFirstDirection::kCW;
2629                 } else {
2630                     expectedDirs[0] = SkPathFirstDirection::kCCW;
2631                 }
2632                 expectedDirs[1] = tests[testIndex].fDirection;
2633                 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, computed, computedDirs));
2634                 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2635                 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2636                 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2637                 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2638             }
2639         }
2640 
2641         // fail, close then line
2642         SkPath path1;
2643         if (rectFirst) {
2644             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2645         }
2646         path1.moveTo(r1[0].fX, r1[0].fY);
2647         for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2648             path1.lineTo(r1[index].fX, r1[index].fY);
2649         }
2650         path1.close();
2651         path1.lineTo(1, 0);
2652         if (!rectFirst) {
2653             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2654         }
2655         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2656 
2657         // fail, move in the middle
2658         path1.reset();
2659         if (rectFirst) {
2660             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2661         }
2662         path1.moveTo(r1[0].fX, r1[0].fY);
2663         for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2664             if (index == 2) {
2665                 path1.moveTo(1, .5f);
2666             }
2667             path1.lineTo(r1[index].fX, r1[index].fY);
2668         }
2669         path1.close();
2670         if (!rectFirst) {
2671             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2672         }
2673         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2674 
2675         // fail, move on the edge
2676         path1.reset();
2677         if (rectFirst) {
2678             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2679         }
2680         for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2681             path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2682             path1.lineTo(r1[index].fX, r1[index].fY);
2683         }
2684         path1.close();
2685         if (!rectFirst) {
2686             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2687         }
2688         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2689 
2690         // fail, quad
2691         path1.reset();
2692         if (rectFirst) {
2693             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2694         }
2695         path1.moveTo(r1[0].fX, r1[0].fY);
2696         for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2697             if (index == 2) {
2698                 path1.quadTo(1, .5f, 1, .5f);
2699             }
2700             path1.lineTo(r1[index].fX, r1[index].fY);
2701         }
2702         path1.close();
2703         if (!rectFirst) {
2704             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2705         }
2706         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2707 
2708         // fail, cubic
2709         path1.reset();
2710         if (rectFirst) {
2711             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCW);
2712         }
2713         path1.moveTo(r1[0].fX, r1[0].fY);
2714         for (index = 1; index < SkToInt(std::size(r1)); ++index) {
2715             if (index == 2) {
2716                 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2717             }
2718             path1.lineTo(r1[index].fX, r1[index].fY);
2719         }
2720         path1.close();
2721         if (!rectFirst) {
2722             path1.addRect(-1, -1, 2, 2, SkPathDirection::kCCW);
2723         }
2724         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2725 
2726         // fail,  not nested
2727         path1.reset();
2728         path1.addRect(1, 1, 3, 3, SkPathDirection::kCW);
2729         path1.addRect(2, 2, 4, 4, SkPathDirection::kCW);
2730         REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
2731     }
2732 
2733     //  pass, constructed explicitly from manually closed rects specified as moves/lines.
2734     SkPath path;
2735     path.moveTo(0, 0);
2736     path.lineTo(10, 0);
2737     path.lineTo(10, 10);
2738     path.lineTo(0, 10);
2739     path.lineTo(0, 0);
2740     path.moveTo(1, 1);
2741     path.lineTo(9, 1);
2742     path.lineTo(9, 9);
2743     path.lineTo(1, 9);
2744     path.lineTo(1, 1);
2745     REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, nullptr));
2746 
2747     // pass, stroke rect
2748     SkPath src, dst;
2749     src.addRect(1, 1, 7, 7, SkPathDirection::kCW);
2750     SkPaint strokePaint;
2751     strokePaint.setStyle(SkPaint::kStroke_Style);
2752     strokePaint.setStrokeWidth(2);
2753     skpathutils::FillPathWithPaint(src, strokePaint, &dst);
2754     REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(dst, nullptr));
2755 }
2756 
write_and_read_back(skiatest::Reporter * reporter,const SkPath & p)2757 static void write_and_read_back(skiatest::Reporter* reporter,
2758                                 const SkPath& p) {
2759     SkBinaryWriteBuffer writer({});
2760     writer.writePath(p);
2761     size_t size = writer.bytesWritten();
2762     SkAutoMalloc storage(size);
2763     writer.writeToMemory(storage.get());
2764     SkReadBuffer reader(storage.get(), size);
2765 
2766     SkPath readBack;
2767     REPORTER_ASSERT(reporter, readBack != p);
2768     reader.readPath(&readBack);
2769     REPORTER_ASSERT(reporter, readBack == p);
2770 
2771     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(readBack) ==
2772                               SkPathPriv::GetConvexityOrUnknown(p));
2773 
2774     SkRect oval0, oval1;
2775     SkPathDirection dir0, dir1;
2776     unsigned start0, start1;
2777     REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
2778     if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2779         SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
2780         REPORTER_ASSERT(reporter, oval0 == oval1);
2781         REPORTER_ASSERT(reporter, dir0 == dir1);
2782         REPORTER_ASSERT(reporter, start0 == start1);
2783     }
2784     REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2785     SkRRect rrect0, rrect1;
2786     if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2787         SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
2788         REPORTER_ASSERT(reporter, rrect0 == rrect1);
2789         REPORTER_ASSERT(reporter, dir0 == dir1);
2790         REPORTER_ASSERT(reporter, start0 == start1);
2791     }
2792     const SkRect& origBounds = p.getBounds();
2793     const SkRect& readBackBounds = readBack.getBounds();
2794 
2795     REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2796 }
2797 
test_flattening(skiatest::Reporter * reporter)2798 static void test_flattening(skiatest::Reporter* reporter) {
2799     SkPath p;
2800 
2801     static const SkPoint pts[] = {
2802         { 0, 0 },
2803         { SkIntToScalar(10), SkIntToScalar(10) },
2804         { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2805         { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2806     };
2807     p.moveTo(pts[0]);
2808     p.lineTo(pts[1]);
2809     p.quadTo(pts[2], pts[3]);
2810     p.cubicTo(pts[4], pts[5], pts[6]);
2811 
2812     write_and_read_back(reporter, p);
2813 
2814     // create a buffer that should be much larger than the path so we don't
2815     // kill our stack if writer goes too far.
2816     char buffer[1024];
2817     size_t size1 = p.writeToMemory(nullptr);
2818     size_t size2 = p.writeToMemory(buffer);
2819     REPORTER_ASSERT(reporter, size1 == size2);
2820 
2821     SkPath p2;
2822     size_t size3 = p2.readFromMemory(buffer, 1024);
2823     REPORTER_ASSERT(reporter, size1 == size3);
2824     REPORTER_ASSERT(reporter, p == p2);
2825 
2826     size3 = p2.readFromMemory(buffer, 0);
2827     REPORTER_ASSERT(reporter, !size3);
2828 
2829     SkPath tooShort;
2830     size3 = tooShort.readFromMemory(buffer, size1 - 1);
2831     REPORTER_ASSERT(reporter, tooShort.isEmpty());
2832 
2833     char buffer2[1024];
2834     size3 = p2.writeToMemory(buffer2);
2835     REPORTER_ASSERT(reporter, size1 == size3);
2836     REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2837 
2838     // test persistence of the oval flag & convexity
2839     {
2840         SkPath oval;
2841         SkRect rect = SkRect::MakeWH(10, 10);
2842         oval.addOval(rect);
2843 
2844         write_and_read_back(reporter, oval);
2845     }
2846 }
2847 
test_transform(skiatest::Reporter * reporter)2848 static void test_transform(skiatest::Reporter* reporter) {
2849     SkPath p;
2850 
2851 #define CONIC_PERSPECTIVE_BUG_FIXED 0
2852     static const SkPoint pts[] = {
2853         { 0, 0 },  // move
2854         { SkIntToScalar(10), SkIntToScalar(10) },  // line
2855         { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },  // quad
2856         { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) },  // cubic
2857 #if CONIC_PERSPECTIVE_BUG_FIXED
2858         { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) },  // conic
2859 #endif
2860     };
2861     const int kPtCount = std::size(pts);
2862 
2863     p.moveTo(pts[0]);
2864     p.lineTo(pts[1]);
2865     p.quadTo(pts[2], pts[3]);
2866     p.cubicTo(pts[4], pts[5], pts[6]);
2867 #if CONIC_PERSPECTIVE_BUG_FIXED
2868     p.conicTo(pts[4], pts[5], 0.5f);
2869 #endif
2870     p.close();
2871 
2872     {
2873         SkMatrix matrix;
2874         matrix.reset();
2875         SkPath p1;
2876         p.transform(matrix, &p1);
2877         REPORTER_ASSERT(reporter, p == p1);
2878     }
2879 
2880 
2881     {
2882         SkMatrix matrix;
2883         matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2884 
2885         SkPath p1;      // Leave p1 non-unique (i.e., the empty path)
2886 
2887         p.transform(matrix, &p1);
2888         SkPoint pts1[kPtCount];
2889         int count = p1.getPoints(pts1, kPtCount);
2890         REPORTER_ASSERT(reporter, kPtCount == count);
2891         for (int i = 0; i < count; ++i) {
2892             SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2893             REPORTER_ASSERT(reporter, newPt == pts1[i]);
2894         }
2895     }
2896 
2897     {
2898         SkMatrix matrix;
2899         matrix.reset();
2900         matrix.setPerspX(4);
2901 
2902         SkPath p1;
2903         p1.moveTo(SkPoint::Make(0, 0));
2904 
2905         p.transform(matrix, &p1, SkApplyPerspectiveClip::kNo);
2906         REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2907         p1.transform(matrix, nullptr, SkApplyPerspectiveClip::kNo);
2908         SkRect pBounds = p.getBounds();
2909         SkRect p1Bounds = p1.getBounds();
2910         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2911         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2912         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2913         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2914     }
2915 
2916     p.reset();
2917     p.addCircle(0, 0, 1, SkPathDirection::kCW);
2918 
2919     {
2920         SkMatrix matrix;
2921         matrix.reset();
2922         SkPath p1;
2923         p1.moveTo(SkPoint::Make(0, 0));
2924 
2925         p.transform(matrix, &p1);
2926         REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kCW);
2927     }
2928 
2929 
2930     {
2931         SkMatrix matrix;
2932         matrix.reset();
2933         matrix.setScaleX(-1);
2934         SkPath p1;
2935         p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2936 
2937         p.transform(matrix, &p1);
2938         REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kCCW);
2939     }
2940 
2941     {
2942         SkMatrix matrix;
2943         matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2944         SkPath p1;
2945         p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2946 
2947         p.transform(matrix, &p1);
2948         REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p1) == SkPathFirstDirection::kUnknown);
2949     }
2950 
2951     {
2952         SkPath p1;
2953         p1.addRect({ 10, 20, 30, 40 });
2954         SkPath p2;
2955         p2.addRect({ 10, 20, 30, 40 });
2956         uint32_t id1 = p1.getGenerationID();
2957         uint32_t id2 = p2.getGenerationID();
2958         REPORTER_ASSERT(reporter, id1 != id2);
2959         SkMatrix matrix;
2960         matrix.setScale(2, 2);
2961         p1.transform(matrix, &p2);
2962         REPORTER_ASSERT(reporter, id1 == p1.getGenerationID());
2963         REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
2964         p1.transform(matrix);
2965         REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
2966     }
2967 }
2968 
test_zero_length_paths(skiatest::Reporter * reporter)2969 static void test_zero_length_paths(skiatest::Reporter* reporter) {
2970     SkPath  p;
2971     uint8_t verbs[32];
2972 
2973     struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2974         const char* testPath;
2975         const size_t numResultPts;
2976         const SkRect resultBound;
2977         const SkPath::Verb* resultVerbs;
2978         const size_t numResultVerbs;
2979     };
2980 
2981     static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2982     static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2983     static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2984     static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2985     static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2986     static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2987     static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2988     static const SkPath::Verb resultVerbs8[] = {
2989         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2990     };
2991     static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2992     static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2993     static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2994     static const SkPath::Verb resultVerbs12[] = {
2995         SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2996     };
2997     static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2998     static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2999     static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
3000     static const SkPath::Verb resultVerbs16[] = {
3001         SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
3002     };
3003     static const struct zeroPathTestData gZeroLengthTests[] = {
3004         { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, std::size(resultVerbs1) },
3005         { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, std::size(resultVerbs2) },
3006         { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, std::size(resultVerbs3) },
3007         { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, std::size(resultVerbs4) },
3008         { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, std::size(resultVerbs5) },
3009         { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, std::size(resultVerbs6) },
3010         { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, std::size(resultVerbs7) },
3011         { "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, std::size(resultVerbs8) },
3012         { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, std::size(resultVerbs9) },
3013         { "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, std::size(resultVerbs10) },
3014         { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, std::size(resultVerbs11) },
3015         { "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, std::size(resultVerbs12) },
3016         { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, std::size(resultVerbs13) },
3017         { "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,
3018             std::size(resultVerbs14)
3019         },
3020         { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, std::size(resultVerbs15) },
3021         { "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,
3022             std::size(resultVerbs16)
3023         }
3024     };
3025 
3026     for (size_t i = 0; i < std::size(gZeroLengthTests); ++i) {
3027         p.reset();
3028         bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
3029         REPORTER_ASSERT(reporter, valid);
3030         REPORTER_ASSERT(reporter, !p.isEmpty());
3031         REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
3032         REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
3033         REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, std::size(verbs)));
3034         for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
3035             REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
3036         }
3037     }
3038 }
3039 
3040 struct SegmentInfo {
3041     SkPath fPath;
3042     int    fPointCount;
3043 };
3044 
3045 #define kCurveSegmentMask   (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
3046 
test_segment_masks(skiatest::Reporter * reporter)3047 static void test_segment_masks(skiatest::Reporter* reporter) {
3048     SkPath p, p2;
3049 
3050     p.moveTo(0, 0);
3051     p.quadTo(100, 100, 200, 200);
3052     REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
3053     REPORTER_ASSERT(reporter, !p.isEmpty());
3054     p2 = p;
3055     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
3056     p.cubicTo(100, 100, 200, 200, 300, 300);
3057     REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
3058     REPORTER_ASSERT(reporter, !p.isEmpty());
3059     p2 = p;
3060     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
3061 
3062     p.reset();
3063     p.moveTo(0, 0);
3064     p.cubicTo(100, 100, 200, 200, 300, 300);
3065     REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
3066     p2 = p;
3067     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
3068 
3069     REPORTER_ASSERT(reporter, !p.isEmpty());
3070 }
3071 
test_iter(skiatest::Reporter * reporter)3072 static void test_iter(skiatest::Reporter* reporter) {
3073     SkPath  p;
3074     SkPoint pts[4];
3075 
3076     // Test an iterator with no path
3077     SkPath::Iter noPathIter;
3078     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3079 
3080     // Test that setting an empty path works
3081     noPathIter.setPath(p, false);
3082     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3083 
3084     // Test that close path makes no difference for an empty path
3085     noPathIter.setPath(p, true);
3086     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3087 
3088     // Test an iterator with an initial empty path
3089     SkPath::Iter iter(p, false);
3090     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3091 
3092     // Test that close path makes no difference
3093     iter.setPath(p, true);
3094     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3095 
3096 
3097     struct iterTestData {
3098         const char* testPath;
3099         const bool forceClose;
3100         const size_t* numResultPtsPerVerb;
3101         const SkPoint* resultPts;
3102         const SkPath::Verb* resultVerbs;
3103         const size_t numResultVerbs;
3104     };
3105 
3106     static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
3107     static const SkPath::Verb resultVerbs2[] = {
3108         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
3109     };
3110     static const SkPath::Verb resultVerbs3[] = {
3111         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
3112     };
3113     static const size_t resultPtsSizes1[] = { 0 };
3114     static const size_t resultPtsSizes2[] = { 1, 2, 1, 1, 0 };
3115     static const size_t resultPtsSizes3[] = { 1, 2, 1, 1, 1, 0 };
3116     static const SkPoint* resultPts1 = nullptr;
3117     static const SkPoint resultPts2[] = {
3118         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
3119     };
3120     static const SkPoint resultPts3[] = {
3121         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
3122     };
3123     static const struct iterTestData gIterTests[] = {
3124         { "M 1 0", false, resultPtsSizes1, resultPts1, resultVerbs1, std::size(resultVerbs1) },
3125         { "z", false, resultPtsSizes1, resultPts1, resultVerbs1, std::size(resultVerbs1) },
3126         { "z", true, resultPtsSizes1, resultPts1, resultVerbs1, std::size(resultVerbs1) },
3127         { "M 1 0 L 1 0 M 0 0 z", false, resultPtsSizes2, resultPts2, resultVerbs2, std::size(resultVerbs2) },
3128         { "M 1 0 L 1 0 M 0 0 z", true, resultPtsSizes3, resultPts3, resultVerbs3, std::size(resultVerbs3) }
3129     };
3130 
3131     for (size_t i = 0; i < std::size(gIterTests); ++i) {
3132         p.reset();
3133         bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
3134         REPORTER_ASSERT(reporter, valid);
3135         iter.setPath(p, gIterTests[i].forceClose);
3136         int j = 0, l = 0;
3137         do {
3138             REPORTER_ASSERT(reporter, iter.next(pts) == gIterTests[i].resultVerbs[j]);
3139             for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
3140                 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
3141             }
3142         } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
3143         REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
3144     }
3145 
3146     p.reset();
3147     iter.setPath(p, false);
3148     REPORTER_ASSERT(reporter, !iter.isClosedContour());
3149     p.lineTo(1, 1);
3150     p.close();
3151     iter.setPath(p, false);
3152     REPORTER_ASSERT(reporter, iter.isClosedContour());
3153     p.reset();
3154     iter.setPath(p, true);
3155     REPORTER_ASSERT(reporter, !iter.isClosedContour());
3156     p.lineTo(1, 1);
3157     iter.setPath(p, true);
3158     REPORTER_ASSERT(reporter, iter.isClosedContour());
3159     p.moveTo(0, 0);
3160     p.lineTo(2, 2);
3161     iter.setPath(p, false);
3162     REPORTER_ASSERT(reporter, !iter.isClosedContour());
3163 
3164     // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
3165     // check to see if the result is correct.
3166     for (int setNaN = 0; setNaN < 4; ++setNaN) {
3167         p.reset();
3168         p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
3169         p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
3170         iter.setPath(p, true);
3171         iter.next(pts);
3172         iter.next(pts);
3173         REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts));
3174     }
3175 
3176     p.reset();
3177     p.quadTo(0, 0, 0, 0);
3178     iter.setPath(p, false);
3179     iter.next(pts);
3180     REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts));
3181 
3182     p.reset();
3183     p.conicTo(0, 0, 0, 0, 0.5f);
3184     iter.setPath(p, false);
3185     iter.next(pts);
3186     REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3187 
3188     p.reset();
3189     p.cubicTo(0, 0, 0, 0, 0, 0);
3190     iter.setPath(p, false);
3191     iter.next(pts);
3192     REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
3193 
3194     p.moveTo(1, 1);  // add a trailing moveto
3195     iter.setPath(p, false);
3196     iter.next(pts);
3197     REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
3198 
3199     // The GM degeneratesegments.cpp test is more extensive
3200 
3201     // Test out mixed degenerate and non-degenerate geometry with Conics
3202     const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3203     SkRect r = SkRect::MakeWH(100, 100);
3204     SkRRect rr;
3205     rr.setRectRadii(r, radii);
3206     p.reset();
3207     p.addRRect(rr);
3208     iter.setPath(p, false);
3209     REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3210     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3211     return;
3212     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3213     REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3214     REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
3215 }
3216 
test_range_iter(skiatest::Reporter * reporter)3217 static void test_range_iter(skiatest::Reporter* reporter) {
3218     SkPath path;
3219 
3220     // Test an iterator with an initial empty path
3221     SkPathPriv::Iterate iterate(path);
3222     REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
3223 
3224     // Test that a move-only path returns the move.
3225     path.moveTo(SK_Scalar1, 0);
3226     iterate = SkPathPriv::Iterate(path);
3227     SkPathPriv::RangeIter iter = iterate.begin();
3228     {
3229         auto [verb, pts, w] = *iter++;
3230         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3231         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3232         REPORTER_ASSERT(reporter, pts[0].fY == 0);
3233     }
3234     REPORTER_ASSERT(reporter, iter == iterate.end());
3235 
3236     // No matter how many moves we add, we should get them all back
3237     path.moveTo(SK_Scalar1*2, SK_Scalar1);
3238     path.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3239     iterate = SkPathPriv::Iterate(path);
3240     iter = iterate.begin();
3241     {
3242         auto [verb, pts, w] = *iter++;
3243         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3244         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3245         REPORTER_ASSERT(reporter, pts[0].fY == 0);
3246     }
3247     {
3248         auto [verb, pts, w] = *iter++;
3249         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3250         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3251         REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3252     }
3253     {
3254         auto [verb, pts, w] = *iter++;
3255         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3256         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3257         REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3258     }
3259     REPORTER_ASSERT(reporter, iter == iterate.end());
3260 
3261     // Initial close is never ever stored
3262     path.reset();
3263     path.close();
3264     iterate = SkPathPriv::Iterate(path);
3265     REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
3266 
3267     // Move/close sequences
3268     path.reset();
3269     path.close(); // Not stored, no purpose
3270     path.moveTo(SK_Scalar1, 0);
3271     path.close();
3272     path.close(); // Not stored, no purpose
3273     path.moveTo(SK_Scalar1*2, SK_Scalar1);
3274     path.close();
3275     path.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3276     path.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3277     path.close();
3278     iterate = SkPathPriv::Iterate(path);
3279     iter = iterate.begin();
3280     {
3281         auto [verb, pts, w] = *iter++;
3282         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3283         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3284         REPORTER_ASSERT(reporter, pts[0].fY == 0);
3285     }
3286     {
3287         auto [verb, pts, w] = *iter++;
3288         REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3289     }
3290     {
3291         auto [verb, pts, w] = *iter++;
3292         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3293         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3294         REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3295     }
3296     {
3297         auto [verb, pts, w] = *iter++;
3298         REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3299     }
3300     {
3301         auto [verb, pts, w] = *iter++;
3302         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3303         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3304         REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3305     }
3306     {
3307         auto [verb, pts, w] = *iter++;
3308         REPORTER_ASSERT(reporter, verb == SkPathVerb::kMove);
3309         REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3310         REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3311     }
3312     {
3313         auto [verb, pts, w] = *iter++;
3314         REPORTER_ASSERT(reporter, verb == SkPathVerb::kClose);
3315     }
3316     REPORTER_ASSERT(reporter, iter == iterate.end());
3317 
3318     // Generate random paths and verify
3319     SkPoint randomPts[25];
3320     for (int i = 0; i < 5; ++i) {
3321         for (int j = 0; j < 5; ++j) {
3322             randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3323         }
3324     }
3325 
3326     // Max of 10 segments, max 3 points per segment
3327     SkRandom rand(9876543);
3328     SkPoint expectedPts[31]; // May have leading moveTo
3329     SkPathVerb expectedVerbs[22]; // May have leading moveTo
3330     SkPathVerb nextVerb;
3331 
3332     for (int i = 0; i < 500; ++i) {
3333         path.reset();
3334         bool lastWasClose = true;
3335         bool haveMoveTo = false;
3336         SkPoint lastMoveToPt = { 0, 0 };
3337         int numPoints = 0;
3338         int numVerbs = (rand.nextU() >> 16) % 10;
3339         int numIterVerbs = 0;
3340         for (int j = 0; j < numVerbs; ++j) {
3341             do {
3342                 nextVerb = static_cast<SkPathVerb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3343             } while (lastWasClose && nextVerb == SkPathVerb::kClose);
3344             switch (nextVerb) {
3345                 case SkPathVerb::kMove:
3346                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3347                     path.moveTo(expectedPts[numPoints]);
3348                     lastMoveToPt = expectedPts[numPoints];
3349                     numPoints += 1;
3350                     lastWasClose = false;
3351                     haveMoveTo = true;
3352                     break;
3353                 case SkPathVerb::kLine:
3354                     if (!haveMoveTo) {
3355                         expectedPts[numPoints++] = lastMoveToPt;
3356                         expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
3357                         haveMoveTo = true;
3358                     }
3359                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3360                     path.lineTo(expectedPts[numPoints]);
3361                     numPoints += 1;
3362                     lastWasClose = false;
3363                     break;
3364                 case SkPathVerb::kQuad:
3365                     if (!haveMoveTo) {
3366                         expectedPts[numPoints++] = lastMoveToPt;
3367                         expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
3368                         haveMoveTo = true;
3369                     }
3370                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3371                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3372                     path.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3373                     numPoints += 2;
3374                     lastWasClose = false;
3375                     break;
3376                 case SkPathVerb::kConic:
3377                     if (!haveMoveTo) {
3378                         expectedPts[numPoints++] = lastMoveToPt;
3379                         expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
3380                         haveMoveTo = true;
3381                     }
3382                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3383                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3384                     path.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3385                                  rand.nextUScalar1() * 4);
3386                     numPoints += 2;
3387                     lastWasClose = false;
3388                     break;
3389                 case SkPathVerb::kCubic:
3390                     if (!haveMoveTo) {
3391                         expectedPts[numPoints++] = lastMoveToPt;
3392                         expectedVerbs[numIterVerbs++] = SkPathVerb::kMove;
3393                         haveMoveTo = true;
3394                     }
3395                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3396                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3397                     expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3398                     path.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3399                                  expectedPts[numPoints + 2]);
3400                     numPoints += 3;
3401                     lastWasClose = false;
3402                     break;
3403                 case SkPathVerb::kClose:
3404                     path.close();
3405                     haveMoveTo = false;
3406                     lastWasClose = true;
3407                     break;
3408                 default:
3409                     SkDEBUGFAIL("unexpected verb");
3410             }
3411             expectedVerbs[numIterVerbs++] = nextVerb;
3412         }
3413 
3414         numVerbs = numIterVerbs;
3415         numIterVerbs = 0;
3416         int numIterPts = 0;
3417         SkPoint lastMoveTo;
3418         SkPoint lastPt;
3419         lastMoveTo.set(0, 0);
3420         lastPt.set(0, 0);
3421         for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
3422             REPORTER_ASSERT(reporter, verb == expectedVerbs[numIterVerbs]);
3423             numIterVerbs++;
3424             switch (verb) {
3425                 case SkPathVerb::kMove:
3426                     REPORTER_ASSERT(reporter, numIterPts < numPoints);
3427                     REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3428                     lastPt = lastMoveTo = pts[0];
3429                     numIterPts += 1;
3430                     break;
3431                 case SkPathVerb::kLine:
3432                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3433                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3434                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3435                     lastPt = pts[1];
3436                     numIterPts += 1;
3437                     break;
3438                 case SkPathVerb::kQuad:
3439                 case SkPathVerb::kConic:
3440                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3441                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3442                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3443                     REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3444                     lastPt = pts[2];
3445                     numIterPts += 2;
3446                     break;
3447                 case SkPathVerb::kCubic:
3448                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3449                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3450                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3451                     REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3452                     REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3453                     lastPt = pts[3];
3454                     numIterPts += 3;
3455                     break;
3456                 case SkPathVerb::kClose:
3457                     lastPt = lastMoveTo;
3458                     break;
3459                 default:
3460                     SkDEBUGFAIL("unexpected verb");
3461             }
3462         }
3463         REPORTER_ASSERT(reporter, numIterPts == numPoints);
3464         REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3465     }
3466 }
3467 
check_for_circle(skiatest::Reporter * reporter,const SkPath & path,bool expectedCircle,SkPathFirstDirection expectedDir)3468 static void check_for_circle(skiatest::Reporter* reporter,
3469                              const SkPath& path,
3470                              bool expectedCircle,
3471                              SkPathFirstDirection expectedDir) {
3472     SkRect rect = SkRect::MakeEmpty();
3473     REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
3474     SkPathDirection isOvalDir;
3475     unsigned isOvalStart;
3476     if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
3477         REPORTER_ASSERT(reporter, rect.height() == rect.width());
3478         REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3479         SkPath tmpPath;
3480         tmpPath.addOval(rect, isOvalDir, isOvalStart);
3481         REPORTER_ASSERT(reporter, path == tmpPath);
3482     }
3483     REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(path) == expectedDir);
3484 }
3485 
test_circle_skew(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3486 static void test_circle_skew(skiatest::Reporter* reporter,
3487                              const SkPath& path,
3488                              SkPathFirstDirection dir) {
3489     SkPath tmp;
3490 
3491     SkMatrix m;
3492     m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3493     path.transform(m, &tmp);
3494     // this matrix reverses the direction.
3495     if (SkPathFirstDirection::kCCW == dir) {
3496         dir = SkPathFirstDirection::kCW;
3497     } else {
3498         REPORTER_ASSERT(reporter, SkPathFirstDirection::kCW == dir);
3499         dir = SkPathFirstDirection::kCCW;
3500     }
3501     check_for_circle(reporter, tmp, false, dir);
3502 }
3503 
test_circle_translate(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3504 static void test_circle_translate(skiatest::Reporter* reporter,
3505                                   const SkPath& path,
3506                                   SkPathFirstDirection dir) {
3507     SkPath tmp;
3508 
3509     // translate at small offset
3510     SkMatrix m;
3511     m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3512     path.transform(m, &tmp);
3513     check_for_circle(reporter, tmp, true, dir);
3514 
3515     tmp.reset();
3516     m.reset();
3517 
3518     // translate at a relatively big offset
3519     m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3520     path.transform(m, &tmp);
3521     check_for_circle(reporter, tmp, true, dir);
3522 }
3523 
test_circle_rotate(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3524 static void test_circle_rotate(skiatest::Reporter* reporter,
3525                                const SkPath& path,
3526                                SkPathFirstDirection dir) {
3527     for (int angle = 0; angle < 360; ++angle) {
3528         SkPath tmp;
3529         SkMatrix m;
3530         m.setRotate(SkIntToScalar(angle));
3531         path.transform(m, &tmp);
3532 
3533         // TODO: a rotated circle whose rotated angle is not a multiple of 90
3534         // degrees is not an oval anymore, this can be improved.  we made this
3535         // for the simplicity of our implementation.
3536         if (angle % 90 == 0) {
3537             check_for_circle(reporter, tmp, true, dir);
3538         } else {
3539             check_for_circle(reporter, tmp, false, dir);
3540         }
3541     }
3542 }
3543 
test_circle_mirror_x(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3544 static void test_circle_mirror_x(skiatest::Reporter* reporter,
3545                                  const SkPath& path,
3546                                  SkPathFirstDirection dir) {
3547     SkPath tmp;
3548     SkMatrix m;
3549     m.reset();
3550     m.setScaleX(-SK_Scalar1);
3551     path.transform(m, &tmp);
3552     if (SkPathFirstDirection::kCW == dir) {
3553         dir = SkPathFirstDirection::kCCW;
3554     } else {
3555         REPORTER_ASSERT(reporter, SkPathFirstDirection::kCCW == dir);
3556         dir = SkPathFirstDirection::kCW;
3557     }
3558     check_for_circle(reporter, tmp, true, dir);
3559 }
3560 
test_circle_mirror_y(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3561 static void test_circle_mirror_y(skiatest::Reporter* reporter,
3562                                  const SkPath& path,
3563                                  SkPathFirstDirection dir) {
3564     SkPath tmp;
3565     SkMatrix m;
3566     m.reset();
3567     m.setScaleY(-SK_Scalar1);
3568     path.transform(m, &tmp);
3569 
3570     if (SkPathFirstDirection::kCW == dir) {
3571         dir = SkPathFirstDirection::kCCW;
3572     } else {
3573         REPORTER_ASSERT(reporter, SkPathFirstDirection::kCCW == dir);
3574         dir = SkPathFirstDirection::kCW;
3575     }
3576 
3577     check_for_circle(reporter, tmp, true, dir);
3578 }
3579 
test_circle_mirror_xy(skiatest::Reporter * reporter,const SkPath & path,SkPathFirstDirection dir)3580 static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3581                                  const SkPath& path,
3582                                  SkPathFirstDirection dir) {
3583     SkPath tmp;
3584     SkMatrix m;
3585     m.reset();
3586     m.setScaleX(-SK_Scalar1);
3587     m.setScaleY(-SK_Scalar1);
3588     path.transform(m, &tmp);
3589 
3590     check_for_circle(reporter, tmp, true, dir);
3591 }
3592 
test_circle_with_direction(skiatest::Reporter * reporter,SkPathDirection inDir)3593 static void test_circle_with_direction(skiatest::Reporter* reporter,
3594                                        SkPathDirection inDir) {
3595     const SkPathFirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3596     SkPath path;
3597 
3598     // circle at origin
3599     path.addCircle(0, 0, SkIntToScalar(20), inDir);
3600 
3601     check_for_circle(reporter, path, true, dir);
3602     test_circle_rotate(reporter, path, dir);
3603     test_circle_translate(reporter, path, dir);
3604     test_circle_skew(reporter, path, dir);
3605     test_circle_mirror_x(reporter, path, dir);
3606     test_circle_mirror_y(reporter, path, dir);
3607     test_circle_mirror_xy(reporter, path, dir);
3608 
3609     // circle at an offset at (10, 10)
3610     path.reset();
3611     path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3612                    SkIntToScalar(20), inDir);
3613 
3614     check_for_circle(reporter, path, true, dir);
3615     test_circle_rotate(reporter, path, dir);
3616     test_circle_translate(reporter, path, dir);
3617     test_circle_skew(reporter, path, dir);
3618     test_circle_mirror_x(reporter, path, dir);
3619     test_circle_mirror_y(reporter, path, dir);
3620     test_circle_mirror_xy(reporter, path, dir);
3621 
3622     // Try different starting points for the contour.
3623     for (unsigned start = 0; start < 4; ++start) {
3624         path.reset();
3625         path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3626         test_circle_rotate(reporter, path, dir);
3627         test_circle_translate(reporter, path, dir);
3628         test_circle_skew(reporter, path, dir);
3629         test_circle_mirror_x(reporter, path, dir);
3630         test_circle_mirror_y(reporter, path, dir);
3631         test_circle_mirror_xy(reporter, path, dir);
3632     }
3633 }
3634 
test_circle_with_add_paths(skiatest::Reporter * reporter)3635 static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3636     SkPath path;
3637     SkPath circle;
3638     SkPath rect;
3639     SkPath empty;
3640 
3641     const SkPathDirection kCircleDir = SkPathDirection::kCW;
3642     const SkPathDirection kCircleDirOpposite = SkPathDirection::kCCW;
3643 
3644     circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3645     rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3646                  SkIntToScalar(20), SkIntToScalar(20), SkPathDirection::kCW);
3647 
3648     SkMatrix translate;
3649     translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3650 
3651     // Although all the path concatenation related operations leave
3652     // the path a circle, most mark it as a non-circle for simplicity
3653 
3654     // empty + circle (translate)
3655     path = empty;
3656     path.addPath(circle, translate);
3657     check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3658 
3659     // circle + empty (translate)
3660     path = circle;
3661     path.addPath(empty, translate);
3662 
3663     check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3664 
3665     // test reverseAddPath
3666     path = circle;
3667     path.reverseAddPath(rect);
3668     check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3669 }
3670 
test_circle(skiatest::Reporter * reporter)3671 static void test_circle(skiatest::Reporter* reporter) {
3672     test_circle_with_direction(reporter, SkPathDirection::kCW);
3673     test_circle_with_direction(reporter, SkPathDirection::kCCW);
3674 
3675     // multiple addCircle()
3676     SkPath path;
3677     path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
3678     path.addCircle(0, 0, SkIntToScalar(20), SkPathDirection::kCW);
3679     check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
3680 
3681     // some extra lineTo() would make isOval() fail
3682     path.reset();
3683     path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
3684     path.lineTo(0, 0);
3685     check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
3686 
3687     // not back to the original point
3688     path.reset();
3689     path.addCircle(0, 0, SkIntToScalar(10), SkPathDirection::kCW);
3690     path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3691     check_for_circle(reporter, path, false, SkPathFirstDirection::kCW);
3692 
3693     test_circle_with_add_paths(reporter);
3694 
3695     // test negative radius
3696     path.reset();
3697     path.addCircle(0, 0, -1, SkPathDirection::kCW);
3698     REPORTER_ASSERT(reporter, path.isEmpty());
3699 }
3700 
test_oval(skiatest::Reporter * reporter)3701 static void test_oval(skiatest::Reporter* reporter) {
3702     SkRect rect;
3703     SkMatrix m;
3704     SkPath path;
3705     unsigned start = 0;
3706     SkPathDirection dir = SkPathDirection::kCCW;
3707 
3708     rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3709     path.addOval(rect);
3710 
3711     // Defaults to dir = CW and start = 1
3712     REPORTER_ASSERT(reporter, path.isOval(nullptr));
3713 
3714     m.setRotate(SkIntToScalar(90));
3715     SkPath tmp;
3716     path.transform(m, &tmp);
3717     // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3718     // is unchanged.
3719     REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
3720     REPORTER_ASSERT(reporter, 2 == start);
3721     REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
3722 
3723     m.reset();
3724     m.setRotate(SkIntToScalar(30));
3725     tmp.reset();
3726     path.transform(m, &tmp);
3727     // an oval rotated 30 degrees is not an oval anymore.
3728     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3729 
3730     // since empty path being transformed.
3731     path.reset();
3732     tmp.reset();
3733     m.reset();
3734     path.transform(m, &tmp);
3735     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3736 
3737     // empty path is not an oval
3738     tmp.reset();
3739     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3740 
3741     // only has moveTo()s
3742     tmp.reset();
3743     tmp.moveTo(0, 0);
3744     tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3745     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3746 
3747     // mimic WebKit's calling convention,
3748     // call moveTo() first and then call addOval()
3749     path.reset();
3750     path.moveTo(0, 0);
3751     path.addOval(rect);
3752     REPORTER_ASSERT(reporter, path.isOval(nullptr));
3753 
3754     // copy path
3755     path.reset();
3756     tmp.reset();
3757     tmp.addOval(rect);
3758     path = tmp;
3759     REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
3760     REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
3761     REPORTER_ASSERT(reporter, 1 == start);
3762 }
3763 
test_empty(skiatest::Reporter * reporter,const SkPath & p)3764 static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3765     SkPath  empty;
3766 
3767     REPORTER_ASSERT(reporter, p.isEmpty());
3768     REPORTER_ASSERT(reporter, 0 == p.countPoints());
3769     REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3770     REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3771     REPORTER_ASSERT(reporter, p.isConvex());
3772     REPORTER_ASSERT(reporter, p.getFillType() == SkPathFillType::kWinding);
3773     REPORTER_ASSERT(reporter, !p.isInverseFillType());
3774     REPORTER_ASSERT(reporter, p == empty);
3775     REPORTER_ASSERT(reporter, !(p != empty));
3776 }
3777 
test_rrect_is_convex(skiatest::Reporter * reporter,SkPath * path,SkPathDirection dir)3778 static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3779                                  SkPathDirection dir) {
3780     REPORTER_ASSERT(reporter, path->isConvex());
3781     REPORTER_ASSERT(reporter,
3782                     SkPathPriv::ComputeFirstDirection(*path) == SkPathPriv::AsFirstDirection(dir));
3783     SkPathPriv::ForceComputeConvexity(*path);
3784     REPORTER_ASSERT(reporter, path->isConvex());
3785     path->reset();
3786 }
3787 
test_rrect_convexity_is_unknown(skiatest::Reporter * reporter,SkPath * path,SkPathDirection dir)3788 static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3789                                  SkPathDirection dir) {
3790     REPORTER_ASSERT(reporter, path->isConvex());
3791     REPORTER_ASSERT(reporter,
3792                     SkPathPriv::ComputeFirstDirection(*path) == SkPathPriv::AsFirstDirection(dir));
3793     SkPathPriv::ForceComputeConvexity(*path);
3794     REPORTER_ASSERT(reporter, !path->isConvex());
3795     path->reset();
3796 }
3797 
test_rrect(skiatest::Reporter * reporter)3798 static void test_rrect(skiatest::Reporter* reporter) {
3799     SkPath p;
3800     SkRRect rr;
3801     SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3802     SkRect r = {10, 20, 30, 40};
3803     rr.setRectRadii(r, radii);
3804     p.addRRect(rr);
3805     test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3806     p.addRRect(rr, SkPathDirection::kCCW);
3807     test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
3808     p.addRoundRect(r, &radii[0].fX);
3809     test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3810     p.addRoundRect(r, &radii[0].fX, SkPathDirection::kCCW);
3811     test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
3812     p.addRoundRect(r, radii[1].fX, radii[1].fY);
3813     test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3814     p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPathDirection::kCCW);
3815     test_rrect_is_convex(reporter, &p, SkPathDirection::kCCW);
3816     for (size_t i = 0; i < std::size(radii); ++i) {
3817         SkVector save = radii[i];
3818         radii[i].set(0, 0);
3819         rr.setRectRadii(r, radii);
3820         p.addRRect(rr);
3821         test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3822         radii[i] = save;
3823     }
3824     p.addRoundRect(r, 0, 0);
3825     SkRect returnedRect;
3826     REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3827     REPORTER_ASSERT(reporter, returnedRect == r);
3828     test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3829     SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3830     rr.setRectRadii(r, zeroRadii);
3831     p.addRRect(rr);
3832     bool closed;
3833     SkPathDirection dir;
3834     REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3835     REPORTER_ASSERT(reporter, closed);
3836     REPORTER_ASSERT(reporter, SkPathDirection::kCW == dir);
3837     test_rrect_is_convex(reporter, &p, SkPathDirection::kCW);
3838     p.addRRect(rr, SkPathDirection::kCW);
3839     p.addRRect(rr, SkPathDirection::kCW);
3840     REPORTER_ASSERT(reporter, !p.isConvex());
3841     p.reset();
3842     p.addRRect(rr, SkPathDirection::kCCW);
3843     p.addRRect(rr, SkPathDirection::kCCW);
3844     REPORTER_ASSERT(reporter, !p.isConvex());
3845     p.reset();
3846     SkRect emptyR = {10, 20, 10, 30};
3847     rr.setRectRadii(emptyR, radii);
3848     p.addRRect(rr);
3849     // The round rect is "empty" in that it has no fill area. However,
3850     // the path isn't "empty" in that it should have verbs and points.
3851     REPORTER_ASSERT(reporter, !p.isEmpty());
3852     p.reset();
3853     SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3854     rr.setRectRadii(largeR, radii);
3855     p.addRRect(rr);
3856     test_rrect_convexity_is_unknown(reporter, &p, SkPathDirection::kCW);
3857 
3858     // we check for non-finites
3859     SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3860     rr.setRectRadii(infR, radii);
3861     REPORTER_ASSERT(reporter, rr.isEmpty());
3862 }
3863 
test_arc(skiatest::Reporter * reporter)3864 static void test_arc(skiatest::Reporter* reporter) {
3865     SkPath p;
3866     SkRect emptyOval = {10, 20, 30, 20};
3867     REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3868     p.addArc(emptyOval, 1, 2);
3869     REPORTER_ASSERT(reporter, p.isEmpty());
3870     p.reset();
3871     SkRect oval = {10, 20, 30, 40};
3872     p.addArc(oval, 1, 0);
3873     REPORTER_ASSERT(reporter, p.isEmpty());
3874     p.reset();
3875     SkPath cwOval;
3876     cwOval.addOval(oval);
3877     p.addArc(oval, 0, 360);
3878     REPORTER_ASSERT(reporter, p == cwOval);
3879     p.reset();
3880     SkPath ccwOval;
3881     ccwOval.addOval(oval, SkPathDirection::kCCW);
3882     p.addArc(oval, 0, -360);
3883     REPORTER_ASSERT(reporter, p == ccwOval);
3884     p.reset();
3885     p.addArc(oval, 1, 180);
3886     // diagonal colinear points make arc convex
3887     // TODO: one way to keep it concave would be to introduce interpolated on curve points
3888     // between control points and computing the on curve point at scan conversion time
3889     REPORTER_ASSERT(reporter, p.isConvex());
3890     REPORTER_ASSERT(reporter, SkPathPriv::ComputeFirstDirection(p) == SkPathFirstDirection::kCW);
3891     SkPathPriv::ForceComputeConvexity(p);
3892     REPORTER_ASSERT(reporter, p.isConvex());
3893 }
3894 
oval_start_index_to_angle(unsigned start)3895 static inline SkScalar oval_start_index_to_angle(unsigned start) {
3896     switch (start) {
3897         case 0:
3898             return 270.f;
3899         case 1:
3900             return 0.f;
3901         case 2:
3902             return 90.f;
3903         case 3:
3904             return 180.f;
3905         default:
3906             return -1.f;
3907     }
3908 }
3909 
canonical_start_angle(float angle)3910 static inline SkScalar canonical_start_angle(float angle) {
3911     while (angle < 0.f) {
3912         angle += 360.f;
3913     }
3914     while (angle >= 360.f) {
3915         angle -= 360.f;
3916     }
3917     return angle;
3918 }
3919 
check_oval_arc(skiatest::Reporter * reporter,SkScalar start,SkScalar sweep,const SkPath & path)3920 static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3921                            const SkPath& path) {
3922     SkRect r = SkRect::MakeEmpty();
3923     SkPathDirection d = SkPathDirection::kCCW;
3924     unsigned s = ~0U;
3925     bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
3926     REPORTER_ASSERT(reporter, isOval);
3927     SkPath recreatedPath;
3928     recreatedPath.addOval(r, d, s);
3929     REPORTER_ASSERT(reporter, path == recreatedPath);
3930     REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3931     REPORTER_ASSERT(reporter, (SkPathDirection::kCW == d) == (sweep > 0.f));
3932 }
3933 
test_arc_ovals(skiatest::Reporter * reporter)3934 static void test_arc_ovals(skiatest::Reporter* reporter) {
3935     SkRect oval = SkRect::MakeWH(10, 20);
3936     for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3937         for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3938             SkPath path;
3939             path.addArc(oval, start, sweep);
3940             // SkPath's interfaces for inserting and extracting ovals only allow contours
3941             // to start at multiples of 90 degrees.
3942             if (std::fmod(start, 90.f) == 0) {
3943                 check_oval_arc(reporter, start, sweep, path);
3944             } else {
3945                 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3946             }
3947         }
3948         // Test start angles that are nearly at valid oval start angles.
3949         for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3950             for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3951                 SkPath path;
3952                 path.addArc(oval, start + delta, sweep);
3953                 check_oval_arc(reporter, start, sweep, path);
3954             }
3955         }
3956     }
3957 }
3958 
check_move(skiatest::Reporter * reporter,SkPathPriv::RangeIter * iter,SkScalar x0,SkScalar y0)3959 static void check_move(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
3960                        SkScalar x0, SkScalar y0) {
3961     auto [v, pts, w] = *(*iter)++;
3962     REPORTER_ASSERT(reporter, v == SkPathVerb::kMove);
3963     REPORTER_ASSERT(reporter, pts[0].fX == x0);
3964     REPORTER_ASSERT(reporter, pts[0].fY == y0);
3965 }
3966 
check_line(skiatest::Reporter * reporter,SkPathPriv::RangeIter * iter,SkScalar x1,SkScalar y1)3967 static void check_line(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
3968                        SkScalar x1, SkScalar y1) {
3969     auto [v, pts, w] = *(*iter)++;
3970     REPORTER_ASSERT(reporter, v == SkPathVerb::kLine);
3971     REPORTER_ASSERT(reporter, pts[1].fX == x1);
3972     REPORTER_ASSERT(reporter, pts[1].fY == y1);
3973 }
3974 
check_quad(skiatest::Reporter * reporter,SkPathPriv::RangeIter * iter,SkScalar x1,SkScalar y1,SkScalar x2,SkScalar y2)3975 static void check_quad(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter,
3976                        SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3977     auto [v, pts, w] = *(*iter)++;
3978     REPORTER_ASSERT(reporter, v == SkPathVerb::kQuad);
3979     REPORTER_ASSERT(reporter, pts[1].fX == x1);
3980     REPORTER_ASSERT(reporter, pts[1].fY == y1);
3981     REPORTER_ASSERT(reporter, pts[2].fX == x2);
3982     REPORTER_ASSERT(reporter, pts[2].fY == y2);
3983 }
3984 
check_close(skiatest::Reporter * reporter,SkPathPriv::RangeIter * iter)3985 static void check_close(skiatest::Reporter* reporter, SkPathPriv::RangeIter* iter) {
3986     auto [v, pts, w] = *(*iter)++;
3987     REPORTER_ASSERT(reporter, v == SkPathVerb::kClose);
3988 }
3989 
check_done(skiatest::Reporter * reporter,SkPath * p,SkPathPriv::RangeIter * iter)3990 static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPathPriv::RangeIter* iter) {
3991     REPORTER_ASSERT(reporter, *iter == SkPathPriv::Iterate(*p).end());
3992 }
3993 
check_done_and_reset(skiatest::Reporter * reporter,SkPath * p,SkPathPriv::RangeIter * iter)3994 static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p,
3995                                  SkPathPriv::RangeIter* iter) {
3996     check_done(reporter, p, iter);
3997     p->reset();
3998 }
3999 
check_path_is_move_and_reset(skiatest::Reporter * reporter,SkPath * p,SkScalar x0,SkScalar y0)4000 static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
4001                                          SkScalar x0, SkScalar y0) {
4002     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
4003     check_move(reporter, &iter, x0, y0);
4004     check_done_and_reset(reporter, p, &iter);
4005 }
4006 
check_path_is_line_and_reset(skiatest::Reporter * reporter,SkPath * p,SkScalar x1,SkScalar y1)4007 static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
4008                                          SkScalar x1, SkScalar y1) {
4009     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
4010     check_move(reporter, &iter, 0, 0);
4011     check_line(reporter, &iter, x1, y1);
4012     check_done_and_reset(reporter, p, &iter);
4013 }
4014 
check_path_is_line(skiatest::Reporter * reporter,SkPath * p,SkScalar x1,SkScalar y1)4015 static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
4016                                          SkScalar x1, SkScalar y1) {
4017     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
4018     check_move(reporter, &iter, 0, 0);
4019     check_line(reporter, &iter, x1, y1);
4020     check_done(reporter, p, &iter);
4021 }
4022 
check_path_is_line_pair_and_reset(skiatest::Reporter * reporter,SkPath * p,SkScalar x1,SkScalar y1,SkScalar x2,SkScalar y2)4023 static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
4024                                     SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
4025     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
4026     check_move(reporter, &iter, 0, 0);
4027     check_line(reporter, &iter, x1, y1);
4028     check_line(reporter, &iter, x2, y2);
4029     check_done_and_reset(reporter, p, &iter);
4030 }
4031 
check_path_is_quad_and_reset(skiatest::Reporter * reporter,SkPath * p,SkScalar x1,SkScalar y1,SkScalar x2,SkScalar y2)4032 static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
4033                                     SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
4034     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(*p).begin();
4035     check_move(reporter, &iter, 0, 0);
4036     check_quad(reporter, &iter, x1, y1, x2, y2);
4037     check_done_and_reset(reporter, p, &iter);
4038 }
4039 
nearly_equal(const SkRect & a,const SkRect & b)4040 static bool nearly_equal(const SkRect& a, const SkRect& b) {
4041     return  SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
4042             SkScalarNearlyEqual(a.fTop, b.fTop) &&
4043             SkScalarNearlyEqual(a.fRight, b.fRight) &&
4044             SkScalarNearlyEqual(a.fBottom, b.fBottom);
4045 }
4046 
test_rMoveTo(skiatest::Reporter * reporter)4047 static void test_rMoveTo(skiatest::Reporter* reporter) {
4048     SkPath p;
4049     p.moveTo(10, 11);
4050     p.lineTo(20, 21);
4051     p.close();
4052     p.rMoveTo(30, 31);
4053     SkPathPriv::RangeIter iter = SkPathPriv::Iterate(p).begin();
4054     check_move(reporter, &iter, 10, 11);
4055     check_line(reporter, &iter, 20, 21);
4056     check_close(reporter, &iter);
4057     check_move(reporter, &iter, 10 + 30, 11 + 31);
4058     check_done_and_reset(reporter, &p, &iter);
4059 
4060     p.moveTo(10, 11);
4061     p.lineTo(20, 21);
4062     p.rMoveTo(30, 31);
4063     iter = SkPathPriv::Iterate(p).begin();
4064     check_move(reporter, &iter, 10, 11);
4065     check_line(reporter, &iter, 20, 21);
4066     check_move(reporter, &iter, 20 + 30, 21 + 31);
4067     check_done_and_reset(reporter, &p, &iter);
4068 
4069     p.rMoveTo(30, 31);
4070     iter = SkPathPriv::Iterate(p).begin();
4071     check_move(reporter, &iter, 30, 31);
4072     check_done_and_reset(reporter, &p, &iter);
4073 }
4074 
test_arcTo(skiatest::Reporter * reporter)4075 static void test_arcTo(skiatest::Reporter* reporter) {
4076     SkPath p;
4077     p.arcTo(0, 0, 1, 2, 1);
4078     check_path_is_line_and_reset(reporter, &p, 0, 0);
4079     p.arcTo(1, 2, 1, 2, 1);
4080     check_path_is_line_and_reset(reporter, &p, 1, 2);
4081     p.arcTo(1, 2, 3, 4, 0);
4082     check_path_is_line_and_reset(reporter, &p, 1, 2);
4083     p.arcTo(1, 2, 0, 0, 1);
4084     check_path_is_line_and_reset(reporter, &p, 1, 2);
4085     p.arcTo(1, 0, 1, 1, 1);
4086     SkPoint pt;
4087     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
4088     p.reset();
4089     p.arcTo(1, 0, 1, -1, 1);
4090     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
4091     p.reset();
4092     SkRect oval = {1, 2, 3, 4};
4093     p.arcTo(oval, 0, 0, true);
4094     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
4095     p.arcTo(oval, 0, 0, false);
4096     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
4097     p.arcTo(oval, 360, 0, true);
4098     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
4099     p.arcTo(oval, 360, 0, false);
4100     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
4101 
4102     for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
4103         p.arcTo(oval, 0, sweep, false);
4104         REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
4105         sweep += delta;
4106         delta /= 2;
4107     }
4108     for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
4109         p.arcTo(oval, 0, sweep, false);
4110         REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
4111         sweep -= delta;
4112         delta /= 2;
4113     }
4114     SkRect noOvalWidth = {1, 2, 0, 3};
4115     p.reset();
4116     p.arcTo(noOvalWidth, 0, 360, false);
4117     REPORTER_ASSERT(reporter, p.isEmpty());
4118 
4119     SkRect noOvalHeight = {1, 2, 3, 1};
4120     p.reset();
4121     p.arcTo(noOvalHeight, 0, 360, false);
4122     REPORTER_ASSERT(reporter, p.isEmpty());
4123 
4124     // Inspired by http://code.google.com/p/chromium/issues/detail?id=1001768
4125     {
4126       p.reset();
4127       p.moveTo(216, 216);
4128       p.arcTo(216, 108, 0, SkPath::ArcSize::kLarge_ArcSize, SkPathDirection::kCW, 216, 0);
4129       p.arcTo(270, 135, 0, SkPath::ArcSize::kLarge_ArcSize, SkPathDirection::kCCW, 216, 216);
4130 
4131       // The 'arcTo' call should end up exactly at the starting location.
4132       int n = p.countPoints();
4133       REPORTER_ASSERT(reporter, p.getPoint(0) == p.getPoint(n - 1));
4134     }
4135 
4136     // This test, if improperly handled, can create an infinite loop in angles_to_unit_vectors
4137     p.reset();
4138     p.arcTo(SkRect::MakeXYWH(0, 0, 10, 10), -2.61488527e+33f, 359.992157f, false);
4139 }
4140 
test_addPath(skiatest::Reporter * reporter)4141 static void test_addPath(skiatest::Reporter* reporter) {
4142     SkPath p, q;
4143     p.lineTo(1, 2);
4144     q.moveTo(4, 4);
4145     q.lineTo(7, 8);
4146     q.conicTo(8, 7, 6, 5, 0.5f);
4147     q.quadTo(6, 7, 8, 6);
4148     q.cubicTo(5, 6, 7, 8, 7, 5);
4149     q.close();
4150     p.addPath(q, -4, -4);
4151     SkRect expected = {0, 0, 4, 4};
4152     REPORTER_ASSERT(reporter, p.getBounds() == expected);
4153     p.reset();
4154     p.reverseAddPath(q);
4155     SkRect reverseExpected = {4, 4, 8, 8};
4156     REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4157 }
4158 
test_addPathMode(skiatest::Reporter * reporter,bool explicitMoveTo,bool extend)4159 static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
4160     SkPath p, q;
4161     if (explicitMoveTo) {
4162         p.moveTo(1, 1);
4163     }
4164     p.lineTo(1, 2);
4165     if (explicitMoveTo) {
4166         q.moveTo(2, 1);
4167     }
4168     q.lineTo(2, 2);
4169     p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
4170     uint8_t verbs[4];
4171     int verbcount = p.getVerbs(verbs, 4);
4172     REPORTER_ASSERT(reporter, verbcount == 4);
4173     REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
4174     REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
4175     REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
4176     REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
4177 }
4178 
test_extendClosedPath(skiatest::Reporter * reporter)4179 static void test_extendClosedPath(skiatest::Reporter* reporter) {
4180     SkPath p, q;
4181     p.moveTo(1, 1);
4182     p.lineTo(1, 2);
4183     p.lineTo(2, 2);
4184     p.close();
4185     q.moveTo(2, 1);
4186     q.lineTo(2, 3);
4187     p.addPath(q, SkPath::kExtend_AddPathMode);
4188     uint8_t verbs[7];
4189     int verbcount = p.getVerbs(verbs, 7);
4190     REPORTER_ASSERT(reporter, verbcount == 7);
4191     REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
4192     REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
4193     REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
4194     REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
4195     REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
4196     REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
4197     REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
4198 
4199     SkPoint pt;
4200     REPORTER_ASSERT(reporter, p.getLastPt(&pt));
4201     REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
4202     REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
4203 }
4204 
test_addEmptyPath(skiatest::Reporter * reporter,SkPath::AddPathMode mode)4205 static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
4206     SkPath p, q, r;
4207     // case 1: dst is empty
4208     p.moveTo(2, 1);
4209     p.lineTo(2, 3);
4210     q.addPath(p, mode);
4211     REPORTER_ASSERT(reporter, q == p);
4212     // case 2: src is empty
4213     p.addPath(r, mode);
4214     REPORTER_ASSERT(reporter, q == p);
4215     // case 3: src and dst are empty
4216     q.reset();
4217     q.addPath(r, mode);
4218     REPORTER_ASSERT(reporter, q.isEmpty());
4219 }
4220 
test_conicTo_special_case(skiatest::Reporter * reporter)4221 static void test_conicTo_special_case(skiatest::Reporter* reporter) {
4222     SkPath p;
4223     p.conicTo(1, 2, 3, 4, -1);
4224     check_path_is_line_and_reset(reporter, &p, 3, 4);
4225     p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
4226     check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
4227     p.conicTo(1, 2, 3, 4, 1);
4228     check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
4229 }
4230 
test_get_point(skiatest::Reporter * reporter)4231 static void test_get_point(skiatest::Reporter* reporter) {
4232     SkPath p;
4233     SkPoint pt = p.getPoint(0);
4234     REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
4235     REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
4236     REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
4237     p.setLastPt(10, 10);
4238     pt = p.getPoint(0);
4239     REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
4240     REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
4241     p.rMoveTo(10, 10);
4242     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4243 }
4244 
test_contains(skiatest::Reporter * reporter)4245 static void test_contains(skiatest::Reporter* reporter) {
4246     SkPath p;
4247     p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00));  // -7.7191e+19f, 1.50724e+19f
4248     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
4249     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
4250     p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8));  // 8.96947e+19f, -1.75139e+19f
4251     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
4252     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
4253     // this may return true or false, depending on the platform's numerics, but it should not crash
4254     (void) p.contains(-77.2027664f, 15.3066053f);
4255 
4256     p.reset();
4257     p.setFillType(SkPathFillType::kInverseWinding);
4258     REPORTER_ASSERT(reporter, p.contains(0, 0));
4259     p.setFillType(SkPathFillType::kWinding);
4260     REPORTER_ASSERT(reporter, !p.contains(0, 0));
4261     p.moveTo(4, 4);
4262     p.lineTo(6, 8);
4263     p.lineTo(8, 4);
4264     // test on edge
4265     REPORTER_ASSERT(reporter, p.contains(6, 4));
4266     REPORTER_ASSERT(reporter, p.contains(5, 6));
4267     REPORTER_ASSERT(reporter, p.contains(7, 6));
4268     // test quick reject
4269     REPORTER_ASSERT(reporter, !p.contains(4, 0));
4270     REPORTER_ASSERT(reporter, !p.contains(0, 4));
4271     REPORTER_ASSERT(reporter, !p.contains(4, 10));
4272     REPORTER_ASSERT(reporter, !p.contains(10, 4));
4273     // test various crossings in x
4274     REPORTER_ASSERT(reporter, !p.contains(5, 7));
4275     REPORTER_ASSERT(reporter, p.contains(6, 7));
4276     REPORTER_ASSERT(reporter, !p.contains(7, 7));
4277     p.reset();
4278     p.moveTo(4, 4);
4279     p.lineTo(8, 6);
4280     p.lineTo(4, 8);
4281     // test on edge
4282     REPORTER_ASSERT(reporter, p.contains(4, 6));
4283     REPORTER_ASSERT(reporter, p.contains(6, 5));
4284     REPORTER_ASSERT(reporter, p.contains(6, 7));
4285     // test various crossings in y
4286     REPORTER_ASSERT(reporter, !p.contains(7, 5));
4287     REPORTER_ASSERT(reporter, p.contains(7, 6));
4288     REPORTER_ASSERT(reporter, !p.contains(7, 7));
4289     p.reset();
4290     p.moveTo(4, 4);
4291     p.lineTo(8, 4);
4292     p.lineTo(8, 8);
4293     p.lineTo(4, 8);
4294     // test on vertices
4295     REPORTER_ASSERT(reporter, p.contains(4, 4));
4296     REPORTER_ASSERT(reporter, p.contains(8, 4));
4297     REPORTER_ASSERT(reporter, p.contains(8, 8));
4298     REPORTER_ASSERT(reporter, p.contains(4, 8));
4299     p.reset();
4300     p.moveTo(4, 4);
4301     p.lineTo(6, 8);
4302     p.lineTo(2, 8);
4303     // test on edge
4304     REPORTER_ASSERT(reporter, p.contains(5, 6));
4305     REPORTER_ASSERT(reporter, p.contains(4, 8));
4306     REPORTER_ASSERT(reporter, p.contains(3, 6));
4307     p.reset();
4308     p.moveTo(4, 4);
4309     p.lineTo(0, 6);
4310     p.lineTo(4, 8);
4311     // test on edge
4312     REPORTER_ASSERT(reporter, p.contains(2, 5));
4313     REPORTER_ASSERT(reporter, p.contains(2, 7));
4314     REPORTER_ASSERT(reporter, p.contains(4, 6));
4315     // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4316     p.reset();
4317     p.moveTo(4, 0);
4318     p.lineTo(6, 4);
4319     p.lineTo(2, 4);
4320     p.moveTo(4, 0);
4321     p.lineTo(0, 8);
4322     p.lineTo(8, 8);
4323     REPORTER_ASSERT(reporter, !p.contains(1, 2));
4324     REPORTER_ASSERT(reporter, !p.contains(3, 2));
4325     REPORTER_ASSERT(reporter, !p.contains(4, 0));
4326     REPORTER_ASSERT(reporter, p.contains(4, 4));
4327 
4328     // test quads
4329     p.reset();
4330     p.moveTo(4, 4);
4331     p.quadTo(6, 6, 8, 8);
4332     p.quadTo(6, 8, 4, 8);
4333     p.quadTo(4, 6, 4, 4);
4334     REPORTER_ASSERT(reporter, p.contains(5, 6));
4335     REPORTER_ASSERT(reporter, !p.contains(6, 5));
4336     // test quad edge
4337     REPORTER_ASSERT(reporter, p.contains(5, 5));
4338     REPORTER_ASSERT(reporter, p.contains(5, 8));
4339     REPORTER_ASSERT(reporter, p.contains(4, 5));
4340     // test quad endpoints
4341     REPORTER_ASSERT(reporter, p.contains(4, 4));
4342     REPORTER_ASSERT(reporter, p.contains(8, 8));
4343     REPORTER_ASSERT(reporter, p.contains(4, 8));
4344 
4345     p.reset();
4346     const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4347     p.moveTo(qPts[0]);
4348     for (int index = 1; index < (int) std::size(qPts); index += 2) {
4349         p.quadTo(qPts[index], qPts[index + 1]);
4350     }
4351     REPORTER_ASSERT(reporter, p.contains(5, 6));
4352     REPORTER_ASSERT(reporter, !p.contains(6, 5));
4353     // test quad edge
4354     SkPoint halfway;
4355     for (int index = 0; index < (int) std::size(qPts) - 2; index += 2) {
4356         SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4357         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4358     }
4359 
4360     // test conics
4361     p.reset();
4362     const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4363     p.moveTo(kPts[0]);
4364     for (int index = 1; index < (int) std::size(kPts); index += 2) {
4365         p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4366     }
4367     REPORTER_ASSERT(reporter, p.contains(5, 6));
4368     REPORTER_ASSERT(reporter, !p.contains(6, 5));
4369     // test conic edge
4370     for (int index = 0; index < (int) std::size(kPts) - 2; index += 2) {
4371         SkConic conic(&kPts[index], 0.5f);
4372         halfway = conic.evalAt(0.5f);
4373         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4374     }
4375     // test conic end points
4376     REPORTER_ASSERT(reporter, p.contains(4, 4));
4377     REPORTER_ASSERT(reporter, p.contains(8, 8));
4378     REPORTER_ASSERT(reporter, p.contains(4, 8));
4379 
4380     // test cubics
4381     SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4382     for (int i = 0; i < 3; ++i) {
4383         p.reset();
4384         p.setFillType(SkPathFillType::kEvenOdd);
4385         p.moveTo(pts[i].fX, pts[i].fY);
4386         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);
4387         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);
4388         p.close();
4389         REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4390         REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4391         // test cubic edge
4392         SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4393         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4394         SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4395         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4396         // test cubic end points
4397         REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4398         REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4399         REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4400     }
4401 }
4402 
4403 class PathRefTest_Private {
4404 public:
GetFreeSpace(const SkPathRef & ref)4405     static size_t GetFreeSpace(const SkPathRef& ref) {
4406         return   (ref.fPoints.capacity() - ref.fPoints.size()) * sizeof(SkPoint)
4407                + (ref.fVerbs.capacity()  - ref.fVerbs.size())  * sizeof(uint8_t);
4408     }
4409 
TestPathRef(skiatest::Reporter * reporter)4410     static void TestPathRef(skiatest::Reporter* reporter) {
4411         static const int kRepeatCnt = 10;
4412 
4413         sk_sp<SkPathRef> pathRef(new SkPathRef);
4414 
4415         SkPathRef::Editor ed(&pathRef);
4416 
4417         {
4418             ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4419             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4420             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4421             REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4422             for (int i = 0; i < kRepeatCnt; ++i) {
4423                 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4424             }
4425             ed.resetToSize(0, 0, 0);
4426         }
4427 
4428         {
4429             ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4430             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4431             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4432             REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4433             for (int i = 0; i < kRepeatCnt; ++i) {
4434                 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4435             }
4436             ed.resetToSize(0, 0, 0);
4437         }
4438 
4439         {
4440             ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4441             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4442             REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4443             REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4444             for (int i = 0; i < kRepeatCnt; ++i) {
4445                 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4446             }
4447             ed.resetToSize(0, 0, 0);
4448         }
4449 
4450         {
4451             SkScalar* weights = nullptr;
4452             ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4453             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4454             REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4455             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4456             REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4457             REPORTER_ASSERT(reporter, weights);
4458             for (int i = 0; i < kRepeatCnt; ++i) {
4459                 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4460             }
4461             ed.resetToSize(0, 0, 0);
4462         }
4463 
4464         {
4465             ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4466             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4467             REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4468             REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4469             for (int i = 0; i < kRepeatCnt; ++i) {
4470                 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4471             }
4472             ed.resetToSize(0, 0, 0);
4473         }
4474     }
4475 };
4476 
test_operatorEqual(skiatest::Reporter * reporter)4477 static void test_operatorEqual(skiatest::Reporter* reporter) {
4478     SkPath a;
4479     SkPath b;
4480     REPORTER_ASSERT(reporter, a == a);
4481     REPORTER_ASSERT(reporter, a == b);
4482     a.setFillType(SkPathFillType::kInverseWinding);
4483     REPORTER_ASSERT(reporter, a != b);
4484     a.reset();
4485     REPORTER_ASSERT(reporter, a == b);
4486     a.lineTo(1, 1);
4487     REPORTER_ASSERT(reporter, a != b);
4488     a.reset();
4489     REPORTER_ASSERT(reporter, a == b);
4490     a.lineTo(1, 1);
4491     b.lineTo(1, 2);
4492     REPORTER_ASSERT(reporter, a != b);
4493     a.reset();
4494     a.lineTo(1, 2);
4495     REPORTER_ASSERT(reporter, a == b);
4496 }
4497 
compare_dump(skiatest::Reporter * reporter,const SkPath & path,bool dumpAsHex,const char * str)4498 static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool dumpAsHex,
4499                          const char* str) {
4500     SkDynamicMemoryWStream wStream;
4501     path.dump(&wStream, dumpAsHex);
4502     sk_sp<SkData> data = wStream.detachAsData();
4503     REPORTER_ASSERT(reporter, data->size() == strlen(str));
4504     if (strlen(str) > 0) {
4505         REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4506     } else {
4507         REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4508     }
4509 }
4510 
test_dump(skiatest::Reporter * reporter)4511 static void test_dump(skiatest::Reporter* reporter) {
4512     SkPath p;
4513     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n");
4514     p.moveTo(1, 2);
4515     p.lineTo(3, 4);
4516     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n"
4517                                             "path.moveTo(1, 2);\n"
4518                                             "path.lineTo(3, 4);\n");
4519     p.reset();
4520     p.setFillType(SkPathFillType::kEvenOdd);
4521     p.moveTo(1, 2);
4522     p.quadTo(3, 4, 5, 6);
4523     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kEvenOdd);\n"
4524                                             "path.moveTo(1, 2);\n"
4525                                             "path.quadTo(3, 4, 5, 6);\n");
4526     p.reset();
4527     p.setFillType(SkPathFillType::kInverseWinding);
4528     p.moveTo(1, 2);
4529     p.conicTo(3, 4, 5, 6, 0.5f);
4530     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kInverseWinding);\n"
4531                                             "path.moveTo(1, 2);\n"
4532                                             "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4533     p.reset();
4534     p.setFillType(SkPathFillType::kInverseEvenOdd);
4535     p.moveTo(1, 2);
4536     p.cubicTo(3, 4, 5, 6, 7, 8);
4537     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kInverseEvenOdd);\n"
4538                                             "path.moveTo(1, 2);\n"
4539                                             "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4540     p.reset();
4541     p.setFillType(SkPathFillType::kWinding);
4542     p.moveTo(1, 2);
4543     p.lineTo(3, 4);
4544     compare_dump(reporter, p, true,
4545                  "path.setFillType(SkPathFillType::kWinding);\n"
4546                  "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));  // 1, 2\n"
4547                  "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));  // 3, 4\n");
4548     p.reset();
4549     p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4550     p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4551     compare_dump(reporter, p, false, "path.setFillType(SkPathFillType::kWinding);\n"
4552                                             "path.moveTo(1, 2);\n"
4553                                             "path.lineTo(3, 4);\n");
4554 }
4555 
4556 namespace {
4557 
4558 class ChangeListener : public SkIDChangeListener {
4559 public:
ChangeListener(bool * changed)4560     ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
~ChangeListener()4561     ~ChangeListener() override {}
changed()4562     void changed() override { *fChanged = true; }
4563 
4564 private:
4565     bool* fChanged;
4566 };
4567 
4568 }  // namespace
4569 
4570 class PathTest_Private {
4571 public:
GetFreeSpace(const SkPath & path)4572     static size_t GetFreeSpace(const SkPath& path) {
4573         return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
4574     }
4575 
TestPathTo(skiatest::Reporter * reporter)4576     static void TestPathTo(skiatest::Reporter* reporter) {
4577         SkPath p, q;
4578         p.lineTo(4, 4);
4579         p.reversePathTo(q);
4580         check_path_is_line(reporter, &p, 4, 4);
4581         q.moveTo(-4, -4);
4582         p.reversePathTo(q);
4583         check_path_is_line(reporter, &p, 4, 4);
4584         q.lineTo(7, 8);
4585         q.conicTo(8, 7, 6, 5, 0.5f);
4586         q.quadTo(6, 7, 8, 6);
4587         q.cubicTo(5, 6, 7, 8, 7, 5);
4588         q.close();
4589         p.reversePathTo(q);
4590         SkRect reverseExpected = {-4, -4, 8, 8};
4591         REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4592     }
4593 
TestPathrefListeners(skiatest::Reporter * reporter)4594     static void TestPathrefListeners(skiatest::Reporter* reporter) {
4595         SkPath p;
4596 
4597         bool changed = false;
4598         p.moveTo(0, 0);
4599 
4600         // Check that listener is notified on moveTo().
4601 
4602         SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
4603         REPORTER_ASSERT(reporter, !changed);
4604         p.moveTo(10, 0);
4605         REPORTER_ASSERT(reporter, changed);
4606 
4607         // Check that listener is notified on lineTo().
4608         SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
4609         REPORTER_ASSERT(reporter, !changed);
4610         p.lineTo(20, 0);
4611         REPORTER_ASSERT(reporter, changed);
4612 
4613         // Check that listener is notified on reset().
4614         SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
4615         REPORTER_ASSERT(reporter, !changed);
4616         p.reset();
4617         REPORTER_ASSERT(reporter, changed);
4618 
4619         p.moveTo(0, 0);
4620 
4621         // Check that listener is notified on rewind().
4622         SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
4623         REPORTER_ASSERT(reporter, !changed);
4624         p.rewind();
4625         REPORTER_ASSERT(reporter, changed);
4626 
4627         // Check that listener is notified on transform().
4628         {
4629             SkPath q;
4630             q.moveTo(10, 10);
4631             SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4632             REPORTER_ASSERT(reporter, !changed);
4633             SkMatrix matrix;
4634             matrix.setScale(2, 2);
4635             p.transform(matrix, &q);
4636             REPORTER_ASSERT(reporter, changed);
4637         }
4638 
4639         // Check that listener is notified when pathref is deleted.
4640         {
4641             SkPath q;
4642             q.moveTo(10, 10);
4643             SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4644             REPORTER_ASSERT(reporter, !changed);
4645         }
4646         // q went out of scope.
4647         REPORTER_ASSERT(reporter, changed);
4648     }
4649 };
4650 
test_crbug_629455(skiatest::Reporter * reporter)4651 static void test_crbug_629455(skiatest::Reporter* reporter) {
4652     SkPath path;
4653     path.moveTo(0, 0);
4654     path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4655                  SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4656                  SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4657 //  AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4658     path.lineTo(0, 0);
4659     test_draw_AA_path(100, 100, path);
4660 }
4661 
test_fuzz_crbug_662952(skiatest::Reporter * reporter)4662 static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4663     SkPath path;
4664     path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000));  // 8.6f, 9.75f
4665     path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000));  // 8.65f, 9.75f
4666     path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666));  // 8.65f, 9.9f
4667     path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666));  // 8.6f, 9.9f
4668     path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000));  // 8.6f, 9.75f
4669     path.close();
4670 
4671     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
4672     SkPaint paint;
4673     paint.setAntiAlias(true);
4674     surface->getCanvas()->clipPath(path, true);
4675     surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4676 }
4677 
test_path_crbugskia6003()4678 static void test_path_crbugskia6003() {
4679     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(500, 500)));
4680     SkCanvas* canvas = surface->getCanvas();
4681     SkPaint paint;
4682     paint.setAntiAlias(true);
4683     SkPath path;
4684     path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a));  // 165.9f, 80.8f
4685     path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a));  // 165.9f, 81.3f
4686     path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a));  // 165.7f, 81.3f
4687     path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666));  // 165.7f, 80.7f
4688     path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666));  // 165.7f, 79.7f
4689     // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4690     path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4691             SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4692     // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4693     path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4694             SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4695     path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a));  // 165.9f, 80.8f
4696     path.close();
4697     canvas->clipPath(path, true);
4698     canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4699 }
4700 
test_fuzz_crbug_662730(skiatest::Reporter * reporter)4701 static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4702     SkPath path;
4703     path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000));  // 0, 0
4704     path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737));  // -1.2731e+13f, 1.09205e-05f
4705     path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737));  // 1.09205e-05f, 1.09205e-05f
4706     path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800));  // 1.09205e-05f, 1.57842e-40f
4707     path.close();
4708     test_draw_AA_path(100, 100, path);
4709 }
4710 
test_skbug_6947()4711 static void test_skbug_6947() {
4712     SkPath path;
4713     SkPoint points[] =
4714         {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4715          {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4716          {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4717          {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4718          {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4719          {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4720          {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4721          {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4722          {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4723          {125.119476f, 1.50011659f}};
4724     constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4725     constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4726     constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4727     SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4728             kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4729     int pointIndex = 0;
4730     for(auto verb : verbs) {
4731         switch (verb) {
4732             case kMove:
4733                 path.moveTo(points[pointIndex++]);
4734                 break;
4735             case kLine:
4736                 path.lineTo(points[pointIndex++]);
4737                 break;
4738             case kClose:
4739             default:
4740                 path.close();
4741                 break;
4742         }
4743     }
4744     test_draw_AA_path(250, 125, path);
4745 }
4746 
test_skbug_7015()4747 static void test_skbug_7015() {
4748     SkPath path;
4749     path.setFillType(SkPathFillType::kWinding);
4750     path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));  // 273.5f, 296.969f
4751     path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08));  // 269.5f, 296.969f
4752     // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4753     path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4754             SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4755             SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4756     path.close();
4757     test_draw_AA_path(500, 500, path);
4758 }
4759 
test_skbug_7051()4760 static void test_skbug_7051() {
4761     SkPath path;
4762     path.moveTo(10, 10);
4763     path.cubicTo(10, 20, 10, 30, 30, 30);
4764     path.lineTo(50, 20);
4765     path.lineTo(50, 10);
4766     path.close();
4767     test_draw_AA_path(100, 100, path);
4768 }
4769 
test_skbug_7435()4770 static void test_skbug_7435() {
4771     SkPaint paint;
4772     SkPath path;
4773     path.setFillType(SkPathFillType::kWinding);
4774     path.moveTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d));  // 1.80306e+38f, -1.8077e+38f
4775     path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a));  // 1.48404e+38f, -1.48868e+38f
4776     path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2));  // 1.48389e+38f, -1.48883e+38f
4777     path.lineTo(SkBits2Float(0x7ef348e9), SkBits2Float(0xfef403c6));  // 1.6169e+38f, -1.62176e+38f
4778     path.lineTo(SkBits2Float(0x7ef74c4e), SkBits2Float(0xfef803cb));  // 1.64358e+38f, -1.64834e+38f
4779     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
4780     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
4781     path.lineTo(SkBits2Float(0x7edb57a9), SkBits2Float(0xfedbfe06));  // 1.45778e+38f, -1.4621e+38f
4782     path.lineTo(SkBits2Float(0x7e875976), SkBits2Float(0xfe87fdb3));  // 8.99551e+37f, -9.03815e+37f
4783     path.lineTo(SkBits2Float(0x7ded5c2b), SkBits2Float(0xfdeff59e));  // 3.94382e+37f, -3.98701e+37f
4784     path.lineTo(SkBits2Float(0x7d7a78a7), SkBits2Float(0xfd7fda0f));  // 2.08083e+37f, -2.12553e+37f
4785     path.lineTo(SkBits2Float(0x7d7a6403), SkBits2Float(0xfd7fe461));  // 2.08016e+37f, -2.12587e+37f
4786     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
4787     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
4788     path.lineTo(SkBits2Float(0x7d8d2067), SkBits2Float(0xfd900bdb));  // 2.34487e+37f, -2.39338e+37f
4789     path.lineTo(SkBits2Float(0x7ddd137a), SkBits2Float(0xfde00c2d));  // 3.67326e+37f, -3.72263e+37f
4790     path.lineTo(SkBits2Float(0x7ddd2a1b), SkBits2Float(0xfddff58e));  // 3.67473e+37f, -3.72116e+37f
4791     path.lineTo(SkBits2Float(0x7c694ae5), SkBits2Float(0xfc7fa67c));  // 4.8453e+36f, -5.30965e+36f
4792     path.lineTo(SkBits2Float(0xfc164a8b), SkBits2Float(0x7c005af5));  // -3.12143e+36f, 2.66584e+36f
4793     path.lineTo(SkBits2Float(0xfc8ae983), SkBits2Float(0x7c802da7));  // -5.77019e+36f, 5.32432e+36f
4794     path.lineTo(SkBits2Float(0xfc8b16d9), SkBits2Float(0x7c80007b));  // -5.77754e+36f, 5.31699e+36f
4795     path.lineTo(SkBits2Float(0xfc8b029c), SkBits2Float(0x7c7f8788));  // -5.77426e+36f, 5.30714e+36f
4796     path.lineTo(SkBits2Float(0xfc8b0290), SkBits2Float(0x7c7f8790));  // -5.77425e+36f, 5.30714e+36f
4797     path.lineTo(SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f));  // -5.77753e+36f, 5.31699e+36f
4798     path.lineTo(SkBits2Float(0xfc8b4409), SkBits2Float(0x7c7fa672));  // -5.78487e+36f, 5.30965e+36f
4799     path.lineTo(SkBits2Float(0x7d7aa2ba), SkBits2Float(0xfd800bd1));  // 2.0822e+37f, -2.12753e+37f
4800     path.lineTo(SkBits2Float(0x7e8757ee), SkBits2Float(0xfe88035b));  // 8.99512e+37f, -9.03962e+37f
4801     path.lineTo(SkBits2Float(0x7ef7552d), SkBits2Float(0xfef803ca));  // 1.64381e+38f, -1.64834e+38f
4802     path.lineTo(SkBits2Float(0x7f0fa653), SkBits2Float(0xff1001f9));  // 1.90943e+38f, -1.91419e+38f
4803     path.lineTo(SkBits2Float(0x7f0fa926), SkBits2Float(0xff0fff24));  // 1.90958e+38f, -1.91404e+38f
4804     path.lineTo(SkBits2Float(0x7f0da75c), SkBits2Float(0xff0dff22));  // 1.8829e+38f, -1.88746e+38f
4805     path.lineTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d));  // 1.80306e+38f, -1.8077e+38f
4806     path.close();
4807     path.moveTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1));  // 1.80291e+38f, -1.80785e+38f
4808     path.lineTo(SkBits2Float(0x7f0da48a), SkBits2Float(0xff0e01f8));  // 1.88275e+38f, -1.88761e+38f
4809     path.lineTo(SkBits2Float(0x7f0fa654), SkBits2Float(0xff1001fa));  // 1.90943e+38f, -1.91419e+38f
4810     path.lineTo(SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f));  // 1.90951e+38f, -1.91412e+38f
4811     path.lineTo(SkBits2Float(0x7f0fa927), SkBits2Float(0xff0fff25));  // 1.90958e+38f, -1.91404e+38f
4812     path.lineTo(SkBits2Float(0x7ef75ad5), SkBits2Float(0xfef7fe22));  // 1.64395e+38f, -1.64819e+38f
4813     path.lineTo(SkBits2Float(0x7e875d96), SkBits2Float(0xfe87fdb3));  // 8.99659e+37f, -9.03815e+37f
4814     path.lineTo(SkBits2Float(0x7d7acff6), SkBits2Float(0xfd7fea5b));  // 2.08367e+37f, -2.12606e+37f
4815     path.lineTo(SkBits2Float(0xfc8b0588), SkBits2Float(0x7c8049b7));  // -5.77473e+36f, 5.32887e+36f
4816     path.lineTo(SkBits2Float(0xfc8b2b16), SkBits2Float(0x7c803d32));  // -5.78083e+36f, 5.32684e+36f
4817     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
4818     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
4819     path.lineTo(SkBits2Float(0xfc16ffaa), SkBits2Float(0x7bff4c12));  // -3.13612e+36f, 2.65116e+36f
4820     path.lineTo(SkBits2Float(0x7c6895e0), SkBits2Float(0xfc802dc0));  // 4.83061e+36f, -5.32434e+36f
4821     path.lineTo(SkBits2Float(0x7ddd137b), SkBits2Float(0xfde00c2e));  // 3.67326e+37f, -3.72263e+37f
4822     path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de));  // 3.67399e+37f, -3.72189e+37f
4823     path.lineTo(SkBits2Float(0x7ddd2a1c), SkBits2Float(0xfddff58f));  // 3.67473e+37f, -3.72116e+37f
4824     path.lineTo(SkBits2Float(0x7d8d3711), SkBits2Float(0xfd8ff543));  // 2.34634e+37f, -2.39191e+37f
4825     path.lineTo(SkBits2Float(0x7d7a88fe), SkBits2Float(0xfd7fea69));  // 2.08136e+37f, -2.12606e+37f
4826     path.lineTo(SkBits2Float(0x7d7a7254), SkBits2Float(0xfd800080));  // 2.08063e+37f, -2.1268e+37f
4827     path.lineTo(SkBits2Float(0x7d7a80a4), SkBits2Float(0xfd800ed0));  // 2.08109e+37f, -2.12773e+37f
4828     path.lineTo(SkBits2Float(0x7d7a80a8), SkBits2Float(0xfd800ecf));  // 2.08109e+37f, -2.12773e+37f
4829     path.lineTo(SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f));  // 2.08063e+37f, -2.1268e+37f
4830     path.lineTo(SkBits2Float(0x7d7a5bb9), SkBits2Float(0xfd800bd0));  // 2.0799e+37f, -2.12753e+37f
4831     path.lineTo(SkBits2Float(0x7ded458b), SkBits2Float(0xfdf00c3e));  // 3.94235e+37f, -3.98848e+37f
4832     path.lineTo(SkBits2Float(0x7e8753ce), SkBits2Float(0xfe88035b));  // 8.99405e+37f, -9.03962e+37f
4833     path.lineTo(SkBits2Float(0x7edb5201), SkBits2Float(0xfedc03ae));  // 1.45763e+38f, -1.46225e+38f
4834     path.lineTo(SkBits2Float(0x7ef74c4d), SkBits2Float(0xfef803ca));  // 1.64358e+38f, -1.64834e+38f
4835     path.lineTo(SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6));  // 1.64365e+38f, -1.64827e+38f
4836     path.lineTo(SkBits2Float(0x7ef751f4), SkBits2Float(0xfef7fe21));  // 1.64372e+38f, -1.64819e+38f
4837     path.lineTo(SkBits2Float(0x7ef34e91), SkBits2Float(0xfef3fe1e));  // 1.61705e+38f, -1.62161e+38f
4838     path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a));  // 1.48404e+38f, -1.48868e+38f
4839     path.lineTo(SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de));  // 1.48397e+38f, -1.48876e+38f
4840     path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2));  // 1.48389e+38f, -1.48883e+38f
4841     path.lineTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1));  // 1.80291e+38f, -1.80785e+38f
4842     path.close();
4843     path.moveTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f));  // -4.59851e+35f, 7.34402e+33f
4844     path.lineTo(SkBits2Float(0xfd6597e5), SkBits2Float(0x7d60177f));  // -1.90739e+37f, 1.86168e+37f
4845     path.lineTo(SkBits2Float(0xfde2cea1), SkBits2Float(0x7de00c2e));  // -3.76848e+37f, 3.72263e+37f
4846     path.lineTo(SkBits2Float(0xfe316511), SkBits2Float(0x7e300657));  // -5.89495e+37f, 5.84943e+37f
4847     path.lineTo(SkBits2Float(0xfe415da1), SkBits2Float(0x7e400666));  // -6.42568e+37f, 6.38112e+37f
4848     path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4000be));  // -6.42641e+37f, 6.38039e+37f
4849     path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e3ff8be));  // -6.42641e+37f, 6.37935e+37f
4850     path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e3ff8be));  // -6.42641e+37f, 6.37935e+37f
4851     path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e3ff8be));  // -6.42591e+37f, 6.37935e+37f
4852     path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e3ff8be));  // -6.42544e+37f, 6.37935e+37f
4853     path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be));  // -6.42544e+37f, 6.38039e+37f
4854     path.lineTo(SkBits2Float(0xfe416171), SkBits2Float(0x7e3ffb16));  // -6.42617e+37f, 6.37966e+37f
4855     path.lineTo(SkBits2Float(0xfe016131), SkBits2Float(0x7dfff5ae));  // -4.29938e+37f, 4.25286e+37f
4856     path.lineTo(SkBits2Float(0xfe0155e2), SkBits2Float(0x7e000628));  // -4.29791e+37f, 4.25433e+37f
4857     path.lineTo(SkBits2Float(0xfe0958ea), SkBits2Float(0x7e080630));  // -4.56415e+37f, 4.52018e+37f
4858     path.lineTo(SkBits2Float(0xfe115c92), SkBits2Float(0x7e100638));  // -4.83047e+37f, 4.78603e+37f
4859     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
4860     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
4861     path.lineTo(SkBits2Float(0xfe016b92), SkBits2Float(0x7dfff5af));  // -4.30072e+37f, 4.25286e+37f
4862     path.lineTo(SkBits2Float(0xfdc2d963), SkBits2Float(0x7dbff56e));  // -3.23749e+37f, 3.18946e+37f
4863     path.lineTo(SkBits2Float(0xfd65ae25), SkBits2Float(0x7d5fea3d));  // -1.90811e+37f, 1.86021e+37f
4864     path.lineTo(SkBits2Float(0xfab448de), SkBits2Float(0xf7b50a19));  // -4.68046e+35f, -7.34383e+33f
4865     path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000));  // -4.60703e+35f, 200
4866     path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x7800007f));  // -4.60703e+35f, 1.03848e+34f
4867     path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x7800007f));  // -4.67194e+35f, 1.03848e+34f
4868     path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000));  // -4.67194e+35f, 200
4869     path.lineTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f));  // -4.59851e+35f, 7.34402e+33f
4870     path.close();
4871     path.moveTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e));  // -4.71494e+35f, -1.03847e+34f
4872     path.lineTo(SkBits2Float(0xfaa7cc52), SkBits2Float(0xf800007f));  // -4.35629e+35f, -1.03848e+34f
4873     path.lineTo(SkBits2Float(0xfd6580e5), SkBits2Float(0x7d60177f));  // -1.90664e+37f, 1.86168e+37f
4874     path.lineTo(SkBits2Float(0xfdc2c2c1), SkBits2Float(0x7dc00c0f));  // -3.23602e+37f, 3.19093e+37f
4875     path.lineTo(SkBits2Float(0xfe016040), SkBits2Float(0x7e000626));  // -4.29925e+37f, 4.25433e+37f
4876     path.lineTo(SkBits2Float(0xfe115c90), SkBits2Float(0x7e100636));  // -4.83047e+37f, 4.78603e+37f
4877     path.lineTo(SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f));  // -4.8312e+37f, 4.78529e+37f
4878     path.lineTo(SkBits2Float(0xfe1167e0), SkBits2Float(0x7e0ffae6));  // -4.83194e+37f, 4.78456e+37f
4879     path.lineTo(SkBits2Float(0xfe096438), SkBits2Float(0x7e07fade));  // -4.56562e+37f, 4.51871e+37f
4880     path.lineTo(SkBits2Float(0xfe016130), SkBits2Float(0x7dfff5ac));  // -4.29938e+37f, 4.25286e+37f
4881     path.lineTo(SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f));  // -4.29864e+37f, 4.25359e+37f
4882     path.lineTo(SkBits2Float(0xfe0155e1), SkBits2Float(0x7e000627));  // -4.29791e+37f, 4.25433e+37f
4883     path.lineTo(SkBits2Float(0xfe415879), SkBits2Float(0x7e4008bf));  // -6.42501e+37f, 6.38143e+37f
4884     path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e4008bf));  // -6.42591e+37f, 6.38143e+37f
4885     path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e4008bf));  // -6.42641e+37f, 6.38143e+37f
4886     path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4008bf));  // -6.42641e+37f, 6.38143e+37f
4887     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
4888     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
4889     path.lineTo(SkBits2Float(0xfe317061), SkBits2Float(0x7e2ffb07));  // -5.89642e+37f, 5.84796e+37f
4890     path.lineTo(SkBits2Float(0xfde2e542), SkBits2Float(0x7ddff58e));  // -3.76995e+37f, 3.72116e+37f
4891     path.lineTo(SkBits2Float(0xfd65c525), SkBits2Float(0x7d5fea3d));  // -1.90886e+37f, 1.86021e+37f
4892     path.lineTo(SkBits2Float(0xfab6c8db), SkBits2Float(0xf7b50b4f));  // -4.74536e+35f, -7.34402e+33f
4893     path.lineTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e));  // -4.71494e+35f, -1.03847e+34f
4894     path.close();
4895     path.moveTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000));  // -4.67194e+35f, 200
4896     path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000));  // -4.60703e+35f, 200
4897     path.quadTo(SkBits2Float(0xfd0593a5), SkBits2Float(0x7d00007f), SkBits2Float(0xfd659785), SkBits2Float(0x7d6000de));  // -1.10971e+37f, 1.0634e+37f, -1.90737e+37f, 1.86095e+37f
4898     path.quadTo(SkBits2Float(0xfda2cdf2), SkBits2Float(0x7da0009f), SkBits2Float(0xfdc2ce12), SkBits2Float(0x7dc000be));  // -2.70505e+37f, 2.6585e+37f, -3.23675e+37f, 3.1902e+37f
4899     path.quadTo(SkBits2Float(0xfde2ce31), SkBits2Float(0x7de000de), SkBits2Float(0xfe0165e9), SkBits2Float(0x7e00007f));  // -3.76845e+37f, 3.72189e+37f, -4.29999e+37f, 4.25359e+37f
4900     path.quadTo(SkBits2Float(0xfe1164b9), SkBits2Float(0x7e10008f), SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f));  // -4.83153e+37f, 4.78529e+37f, -4.8312e+37f, 4.78529e+37f
4901     path.quadTo(SkBits2Float(0xfe116039), SkBits2Float(0x7e10008f), SkBits2Float(0xfe095e91), SkBits2Float(0x7e080087));  // -4.83094e+37f, 4.78529e+37f, -4.56488e+37f, 4.51944e+37f
4902     path.quadTo(SkBits2Float(0xfe015d09), SkBits2Float(0x7e00007f), SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f));  // -4.29884e+37f, 4.25359e+37f, -4.29864e+37f, 4.25359e+37f
4903     path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be));  // -6.42544e+37f, 6.38039e+37f
4904     path.quadTo(SkBits2Float(0xfe415da9), SkBits2Float(0x7e4000be), SkBits2Float(0xfe415f69), SkBits2Float(0x7e4000be));  // -6.42568e+37f, 6.38039e+37f, -6.42591e+37f, 6.38039e+37f
4905     path.quadTo(SkBits2Float(0xfe416149), SkBits2Float(0x7e4000be), SkBits2Float(0xfe416349), SkBits2Float(0x7e4000be));  // -6.42615e+37f, 6.38039e+37f, -6.42641e+37f, 6.38039e+37f
4906     path.quadTo(SkBits2Float(0xfe416849), SkBits2Float(0x7e4000be), SkBits2Float(0xfe316ab9), SkBits2Float(0x7e3000af));  // -6.42706e+37f, 6.38039e+37f, -5.89569e+37f, 5.84869e+37f
4907     path.quadTo(SkBits2Float(0xfe216d29), SkBits2Float(0x7e20009f), SkBits2Float(0xfde2d9f2), SkBits2Float(0x7de000de));  // -5.36431e+37f, 5.31699e+37f, -3.76921e+37f, 3.72189e+37f
4908     path.quadTo(SkBits2Float(0xfda2d9b2), SkBits2Float(0x7da0009f), SkBits2Float(0xfd65ae85), SkBits2Float(0x7d6000de));  // -2.70582e+37f, 2.6585e+37f, -1.90812e+37f, 1.86095e+37f
4909     path.quadTo(SkBits2Float(0xfd05a9a6), SkBits2Float(0x7d00007f), SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000));  // -1.11043e+37f, 1.0634e+37f, -4.67194e+35f, 200
4910     path.close();
4911     path.moveTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087));  // 1.80299e+38f, -1.80778e+38f
4912     path.quadTo(SkBits2Float(0x7f0ba519), SkBits2Float(0xff0c008b), SkBits2Float(0x7f0da5f3), SkBits2Float(0xff0e008d));  // 1.8562e+38f, -1.86095e+38f, 1.88283e+38f, -1.88753e+38f
4913     path.quadTo(SkBits2Float(0x7f0fa6d5), SkBits2Float(0xff10008f), SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f));  // 1.90946e+38f, -1.91412e+38f, 1.90951e+38f, -1.91412e+38f
4914     path.quadTo(SkBits2Float(0x7f0faa7d), SkBits2Float(0xff10008f), SkBits2Float(0x7ef75801), SkBits2Float(0xfef800f6));  // 1.90965e+38f, -1.91412e+38f, 1.64388e+38f, -1.64827e+38f
4915     path.quadTo(SkBits2Float(0x7ecf5b09), SkBits2Float(0xfed000ce), SkBits2Float(0x7e875ac2), SkBits2Float(0xfe880087));  // 1.37811e+38f, -1.38242e+38f, 8.99585e+37f, -9.03889e+37f
4916     path.quadTo(SkBits2Float(0x7e0eb505), SkBits2Float(0xfe10008f), SkBits2Float(0x7d7ab958), SkBits2Float(0xfd80007f));  // 4.74226e+37f, -4.78529e+37f, 2.08293e+37f, -2.1268e+37f
4917     path.quadTo(SkBits2Float(0xfc8ac1cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f));  // -5.76374e+36f, 5.31699e+36f, -5.77753e+36f, 5.31699e+36f
4918     path.quadTo(SkBits2Float(0xfc8b36cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc16a51a), SkBits2Float(0x7c00007f));  // -5.78273e+36f, 5.31699e+36f, -3.12877e+36f, 2.6585e+36f
4919     path.quadTo(SkBits2Float(0xfab6e4de), SkBits2Float(0x43480000), SkBits2Float(0x7c68f062), SkBits2Float(0xfc80007f));  // -4.7482e+35f, 200, 4.83795e+36f, -5.31699e+36f
4920     path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de));  // 3.67399e+37f, -3.72189e+37f
4921     path.quadTo(SkBits2Float(0x7d9d254b), SkBits2Float(0xfda0009f), SkBits2Float(0x7d8d2bbc), SkBits2Float(0xfd90008f));  // 2.61103e+37f, -2.6585e+37f, 2.3456e+37f, -2.39265e+37f
4922     path.quadTo(SkBits2Float(0x7d7a64d8), SkBits2Float(0xfd80007f), SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f));  // 2.08019e+37f, -2.1268e+37f, 2.08063e+37f, -2.1268e+37f
4923     path.quadTo(SkBits2Float(0x7d7a9058), SkBits2Float(0xfd80007f), SkBits2Float(0x7ded50db), SkBits2Float(0xfdf000ee));  // 2.0816e+37f, -2.1268e+37f, 3.94309e+37f, -3.98774e+37f
4924     path.quadTo(SkBits2Float(0x7e2eace5), SkBits2Float(0xfe3000af), SkBits2Float(0x7e8756a2), SkBits2Float(0xfe880087));  // 5.80458e+37f, -5.84869e+37f, 8.99478e+37f, -9.03889e+37f
4925     path.quadTo(SkBits2Float(0x7ebf56d9), SkBits2Float(0xfec000be), SkBits2Float(0x7edb54d5), SkBits2Float(0xfedc00da));  // 1.27167e+38f, -1.27608e+38f, 1.45771e+38f, -1.46217e+38f
4926     path.quadTo(SkBits2Float(0x7ef752e1), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6));  // 1.64375e+38f, -1.64827e+38f, 1.64365e+38f, -1.64827e+38f
4927     path.quadTo(SkBits2Float(0x7ef74d71), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef34bbd), SkBits2Float(0xfef400f2));  // 1.64361e+38f, -1.64827e+38f, 1.61698e+38f, -1.62168e+38f
4928     path.quadTo(SkBits2Float(0x7eef4a19), SkBits2Float(0xfef000ee), SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de));  // 1.59035e+38f, -1.5951e+38f, 1.48397e+38f, -1.48876e+38f
4929     path.lineTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087));  // 1.80299e+38f, -1.80778e+38f
4930     path.close();
4931     SkSurfaces::Raster(SkImageInfo::MakeN32Premul(250, 250), nullptr)
4932             ->getCanvas()
4933             ->drawPath(path, paint);
4934 }
4935 
test_interp(skiatest::Reporter * reporter)4936 static void test_interp(skiatest::Reporter* reporter) {
4937     SkPath p1, p2, out;
4938     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4939     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4940     REPORTER_ASSERT(reporter, p1 == out);
4941     REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4942     REPORTER_ASSERT(reporter, p1 == out);
4943     p1.moveTo(0, 2);
4944     p1.lineTo(0, 4);
4945     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4946     REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4947     p2.moveTo(6, 0);
4948     p2.lineTo(8, 0);
4949     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4950     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4951     REPORTER_ASSERT(reporter, p2 == out);
4952     REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4953     REPORTER_ASSERT(reporter, p1 == out);
4954     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4955     REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4956     p1.reset();
4957     p1.moveTo(4, 4);
4958     p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4959     p2.reset();
4960     p2.moveTo(4, 2);
4961     p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4962     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4963     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4964     REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4965     p2.reset();
4966     p2.moveTo(4, 2);
4967     p2.conicTo(6, 3, 6, 5, 1);
4968     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4969     p2.reset();
4970     p2.moveTo(4, 4);
4971     p2.conicTo(5, 4, 5, 5, 0.5f);
4972     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4973 }
4974 
DEF_TEST(PathInterp,reporter)4975 DEF_TEST(PathInterp, reporter) {
4976     test_interp(reporter);
4977 }
4978 
DEF_TEST(PathBigCubic,reporter)4979 DEF_TEST(PathBigCubic, reporter) {
4980     SkPath path;
4981     path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000));  // 0, 0
4982     path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8));  // 512, 1.10401e-05f
4983     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
4984     path.moveTo(0, 512);
4985 
4986     // this call should not assert
4987     SkSurfaces::Raster(SkImageInfo::MakeN32Premul(255, 255), nullptr)
4988             ->getCanvas()
4989             ->drawPath(path, SkPaint());
4990 }
4991 
DEF_TEST(PathContains,reporter)4992 DEF_TEST(PathContains, reporter) {
4993     test_contains(reporter);
4994 }
4995 
DEF_TEST(Paths,reporter)4996 DEF_TEST(Paths, reporter) {
4997     test_fuzz_crbug_647922();
4998     test_fuzz_crbug_643933();
4999     test_sect_with_horizontal_needs_pinning();
5000     test_iterative_intersect_line();
5001     test_crbug_629455(reporter);
5002     test_fuzz_crbug_627414(reporter);
5003     test_path_crbug364224();
5004     test_fuzz_crbug_662952(reporter);
5005     test_fuzz_crbug_662730(reporter);
5006     test_fuzz_crbug_662780();
5007     test_mask_overflow();
5008     test_path_crbugskia6003();
5009     test_fuzz_crbug_668907();
5010     test_skbug_6947();
5011     test_skbug_7015();
5012     test_skbug_7051();
5013     test_skbug_7435();
5014 
5015     SkSize::Make(3, 4);
5016 
5017     SkPath  p, empty;
5018     SkRect  bounds, bounds2;
5019     test_empty(reporter, p);
5020 
5021     REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
5022 
5023     // this triggers a code path in SkPath::operator= which is otherwise unexercised
5024     SkPath& self = p;
5025     p = self;
5026 
5027     // this triggers a code path in SkPath::swap which is otherwise unexercised
5028     p.swap(self);
5029 
5030     bounds.setLTRB(0, 0, SK_Scalar1, SK_Scalar1);
5031 
5032     p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
5033     check_convex_bounds(reporter, p, bounds);
5034     // we have quads or cubics
5035     REPORTER_ASSERT(reporter,
5036                     p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
5037     REPORTER_ASSERT(reporter, !p.isEmpty());
5038 
5039     p.reset();
5040     test_empty(reporter, p);
5041 
5042     p.addOval(bounds);
5043     check_convex_bounds(reporter, p, bounds);
5044     REPORTER_ASSERT(reporter, !p.isEmpty());
5045 
5046     p.rewind();
5047     test_empty(reporter, p);
5048 
5049     p.addRect(bounds);
5050     check_convex_bounds(reporter, p, bounds);
5051     // we have only lines
5052     REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
5053     REPORTER_ASSERT(reporter, !p.isEmpty());
5054 
5055     REPORTER_ASSERT(reporter, p != empty);
5056     REPORTER_ASSERT(reporter, !(p == empty));
5057 
5058     // do getPoints and getVerbs return the right result
5059     REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
5060     REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
5061     SkPoint pts[4];
5062     int count = p.getPoints(pts, 4);
5063     REPORTER_ASSERT(reporter, count == 4);
5064     uint8_t verbs[6];
5065     verbs[5] = 0xff;
5066     p.getVerbs(verbs, 5);
5067     REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
5068     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
5069     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
5070     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
5071     REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
5072     REPORTER_ASSERT(reporter, 0xff == verbs[5]);
5073     bounds2.setBounds(pts, 4);
5074     REPORTER_ASSERT(reporter, bounds == bounds2);
5075 
5076     bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
5077     p.offset(SK_Scalar1*3, SK_Scalar1*4);
5078     REPORTER_ASSERT(reporter, bounds == p.getBounds());
5079 
5080     REPORTER_ASSERT(reporter, p.isRect(nullptr));
5081     bounds2.setEmpty();
5082     REPORTER_ASSERT(reporter, p.isRect(&bounds2));
5083     REPORTER_ASSERT(reporter, bounds == bounds2);
5084 
5085     // now force p to not be a rect
5086     bounds.setWH(SK_Scalar1/2, SK_Scalar1/2);
5087     p.addRect(bounds);
5088     REPORTER_ASSERT(reporter, !p.isRect(nullptr));
5089 
5090     // Test an edge case w.r.t. the bound returned by isRect (i.e., the
5091     // path has a trailing moveTo. Please see crbug.com\445368)
5092     {
5093         SkRect r;
5094         p.reset();
5095         p.addRect(bounds);
5096         REPORTER_ASSERT(reporter, p.isRect(&r));
5097         REPORTER_ASSERT(reporter, r == bounds);
5098         // add a moveTo outside of our bounds
5099         p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
5100         REPORTER_ASSERT(reporter, p.isRect(&r));
5101         REPORTER_ASSERT(reporter, r == bounds);
5102     }
5103 
5104     test_operatorEqual(reporter);
5105     test_isLine(reporter);
5106     test_isRect(reporter);
5107     test_is_closed_rect(reporter);
5108     test_isNestedFillRects(reporter);
5109     test_isArc(reporter);
5110     test_zero_length_paths(reporter);
5111     test_direction(reporter);
5112     test_convexity(reporter);
5113     test_convexity2(reporter);
5114     test_convexity_doubleback(reporter);
5115     test_conservativelyContains(reporter);
5116     test_close(reporter);
5117     test_segment_masks(reporter);
5118     test_flattening(reporter);
5119     test_transform(reporter);
5120     test_bounds(reporter);
5121     test_iter(reporter);
5122     test_range_iter(reporter);
5123     test_circle(reporter);
5124     test_oval(reporter);
5125     test_strokerec(reporter);
5126     test_addPoly(reporter);
5127     test_isfinite(reporter);
5128     test_isfinite_after_transform(reporter);
5129     test_islastcontourclosed(reporter);
5130     test_arb_round_rect_is_convex(reporter);
5131     test_arb_zero_rad_round_rect_is_rect(reporter);
5132     test_addrect(reporter);
5133     test_addrect_isfinite(reporter);
5134     test_tricky_cubic();
5135     test_clipped_cubic();
5136     test_crbug_170666();
5137     test_crbug_493450(reporter);
5138     test_crbug_495894(reporter);
5139     test_crbug_613918();
5140     test_bad_cubic_crbug229478();
5141     test_bad_cubic_crbug234190();
5142     test_gen_id(reporter);
5143     test_path_close_issue1474(reporter);
5144     test_path_to_region(reporter);
5145     test_rrect(reporter);
5146     test_rMoveTo(reporter);
5147     test_arc(reporter);
5148     test_arc_ovals(reporter);
5149     test_arcTo(reporter);
5150     test_addPath(reporter);
5151     test_addPathMode(reporter, false, false);
5152     test_addPathMode(reporter, true, false);
5153     test_addPathMode(reporter, false, true);
5154     test_addPathMode(reporter, true, true);
5155     test_extendClosedPath(reporter);
5156     test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
5157     test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
5158     test_conicTo_special_case(reporter);
5159     test_get_point(reporter);
5160     test_contains(reporter);
5161     PathTest_Private::TestPathTo(reporter);
5162     PathRefTest_Private::TestPathRef(reporter);
5163     PathTest_Private::TestPathrefListeners(reporter);
5164     test_dump(reporter);
5165     test_path_crbug389050(reporter);
5166     test_path_crbugskia2820(reporter);
5167     test_path_crbugskia5995();
5168     test_skbug_3469(reporter);
5169     test_skbug_3239(reporter);
5170     test_bounds_crbug_513799(reporter);
5171     test_fuzz_crbug_638223();
5172 }
5173 
DEF_TEST(conservatively_contains_rect,reporter)5174 DEF_TEST(conservatively_contains_rect, reporter) {
5175     SkPath path;
5176 
5177     path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8));  // 512, 1.10401e-05f
5178     // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
5179     path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
5180                  SkBits2Float(0x00000100), SkBits2Float(0x00000000),
5181                  SkBits2Float(0x00000100), SkBits2Float(0x00000000));
5182     path.moveTo(0, 0);
5183 
5184     // this should not assert
5185     path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
5186 }
5187 
5188 ///////////////////////////////////////////////////////////////////////////////////////////////////
5189 
rand_path(SkPath * path,SkRandom & rand,SkPath::Verb verb,int n)5190 static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
5191     for (int i = 0; i < n; ++i) {
5192         switch (verb) {
5193             case SkPath::kLine_Verb:
5194                 path->lineTo(rand.nextF()*100, rand.nextF()*100);
5195                 break;
5196             case SkPath::kQuad_Verb:
5197                 path->quadTo(rand.nextF()*100, rand.nextF()*100,
5198                              rand.nextF()*100, rand.nextF()*100);
5199                 break;
5200             case SkPath::kConic_Verb:
5201                 path->conicTo(rand.nextF()*100, rand.nextF()*100,
5202                               rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
5203                 break;
5204             case SkPath::kCubic_Verb:
5205                 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
5206                               rand.nextF()*100, rand.nextF()*100,
5207                               rand.nextF()*100, rand.nextF()*100);
5208                 break;
5209             default:
5210                 SkASSERT(false);
5211         }
5212     }
5213 }
5214 
DEF_TEST(path_tight_bounds,reporter)5215 DEF_TEST(path_tight_bounds, reporter) {
5216     SkRandom rand;
5217 
5218     const SkPath::Verb verbs[] = {
5219         SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
5220     };
5221     for (int i = 0; i < 1000; ++i) {
5222         for (int n = 1; n <= 10; n += 9) {
5223             for (SkPath::Verb verb : verbs) {
5224                 SkPath path;
5225                 rand_path(&path, rand, verb, n);
5226                 SkRect bounds = path.getBounds();
5227                 SkRect tight = path.computeTightBounds();
5228                 REPORTER_ASSERT(reporter, bounds.contains(tight));
5229 
5230                 SkRect tight2;
5231                 TightBounds(path, &tight2);
5232                 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
5233             }
5234         }
5235     }
5236 }
5237 
DEF_TEST(skbug_6450,r)5238 DEF_TEST(skbug_6450, r) {
5239     SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
5240     SkVector rdi[4] = {
5241         { 1.81159976e-09f, 7.58768801e-05f },
5242         { 0.000118725002f, 0.000118725002f },
5243         { 0.000118725002f, 0.000118725002f },
5244         { 0.000118725002f, 0.486297607f }
5245     };
5246     SkRRect irr;
5247     irr.setRectRadii(ri, rdi);
5248     SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
5249     SkVector rdo[4] = {
5250         { 0, 0 },
5251         { 0.0103298295f, 0.185887396f },
5252         { 2.52999727e-29f, 169.001938f },
5253         { 195.262741f, 195.161255f }
5254     };
5255     SkRRect orr;
5256     orr.setRectRadii(ro, rdo);
5257     SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
5258 }
5259 
DEF_TEST(PathRefSerialization,reporter)5260 DEF_TEST(PathRefSerialization, reporter) {
5261     SkPath path;
5262     const size_t numMoves = 5;
5263     const size_t numConics = 7;
5264     const size_t numPoints = numMoves + 2 * numConics;
5265     const size_t numVerbs = numMoves + numConics;
5266     for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
5267     for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
5268     REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
5269     REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
5270 
5271     // Verify that path serializes/deserializes properly.
5272     sk_sp<SkData> data = path.serialize();
5273     size_t bytesWritten = data->size();
5274 
5275     {
5276         SkPath readBack;
5277         REPORTER_ASSERT(reporter, readBack != path);
5278         size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
5279         REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
5280         REPORTER_ASSERT(reporter, readBack == path);
5281     }
5282 
5283     // One less byte (rounded down to alignment) than was written will also
5284     // fail to be deserialized.
5285     {
5286         SkPath readBack;
5287         size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
5288         REPORTER_ASSERT(reporter, !bytesRead);
5289     }
5290 }
5291 
DEF_TEST(NonFinitePathIteration,reporter)5292 DEF_TEST(NonFinitePathIteration, reporter) {
5293     SkPath path;
5294     path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
5295     SkPathPriv::Iterate iterate(path);
5296     REPORTER_ASSERT(reporter, iterate.begin() == iterate.end());
5297 }
5298 
DEF_TEST(AndroidArc,reporter)5299 DEF_TEST(AndroidArc, reporter) {
5300     const char* tests[] = {
5301          "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",
5302         ("M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
5303             " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z"),
5304          "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
5305     };
5306     for (auto test : tests) {
5307         SkPath aPath;
5308         SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
5309         SkASSERT(aPath.isConvex());
5310         for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
5311             SkPath scalePath = aPath;
5312             SkMatrix matrix;
5313             matrix.setScale(scale, scale);
5314             scalePath.transform(matrix);
5315             SkASSERT(scalePath.isConvex());
5316         }
5317         for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
5318             SkPath scalePath = aPath;
5319             SkMatrix matrix;
5320             matrix.setScale(scale, scale);
5321             scalePath.transform(matrix);
5322             SkASSERT(scalePath.isConvex());
5323         }
5324     }
5325 }
5326 
5327 /*
5328  *  Try a range of crazy values, just to ensure that we don't assert/crash.
5329  */
DEF_TEST(HugeGeometry,reporter)5330 DEF_TEST(HugeGeometry, reporter) {
5331     auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100));
5332     auto canvas = surf->getCanvas();
5333 
5334     const bool aas[] = { false, true };
5335     const SkPaint::Style styles[] = {
5336         SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
5337     };
5338     const SkScalar values[] = {
5339         0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
5340         SK_ScalarInfinity
5341     };
5342 
5343     SkPaint paint;
5344     for (auto x : values) {
5345         SkRect r = { -x, -x, x, x };
5346         for (auto width : values) {
5347             paint.setStrokeWidth(width);
5348             for (auto aa : aas) {
5349                 paint.setAntiAlias(aa);
5350                 for (auto style : styles) {
5351                     paint.setStyle(style);
5352                     canvas->drawRect(r, paint);
5353                     canvas->drawOval(r, paint);
5354                 }
5355             }
5356         }
5357     }
5358 
5359 }
5360 
5361 // Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
DEF_TEST(ClipPath_nonfinite,reporter)5362 DEF_TEST(ClipPath_nonfinite, reporter) {
5363     auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(10, 10));
5364     SkCanvas* canvas = surf->getCanvas();
5365 
5366     REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5367     for (bool aa : {false, true}) {
5368         for (auto ft : {SkPathFillType::kWinding, SkPathFillType::kInverseWinding}) {
5369             for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
5370                 for (int bits = 1; bits <= 15; ++bits) {
5371                     SkPoint p0 = { 0, 0 };
5372                     SkPoint p1 = { 0, 0 };
5373                     if (bits & 1) p0.fX = -bad;
5374                     if (bits & 2) p0.fY = -bad;
5375                     if (bits & 4) p1.fX = bad;
5376                     if (bits & 8) p1.fY = bad;
5377 
5378                     SkPath path;
5379                     path.moveTo(p0);
5380                     path.lineTo(p1);
5381                     path.setFillType(ft);
5382                     canvas->save();
5383                     canvas->clipPath(path, aa);
5384                     REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
5385                     canvas->restore();
5386                 }
5387             }
5388         }
5389     }
5390     REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5391 }
5392 
5393 // skbug.com/7792
DEF_TEST(Path_isRect,reporter)5394 DEF_TEST(Path_isRect, reporter) {
5395     auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
5396         SkPath path;
5397         for (size_t index = 0; index < count; ++index) {
5398             index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
5399         }
5400         if (close) {
5401             path.close();
5402         }
5403         return path;
5404     };
5405     auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
5406         SkPath path;
5407         for (size_t index = 0; index < count; ++index) {
5408             switch (verbs[index]) {
5409                 case SkPath::kMove_Verb:
5410                     path.moveTo(*points++);
5411                     break;
5412                 case SkPath::kLine_Verb:
5413                     path.lineTo(*points++);
5414                     break;
5415                 case SkPath::kClose_Verb:
5416                     path.close();
5417                     break;
5418                 default:
5419                     SkASSERT(0);
5420             }
5421         }
5422         return path;
5423     };
5424     // isolated from skbug.com/7792 (bug description)
5425     SkRect rect;
5426     SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5427     SkPath path = makePath(points, std::size(points), false);
5428     REPORTER_ASSERT(reporter, path.isRect(&rect));
5429     SkRect compare;
5430     compare.setBounds(&points[1], std::size(points) - 1);
5431     REPORTER_ASSERT(reporter, rect == compare);
5432     // isolated from skbug.com/7792#c3
5433     SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
5434     path = makePath(points3, std::size(points3), true);
5435     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5436     // isolated from skbug.com/7792#c9
5437     SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5438     path = makePath(points9, std::size(points9), true);
5439     REPORTER_ASSERT(reporter, path.isRect(&rect));
5440     compare.setBounds(&points9[1], std::size(points9) - 1);
5441     REPORTER_ASSERT(reporter, rect == compare);
5442     // isolated from skbug.com/7792#c11
5443     SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5444                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
5445     SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
5446     path = makePath2(points11, verbs11, std::size(verbs11));
5447     REPORTER_ASSERT(reporter, path.isRect(&rect));
5448     compare.setBounds(&points11[0], std::size(points11));
5449     REPORTER_ASSERT(reporter, rect == compare);
5450     // isolated from skbug.com/7792#c14
5451     SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
5452                                SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5453                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
5454                                SkPath::kLine_Verb, SkPath::kClose_Verb };
5455     SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
5456                            {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
5457     path = makePath2(points14, verbs14, std::size(verbs14));
5458     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5459     // isolated from skbug.com/7792#c15
5460     SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5461                                SkPath::kLine_Verb, SkPath::kMove_Verb };
5462     SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
5463     path = makePath2(points15, verbs15, std::size(verbs15));
5464     REPORTER_ASSERT(reporter, path.isRect(&rect));
5465     compare.setBounds(&points15[0], std::size(points15) - 1);
5466     REPORTER_ASSERT(reporter, rect == compare);
5467     // isolated from skbug.com/7792#c17
5468     SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
5469     path = makePath(points17, std::size(points17), true);
5470     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5471     // isolated from skbug.com/7792#c19
5472     SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5473                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5474                                SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
5475                                SkPath::kLine_Verb, SkPath::kLine_Verb };
5476     SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5477                            {75, 150}, {10, 10}, {30, 10}, {10, 30} };
5478     path = makePath2(points19, verbs19, std::size(verbs19));
5479     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5480     // isolated from skbug.com/7792#c23
5481     SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5482                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5483                                SkPath::kLine_Verb, SkPath::kClose_Verb };
5484     SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5485                            {75, 150} };
5486     path = makePath2(points23, verbs23, std::size(verbs23));
5487     REPORTER_ASSERT(reporter, path.isRect(&rect));
5488     compare.setBounds(&points23[0], std::size(points23));
5489     REPORTER_ASSERT(reporter, rect == compare);
5490     // isolated from skbug.com/7792#c29
5491     SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5492                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5493                                SkPath::kClose_Verb };
5494     SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5495     path = makePath2(points29, verbs29, std::size(verbs29));
5496     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5497     // isolated from skbug.com/7792#c31
5498     SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5499                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5500                                SkPath::kClose_Verb };
5501     SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5502     path = makePath2(points31, verbs31, std::size(verbs31));
5503     REPORTER_ASSERT(reporter, path.isRect(&rect));
5504     compare.setBounds(&points31[0], 4);
5505     REPORTER_ASSERT(reporter, rect == compare);
5506     // isolated from skbug.com/7792#c36
5507     SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5508                                SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb  };
5509     SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5510     path = makePath2(points36, verbs36, std::size(verbs36));
5511     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5512     // isolated from skbug.com/7792#c39
5513     SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5514                                SkPath::kLine_Verb };
5515     SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5516     path = makePath2(points39, verbs39, std::size(verbs39));
5517     REPORTER_ASSERT(reporter, !path.isRect(&rect));
5518     // isolated from zero_length_paths_aa
5519     SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5520                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5521                                SkPath::kLine_Verb, SkPath::kClose_Verb };
5522     SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5523                            {32, 2} };
5524     path = makePath2(pointsAA, verbsAA, std::size(verbsAA));
5525     REPORTER_ASSERT(reporter, path.isRect(&rect));
5526     compare.setBounds(&pointsAA[0], std::size(pointsAA));
5527     REPORTER_ASSERT(reporter, rect == compare);
5528     // isolated from skbug.com/7792#c41
5529     SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5530                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5531                                SkPath::kClose_Verb };
5532     SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5533     path = makePath2(points41, verbs41, std::size(verbs41));
5534     REPORTER_ASSERT(reporter, path.isRect(&rect));
5535     compare.setBounds(&points41[1], 4);
5536     REPORTER_ASSERT(reporter, rect == compare);
5537     // isolated from skbug.com/7792#c53
5538     SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5539                                SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5540                                SkPath::kClose_Verb };
5541     SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5542     path = makePath2(points53, verbs53, std::size(verbs53));
5543     REPORTER_ASSERT(reporter, path.isRect(&rect));
5544     compare.setBounds(&points53[1], 4);
5545     REPORTER_ASSERT(reporter, rect == compare);
5546 }
5547 
5548 // Be sure we can safely add ourselves
DEF_TEST(Path_self_add,reporter)5549 DEF_TEST(Path_self_add, reporter) {
5550     // The possible problem is that during path.add() we may have to grow the dst buffers as
5551     // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
5552     // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
5553     // garbage.
5554     //
5555     // The test runs though verious sized src paths, since its not defined publicly what the
5556     // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
5557     // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
5558     // an ASAN error w/o the fix to SkPath::addPath().
5559     //
5560     for (int count = 0; count < 10; ++count) {
5561         SkPath path;
5562         for (int add = 0; add < count; ++add) {
5563             // just add some stuff, so we have something to copy/append in addPath()
5564             path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
5565         }
5566         path.addPath(path, 1, 2);
5567         path.addPath(path, 3, 4);
5568     }
5569 }
5570 
draw_triangle(SkCanvas * canvas,const SkPoint pts[])5571 static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5572     // draw in different ways, looking for an assert
5573 
5574     {
5575         SkPath path;
5576         path.addPoly(pts, 3, false);
5577         canvas->drawPath(path, SkPaint());
5578     }
5579 
5580     const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5581     auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5582     canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5583 }
5584 
DEF_TEST(triangle_onehalf,reporter)5585 DEF_TEST(triangle_onehalf, reporter) {
5586     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(100, 100)));
5587 
5588     const SkPoint pts[] = {
5589         {  0.499069244f, 9.63295173f },
5590         {  0.499402374f, 7.88207579f },
5591         { 10.2363272f,   0.49999997f }
5592     };
5593     draw_triangle(surface->getCanvas(), pts);
5594 }
5595 
DEF_TEST(triangle_big,reporter)5596 DEF_TEST(triangle_big, reporter) {
5597     auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(4, 4304)));
5598 
5599     // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5600     // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5601     // be conservative, so we invoke clipping if we get in this situation.
5602     // This test was added to demonstrate the need for this conservative bounds calc.
5603     // (found by a fuzzer)
5604     const SkPoint pts[] = {
5605         { 0.327190518f, -114.945152f },
5606         { -0.5f, 1.00003874f },
5607         { 0.666425824f, 4304.26172f },
5608     };
5609     draw_triangle(surface->getCanvas(), pts);
5610 }
5611 
add_verbs(SkPath * path,int count)5612 static void add_verbs(SkPath* path, int count) {
5613     path->moveTo(0, 0);
5614     for (int i = 0; i < count; ++i) {
5615         switch (i & 3) {
5616             case 0: path->lineTo(10, 20); break;
5617             case 1: path->quadTo(5, 6, 7, 8); break;
5618             case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
5619             case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
5620         }
5621     }
5622 }
5623 
5624 // Make sure when we call shrinkToFit() that we always shrink (or stay the same)
5625 // and that if we call twice, we stay the same.
DEF_TEST(Path_shrinkToFit,reporter)5626 DEF_TEST(Path_shrinkToFit, reporter) {
5627     for (int verbs = 0; verbs < 100; ++verbs) {
5628         SkPath unique_path, shared_path;
5629         add_verbs(&unique_path, verbs);
5630         add_verbs(&shared_path, verbs);
5631 
5632         const SkPath copy = shared_path;
5633 
5634         REPORTER_ASSERT(reporter, shared_path == unique_path);
5635         REPORTER_ASSERT(reporter, shared_path == copy);
5636 
5637         uint32_t uID = unique_path.getGenerationID();
5638         uint32_t sID = shared_path.getGenerationID();
5639         uint32_t cID =        copy.getGenerationID();
5640         REPORTER_ASSERT(reporter, sID == cID);
5641 
5642         SkPathPriv::ShrinkToFit(&unique_path);
5643         SkPathPriv::ShrinkToFit(&shared_path);
5644         REPORTER_ASSERT(reporter, shared_path == unique_path);
5645         REPORTER_ASSERT(reporter, shared_path == copy);
5646 
5647         // since the unique_path is "unique", it's genID need not have changed even though
5648         // unique_path has changed (been shrunk)
5649         REPORTER_ASSERT(reporter, uID == unique_path.getGenerationID());
5650         // since the copy has not been changed, its ID should be the same
5651         REPORTER_ASSERT(reporter, cID == copy.getGenerationID());
5652         // but since shared_path has changed, and was not uniquely owned, it's gen ID needs to have
5653         // changed, breaking the "sharing" -- this is done defensively in case there were any
5654         // outstanding Iterators active on copy, which could have been invalidated during
5655         // shrinkToFit.
5656         REPORTER_ASSERT(reporter, sID != shared_path.getGenerationID());
5657     }
5658 }
5659 
DEF_TEST(Path_setLastPt,r)5660 DEF_TEST(Path_setLastPt, r) {
5661     // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
5662     SkPath p;
5663     p.moveTo(0,0);
5664     p.moveTo(20,01);
5665     p.moveTo(20,10);
5666     p.moveTo(20,61);
5667     REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
5668 
5669     p.setLastPt(30,01);
5670     REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10));  // was {0,0, 20,61}
5671 
5672     REPORTER_ASSERT(r, p.isValid());
5673 }
5674 
DEF_TEST(Path_increserve_handle_neg_crbug_883666,r)5675 DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
5676     SkPath path;
5677 
5678     path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
5679 
5680     // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
5681     SkPath shallowPath = path;
5682 
5683     // make sure we don't assert/crash on this.
5684     shallowPath.incReserve(0xffffffff);
5685 }
5686 
5687 ////////////////////////////////////////////////////////////////////////////////////////////////
5688 
5689 /*
5690  *  For speed, we tried to preserve useful/expensive attributes about paths,
5691  *      - convexity, isrect, isoval, ...
5692  *  Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
5693  *  is axis-aligned (e.g. scale+translate)
5694  */
5695 
5696 struct Xforms {
5697     SkMatrix    fIM, fTM, fSM, fRM;
5698 
XformsXforms5699     Xforms() {
5700         fIM.reset();
5701         fTM.setTranslate(10, 20);
5702         fSM.setScale(2, 3);
5703         fRM.setRotate(30);
5704     }
5705 };
5706 
conditional_convex(const SkPath & path,bool is_convex)5707 static bool conditional_convex(const SkPath& path, bool is_convex) {
5708     SkPathConvexity c = SkPathPriv::GetConvexityOrUnknown(path);
5709     return is_convex ? (c == SkPathConvexity::kConvex) : (c != SkPathConvexity::kConvex);
5710 }
5711 
5712 // expect axis-aligned shape to survive assignment, identity and scale/translate matrices
5713 template <typename ISA>
survive(SkPath * path,const Xforms & x,bool isAxisAligned,skiatest::Reporter * reporter,ISA isa_proc)5714 void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
5715              ISA isa_proc) {
5716     REPORTER_ASSERT(reporter, isa_proc(*path));
5717     // force the issue (computing convexity) the first time.
5718     REPORTER_ASSERT(reporter, path->isConvex());
5719 
5720     SkPath path2;
5721 
5722     // a path's isa and convexity should survive assignment
5723     path2 = *path;
5724     REPORTER_ASSERT(reporter, isa_proc(path2));
5725     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) == SkPathConvexity::kConvex);
5726 
5727     // a path's isa and convexity should identity transform
5728     path->transform(x.fIM, &path2);
5729     path->transform(x.fIM);
5730     REPORTER_ASSERT(reporter, isa_proc(path2));
5731     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) == SkPathConvexity::kConvex);
5732     REPORTER_ASSERT(reporter, isa_proc(*path));
5733     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(*path) == SkPathConvexity::kConvex);
5734 
5735     // a path's isa should survive translation, convexity depends on axis alignment
5736     path->transform(x.fTM, &path2);
5737     path->transform(x.fTM);
5738     REPORTER_ASSERT(reporter, isa_proc(path2));
5739     REPORTER_ASSERT(reporter, isa_proc(*path));
5740     REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5741     REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
5742 
5743     // a path's isa should survive scaling, convexity depends on axis alignment
5744     path->transform(x.fSM, &path2);
5745     path->transform(x.fSM);
5746     REPORTER_ASSERT(reporter, isa_proc(path2));
5747     REPORTER_ASSERT(reporter, isa_proc(*path));
5748     REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5749     REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
5750 
5751     // For security, post-rotation, we can't assume we're still convex. It might prove to be,
5752     // in fact, still be convex, be we can't have cached that setting, hence the call to
5753     // getConvexityOrUnknown() instead of getConvexity().
5754     path->transform(x.fRM, &path2);
5755     path->transform(x.fRM);
5756     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(path2) != SkPathConvexity::kConvex);
5757     REPORTER_ASSERT(reporter, SkPathPriv::GetConvexityOrUnknown(*path) != SkPathConvexity::kConvex);
5758 
5759     if (isAxisAligned) {
5760         REPORTER_ASSERT(reporter, !isa_proc(path2));
5761         REPORTER_ASSERT(reporter, !isa_proc(*path));
5762     }
5763 }
5764 
DEF_TEST(Path_survive_transform,r)5765 DEF_TEST(Path_survive_transform, r) {
5766     const Xforms x;
5767 
5768     SkPath path;
5769     path.addRect({10, 10, 40, 50});
5770     survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
5771 
5772     path.reset();
5773     path.addOval({10, 10, 40, 50});
5774     survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
5775 
5776     path.reset();
5777     path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
5778     survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
5779 
5780     // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
5781     path.reset();
5782     path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
5783     REPORTER_ASSERT(r, path.isConvex());
5784     survive(&path, x, false, r, [](const SkPath& p) { return true; });
5785 }
5786 
DEF_TEST(path_last_move_to_index,r)5787 DEF_TEST(path_last_move_to_index, r) {
5788     // Make sure that copyPath is safe after the call to path.offset().
5789     // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
5790     // set it to path's value inside SkPath::transform()
5791 
5792     const char text[] = "hello";
5793     constexpr size_t len = sizeof(text) - 1;
5794     SkGlyphID glyphs[len];
5795 
5796     SkFont font = ToolUtils::DefaultFont();
5797     font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
5798 
5799     SkPath copyPath;
5800     font.getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
5801         if (src) {
5802             ((SkPath*)ctx)->addPath(*src, mx);
5803         }
5804     }, &copyPath);
5805 
5806     SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
5807     SkPath path;
5808     path.addRoundRect({10, 10, 110, 110}, radii);
5809     path.offset(0, 5, &(copyPath));                     // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
5810 
5811     copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
5812 }
5813 
test_edger(skiatest::Reporter * r,const std::initializer_list<SkPath::Verb> & in,const std::initializer_list<SkPath::Verb> & expected)5814 static void test_edger(skiatest::Reporter* r,
5815                        const std::initializer_list<SkPath::Verb>& in,
5816                        const std::initializer_list<SkPath::Verb>& expected) {
5817     SkPath path;
5818     SkScalar x = 0, y = 0;
5819     for (auto v : in) {
5820         switch (v) {
5821             case SkPath::kMove_Verb: path.moveTo(x++, y++); break;
5822             case SkPath::kLine_Verb: path.lineTo(x++, y++); break;
5823             case SkPath::kClose_Verb: path.close(); break;
5824             default: SkASSERT(false);
5825         }
5826     }
5827 
5828     SkPathEdgeIter iter(path);
5829     for (auto v : expected) {
5830         auto e = iter.next();
5831         REPORTER_ASSERT(r, e);
5832         REPORTER_ASSERT(r, SkPathEdgeIter::EdgeToVerb(e.fEdge) == v);
5833     }
5834     REPORTER_ASSERT(r, !iter.next());
5835 }
5836 
assert_points(skiatest::Reporter * reporter,const SkPath & path,const std::initializer_list<SkPoint> & list)5837 static void assert_points(skiatest::Reporter* reporter,
5838                           const SkPath& path, const std::initializer_list<SkPoint>& list) {
5839     const SkPoint* expected = list.begin();
5840     SkPath::RawIter iter(path);
5841     for (size_t i = 0;;) {
5842         SkPoint pts[4];
5843         switch (iter.next(pts)) {
5844             case SkPath::kDone_Verb:
5845                 REPORTER_ASSERT(reporter, i == list.size());
5846                 return;
5847             case SkPath::kMove_Verb:
5848                 REPORTER_ASSERT(reporter, pts[0] == expected[i]);
5849                 i++;
5850                 break;
5851             case SkPath::kLine_Verb:
5852                 REPORTER_ASSERT(reporter, pts[1] == expected[i]);
5853                 i++;
5854                 break;
5855             case SkPath::kClose_Verb: break;
5856             default: SkASSERT(false);
5857         }
5858     }
5859 }
5860 
test_addRect_and_trailing_lineTo(skiatest::Reporter * reporter)5861 static void test_addRect_and_trailing_lineTo(skiatest::Reporter* reporter) {
5862     SkPath path;
5863     const SkRect r = {1, 2, 3, 4};
5864     // build our default p-array clockwise
5865     const SkPoint p[] = {
5866         {r.fLeft,  r.fTop},    {r.fRight, r.fTop},
5867         {r.fRight, r.fBottom}, {r.fLeft,  r.fBottom},
5868     };
5869 
5870     for (auto dir : {SkPathDirection::kCW, SkPathDirection::kCCW}) {
5871         int increment = dir == SkPathDirection::kCW ? 1 : 3;
5872         for (int i = 0; i < 4; ++i) {
5873             path.reset();
5874             path.addRect(r, dir, i);
5875 
5876             // check that we return the 4 ponts in the expected order
5877             SkPoint e[4];
5878             for (int j = 0; j < 4; ++j) {
5879                 int index = (i + j*increment) % 4;
5880                 e[j] = p[index];
5881             }
5882             assert_points(reporter, path, {
5883                 e[0], e[1], e[2], e[3]
5884             });
5885 
5886             // check that the new line begins where the rect began
5887             path.lineTo(7,8);
5888             assert_points(reporter, path, {
5889                 e[0], e[1], e[2], e[3],
5890                 e[0], {7,8},
5891             });
5892         }
5893     }
5894 
5895     // now add a moveTo before the rect, just to be sure we don't always look at
5896     // the "first" point in the path when we handle the trailing lineTo
5897     path.reset();
5898     path.moveTo(7, 8);
5899     path.addRect(r, SkPathDirection::kCW, 2);
5900     path.lineTo(5, 6);
5901 
5902     assert_points(reporter, path, {
5903         {7,8},                  // initial moveTo
5904         p[2], p[3], p[0], p[1], // rect
5905         p[2], {5, 6},           // trailing line
5906     });
5907 }
5908 
5909 /*
5910  *  SkPath allows the caller to "skip" calling moveTo for contours. If lineTo (or a curve) is
5911  *  called on an empty path, a 'moveTo(0,0)' will automatically be injected. If the path is
5912  *  not empty, but its last contour has been "closed", then it will inject a moveTo corresponding
5913  *  to where the last contour itself started (i.e. its moveTo).
5914  *
5915  *  This test exercises this in a particular case:
5916  *      path.moveTo(...)                <-- needed to show the bug
5917  *      path.moveTo....close()
5918  *      // at this point, the path's verbs are: M M ... C
5919  *
5920  *      path.lineTo(...)
5921  *      // after lineTo,  the path's verbs are: M M ... C M L
5922  */
test_addPath_and_injected_moveTo(skiatest::Reporter * reporter)5923 static void test_addPath_and_injected_moveTo(skiatest::Reporter* reporter) {
5924     /*
5925      *  Given a path, and the expected last-point and last-move-to in it,
5926      *  assert that, after a lineTo(), that the injected moveTo corresponds
5927      *  to the expected value.
5928      */
5929     auto test_before_after_lineto = [reporter](SkPath& path,
5930                                                SkPoint expectedLastPt,
5931                                                SkPoint expectedMoveTo) {
5932         SkPoint p = path.getPoint(path.countPoints() - 1);
5933         REPORTER_ASSERT(reporter, p == expectedLastPt);
5934 
5935         const SkPoint newLineTo = {1234, 5678};
5936         path.lineTo(newLineTo);
5937 
5938         p = path.getPoint(path.countPoints() - 2);
5939         REPORTER_ASSERT(reporter, p == expectedMoveTo); // this was injected by lineTo()
5940 
5941         p = path.getPoint(path.countPoints() - 1);
5942         REPORTER_ASSERT(reporter, p == newLineTo);
5943     };
5944 
5945     SkPath path1;
5946     path1.moveTo(230, 230); // Needed to show the bug: a moveTo before the addRect
5947     path1.moveTo(20,30).lineTo(40,30).lineTo(40,50).lineTo(20,50);
5948     SkPath path1c(path1);
5949     path1c.close();
5950 
5951     SkPath path2;
5952     // If path2 contains zero points, the update calculation isn't tested.
5953     path2.moveTo(144, 72);
5954     path2.lineTo(146, 72);
5955     SkPath path2c(path2);
5956     path2c.close();
5957     SkPath path3(path2);
5958     SkPath path3c(path2c);
5959 
5960     // Test addPath, adding a path that ends with close.
5961     // The start point of the last contour added,
5962     // and the internal flag tracking whether it is closed,
5963     // must be updated correctly.
5964     path2.addPath(path1c);
5965     path2c.addPath(path1c);
5966     // At this point, path1c, path2, and path2c should end the same way.
5967     test_before_after_lineto(path1c, {20,50}, {20,30});
5968     test_before_after_lineto(path2, {20,50}, {20,30});
5969     test_before_after_lineto(path2c, {20,50}, {20,30});
5970 
5971     // Test addPath, adding a path not ending in close.
5972     path3.addPath(path1);
5973     path3c.addPath(path1);
5974     // At this point, path1, path3, and path3c should end the same way.
5975     test_before_after_lineto(path1, {20,50}, {20,50});
5976     test_before_after_lineto(path3, {20,50}, {20,50});
5977     test_before_after_lineto(path3c, {20,50}, {20,50});
5978 }
5979 
DEF_TEST(pathedger,r)5980 DEF_TEST(pathedger, r) {
5981     auto M = SkPath::kMove_Verb;
5982     auto L = SkPath::kLine_Verb;
5983     auto C = SkPath::kClose_Verb;
5984 
5985     test_edger(r, { M }, {});
5986     test_edger(r, { M, M }, {});
5987     test_edger(r, { M, C }, {});
5988     test_edger(r, { M, M, C }, {});
5989     test_edger(r, { M, L }, { L, L });
5990     test_edger(r, { M, L, C }, { L, L });
5991     test_edger(r, { M, L, L }, { L, L, L });
5992     test_edger(r, { M, L, L, C }, { L, L, L });
5993 
5994     test_edger(r, { M, L, L, M, L, L }, { L, L, L,   L, L, L });
5995 
5996     test_addRect_and_trailing_lineTo(r);
5997     test_addPath_and_injected_moveTo(r);
5998 }
5999 
DEF_TEST(path_addpath_crbug_1153516,r)6000 DEF_TEST(path_addpath_crbug_1153516, r) {
6001     // When we add a closed path to another path, verify
6002     // that the result has the right value for last contour start point.
6003     SkPath p1, p2;
6004     p2.lineTo(10,20);
6005     p1.addRect({143,226,200,241});
6006     p2.addPath(p1);
6007     p2.lineTo(262,513); // this should not assert
6008     SkPoint rectangleStart = {143, 226};
6009     SkPoint lineEnd = {262, 513};
6010     SkPoint actualMoveTo = p2.getPoint(p2.countPoints() - 2);
6011     REPORTER_ASSERT(r, actualMoveTo == rectangleStart );
6012     SkPoint actualLineTo = p2.getPoint(p2.countPoints() - 1);
6013     REPORTER_ASSERT(r, actualLineTo == lineEnd);
6014 
6015     // Verify adding a closed path to itself
6016     p1.addPath(p1);
6017     p1.lineTo(262,513);
6018     actualMoveTo = p1.getPoint(p1.countPoints() - 2);
6019     REPORTER_ASSERT(r, actualMoveTo == rectangleStart );
6020     actualLineTo = p1.getPoint(p1.countPoints() - 1);
6021     REPORTER_ASSERT(r, actualLineTo == lineEnd);
6022  }
6023 
DEF_TEST(path_convexity_scale_way_down,r)6024 DEF_TEST(path_convexity_scale_way_down, r) {
6025     SkPath path = SkPathBuilder().moveTo(0,0).lineTo(1, 0)
6026                                  .lineTo(1,1).lineTo(0,1)
6027                                  .detach();
6028 
6029     REPORTER_ASSERT(r, path.isConvex());
6030     SkPath path2;
6031     const SkScalar scale = 1e-8f;
6032     path.transform(SkMatrix::Scale(scale, scale), &path2);
6033     SkPathPriv::ForceComputeConvexity(path2);
6034     REPORTER_ASSERT(r, path2.isConvex());
6035 }
6036 
6037 // crbug.com/1187385
DEF_TEST(path_moveto_addrect,r)6038 DEF_TEST(path_moveto_addrect, r) {
6039     // Test both an empty and non-empty rect passed to SkPath::addRect
6040     SkRect rects[] = {{207.0f, 237.0f, 300.0f, 237.0f},
6041                       {207.0f, 237.0f, 300.0f, 267.0f}};
6042 
6043     for (SkRect rect: rects) {
6044         for (int numExtraMoveTos : {0, 1, 2, 3}) {
6045             SkPath path;
6046             // Convexity and contains functions treat the path as a simple fill, so consecutive
6047             // moveTos are collapsed together.
6048             for (int i = 0; i < numExtraMoveTos; ++i) {
6049                 path.moveTo(i, i);
6050             }
6051             path.addRect(rect);
6052 
6053             REPORTER_ASSERT(r, (numExtraMoveTos + 1) == SkPathPriv::LeadingMoveToCount(path));
6054 
6055             // addRect should mark the path as known convex automatically (i.e. it wasn't set
6056             // to unknown after edits)
6057             SkPathConvexity origConvexity = SkPathPriv::GetConvexityOrUnknown(path);
6058             REPORTER_ASSERT(r, origConvexity == SkPathConvexity::kConvex);
6059 
6060             // but it should also agree with the regular convexity computation
6061             SkPathPriv::ForceComputeConvexity(path);
6062             REPORTER_ASSERT(r, path.isConvex());
6063 
6064             SkRect query = rect.makeInset(10.f, 0.f);
6065             REPORTER_ASSERT(r, path.conservativelyContainsRect(query));
6066         }
6067     }
6068 }
6069 
6070 // crbug.com/1220754
DEF_TEST(path_moveto_twopass_convexity,r)6071 DEF_TEST(path_moveto_twopass_convexity, r) {
6072     // There had been a bug when the last moveTo index > 0, the calculated point count was incorrect
6073     // and the BySign convexity pass would not evaluate the entire path, effectively only using the
6074     // winding rule for determining convexity.
6075     SkPath path;
6076     path.setFillType(SkPathFillType::kWinding);
6077     path.moveTo(3.25f, 115.5f);
6078     path.conicTo(9.98099e+17f, 2.83874e+15f, 1.75098e-30f, 1.75097e-30f, 1.05385e+18f);
6079     path.conicTo(9.96938e+17f, 6.3804e+19f, 9.96934e+17f, 1.75096e-30f, 1.75096e-30f);
6080     path.quadTo(1.28886e+10f, 9.9647e+17f, 9.98101e+17f, 2.61006e+15f);
6081     REPORTER_ASSERT(r, !path.isConvex());
6082 
6083     SkPath pathWithExtraMoveTo;
6084     pathWithExtraMoveTo.setFillType(SkPathFillType::kWinding);
6085     pathWithExtraMoveTo.moveTo(5.90043e-39f, 1.34525e-43f);
6086     pathWithExtraMoveTo.addPath(path);
6087     REPORTER_ASSERT(r, !pathWithExtraMoveTo.isConvex());
6088 }
6089 
6090 // crbug.com/1154864
DEF_TEST(path_walk_simple_edges_1154864,r)6091 DEF_TEST(path_walk_simple_edges_1154864, r) {
6092     // Drawing this path triggered an assert in walk_simple_edges:
6093     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(32, 32));
6094 
6095     SkPath path;
6096     path.setFillType(SkPathFillType::kWinding);
6097     path.moveTo(0.00665998459f, 2);
6098     path.quadTo(0.00665998459f, 4, -1.99334002f, 4);
6099     path.quadTo(-3.99334002f, 4, -3.99334002f, 2);
6100     path.quadTo(-3.99334002f, 0, -1.99334002f, 0);
6101     path.quadTo(0.00665998459f, 0, 0.00665998459f, 2);
6102     path.close();
6103 
6104     SkPaint paint;
6105     paint.setAntiAlias(true);
6106     surface->getCanvas()->drawPath(path, paint);
6107 }
6108