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