compare degenerates with tolerance
[platform/upstream/libSkiaSharp.git] / tests / PathTest.cpp
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 <cmath>
9 #include "SkCanvas.h"
10 #include "SkGeometry.h"
11 #include "SkPaint.h"
12 #include "SkParse.h"
13 #include "SkParsePath.h"
14 #include "SkPathPriv.h"
15 #include "SkPathEffect.h"
16 #include "SkRRect.h"
17 #include "SkRandom.h"
18 #include "SkReader32.h"
19 #include "SkSize.h"
20 #include "SkStream.h"
21 #include "SkStrokeRec.h"
22 #include "SkSurface.h"
23 #include "SkTypes.h"
24 #include "SkWriter32.h"
25 #include "Test.h"
26
27 static void set_radii(SkVector radii[4], int index, float rad) {
28     sk_bzero(radii, sizeof(SkVector) * 4);
29     radii[index].set(rad, rad);
30 }
31
32 static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33                            const SkVector radii[4]) {
34     SkRRect rrect;
35     rrect.setRectRadii(bounds, radii);
36     REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38     SkPath path;
39     // this line should not assert in the debug build (from validate)
40     path.addRRect(rrect);
41     REPORTER_ASSERT(reporter, bounds == path.getBounds());
42 }
43
44 static void test_skbug_3469(skiatest::Reporter* reporter) {
45     SkPath path;
46     path.moveTo(20, 20);
47     path.quadTo(20, 50, 80, 50);
48     path.quadTo(20, 50, 20, 80);
49     REPORTER_ASSERT(reporter, !path.isConvex());
50 }
51
52 static void test_skbug_3239(skiatest::Reporter* reporter) {
53     const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54     const float max = SkBits2Float(0x4b7f1c1d); /*  16718877.000000 */
55     const float big = SkBits2Float(0x4b7f1bd7); /*  16718807.000000 */
56
57     const float rad = 33436320;
58
59     const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60     const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62     SkVector radii[4];
63     for (int i = 0; i < 4; ++i) {
64         set_radii(radii, i, rad);
65         test_add_rrect(reporter, rectx, radii);
66         test_add_rrect(reporter, recty, radii);
67     }
68 }
69
70 static void make_path_crbug364224(SkPath* path) {
71     path->reset();
72     path->moveTo(3.747501373f, 2.724499941f);
73     path->lineTo(3.747501373f, 3.75f);
74     path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75     path->lineTo(0.7475013733f, 4.0f);
76     path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77     path->lineTo(0.4975013733f, 1.0f);
78     path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79     path->lineTo(3.497501373f, 0.75f);
80     path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81     path->lineTo(3.715001345f, 0.5512499809f);
82     path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83     path->lineTo(0.7475013733f, 0.4999999702f);
84     path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85     path->lineTo(0.2475013733f, 3.75f);
86     path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87     path->lineTo(3.497501373f, 4.25f);
88     path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89     path->lineTo(3.997501373f, 2.474750042f);
90     path->lineTo(3.747501373f, 2.724499941f);
91     path->close();
92 }
93
94 static void make_path_crbug364224_simplified(SkPath* path) {
95     path->moveTo(3.747501373f, 2.724499941f);
96     path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97     path->close();
98 }
99
100 static void test_sect_with_horizontal_needs_pinning() {
101     // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
102     // intersection.
103     SkPath path;
104     path.reset();
105     path.moveTo(-540000, -720000);
106     path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
107     path.lineTo(1, 1);
108
109     // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
110     SkPaint paint;
111     SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
112 }
113
114 static void test_path_crbug364224() {
115     SkPath path;
116     SkPaint paint;
117     auto surface(SkSurface::MakeRasterN32Premul(84, 88));
118     SkCanvas* canvas = surface->getCanvas();
119
120     make_path_crbug364224_simplified(&path);
121     canvas->drawPath(path, paint);
122
123     make_path_crbug364224(&path);
124     canvas->drawPath(path, paint);
125 }
126
127 // this is a unit test instead of a GM because it doesn't draw anything
128 static void test_fuzz_crbug_638223() {
129     auto surface(SkSurface::MakeRasterN32Premul(250, 250));
130     SkCanvas* canvas = surface->getCanvas();
131     SkPath path;
132     path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01));  // 50474, 161.113f
133     path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
134         SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
135         SkBits2Float(0x6bf9abea));  // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
136     SkPaint paint;
137     paint.setAntiAlias(true);
138     canvas->drawPath(path, paint);
139 }
140
141 static void test_fuzz_crbug_643933() {
142     auto surface(SkSurface::MakeRasterN32Premul(250, 250));
143     SkCanvas* canvas = surface->getCanvas();
144     SkPaint paint;
145     paint.setAntiAlias(true);
146     SkPath path;
147     path.moveTo(0, 0);
148     path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff),  // 2.93943e-39f, 14.125f
149             SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
150             SkBits2Float(0x721aee0c));  // 3.0687e+30f
151     canvas->drawPath(path, paint);
152     path.reset();
153     path.moveTo(0, 0);
154     path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff),  // 4.58981e-41f, 14.625f
155         SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04),  // 510.269f, 29.8643f
156         SkBits2Float(0x5d06eff8));  // 6.07704e+17f
157     canvas->drawPath(path, paint);
158 }
159
160 /**
161  * In debug mode, this path was causing an assertion to fail in
162  * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
163  */
164 static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
165     SkPoint orig, p1, p2, p3;
166     orig = SkPoint::Make(1.f, 1.f);
167     p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
168     p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
169     p3 = SkPoint::Make(2.f, 2.f);
170
171     path->reset();
172     path->moveTo(orig);
173     path->cubicTo(p1, p2, p3);
174     path->close();
175 }
176
177 static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
178     SkPath path;
179     make_path_crbugskia2820(&path, reporter);
180
181     SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
182     stroke.setStrokeStyle(2 * SK_Scalar1);
183     stroke.applyToPath(&path, path);
184 }
185
186 static void make_path0(SkPath* path) {
187     // from  *  https://code.google.com/p/skia/issues/detail?id=1706
188
189     path->moveTo(146.939f, 1012.84f);
190     path->lineTo(181.747f, 1009.18f);
191     path->lineTo(182.165f, 1013.16f);
192     path->lineTo(147.357f, 1016.82f);
193     path->lineTo(146.939f, 1012.84f);
194     path->close();
195 }
196
197 static void make_path1(SkPath* path) {
198     path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
199 }
200
201 typedef void (*PathProc)(SkPath*);
202
203 /*
204  *  Regression test: we used to crash (overwrite internal storage) during
205  *  construction of the region when the path was INVERSE. That is now fixed,
206  *  so test these regions (which used to assert/crash).
207  *
208  *  https://code.google.com/p/skia/issues/detail?id=1706
209  */
210 static void test_path_to_region(skiatest::Reporter* reporter) {
211     PathProc procs[] = {
212         make_path0,
213         make_path1,
214     };
215
216     SkRegion clip;
217     clip.setRect(0, 0, 1255, 1925);
218
219     for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
220         SkPath path;
221         procs[i](&path);
222
223         SkRegion rgn;
224         rgn.setPath(path, clip);
225         path.toggleInverseFillType();
226         rgn.setPath(path, clip);
227     }
228 }
229
230 #ifdef SK_BUILD_FOR_WIN
231     #define SUPPRESS_VISIBILITY_WARNING
232 #else
233     #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
234 #endif
235
236 static void test_path_close_issue1474(skiatest::Reporter* reporter) {
237     // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
238     // are relative to the point we close to, not relative to the point we close from.
239     SkPath path;
240     SkPoint last;
241
242     // Test rLineTo().
243     path.rLineTo(0, 100);
244     path.rLineTo(100, 0);
245     path.close();          // Returns us back to 0,0.
246     path.rLineTo(50, 50);  // This should go to 50,50.
247
248     path.getLastPt(&last);
249     REPORTER_ASSERT(reporter, 50 == last.fX);
250     REPORTER_ASSERT(reporter, 50 == last.fY);
251
252     // Test rQuadTo().
253     path.rewind();
254     path.rLineTo(0, 100);
255     path.rLineTo(100, 0);
256     path.close();
257     path.rQuadTo(50, 50, 75, 75);
258
259     path.getLastPt(&last);
260     REPORTER_ASSERT(reporter, 75 == last.fX);
261     REPORTER_ASSERT(reporter, 75 == last.fY);
262
263     // Test rConicTo().
264     path.rewind();
265     path.rLineTo(0, 100);
266     path.rLineTo(100, 0);
267     path.close();
268     path.rConicTo(50, 50, 85, 85, 2);
269
270     path.getLastPt(&last);
271     REPORTER_ASSERT(reporter, 85 == last.fX);
272     REPORTER_ASSERT(reporter, 85 == last.fY);
273
274     // Test rCubicTo().
275     path.rewind();
276     path.rLineTo(0, 100);
277     path.rLineTo(100, 0);
278     path.close();
279     path.rCubicTo(50, 50, 85, 85, 95, 95);
280
281     path.getLastPt(&last);
282     REPORTER_ASSERT(reporter, 95 == last.fX);
283     REPORTER_ASSERT(reporter, 95 == last.fY);
284 }
285
286 static void test_gen_id(skiatest::Reporter* reporter) {
287     SkPath a, b;
288     REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
289
290     a.moveTo(0, 0);
291     const uint32_t z = a.getGenerationID();
292     REPORTER_ASSERT(reporter, z != b.getGenerationID());
293
294     a.reset();
295     REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
296
297     a.moveTo(1, 1);
298     const uint32_t y = a.getGenerationID();
299     REPORTER_ASSERT(reporter, z != y);
300
301     b.moveTo(2, 2);
302     const uint32_t x = b.getGenerationID();
303     REPORTER_ASSERT(reporter, x != y && x != z);
304
305     a.swap(b);
306     REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
307
308     b = a;
309     REPORTER_ASSERT(reporter, b.getGenerationID() == x);
310
311     SkPath c(a);
312     REPORTER_ASSERT(reporter, c.getGenerationID() == x);
313
314     c.lineTo(3, 3);
315     const uint32_t w = c.getGenerationID();
316     REPORTER_ASSERT(reporter, b.getGenerationID() == x);
317     REPORTER_ASSERT(reporter, a.getGenerationID() == x);
318     REPORTER_ASSERT(reporter, w != x);
319
320 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
321     static bool kExpectGenIDToIgnoreFill = false;
322 #else
323     static bool kExpectGenIDToIgnoreFill = true;
324 #endif
325
326     c.toggleInverseFillType();
327     const uint32_t v = c.getGenerationID();
328     REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
329
330     c.rewind();
331     REPORTER_ASSERT(reporter, v != c.getGenerationID());
332 }
333
334 // This used to assert in the debug build, as the edges did not all line-up.
335 static void test_bad_cubic_crbug234190() {
336     SkPath path;
337     path.moveTo(13.8509f, 3.16858f);
338     path.cubicTo(-2.35893e+08f, -4.21044e+08f,
339                  -2.38991e+08f, -4.26573e+08f,
340                  -2.41016e+08f, -4.30188e+08f);
341
342     SkPaint paint;
343     paint.setAntiAlias(true);
344     auto surface(SkSurface::MakeRasterN32Premul(84, 88));
345     surface->getCanvas()->drawPath(path, paint);
346 }
347
348 static void test_bad_cubic_crbug229478() {
349     const SkPoint pts[] = {
350         { 4595.91064f,    -11596.9873f },
351         { 4597.2168f,    -11595.9414f },
352         { 4598.52344f,    -11594.8955f },
353         { 4599.83008f,    -11593.8496f },
354     };
355
356     SkPath path;
357     path.moveTo(pts[0]);
358     path.cubicTo(pts[1], pts[2], pts[3]);
359
360     SkPaint paint;
361     paint.setStyle(SkPaint::kStroke_Style);
362     paint.setStrokeWidth(20);
363
364     SkPath dst;
365     // Before the fix, this would infinite-recurse, and run out of stack
366     // because we would keep trying to subdivide a degenerate cubic segment.
367     paint.getFillPath(path, &dst, nullptr);
368 }
369
370 static void build_path_170666(SkPath& path) {
371     path.moveTo(17.9459f, 21.6344f);
372     path.lineTo(139.545f, -47.8105f);
373     path.lineTo(139.545f, -47.8105f);
374     path.lineTo(131.07f, -47.3888f);
375     path.lineTo(131.07f, -47.3888f);
376     path.lineTo(122.586f, -46.9532f);
377     path.lineTo(122.586f, -46.9532f);
378     path.lineTo(18076.6f, 31390.9f);
379     path.lineTo(18076.6f, 31390.9f);
380     path.lineTo(18085.1f, 31390.5f);
381     path.lineTo(18085.1f, 31390.5f);
382     path.lineTo(18076.6f, 31390.9f);
383     path.lineTo(18076.6f, 31390.9f);
384     path.lineTo(17955, 31460.3f);
385     path.lineTo(17955, 31460.3f);
386     path.lineTo(17963.5f, 31459.9f);
387     path.lineTo(17963.5f, 31459.9f);
388     path.lineTo(17971.9f, 31459.5f);
389     path.lineTo(17971.9f, 31459.5f);
390     path.lineTo(17.9551f, 21.6205f);
391     path.lineTo(17.9551f, 21.6205f);
392     path.lineTo(9.47091f, 22.0561f);
393     path.lineTo(9.47091f, 22.0561f);
394     path.lineTo(17.9459f, 21.6344f);
395     path.lineTo(17.9459f, 21.6344f);
396     path.close();path.moveTo(0.995934f, 22.4779f);
397     path.lineTo(0.986725f, 22.4918f);
398     path.lineTo(0.986725f, 22.4918f);
399     path.lineTo(17955, 31460.4f);
400     path.lineTo(17955, 31460.4f);
401     path.lineTo(17971.9f, 31459.5f);
402     path.lineTo(17971.9f, 31459.5f);
403     path.lineTo(18093.6f, 31390.1f);
404     path.lineTo(18093.6f, 31390.1f);
405     path.lineTo(18093.6f, 31390);
406     path.lineTo(18093.6f, 31390);
407     path.lineTo(139.555f, -47.8244f);
408     path.lineTo(139.555f, -47.8244f);
409     path.lineTo(122.595f, -46.9671f);
410     path.lineTo(122.595f, -46.9671f);
411     path.lineTo(0.995934f, 22.4779f);
412     path.lineTo(0.995934f, 22.4779f);
413     path.close();
414     path.moveTo(5.43941f, 25.5223f);
415     path.lineTo(798267, -28871.1f);
416     path.lineTo(798267, -28871.1f);
417     path.lineTo(3.12512e+06f, -113102);
418     path.lineTo(3.12512e+06f, -113102);
419     path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
420     path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
421     path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
422     path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
423     path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
424     path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
425     path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
426     path.lineTo(2.78271e+08f, -1.00733e+07f);
427     path.lineTo(2.78271e+08f, -1.00733e+07f);
428     path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
429     path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
430     path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
431     path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
432     path.lineTo(2.77473e+08f, -1.00444e+07f);
433     path.lineTo(2.77473e+08f, -1.00444e+07f);
434     path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
435     path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
436     path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
437     path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
438     path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
439     path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
440     path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
441     path.lineTo(798284, -28872);
442     path.lineTo(798284, -28872);
443     path.lineTo(22.4044f, 24.6677f);
444     path.lineTo(22.4044f, 24.6677f);
445     path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
446     path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
447     path.close();
448 }
449
450 static void build_path_simple_170666(SkPath& path) {
451     path.moveTo(126.677f, 24.1591f);
452     path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
453 }
454
455 // This used to assert in the SK_DEBUG build, as the clip step would fail with
456 // too-few interations in our cubic-line intersection code. That code now runs
457 // 24 interations (instead of 16).
458 static void test_crbug_170666() {
459     SkPath path;
460     SkPaint paint;
461     paint.setAntiAlias(true);
462
463     auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
464
465     build_path_simple_170666(path);
466     surface->getCanvas()->drawPath(path, paint);
467
468     build_path_170666(path);
469     surface->getCanvas()->drawPath(path, paint);
470 }
471
472
473 static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
474         SkScalar tx, SkScalar ty, SkScalar scale) {
475     SkPath smallPath;
476     SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
477     bool smallConvex = smallPath.isConvex();
478     SkPath largePath;
479     SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
480     SkMatrix matrix;
481     matrix.reset();
482     matrix.preTranslate(100, 100);
483     matrix.preScale(scale, scale);
484     largePath.transform(matrix);
485     bool largeConvex = largePath.isConvex();
486     REPORTER_ASSERT(reporter, smallConvex == largeConvex);
487 }
488
489 static void test_crbug_493450(skiatest::Reporter* reporter) {
490     const char reducedCase[] =
491         "M0,0"
492         "L0.0002, 0"
493         "L0.0002, 0.0002"
494         "L0.0001, 0.0001"
495         "L0,0.0002"
496         "Z";
497     test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
498     const char originalFiddleData[] =
499         "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
500         "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
501         "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
502         "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
503         "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
504     test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
505             826357.3384828606f);
506 }
507
508 static void test_crbug_495894(skiatest::Reporter* reporter) {
509     const char originalFiddleData[] =
510         "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
511         "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
512         "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
513         "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
514         "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
515         "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
516         "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
517         "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
518         "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
519         "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
520         "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
521         "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
522         "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
523         "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
524         "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
525         "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
526         "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
527         "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
528         "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
529         "L-0.33993994441916414,-0.11288906492739594Z";
530     test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
531             65536);
532 }
533
534 static void test_crbug_613918() {
535     SkPath path;
536     path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
537     path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
538     path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
539
540     // This call could lead to an assert or uninitialized read due to a failure
541     // to check the return value from SkCubicClipper::ChopMonoAtY.
542     path.contains(-1.84817e-08f, 1.15465e-08f);
543 }
544
545 static void test_addrect(skiatest::Reporter* reporter) {
546     SkPath path;
547     path.lineTo(0, 0);
548     path.addRect(SkRect::MakeWH(50, 100));
549     REPORTER_ASSERT(reporter, path.isRect(nullptr));
550
551     path.reset();
552     path.lineTo(FLT_EPSILON, FLT_EPSILON);
553     path.addRect(SkRect::MakeWH(50, 100));
554     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
555
556     path.reset();
557     path.quadTo(0, 0, 0, 0);
558     path.addRect(SkRect::MakeWH(50, 100));
559     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
560
561     path.reset();
562     path.conicTo(0, 0, 0, 0, 0.5f);
563     path.addRect(SkRect::MakeWH(50, 100));
564     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
565
566     path.reset();
567     path.cubicTo(0, 0, 0, 0, 0, 0);
568     path.addRect(SkRect::MakeWH(50, 100));
569     REPORTER_ASSERT(reporter, !path.isRect(nullptr));
570 }
571
572 // Make sure we stay non-finite once we get there (unless we reset or rewind).
573 static void test_addrect_isfinite(skiatest::Reporter* reporter) {
574     SkPath path;
575
576     path.addRect(SkRect::MakeWH(50, 100));
577     REPORTER_ASSERT(reporter, path.isFinite());
578
579     path.moveTo(0, 0);
580     path.lineTo(SK_ScalarInfinity, 42);
581     REPORTER_ASSERT(reporter, !path.isFinite());
582
583     path.addRect(SkRect::MakeWH(50, 100));
584     REPORTER_ASSERT(reporter, !path.isFinite());
585
586     path.reset();
587     REPORTER_ASSERT(reporter, path.isFinite());
588
589     path.addRect(SkRect::MakeWH(50, 100));
590     REPORTER_ASSERT(reporter, path.isFinite());
591 }
592
593 static void build_big_path(SkPath* path, bool reducedCase) {
594     if (reducedCase) {
595         path->moveTo(577330, 1971.72f);
596         path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
597     } else {
598         path->moveTo(60.1631f, 7.70567f);
599         path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
600         path->lineTo(577379, 1977.77f);
601         path->quadTo(577364, 1979.57f, 577325, 1980.26f);
602         path->quadTo(577286, 1980.95f, 577245, 1980.13f);
603         path->quadTo(577205, 1979.3f, 577187, 1977.45f);
604         path->quadTo(577168, 1975.6f, 577183, 1973.8f);
605         path->quadTo(577198, 1972, 577238, 1971.31f);
606         path->quadTo(577277, 1970.62f, 577317, 1971.45f);
607         path->quadTo(577330, 1971.72f, 577341, 1972.11f);
608         path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
609         path->moveTo(306.718f, -32.912f);
610         path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
611     }
612 }
613
614 static void test_clipped_cubic() {
615     auto surface(SkSurface::MakeRasterN32Premul(640, 480));
616
617     // This path used to assert, because our cubic-chopping code incorrectly
618     // moved control points after the chop. This test should be run in SK_DEBUG
619     // mode to ensure that we no long assert.
620     SkPath path;
621     for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
622         build_big_path(&path, SkToBool(doReducedCase));
623
624         SkPaint paint;
625         for (int doAA = 0; doAA <= 1; ++doAA) {
626             paint.setAntiAlias(SkToBool(doAA));
627             surface->getCanvas()->drawPath(path, paint);
628         }
629     }
630 }
631
632 static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
633     if (expected != bounds) {
634         ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
635                bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
636                expected.left(), expected.top(), expected.right(), expected.bottom());
637     }
638 }
639
640 static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
641     SkPath path;
642 #if 0
643     // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
644     // rewritten them to avoid this (compiler-bug?).
645     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
646
647     path.moveTo(-5, -8);
648     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
649
650     path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
651     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
652
653     path.moveTo(1, 2);
654     REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
655 #else
656     dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
657
658     path.moveTo(-5, -8);    // should set the bounds
659     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
660
661     path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
662     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
663
664     path.moveTo(1, 2);  // don't expect this to have changed the bounds
665     dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
666 #endif
667 }
668
669 #include "SkSurface.h"
670 static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
671     SkPath path;
672     path.moveTo(0, 0);
673     path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
674
675     SkPaint paint;
676     paint.setAntiAlias(true);
677
678     auto surf = SkSurface::MakeRasterN32Premul(100, 100);
679     surf->getCanvas()->drawPath(path, paint);
680 }
681
682 // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
683 // which triggered an assert, from a tricky cubic. This test replicates that
684 // example, so we can ensure that we handle it (in SkEdge.cpp), and don't
685 // assert in the SK_DEBUG build.
686 static void test_tricky_cubic() {
687     const SkPoint pts[] = {
688         { SkDoubleToScalar(18.8943768),    SkDoubleToScalar(129.121277) },
689         { SkDoubleToScalar(18.8937435),    SkDoubleToScalar(129.121689) },
690         { SkDoubleToScalar(18.8950119),    SkDoubleToScalar(129.120422) },
691         { SkDoubleToScalar(18.5030727),    SkDoubleToScalar(129.13121)  },
692     };
693
694     SkPath path;
695     path.moveTo(pts[0]);
696     path.cubicTo(pts[1], pts[2], pts[3]);
697
698     SkPaint paint;
699     paint.setAntiAlias(true);
700
701     SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
702 }
703
704 // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
705 //
706 static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
707     SkPath path;
708     path.quadTo(157, 366, 286, 208);
709     path.arcTo(37, 442, 315, 163, 957494590897113.0f);
710
711     SkMatrix matrix;
712     matrix.setScale(1000*1000, 1000*1000);
713
714     // Be sure that path::transform correctly updates isFinite and the bounds
715     // if the transformation overflows. The previous bug was that isFinite was
716     // set to true in this case, but the bounds were not set to empty (which
717     // they should be).
718     while (path.isFinite()) {
719         REPORTER_ASSERT(reporter, path.getBounds().isFinite());
720         REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
721         path.transform(matrix);
722     }
723     REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
724
725     matrix.setTranslate(SK_Scalar1, SK_Scalar1);
726     path.transform(matrix);
727     // we need to still be non-finite
728     REPORTER_ASSERT(reporter, !path.isFinite());
729     REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
730 }
731
732 static void add_corner_arc(SkPath* path, const SkRect& rect,
733                            SkScalar xIn, SkScalar yIn,
734                            int startAngle)
735 {
736
737     SkScalar rx = SkMinScalar(rect.width(), xIn);
738     SkScalar ry = SkMinScalar(rect.height(), yIn);
739
740     SkRect arcRect;
741     arcRect.set(-rx, -ry, rx, ry);
742     switch (startAngle) {
743     case 0:
744         arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
745         break;
746     case 90:
747         arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
748         break;
749     case 180:
750         arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
751         break;
752     case 270:
753         arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
754         break;
755     default:
756         break;
757     }
758
759     path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
760 }
761
762 static void make_arb_round_rect(SkPath* path, const SkRect& r,
763                                 SkScalar xCorner, SkScalar yCorner) {
764     // we are lazy here and use the same x & y for each corner
765     add_corner_arc(path, r, xCorner, yCorner, 270);
766     add_corner_arc(path, r, xCorner, yCorner, 0);
767     add_corner_arc(path, r, xCorner, yCorner, 90);
768     add_corner_arc(path, r, xCorner, yCorner, 180);
769     path->close();
770 }
771
772 // Chrome creates its own round rects with each corner possibly being different.
773 // Performance will suffer if they are not convex.
774 // Note: PathBench::ArbRoundRectBench performs almost exactly
775 // the same test (but with drawing)
776 static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
777     SkRandom rand;
778     SkRect r;
779
780     for (int i = 0; i < 5000; ++i) {
781
782         SkScalar size = rand.nextUScalar1() * 30;
783         if (size < SK_Scalar1) {
784             continue;
785         }
786         r.fLeft = rand.nextUScalar1() * 300;
787         r.fTop =  rand.nextUScalar1() * 300;
788         r.fRight =  r.fLeft + 2 * size;
789         r.fBottom = r.fTop + 2 * size;
790
791         SkPath temp;
792
793         make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
794
795         REPORTER_ASSERT(reporter, temp.isConvex());
796     }
797 }
798
799 // Chrome will sometimes create a 0 radius round rect. The degenerate
800 // quads prevent the path from being converted to a rect
801 // Note: PathBench::ArbRoundRectBench performs almost exactly
802 // the same test (but with drawing)
803 static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
804     SkRandom rand;
805     SkRect r;
806
807     for (int i = 0; i < 5000; ++i) {
808
809         SkScalar size = rand.nextUScalar1() * 30;
810         if (size < SK_Scalar1) {
811             continue;
812         }
813         r.fLeft = rand.nextUScalar1() * 300;
814         r.fTop =  rand.nextUScalar1() * 300;
815         r.fRight =  r.fLeft + 2 * size;
816         r.fBottom = r.fTop + 2 * size;
817
818         SkPath temp;
819
820         make_arb_round_rect(&temp, r, 0, 0);
821
822         SkRect result;
823         REPORTER_ASSERT(reporter, temp.isRect(&result));
824         REPORTER_ASSERT(reporter, r == result);
825     }
826 }
827
828 static void test_rect_isfinite(skiatest::Reporter* reporter) {
829     const SkScalar inf = SK_ScalarInfinity;
830     const SkScalar negInf = SK_ScalarNegativeInfinity;
831     const SkScalar nan = SK_ScalarNaN;
832
833     SkRect r;
834     r.setEmpty();
835     REPORTER_ASSERT(reporter, r.isFinite());
836     r.set(0, 0, inf, negInf);
837     REPORTER_ASSERT(reporter, !r.isFinite());
838     r.set(0, 0, nan, 0);
839     REPORTER_ASSERT(reporter, !r.isFinite());
840
841     SkPoint pts[] = {
842         { 0, 0 },
843         { SK_Scalar1, 0 },
844         { 0, SK_Scalar1 },
845     };
846
847     bool isFine = r.setBoundsCheck(pts, 3);
848     REPORTER_ASSERT(reporter, isFine);
849     REPORTER_ASSERT(reporter, !r.isEmpty());
850
851     pts[1].set(inf, 0);
852     isFine = r.setBoundsCheck(pts, 3);
853     REPORTER_ASSERT(reporter, !isFine);
854     REPORTER_ASSERT(reporter, r.isEmpty());
855
856     pts[1].set(nan, 0);
857     isFine = r.setBoundsCheck(pts, 3);
858     REPORTER_ASSERT(reporter, !isFine);
859     REPORTER_ASSERT(reporter, r.isEmpty());
860 }
861
862 static void test_path_isfinite(skiatest::Reporter* reporter) {
863     const SkScalar inf = SK_ScalarInfinity;
864     const SkScalar negInf = SK_ScalarNegativeInfinity;
865     const SkScalar nan = SK_ScalarNaN;
866
867     SkPath path;
868     REPORTER_ASSERT(reporter, path.isFinite());
869
870     path.reset();
871     REPORTER_ASSERT(reporter, path.isFinite());
872
873     path.reset();
874     path.moveTo(SK_Scalar1, 0);
875     REPORTER_ASSERT(reporter, path.isFinite());
876
877     path.reset();
878     path.moveTo(inf, negInf);
879     REPORTER_ASSERT(reporter, !path.isFinite());
880
881     path.reset();
882     path.moveTo(nan, 0);
883     REPORTER_ASSERT(reporter, !path.isFinite());
884 }
885
886 static void test_isfinite(skiatest::Reporter* reporter) {
887     test_rect_isfinite(reporter);
888     test_path_isfinite(reporter);
889 }
890
891 static void test_islastcontourclosed(skiatest::Reporter* reporter) {
892     SkPath path;
893     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
894     path.moveTo(0, 0);
895     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
896     path.close();
897     REPORTER_ASSERT(reporter, path.isLastContourClosed());
898     path.lineTo(100, 100);
899     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
900     path.moveTo(200, 200);
901     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
902     path.close();
903     REPORTER_ASSERT(reporter, path.isLastContourClosed());
904     path.moveTo(0, 0);
905     REPORTER_ASSERT(reporter, !path.isLastContourClosed());
906 }
907
908 // assert that we always
909 //  start with a moveTo
910 //  only have 1 moveTo
911 //  only have Lines after that
912 //  end with a single close
913 //  only have (at most) 1 close
914 //
915 static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
916                       const SkPoint srcPts[], bool expectClose) {
917     SkPath::RawIter iter(path);
918     SkPoint         pts[4];
919
920     bool firstTime = true;
921     bool foundClose = false;
922     for (;;) {
923         switch (iter.next(pts)) {
924             case SkPath::kMove_Verb:
925                 REPORTER_ASSERT(reporter, firstTime);
926                 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
927                 srcPts++;
928                 firstTime = false;
929                 break;
930             case SkPath::kLine_Verb:
931                 REPORTER_ASSERT(reporter, !firstTime);
932                 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
933                 srcPts++;
934                 break;
935             case SkPath::kQuad_Verb:
936                 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
937                 break;
938             case SkPath::kConic_Verb:
939                 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
940                 break;
941             case SkPath::kCubic_Verb:
942                 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
943                 break;
944             case SkPath::kClose_Verb:
945                 REPORTER_ASSERT(reporter, !firstTime);
946                 REPORTER_ASSERT(reporter, !foundClose);
947                 REPORTER_ASSERT(reporter, expectClose);
948                 foundClose = true;
949                 break;
950             case SkPath::kDone_Verb:
951                 goto DONE;
952         }
953     }
954 DONE:
955     REPORTER_ASSERT(reporter, foundClose == expectClose);
956 }
957
958 static void test_addPoly(skiatest::Reporter* reporter) {
959     SkPoint pts[32];
960     SkRandom rand;
961
962     for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
963         pts[i].fX = rand.nextSScalar1();
964         pts[i].fY = rand.nextSScalar1();
965     }
966
967     for (int doClose = 0; doClose <= 1; ++doClose) {
968         for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
969             SkPath path;
970             path.addPoly(pts, SkToInt(count), SkToBool(doClose));
971             test_poly(reporter, path, pts, SkToBool(doClose));
972         }
973     }
974 }
975
976 static void test_strokerec(skiatest::Reporter* reporter) {
977     SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
978     REPORTER_ASSERT(reporter, rec.isFillStyle());
979
980     rec.setHairlineStyle();
981     REPORTER_ASSERT(reporter, rec.isHairlineStyle());
982
983     rec.setStrokeStyle(SK_Scalar1, false);
984     REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
985
986     rec.setStrokeStyle(SK_Scalar1, true);
987     REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
988
989     rec.setStrokeStyle(0, false);
990     REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
991
992     rec.setStrokeStyle(0, true);
993     REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
994 }
995
996 // Set this for paths that don't have a consistent direction such as a bowtie.
997 // (cheapComputeDirection is not expected to catch these.)
998 const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
999
1000 static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
1001                             SkPathPriv::FirstDirection expected) {
1002     if (expected == kDontCheckDir) {
1003         return;
1004     }
1005     SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1006
1007     SkPathPriv::FirstDirection dir;
1008     if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
1009         REPORTER_ASSERT(reporter, dir == expected);
1010     } else {
1011         REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
1012     }
1013 }
1014
1015 static void test_direction(skiatest::Reporter* reporter) {
1016     size_t i;
1017     SkPath path;
1018     REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1019     REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1020     REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1021     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
1022
1023     static const char* gDegen[] = {
1024         "M 10 10",
1025         "M 10 10 M 20 20",
1026         "M 10 10 L 20 20",
1027         "M 10 10 L 10 10 L 10 10",
1028         "M 10 10 Q 10 10 10 10",
1029         "M 10 10 C 10 10 10 10 10 10",
1030     };
1031     for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1032         path.reset();
1033         bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1034         REPORTER_ASSERT(reporter, valid);
1035         REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1036     }
1037
1038     static const char* gCW[] = {
1039         "M 10 10 L 10 10 Q 20 10 20 20",
1040         "M 10 10 C 20 10 20 20 20 20",
1041         "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1042         // rect with top two corners replaced by cubics with identical middle
1043         // control points
1044         "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1045         "M 20 10 L 0 10 Q 10 10 20 0",  // left, degenerate serif
1046     };
1047     for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1048         path.reset();
1049         bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1050         REPORTER_ASSERT(reporter, valid);
1051         check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1052     }
1053
1054     static const char* gCCW[] = {
1055         "M 10 10 L 10 10 Q 20 10 20 -20",
1056         "M 10 10 C 20 10 20 -20 20 -20",
1057         "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1058         // rect with top two corners replaced by cubics with identical middle
1059         // control points
1060         "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1061         "M 10 10 L 30 10 Q 20 10 10 0",  // right, degenerate serif
1062     };
1063     for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1064         path.reset();
1065         bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1066         REPORTER_ASSERT(reporter, valid);
1067         check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1068     }
1069
1070     // Test two donuts, each wound a different direction. Only the outer contour
1071     // determines the cheap direction
1072     path.reset();
1073     path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1074     path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
1075     check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1076
1077     path.reset();
1078     path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1079     path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
1080     check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1081
1082     // triangle with one point really far from the origin.
1083     path.reset();
1084     // the first point is roughly 1.05e10, 1.05e10
1085     path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1086     path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1087     path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1088     check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1089
1090     path.reset();
1091     path.conicTo(20, 0, 20, 20, 0.5f);
1092     path.close();
1093     check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1094
1095     path.reset();
1096     path.lineTo(1, 1e7f);
1097     path.lineTo(1e7f, 2e7f);
1098     path.close();
1099     REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
1100     check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1101 }
1102
1103 static void add_rect(SkPath* path, const SkRect& r) {
1104     path->moveTo(r.fLeft, r.fTop);
1105     path->lineTo(r.fRight, r.fTop);
1106     path->lineTo(r.fRight, r.fBottom);
1107     path->lineTo(r.fLeft, r.fBottom);
1108     path->close();
1109 }
1110
1111 static void test_bounds(skiatest::Reporter* reporter) {
1112     static const SkRect rects[] = {
1113         { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1114         { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1115         { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1116         { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1117     };
1118
1119     SkPath path0, path1;
1120     for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1121         path0.addRect(rects[i]);
1122         add_rect(&path1, rects[i]);
1123     }
1124
1125     REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1126 }
1127
1128 static void stroke_cubic(const SkPoint pts[4]) {
1129     SkPath path;
1130     path.moveTo(pts[0]);
1131     path.cubicTo(pts[1], pts[2], pts[3]);
1132
1133     SkPaint paint;
1134     paint.setStyle(SkPaint::kStroke_Style);
1135     paint.setStrokeWidth(SK_Scalar1 * 2);
1136
1137     SkPath fill;
1138     paint.getFillPath(path, &fill);
1139 }
1140
1141 // just ensure this can run w/o any SkASSERTS firing in the debug build
1142 // we used to assert due to differences in how we determine a degenerate vector
1143 // but that was fixed with the introduction of SkPoint::CanNormalize
1144 static void stroke_tiny_cubic() {
1145     SkPoint p0[] = {
1146         { 372.0f,   92.0f },
1147         { 372.0f,   92.0f },
1148         { 372.0f,   92.0f },
1149         { 372.0f,   92.0f },
1150     };
1151
1152     stroke_cubic(p0);
1153
1154     SkPoint p1[] = {
1155         { 372.0f,       92.0f },
1156         { 372.0007f,    92.000755f },
1157         { 371.99927f,   92.003922f },
1158         { 371.99826f,   92.003899f },
1159     };
1160
1161     stroke_cubic(p1);
1162 }
1163
1164 static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1165     for (int i = 0; i < 2; ++i) {
1166         SkPath::Iter iter(path, SkToBool(i));
1167         SkPoint mv;
1168         SkPoint pts[4];
1169         SkPath::Verb v;
1170         int nMT = 0;
1171         int nCL = 0;
1172         mv.set(0, 0);
1173         while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1174             switch (v) {
1175                 case SkPath::kMove_Verb:
1176                     mv = pts[0];
1177                     ++nMT;
1178                     break;
1179                 case SkPath::kClose_Verb:
1180                     REPORTER_ASSERT(reporter, mv == pts[0]);
1181                     ++nCL;
1182                     break;
1183                 default:
1184                     break;
1185             }
1186         }
1187         // if we force a close on the interator we should have a close
1188         // for every moveTo
1189         REPORTER_ASSERT(reporter, !i || nMT == nCL);
1190     }
1191 }
1192
1193 static void test_close(skiatest::Reporter* reporter) {
1194     SkPath closePt;
1195     closePt.moveTo(0, 0);
1196     closePt.close();
1197     check_close(reporter, closePt);
1198
1199     SkPath openPt;
1200     openPt.moveTo(0, 0);
1201     check_close(reporter, openPt);
1202
1203     SkPath empty;
1204     check_close(reporter, empty);
1205     empty.close();
1206     check_close(reporter, empty);
1207
1208     SkPath rect;
1209     rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1210     check_close(reporter, rect);
1211     rect.close();
1212     check_close(reporter, rect);
1213
1214     SkPath quad;
1215     quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1216     check_close(reporter, quad);
1217     quad.close();
1218     check_close(reporter, quad);
1219
1220     SkPath cubic;
1221     quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1222                  10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1223     check_close(reporter, cubic);
1224     cubic.close();
1225     check_close(reporter, cubic);
1226
1227     SkPath line;
1228     line.moveTo(SK_Scalar1, SK_Scalar1);
1229     line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1230     check_close(reporter, line);
1231     line.close();
1232     check_close(reporter, line);
1233
1234     SkPath rect2;
1235     rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1236     rect2.close();
1237     rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1238     check_close(reporter, rect2);
1239     rect2.close();
1240     check_close(reporter, rect2);
1241
1242     SkPath oval3;
1243     oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1244     oval3.close();
1245     oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1246     check_close(reporter, oval3);
1247     oval3.close();
1248     check_close(reporter, oval3);
1249
1250     SkPath moves;
1251     moves.moveTo(SK_Scalar1, SK_Scalar1);
1252     moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1253     moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1254     moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1255     check_close(reporter, moves);
1256
1257     stroke_tiny_cubic();
1258 }
1259
1260 static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1261                             SkPath::Convexity expected) {
1262     SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1263     SkPath::Convexity c = copy.getConvexity();
1264     REPORTER_ASSERT(reporter, c == expected);
1265 }
1266
1267 static void test_path_crbug389050(skiatest::Reporter* reporter) {
1268     SkPath  tinyConvexPolygon;
1269     tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1270     tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1271     tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1272     tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1273     tinyConvexPolygon.close();
1274     tinyConvexPolygon.getConvexity();
1275     check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
1276     check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
1277
1278     SkPath  platTriangle;
1279     platTriangle.moveTo(0, 0);
1280     platTriangle.lineTo(200, 0);
1281     platTriangle.lineTo(100, 0.04f);
1282     platTriangle.close();
1283     platTriangle.getConvexity();
1284     check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1285
1286     platTriangle.reset();
1287     platTriangle.moveTo(0, 0);
1288     platTriangle.lineTo(200, 0);
1289     platTriangle.lineTo(100, 0.03f);
1290     platTriangle.close();
1291     platTriangle.getConvexity();
1292     check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1293 }
1294
1295 static void test_convexity2(skiatest::Reporter* reporter) {
1296     SkPath pt;
1297     pt.moveTo(0, 0);
1298     pt.close();
1299     check_convexity(reporter, pt, SkPath::kConvex_Convexity);
1300     check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
1301
1302     SkPath line;
1303     line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1304     line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1305     line.close();
1306     check_convexity(reporter, line, SkPath::kConvex_Convexity);
1307     check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
1308
1309     SkPath triLeft;
1310     triLeft.moveTo(0, 0);
1311     triLeft.lineTo(SK_Scalar1, 0);
1312     triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1313     triLeft.close();
1314     check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
1315     check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
1316
1317     SkPath triRight;
1318     triRight.moveTo(0, 0);
1319     triRight.lineTo(-SK_Scalar1, 0);
1320     triRight.lineTo(SK_Scalar1, SK_Scalar1);
1321     triRight.close();
1322     check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
1323     check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
1324
1325     SkPath square;
1326     square.moveTo(0, 0);
1327     square.lineTo(SK_Scalar1, 0);
1328     square.lineTo(SK_Scalar1, SK_Scalar1);
1329     square.lineTo(0, SK_Scalar1);
1330     square.close();
1331     check_convexity(reporter, square, SkPath::kConvex_Convexity);
1332     check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
1333
1334     SkPath redundantSquare;
1335     redundantSquare.moveTo(0, 0);
1336     redundantSquare.lineTo(0, 0);
1337     redundantSquare.lineTo(0, 0);
1338     redundantSquare.lineTo(SK_Scalar1, 0);
1339     redundantSquare.lineTo(SK_Scalar1, 0);
1340     redundantSquare.lineTo(SK_Scalar1, 0);
1341     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1342     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1343     redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1344     redundantSquare.lineTo(0, SK_Scalar1);
1345     redundantSquare.lineTo(0, SK_Scalar1);
1346     redundantSquare.lineTo(0, SK_Scalar1);
1347     redundantSquare.close();
1348     check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
1349     check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
1350
1351     SkPath bowTie;
1352     bowTie.moveTo(0, 0);
1353     bowTie.lineTo(0, 0);
1354     bowTie.lineTo(0, 0);
1355     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1356     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1357     bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1358     bowTie.lineTo(SK_Scalar1, 0);
1359     bowTie.lineTo(SK_Scalar1, 0);
1360     bowTie.lineTo(SK_Scalar1, 0);
1361     bowTie.lineTo(0, SK_Scalar1);
1362     bowTie.lineTo(0, SK_Scalar1);
1363     bowTie.lineTo(0, SK_Scalar1);
1364     bowTie.close();
1365     check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
1366     check_direction(reporter, bowTie, kDontCheckDir);
1367
1368     SkPath spiral;
1369     spiral.moveTo(0, 0);
1370     spiral.lineTo(100*SK_Scalar1, 0);
1371     spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1372     spiral.lineTo(0, 100*SK_Scalar1);
1373     spiral.lineTo(0, 50*SK_Scalar1);
1374     spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1375     spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1376     spiral.close();
1377     check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
1378     check_direction(reporter, spiral, kDontCheckDir);
1379
1380     SkPath dent;
1381     dent.moveTo(0, 0);
1382     dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1383     dent.lineTo(0, 100*SK_Scalar1);
1384     dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1385     dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1386     dent.close();
1387     check_convexity(reporter, dent, SkPath::kConcave_Convexity);
1388     check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
1389
1390     // https://bug.skia.org/2235
1391     SkPath strokedSin;
1392     for (int i = 0; i < 2000; i++) {
1393         SkScalar x = SkIntToScalar(i) / 2;
1394         SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1395         if (0 == i) {
1396             strokedSin.moveTo(x, y);
1397         } else {
1398             strokedSin.lineTo(x, y);
1399         }
1400     }
1401     SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1402     stroke.setStrokeStyle(2 * SK_Scalar1);
1403     stroke.applyToPath(&strokedSin, strokedSin);
1404     check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1405     check_direction(reporter, strokedSin, kDontCheckDir);
1406
1407     // http://crbug.com/412640
1408     SkPath degenerateConcave;
1409     degenerateConcave.moveTo(148.67912f, 191.875f);
1410     degenerateConcave.lineTo(470.37695f, 7.5f);
1411     degenerateConcave.lineTo(148.67912f, 191.875f);
1412     degenerateConcave.lineTo(41.446522f, 376.25f);
1413     degenerateConcave.lineTo(-55.971577f, 460.0f);
1414     degenerateConcave.lineTo(41.446522f, 376.25f);
1415     check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
1416     check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
1417
1418     // http://crbug.com/433683
1419     SkPath badFirstVector;
1420     badFirstVector.moveTo(501.087708f, 319.610352f);
1421     badFirstVector.lineTo(501.087708f, 319.610352f);
1422     badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1423     badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1424     badFirstVector.lineTo(301.557678f, 98.044601f);
1425     badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1426     badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1427     badFirstVector.lineTo(504.912292f, 316.389648f);
1428     badFirstVector.lineTo(501.087708f, 319.610352f);
1429     badFirstVector.close();
1430     check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
1431 }
1432
1433 static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1434                                 const SkRect& bounds) {
1435     REPORTER_ASSERT(reporter, p.isConvex());
1436     REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1437
1438     SkPath p2(p);
1439     REPORTER_ASSERT(reporter, p2.isConvex());
1440     REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1441
1442     SkPath other;
1443     other.swap(p2);
1444     REPORTER_ASSERT(reporter, other.isConvex());
1445     REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1446 }
1447
1448 static void setFromString(SkPath* path, const char str[]) {
1449     bool first = true;
1450     while (str) {
1451         SkScalar x, y;
1452         str = SkParse::FindScalar(str, &x);
1453         if (nullptr == str) {
1454             break;
1455         }
1456         str = SkParse::FindScalar(str, &y);
1457         SkASSERT(str);
1458         if (first) {
1459             path->moveTo(x, y);
1460             first = false;
1461         } else {
1462             path->lineTo(x, y);
1463         }
1464     }
1465 }
1466
1467 static void test_convexity(skiatest::Reporter* reporter) {
1468     SkPath path;
1469
1470     check_convexity(reporter, path, SkPath::kConvex_Convexity);
1471     path.addCircle(0, 0, SkIntToScalar(10));
1472     check_convexity(reporter, path, SkPath::kConvex_Convexity);
1473     path.addCircle(0, 0, SkIntToScalar(10));   // 2nd circle
1474     check_convexity(reporter, path, SkPath::kConcave_Convexity);
1475
1476     path.reset();
1477     path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
1478     check_convexity(reporter, path, SkPath::kConvex_Convexity);
1479     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1480
1481     path.reset();
1482     path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
1483     check_convexity(reporter, path, SkPath::kConvex_Convexity);
1484     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1485
1486     static const struct {
1487         const char*                 fPathStr;
1488         SkPath::Convexity           fExpectedConvexity;
1489         SkPathPriv::FirstDirection  fExpectedDirection;
1490     } gRec[] = {
1491         { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1492         { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1493         { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1494         { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1495         { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1496         { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
1497         { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1498         { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
1499     };
1500
1501     for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1502         SkPath path;
1503         setFromString(&path, gRec[i].fPathStr);
1504         check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1505         check_direction(reporter, path, gRec[i].fExpectedDirection);
1506         // check after setting the initial convex and direction
1507         if (kDontCheckDir != gRec[i].fExpectedDirection) {
1508             SkPath copy(path);
1509             SkPathPriv::FirstDirection dir;
1510             bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1511             REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
1512                     ^ foundDir);
1513             REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1514             check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1515         }
1516         REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1517         check_direction(reporter, path, gRec[i].fExpectedDirection);
1518     }
1519
1520     static const SkPoint nonFinitePts[] = {
1521         { SK_ScalarInfinity, 0 },
1522         { 0, SK_ScalarInfinity },
1523         { SK_ScalarInfinity, SK_ScalarInfinity },
1524         { SK_ScalarNegativeInfinity, 0},
1525         { 0, SK_ScalarNegativeInfinity },
1526         { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1527         { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1528         { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1529         { SK_ScalarNaN, 0 },
1530         { 0, SK_ScalarNaN },
1531         { SK_ScalarNaN, SK_ScalarNaN },
1532     };
1533
1534     const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1535
1536     static const SkPoint finitePts[] = {
1537         { SK_ScalarMax, 0 },
1538         { 0, SK_ScalarMax },
1539         { SK_ScalarMax, SK_ScalarMax },
1540         { SK_ScalarMin, 0 },
1541         { 0, SK_ScalarMin },
1542         { SK_ScalarMin, SK_ScalarMin },
1543     };
1544
1545     const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1546
1547     for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1548         int i = (int) (index % nonFinitePtsCount);
1549         int f = (int) (index % finitePtsCount);
1550         int g = (int) ((f + 1) % finitePtsCount);
1551         path.reset();
1552         switch (index % 13) {
1553             case 0: path.lineTo(nonFinitePts[i]); break;
1554             case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1555             case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1556             case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1557             case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1558             case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1559             case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1560             case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1561             case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1562             case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1563             case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1564             case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1565             case 12: path.moveTo(nonFinitePts[i]); break;
1566         }
1567         check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1568     }
1569
1570     for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1571         int f = (int) (index % finitePtsCount);
1572         int g = (int) ((f + 1) % finitePtsCount);
1573         path.reset();
1574         int curveSelect = index % 11;
1575         switch (curveSelect) {
1576             case 0: path.moveTo(finitePts[f]); break;
1577             case 1: path.lineTo(finitePts[f]); break;
1578             case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1579             case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1580             case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1581             case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1582             case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1583             case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1584             case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1585             case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1586             case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1587         }
1588         check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1589                 : SkPath::kUnknown_Convexity);
1590     }
1591
1592 }
1593
1594 static void test_isLine(skiatest::Reporter* reporter) {
1595     SkPath path;
1596     SkPoint pts[2];
1597     const SkScalar value = SkIntToScalar(5);
1598
1599     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1600
1601     // set some non-zero values
1602     pts[0].set(value, value);
1603     pts[1].set(value, value);
1604     REPORTER_ASSERT(reporter, !path.isLine(pts));
1605     // check that pts was untouched
1606     REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1607     REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1608
1609     const SkScalar moveX = SkIntToScalar(1);
1610     const SkScalar moveY = SkIntToScalar(2);
1611     REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1612
1613     path.moveTo(moveX, moveY);
1614     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1615     REPORTER_ASSERT(reporter, !path.isLine(pts));
1616     // check that pts was untouched
1617     REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1618     REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1619
1620     const SkScalar lineX = SkIntToScalar(2);
1621     const SkScalar lineY = SkIntToScalar(2);
1622     REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1623
1624     path.lineTo(lineX, lineY);
1625     REPORTER_ASSERT(reporter, path.isLine(nullptr));
1626
1627     REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1628     REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1629     REPORTER_ASSERT(reporter, path.isLine(pts));
1630     REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1631     REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1632
1633     path.lineTo(0, 0);  // too many points/verbs
1634     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1635     REPORTER_ASSERT(reporter, !path.isLine(pts));
1636     REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1637     REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1638
1639     path.reset();
1640     path.quadTo(1, 1, 2, 2);
1641     REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1642 }
1643
1644 static void test_conservativelyContains(skiatest::Reporter* reporter) {
1645     SkPath path;
1646
1647     // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1648     static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1649
1650     // A circle that bounds kBaseRect (with a significant amount of slop)
1651     SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1652     circleR = SkScalarMul(circleR, 1.75f) / 2;
1653     static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1654
1655     // round-rect radii
1656     static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1657
1658     static const struct SUPPRESS_VISIBILITY_WARNING {
1659         SkRect fQueryRect;
1660         bool   fInRect;
1661         bool   fInCircle;
1662         bool   fInRR;
1663         bool   fInCubicRR;
1664     } kQueries[] = {
1665         {kBaseRect, true, true, false, false},
1666
1667         // rect well inside of kBaseRect
1668         {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1669                           kBaseRect.fTop + 0.25f*kBaseRect.height(),
1670                           kBaseRect.fRight - 0.25f*kBaseRect.width(),
1671                           kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1672                           true, true, true, true},
1673
1674         // rects with edges off by one from kBaseRect's edges
1675         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1676                           kBaseRect.width(), kBaseRect.height() + 1),
1677          false, true, false, false},
1678         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1679                           kBaseRect.width() + 1, kBaseRect.height()),
1680          false, true, false, false},
1681         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1682                           kBaseRect.width() + 1, kBaseRect.height() + 1),
1683          false, true, false, false},
1684         {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1685                           kBaseRect.width(), kBaseRect.height()),
1686          false, true, false, false},
1687         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1688                           kBaseRect.width(), kBaseRect.height()),
1689          false, true, false, false},
1690         {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1691                           kBaseRect.width() + 2, kBaseRect.height()),
1692          false, true, false, false},
1693         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1694                           kBaseRect.width() + 2, kBaseRect.height()),
1695          false, true, false, false},
1696
1697         // zero-w/h rects at each corner of kBaseRect
1698         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1699         {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1700         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1701         {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1702
1703         // far away rect
1704         {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1705                           SkIntToScalar(10), SkIntToScalar(10)),
1706          false, false, false, false},
1707
1708         // very large rect containing kBaseRect
1709         {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1710                           kBaseRect.fTop - 5 * kBaseRect.height(),
1711                           11 * kBaseRect.width(), 11 * kBaseRect.height()),
1712          false, false, false, false},
1713
1714         // skinny rect that spans same y-range as kBaseRect
1715         {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1716                           SkIntToScalar(1), kBaseRect.height()),
1717          true, true, true, true},
1718
1719         // short rect that spans same x-range as kBaseRect
1720         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1721          true, true, true, true},
1722
1723         // skinny rect that spans slightly larger y-range than kBaseRect
1724         {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1725                           SkIntToScalar(1), kBaseRect.height() + 1),
1726          false, true, false, false},
1727
1728         // short rect that spans slightly larger x-range than kBaseRect
1729         {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1730                           kBaseRect.width() + 1, SkScalar(1)),
1731          false, true, false, false},
1732     };
1733
1734     for (int inv = 0; inv < 4; ++inv) {
1735         for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1736             SkRect qRect = kQueries[q].fQueryRect;
1737             if (inv & 0x1) {
1738                 SkTSwap(qRect.fLeft, qRect.fRight);
1739             }
1740             if (inv & 0x2) {
1741                 SkTSwap(qRect.fTop, qRect.fBottom);
1742             }
1743             for (int d = 0; d < 2; ++d) {
1744                 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1745                 path.reset();
1746                 path.addRect(kBaseRect, dir);
1747                 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1748                                           path.conservativelyContainsRect(qRect));
1749
1750                 path.reset();
1751                 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1752                 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1753                                           path.conservativelyContainsRect(qRect));
1754
1755                 path.reset();
1756                 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1757                 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1758                                           path.conservativelyContainsRect(qRect));
1759
1760                 path.reset();
1761                 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1762                 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1763                              kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1764                              kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1765                 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1766                 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1767                 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1768                 path.close();
1769                 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1770                                           path.conservativelyContainsRect(qRect));
1771
1772             }
1773             // Slightly non-convex shape, shouldn't contain any rects.
1774             path.reset();
1775             path.moveTo(0, 0);
1776             path.lineTo(SkIntToScalar(50), 0.05f);
1777             path.lineTo(SkIntToScalar(100), 0);
1778             path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1779             path.lineTo(0, SkIntToScalar(100));
1780             path.close();
1781             REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1782         }
1783     }
1784
1785     // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1786     path.reset();
1787     path.moveTo(0, 0);
1788     path.lineTo(SkIntToScalar(100), 0);
1789     path.lineTo(0, SkIntToScalar(100));
1790
1791     // inside, on along top edge
1792     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1793                                                                                SkIntToScalar(10),
1794                                                                                SkIntToScalar(10))));
1795     // above
1796     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1797         SkRect::MakeXYWH(SkIntToScalar(50),
1798                          SkIntToScalar(-10),
1799                          SkIntToScalar(10),
1800                          SkIntToScalar(10))));
1801     // to the left
1802     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1803                                                                                 SkIntToScalar(5),
1804                                                                                 SkIntToScalar(5),
1805                                                                                 SkIntToScalar(5))));
1806
1807     // outside the diagonal edge
1808     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1809                                                                                 SkIntToScalar(200),
1810                                                                                 SkIntToScalar(20),
1811                                                                                 SkIntToScalar(5))));
1812
1813
1814     // Test that multiple move commands do not cause asserts.
1815
1816     // At the time of writing, this would not modify cached convexity. This caused an assert while
1817     // checking conservative containment again. https://bug.skia.org/1460
1818     path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1819 #if 0
1820     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1821                                                                                SkIntToScalar(10),
1822                                                                                SkIntToScalar(10))));
1823 #endif
1824
1825     // Same as above path and first test but with an extra moveTo.
1826     path.reset();
1827     path.moveTo(100, 100);
1828     path.moveTo(0, 0);
1829     path.lineTo(SkIntToScalar(100), 0);
1830     path.lineTo(0, SkIntToScalar(100));
1831
1832     REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1833                                                                                SkIntToScalar(10),
1834                                                                                SkIntToScalar(10))));
1835
1836     // Test that multiple move commands do not cause asserts and that the function
1837     // is not confused by the multiple moves.
1838     path.reset();
1839     path.moveTo(0, 0);
1840     path.lineTo(SkIntToScalar(100), 0);
1841     path.lineTo(0, SkIntToScalar(100));
1842     path.moveTo(0, SkIntToScalar(200));
1843     path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1844     path.lineTo(0, SkIntToScalar(300));
1845
1846     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1847                                                             SkRect::MakeXYWH(SkIntToScalar(50), 0,
1848                                                                              SkIntToScalar(10),
1849                                                                              SkIntToScalar(10))));
1850
1851     path.reset();
1852     path.lineTo(100, 100);
1853     REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
1854 }
1855
1856 static void test_isRect_open_close(skiatest::Reporter* reporter) {
1857     SkPath path;
1858     bool isClosed;
1859
1860     path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1861     path.close();
1862
1863     REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
1864     REPORTER_ASSERT(reporter, isClosed);
1865 }
1866
1867 // Simple isRect test is inline TestPath, below.
1868 // test_isRect provides more extensive testing.
1869 static void test_isRect(skiatest::Reporter* reporter) {
1870     test_isRect_open_close(reporter);
1871
1872     // passing tests (all moveTo / lineTo...
1873     SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1874     SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1875     SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1876     SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1877     SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1878     SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1879     SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1880     SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1881     SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1882     SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1883     SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
1884     SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1885     SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1886     SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
1887     SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
1888
1889     // failing tests
1890     SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1891     SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1892     SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1893     SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1894     SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1895     SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1896     SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1897     SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1898     SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1899     SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1900     SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
1901
1902     // no close, but we should detect them as fillably the same as a rect
1903     SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1904     SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1905     SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1906
1907     // like c2, but we double-back on ourselves
1908     SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1909     // like c2, but we overshoot the start point
1910     SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1911     SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
1912
1913     struct IsRectTest {
1914         SkPoint *fPoints;
1915         int fPointCount;
1916         bool fClose;
1917         bool fIsRect;
1918     } tests[] = {
1919         { r1, SK_ARRAY_COUNT(r1), true, true },
1920         { r2, SK_ARRAY_COUNT(r2), true, true },
1921         { r3, SK_ARRAY_COUNT(r3), true, true },
1922         { r4, SK_ARRAY_COUNT(r4), true, true },
1923         { r5, SK_ARRAY_COUNT(r5), true, true },
1924         { r6, SK_ARRAY_COUNT(r6), true, true },
1925         { r7, SK_ARRAY_COUNT(r7), true, true },
1926         { r8, SK_ARRAY_COUNT(r8), true, true },
1927         { r9, SK_ARRAY_COUNT(r9), true, true },
1928         { ra, SK_ARRAY_COUNT(ra), true, true },
1929         { rb, SK_ARRAY_COUNT(rb), true, true },
1930         { rc, SK_ARRAY_COUNT(rc), true, true },
1931         { rd, SK_ARRAY_COUNT(rd), true, true },
1932         { re, SK_ARRAY_COUNT(re), true, true },
1933         { rf, SK_ARRAY_COUNT(rf), true, true },
1934
1935         { f1, SK_ARRAY_COUNT(f1), true, false },
1936         { f2, SK_ARRAY_COUNT(f2), true, false },
1937         { f3, SK_ARRAY_COUNT(f3), true, false },
1938         { f4, SK_ARRAY_COUNT(f4), true, false },
1939         { f5, SK_ARRAY_COUNT(f5), true, false },
1940         { f6, SK_ARRAY_COUNT(f6), true, false },
1941         { f7, SK_ARRAY_COUNT(f7), true, false },
1942         { f8, SK_ARRAY_COUNT(f8), true, false },
1943         { f9, SK_ARRAY_COUNT(f9), true, false },
1944         { fa, SK_ARRAY_COUNT(fa), true, false },
1945         { fb, SK_ARRAY_COUNT(fb), true, false },
1946
1947         { c1, SK_ARRAY_COUNT(c1), false, true },
1948         { c2, SK_ARRAY_COUNT(c2), false, true },
1949         { c3, SK_ARRAY_COUNT(c3), false, true },
1950
1951         { d1, SK_ARRAY_COUNT(d1), false, false },
1952         { d2, SK_ARRAY_COUNT(d2), false, false },
1953         { d3, SK_ARRAY_COUNT(d3), false, false },
1954     };
1955
1956     const size_t testCount = SK_ARRAY_COUNT(tests);
1957     int index;
1958     for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1959         SkPath path;
1960         path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1961         for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1962             path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
1963         }
1964         if (tests[testIndex].fClose) {
1965             path.close();
1966         }
1967         REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
1968
1969         if (tests[testIndex].fIsRect) {
1970             SkRect computed, expected;
1971             bool isClosed;
1972             SkPath::Direction direction;
1973             SkPathPriv::FirstDirection cheapDirection;
1974             expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
1975             REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
1976             REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1977             REPORTER_ASSERT(reporter, expected == computed);
1978             REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
1979             REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
1980         } else {
1981             SkRect computed;
1982             computed.set(123, 456, 789, 1011);
1983             bool isClosed = (bool)-1;
1984             SkPath::Direction direction = (SkPath::Direction) - 1;
1985             REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
1986             REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1987             REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
1988             REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1989             REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
1990         }
1991     }
1992
1993     // fail, close then line
1994     SkPath path1;
1995     path1.moveTo(r1[0].fX, r1[0].fY);
1996     for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
1997         path1.lineTo(r1[index].fX, r1[index].fY);
1998     }
1999     path1.close();
2000     path1.lineTo(1, 0);
2001     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2002
2003     // fail, move in the middle
2004     path1.reset();
2005     path1.moveTo(r1[0].fX, r1[0].fY);
2006     for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2007         if (index == 2) {
2008             path1.moveTo(1, .5f);
2009         }
2010         path1.lineTo(r1[index].fX, r1[index].fY);
2011     }
2012     path1.close();
2013     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2014
2015     // fail, move on the edge
2016     path1.reset();
2017     for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2018         path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2019         path1.lineTo(r1[index].fX, r1[index].fY);
2020     }
2021     path1.close();
2022     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2023
2024     // fail, quad
2025     path1.reset();
2026     path1.moveTo(r1[0].fX, r1[0].fY);
2027     for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2028         if (index == 2) {
2029             path1.quadTo(1, .5f, 1, .5f);
2030         }
2031         path1.lineTo(r1[index].fX, r1[index].fY);
2032     }
2033     path1.close();
2034     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2035
2036     // fail, cubic
2037     path1.reset();
2038     path1.moveTo(r1[0].fX, r1[0].fY);
2039     for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2040         if (index == 2) {
2041             path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2042         }
2043         path1.lineTo(r1[index].fX, r1[index].fY);
2044     }
2045     path1.close();
2046     REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2047 }
2048
2049 static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2050                                      const SkRect& rect, SkPath::Direction dir, unsigned start) {
2051     SkRect r = SkRect::MakeEmpty();
2052     SkPath::Direction d = SkPath::kCCW_Direction;
2053     unsigned s = ~0U;
2054
2055     REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2056     REPORTER_ASSERT(reporter, r == rect);
2057     REPORTER_ASSERT(reporter, d == dir);
2058     REPORTER_ASSERT(reporter, s == start);
2059 }
2060
2061 static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2062     SkRect r = SkRect::MakeEmpty();
2063     SkPath::Direction d = SkPath::kCCW_Direction;
2064     unsigned s = ~0U;
2065
2066     const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2067     const SkRect emptyRect = SkRect::MakeEmpty();
2068     SkPath path;
2069     for (int start = 0; start < 4; ++start) {
2070         for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2071             SkPath path;
2072             path.addRect(testRect, dir, start);
2073             check_simple_closed_rect(reporter, path, testRect, dir, start);
2074             path.close();
2075             check_simple_closed_rect(reporter, path, testRect, dir, start);
2076             SkPath path2 = path;
2077             path2.lineTo(10, 10);
2078             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2079             path2 = path;
2080             path2.moveTo(10, 10);
2081             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2082             path2 = path;
2083             path2.addRect(testRect, dir, start);
2084             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2085             // Make the path by hand, manually closing it.
2086             path2.reset();
2087             SkPath::RawIter iter(path);
2088             SkPath::Verb v;
2089             SkPoint verbPts[4];
2090             SkPoint firstPt = {0.f, 0.f};
2091             while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2092                 switch(v) {
2093                     case SkPath::kMove_Verb:
2094                         firstPt = verbPts[0];
2095                         path2.moveTo(verbPts[0]);
2096                         break;
2097                     case SkPath::kLine_Verb:
2098                         path2.lineTo(verbPts[1]);
2099                         break;
2100                     default:
2101                         break;
2102                 }
2103             }
2104             // We haven't closed it yet...
2105             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2106             // ... now we do and test again.
2107             path2.lineTo(firstPt);
2108             check_simple_closed_rect(reporter, path2, testRect, dir, start);
2109             // A redundant close shouldn't cause a failure.
2110             path2.close();
2111             check_simple_closed_rect(reporter, path2, testRect, dir, start);
2112             // Degenerate point and line rects are not allowed
2113             path2.reset();
2114             path2.addRect(emptyRect, dir, start);
2115             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2116             SkRect degenRect = testRect;
2117             degenRect.fLeft = degenRect.fRight;
2118             path2.reset();
2119             path2.addRect(degenRect, dir, start);
2120             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2121             degenRect = testRect;
2122             degenRect.fTop = degenRect.fBottom;
2123             path2.reset();
2124             path2.addRect(degenRect, dir, start);
2125             REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2126             // An inverted rect makes a rect path, but changes the winding dir and start point.
2127             SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2128                                             ? SkPath::kCCW_Direction
2129                                             : SkPath::kCW_Direction;
2130             static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2131             static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2132             SkRect swapRect = testRect;
2133             SkTSwap(swapRect.fLeft, swapRect.fRight);
2134             path2.reset();
2135             path2.addRect(swapRect, dir, start);
2136             check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2137             swapRect = testRect;
2138             SkTSwap(swapRect.fTop, swapRect.fBottom);
2139             path2.reset();
2140             path2.addRect(swapRect, dir, start);
2141             check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2142         }
2143     }
2144     // down, up, left, close
2145     path.reset();
2146     path.moveTo(1, 1);
2147     path.lineTo(1, 2);
2148     path.lineTo(1, 1);
2149     path.lineTo(0, 1);
2150     SkRect rect;
2151     SkPath::Direction  dir;
2152     unsigned start;
2153     path.close();
2154     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2155     // right, left, up, close
2156     path.reset();
2157     path.moveTo(1, 1);
2158     path.lineTo(2, 1);
2159     path.lineTo(1, 1);
2160     path.lineTo(1, 0);
2161     path.close();
2162     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2163     // parallelogram with horizontal edges
2164     path.reset();
2165     path.moveTo(1, 0);
2166     path.lineTo(3, 0);
2167     path.lineTo(2, 1);
2168     path.lineTo(0, 1);
2169     path.close();
2170     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2171     // parallelogram with vertical edges
2172     path.reset();
2173     path.moveTo(0, 1);
2174     path.lineTo(0, 3);
2175     path.lineTo(1, 2);
2176     path.lineTo(1, 0);
2177     path.close();
2178     REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2179
2180 }
2181
2182 static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2183     // passing tests (all moveTo / lineTo...
2184     SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2185     SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2186     SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2187     SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2188     SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2189     SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2190     SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2191     SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2192     SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2193     SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2194     SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2195     SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2196     SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2197     SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2198
2199     // failing tests
2200     SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2201     SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2202     SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2203     SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2204     SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2205     SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2206     SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2207     SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2208
2209     // success, no close is OK
2210     SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2211     SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2212
2213     struct IsNestedRectTest {
2214         SkPoint *fPoints;
2215         int fPointCount;
2216         SkPathPriv::FirstDirection fDirection;
2217         bool fClose;
2218         bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2219     } tests[] = {
2220         { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2221         { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2222         { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2223         { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2224         { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2225         { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2226         { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2227         { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2228         { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2229         { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2230         { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection,  true, true },
2231         { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection,  true, true },
2232         { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2233         { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection,  true, true },
2234
2235         { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2236         { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2237         { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2238         { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2239         { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2240         { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2241         { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2242         { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2243
2244         { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2245         { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2246     };
2247
2248     const size_t testCount = SK_ARRAY_COUNT(tests);
2249     int index;
2250     for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2251         for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2252             SkPath path;
2253             if (rectFirst) {
2254                 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2255             }
2256             path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2257             for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2258                 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2259             }
2260             if (tests[testIndex].fClose) {
2261                 path.close();
2262             }
2263             if (!rectFirst) {
2264                 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2265             }
2266             REPORTER_ASSERT(reporter,
2267                     tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
2268             if (tests[testIndex].fIsNestedRect) {
2269                 SkRect expected[2], computed[2];
2270                 SkPathPriv::FirstDirection expectedDirs[2];
2271                 SkPath::Direction computedDirs[2];
2272                 SkRect testBounds;
2273                 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
2274                 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2275                 expected[1] = testBounds;
2276                 if (rectFirst) {
2277                     expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2278                 } else {
2279                     expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2280                 }
2281                 expectedDirs[1] = tests[testIndex].fDirection;
2282                 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
2283                 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2284                 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2285                 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2286                 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2287             }
2288         }
2289
2290         // fail, close then line
2291         SkPath path1;
2292         if (rectFirst) {
2293             path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2294         }
2295         path1.moveTo(r1[0].fX, r1[0].fY);
2296         for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2297             path1.lineTo(r1[index].fX, r1[index].fY);
2298         }
2299         path1.close();
2300         path1.lineTo(1, 0);
2301         if (!rectFirst) {
2302             path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2303         }
2304         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2305
2306         // fail, move in the middle
2307         path1.reset();
2308         if (rectFirst) {
2309             path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2310         }
2311         path1.moveTo(r1[0].fX, r1[0].fY);
2312         for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2313             if (index == 2) {
2314                 path1.moveTo(1, .5f);
2315             }
2316             path1.lineTo(r1[index].fX, r1[index].fY);
2317         }
2318         path1.close();
2319         if (!rectFirst) {
2320             path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2321         }
2322         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2323
2324         // fail, move on the edge
2325         path1.reset();
2326         if (rectFirst) {
2327             path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2328         }
2329         for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2330             path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2331             path1.lineTo(r1[index].fX, r1[index].fY);
2332         }
2333         path1.close();
2334         if (!rectFirst) {
2335             path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2336         }
2337         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2338
2339         // fail, quad
2340         path1.reset();
2341         if (rectFirst) {
2342             path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2343         }
2344         path1.moveTo(r1[0].fX, r1[0].fY);
2345         for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2346             if (index == 2) {
2347                 path1.quadTo(1, .5f, 1, .5f);
2348             }
2349             path1.lineTo(r1[index].fX, r1[index].fY);
2350         }
2351         path1.close();
2352         if (!rectFirst) {
2353             path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2354         }
2355         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2356
2357         // fail, cubic
2358         path1.reset();
2359         if (rectFirst) {
2360             path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2361         }
2362         path1.moveTo(r1[0].fX, r1[0].fY);
2363         for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2364             if (index == 2) {
2365                 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2366             }
2367             path1.lineTo(r1[index].fX, r1[index].fY);
2368         }
2369         path1.close();
2370         if (!rectFirst) {
2371             path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2372         }
2373         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2374
2375         // fail,  not nested
2376         path1.reset();
2377         path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2378         path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
2379         REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2380     }
2381
2382     //  pass, constructed explicitly from manually closed rects specified as moves/lines.
2383     SkPath path;
2384     path.moveTo(0, 0);
2385     path.lineTo(10, 0);
2386     path.lineTo(10, 10);
2387     path.lineTo(0, 10);
2388     path.lineTo(0, 0);
2389     path.moveTo(1, 1);
2390     path.lineTo(9, 1);
2391     path.lineTo(9, 9);
2392     path.lineTo(1, 9);
2393     path.lineTo(1, 1);
2394     REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
2395
2396     // pass, stroke rect
2397     SkPath src, dst;
2398     src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2399     SkPaint strokePaint;
2400     strokePaint.setStyle(SkPaint::kStroke_Style);
2401     strokePaint.setStrokeWidth(2);
2402     strokePaint.getFillPath(src, &dst);
2403     REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
2404 }
2405
2406 static void write_and_read_back(skiatest::Reporter* reporter,
2407                                 const SkPath& p) {
2408     SkWriter32 writer;
2409     writer.writePath(p);
2410     size_t size = writer.bytesWritten();
2411     SkAutoMalloc storage(size);
2412     writer.flatten(storage.get());
2413     SkReader32 reader(storage.get(), size);
2414
2415     SkPath readBack;
2416     REPORTER_ASSERT(reporter, readBack != p);
2417     reader.readPath(&readBack);
2418     REPORTER_ASSERT(reporter, readBack == p);
2419
2420     REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
2421                               p.getConvexityOrUnknown());
2422
2423     SkRect oval0, oval1;
2424     SkPath::Direction dir0, dir1;
2425     unsigned start0, start1;
2426     REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
2427     if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2428         REPORTER_ASSERT(reporter, oval0 == oval1);
2429         REPORTER_ASSERT(reporter, dir0 == dir1);
2430         REPORTER_ASSERT(reporter, start0 == start1);
2431     }
2432     REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2433     SkRRect rrect0, rrect1;
2434     if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2435         REPORTER_ASSERT(reporter, rrect0 == rrect1);
2436         REPORTER_ASSERT(reporter, dir0 == dir1);
2437         REPORTER_ASSERT(reporter, start0 == start1);
2438     }
2439     const SkRect& origBounds = p.getBounds();
2440     const SkRect& readBackBounds = readBack.getBounds();
2441
2442     REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2443 }
2444
2445 static void test_flattening(skiatest::Reporter* reporter) {
2446     SkPath p;
2447
2448     static const SkPoint pts[] = {
2449         { 0, 0 },
2450         { SkIntToScalar(10), SkIntToScalar(10) },
2451         { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2452         { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2453     };
2454     p.moveTo(pts[0]);
2455     p.lineTo(pts[1]);
2456     p.quadTo(pts[2], pts[3]);
2457     p.cubicTo(pts[4], pts[5], pts[6]);
2458
2459     write_and_read_back(reporter, p);
2460
2461     // create a buffer that should be much larger than the path so we don't
2462     // kill our stack if writer goes too far.
2463     char buffer[1024];
2464     size_t size1 = p.writeToMemory(nullptr);
2465     size_t size2 = p.writeToMemory(buffer);
2466     REPORTER_ASSERT(reporter, size1 == size2);
2467
2468     SkPath p2;
2469     size_t size3 = p2.readFromMemory(buffer, 1024);
2470     REPORTER_ASSERT(reporter, size1 == size3);
2471     REPORTER_ASSERT(reporter, p == p2);
2472
2473     size3 = p2.readFromMemory(buffer, 0);
2474     REPORTER_ASSERT(reporter, !size3);
2475
2476     SkPath tooShort;
2477     size3 = tooShort.readFromMemory(buffer, size1 - 1);
2478     REPORTER_ASSERT(reporter, tooShort.isEmpty());
2479
2480     char buffer2[1024];
2481     size3 = p2.writeToMemory(buffer2);
2482     REPORTER_ASSERT(reporter, size1 == size3);
2483     REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2484
2485     // test persistence of the oval flag & convexity
2486     {
2487         SkPath oval;
2488         SkRect rect = SkRect::MakeWH(10, 10);
2489         oval.addOval(rect);
2490
2491         write_and_read_back(reporter, oval);
2492     }
2493 }
2494
2495 static void test_transform(skiatest::Reporter* reporter) {
2496     SkPath p;
2497
2498 #define CONIC_PERSPECTIVE_BUG_FIXED 0
2499     static const SkPoint pts[] = {
2500         { 0, 0 },  // move
2501         { SkIntToScalar(10), SkIntToScalar(10) },  // line
2502         { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },  // quad
2503         { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) },  // cubic
2504 #if CONIC_PERSPECTIVE_BUG_FIXED
2505         { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) },  // conic
2506 #endif
2507     };
2508     const int kPtCount = SK_ARRAY_COUNT(pts);
2509
2510     p.moveTo(pts[0]);
2511     p.lineTo(pts[1]);
2512     p.quadTo(pts[2], pts[3]);
2513     p.cubicTo(pts[4], pts[5], pts[6]);
2514 #if CONIC_PERSPECTIVE_BUG_FIXED
2515     p.conicTo(pts[4], pts[5], 0.5f);
2516 #endif
2517     p.close();
2518
2519     {
2520         SkMatrix matrix;
2521         matrix.reset();
2522         SkPath p1;
2523         p.transform(matrix, &p1);
2524         REPORTER_ASSERT(reporter, p == p1);
2525     }
2526
2527
2528     {
2529         SkMatrix matrix;
2530         matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2531
2532         SkPath p1;      // Leave p1 non-unique (i.e., the empty path)
2533
2534         p.transform(matrix, &p1);
2535         SkPoint pts1[kPtCount];
2536         int count = p1.getPoints(pts1, kPtCount);
2537         REPORTER_ASSERT(reporter, kPtCount == count);
2538         for (int i = 0; i < count; ++i) {
2539             SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2540             REPORTER_ASSERT(reporter, newPt == pts1[i]);
2541         }
2542     }
2543
2544     {
2545         SkMatrix matrix;
2546         matrix.reset();
2547         matrix.setPerspX(4);
2548
2549         SkPath p1;
2550         p1.moveTo(SkPoint::Make(0, 0));
2551
2552         p.transform(matrix, &p1);
2553         REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2554         p1.transform(matrix, nullptr);
2555         SkRect pBounds = p.getBounds();
2556         SkRect p1Bounds = p1.getBounds();
2557         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2558         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2559         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2560         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2561     }
2562
2563     p.reset();
2564     p.addCircle(0, 0, 1, SkPath::kCW_Direction);
2565
2566     {
2567         SkMatrix matrix;
2568         matrix.reset();
2569         SkPath p1;
2570         p1.moveTo(SkPoint::Make(0, 0));
2571
2572         p.transform(matrix, &p1);
2573         REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2574     }
2575
2576
2577     {
2578         SkMatrix matrix;
2579         matrix.reset();
2580         matrix.setScaleX(-1);
2581         SkPath p1;
2582         p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2583
2584         p.transform(matrix, &p1);
2585         REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2586     }
2587
2588     {
2589         SkMatrix matrix;
2590         matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2591         SkPath p1;
2592         p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2593
2594         p.transform(matrix, &p1);
2595         REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2596     }
2597 }
2598
2599 static void test_zero_length_paths(skiatest::Reporter* reporter) {
2600     SkPath  p;
2601     uint8_t verbs[32];
2602
2603     struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2604         const char* testPath;
2605         const size_t numResultPts;
2606         const SkRect resultBound;
2607         const SkPath::Verb* resultVerbs;
2608         const size_t numResultVerbs;
2609     };
2610
2611     static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2612     static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2613     static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2614     static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2615     static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2616     static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2617     static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2618     static const SkPath::Verb resultVerbs8[] = {
2619         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2620     };
2621     static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2622     static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2623     static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2624     static const SkPath::Verb resultVerbs12[] = {
2625         SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2626     };
2627     static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2628     static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2629     static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2630     static const SkPath::Verb resultVerbs16[] = {
2631         SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2632     };
2633     static const struct zeroPathTestData gZeroLengthTests[] = {
2634         { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2635         { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2636         { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2637         { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2638         { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2639         { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) },
2640         { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2641         { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) },
2642         { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2643         { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) },
2644         { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2645         { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) },
2646         { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2647         { "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,
2648             SK_ARRAY_COUNT(resultVerbs14)
2649         },
2650         { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) },
2651         { "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,
2652             SK_ARRAY_COUNT(resultVerbs16)
2653         }
2654     };
2655
2656     for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2657         p.reset();
2658         bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2659         REPORTER_ASSERT(reporter, valid);
2660         REPORTER_ASSERT(reporter, !p.isEmpty());
2661         REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2662         REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2663         REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2664         for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2665             REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2666         }
2667     }
2668 }
2669
2670 struct SegmentInfo {
2671     SkPath fPath;
2672     int    fPointCount;
2673 };
2674
2675 #define kCurveSegmentMask   (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2676
2677 static void test_segment_masks(skiatest::Reporter* reporter) {
2678     SkPath p, p2;
2679
2680     p.moveTo(0, 0);
2681     p.quadTo(100, 100, 200, 200);
2682     REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2683     REPORTER_ASSERT(reporter, !p.isEmpty());
2684     p2 = p;
2685     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2686     p.cubicTo(100, 100, 200, 200, 300, 300);
2687     REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2688     REPORTER_ASSERT(reporter, !p.isEmpty());
2689     p2 = p;
2690     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2691
2692     p.reset();
2693     p.moveTo(0, 0);
2694     p.cubicTo(100, 100, 200, 200, 300, 300);
2695     REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2696     p2 = p;
2697     REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2698
2699     REPORTER_ASSERT(reporter, !p.isEmpty());
2700 }
2701
2702 static void test_iter(skiatest::Reporter* reporter) {
2703     SkPath  p;
2704     SkPoint pts[4];
2705
2706     // Test an iterator with no path
2707     SkPath::Iter noPathIter;
2708     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2709
2710     // Test that setting an empty path works
2711     noPathIter.setPath(p, false);
2712     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2713
2714     // Test that close path makes no difference for an empty path
2715     noPathIter.setPath(p, true);
2716     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2717
2718     // Test an iterator with an initial empty path
2719     SkPath::Iter iter(p, false);
2720     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2721
2722     // Test that close path makes no difference
2723     iter.setPath(p, true);
2724     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2725
2726
2727     struct iterTestData {
2728         const char* testPath;
2729         const bool forceClose;
2730         const bool consumeDegenerates;
2731         const size_t* numResultPtsPerVerb;
2732         const SkPoint* resultPts;
2733         const SkPath::Verb* resultVerbs;
2734         const size_t numResultVerbs;
2735     };
2736
2737     static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2738     static const SkPath::Verb resultVerbs2[] = {
2739         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2740     };
2741     static const SkPath::Verb resultVerbs3[] = {
2742         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2743     };
2744     static const SkPath::Verb resultVerbs4[] = {
2745         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2746     };
2747     static const SkPath::Verb resultVerbs5[] = {
2748         SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2749     };
2750     static const size_t resultPtsSizes1[] = { 0 };
2751     static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2752     static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2753     static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2754     static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
2755     static const SkPoint* resultPts1 = 0;
2756     static const SkPoint resultPts2[] = {
2757         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2758     };
2759     static const SkPoint resultPts3[] = {
2760         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2761         { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2762     };
2763     static const SkPoint resultPts4[] = {
2764         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2765     };
2766     static const SkPoint resultPts5[] = {
2767         { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2768     };
2769     static const struct iterTestData gIterTests[] = {
2770         { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2771         { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2772         { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2773         { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2774         { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2775         { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2776         { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2777         { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2778         { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2779         { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2780         { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2781         { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2782         { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
2783     };
2784
2785     for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2786         p.reset();
2787         bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2788         REPORTER_ASSERT(reporter, valid);
2789         iter.setPath(p, gIterTests[i].forceClose);
2790         int j = 0, l = 0;
2791         do {
2792             REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2793             for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2794                 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2795             }
2796         } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2797         REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2798     }
2799
2800     p.reset();
2801     iter.setPath(p, false);
2802     REPORTER_ASSERT(reporter, !iter.isClosedContour());
2803     p.lineTo(1, 1);
2804     p.close();
2805     iter.setPath(p, false);
2806     REPORTER_ASSERT(reporter, iter.isClosedContour());
2807     p.reset();
2808     iter.setPath(p, true);
2809     REPORTER_ASSERT(reporter, !iter.isClosedContour());
2810     p.lineTo(1, 1);
2811     iter.setPath(p, true);
2812     REPORTER_ASSERT(reporter, iter.isClosedContour());
2813     p.moveTo(0, 0);
2814     p.lineTo(2, 2);
2815     iter.setPath(p, false);
2816     REPORTER_ASSERT(reporter, !iter.isClosedContour());
2817
2818     // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2819     // check to see if the result is correct.
2820     for (int setNaN = 0; setNaN < 4; ++setNaN) {
2821         p.reset();
2822         p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2823         p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2824         iter.setPath(p, true);
2825         iter.next(pts, false);
2826         iter.next(pts, false);
2827         REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2828     }
2829
2830     p.reset();
2831     p.quadTo(0, 0, 0, 0);
2832     iter.setPath(p, false);
2833     iter.next(pts, false);
2834     REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2835     iter.setPath(p, false);
2836     iter.next(pts, false);
2837     REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2838
2839     p.reset();
2840     p.conicTo(0, 0, 0, 0, 0.5f);
2841     iter.setPath(p, false);
2842     iter.next(pts, false);
2843     REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2844     iter.setPath(p, false);
2845     iter.next(pts, false);
2846     REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2847
2848     p.reset();
2849     p.cubicTo(0, 0, 0, 0, 0, 0);
2850     iter.setPath(p, false);
2851     iter.next(pts, false);
2852     REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2853     iter.setPath(p, false);
2854     iter.next(pts, false);
2855     REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2856
2857     p.moveTo(1, 1);  // add a trailing moveto
2858     iter.setPath(p, false);
2859     iter.next(pts, false);
2860     REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2861     iter.setPath(p, false);
2862     iter.next(pts, false);
2863     REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2864
2865     // The GM degeneratesegments.cpp test is more extensive
2866
2867     // Test out mixed degenerate and non-degenerate geometry with Conics
2868     const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2869     SkRect r = SkRect::MakeWH(100, 100);
2870     SkRRect rr;
2871     rr.setRectRadii(r, radii);
2872     p.reset();
2873     p.addRRect(rr);
2874     iter.setPath(p, false);
2875     REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2876     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2877     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2878     REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2879     REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
2880 }
2881
2882 static void test_raw_iter(skiatest::Reporter* reporter) {
2883     SkPath p;
2884     SkPoint pts[4];
2885
2886     // Test an iterator with no path
2887     SkPath::RawIter noPathIter;
2888     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2889     // Test that setting an empty path works
2890     noPathIter.setPath(p);
2891     REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2892
2893     // Test an iterator with an initial empty path
2894     SkPath::RawIter iter(p);
2895     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2896
2897     // Test that a move-only path returns the move.
2898     p.moveTo(SK_Scalar1, 0);
2899     iter.setPath(p);
2900     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2901     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2902     REPORTER_ASSERT(reporter, pts[0].fY == 0);
2903     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2904
2905     // No matter how many moves we add, we should get them all back
2906     p.moveTo(SK_Scalar1*2, SK_Scalar1);
2907     p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2908     iter.setPath(p);
2909     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2910     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2911     REPORTER_ASSERT(reporter, pts[0].fY == 0);
2912     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2913     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2914     REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2915     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2916     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2917     REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2918     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2919
2920     // Initial close is never ever stored
2921     p.reset();
2922     p.close();
2923     iter.setPath(p);
2924     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2925
2926     // Move/close sequences
2927     p.reset();
2928     p.close(); // Not stored, no purpose
2929     p.moveTo(SK_Scalar1, 0);
2930     p.close();
2931     p.close(); // Not stored, no purpose
2932     p.moveTo(SK_Scalar1*2, SK_Scalar1);
2933     p.close();
2934     p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2935     p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2936     p.close();
2937     iter.setPath(p);
2938     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2939     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2940     REPORTER_ASSERT(reporter, pts[0].fY == 0);
2941     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2942     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2943     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2944     REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2945     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2946     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2947     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2948     REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2949     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2950     REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2951     REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2952     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
2953     REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2954
2955     // Generate random paths and verify
2956     SkPoint randomPts[25];
2957     for (int i = 0; i < 5; ++i) {
2958         for (int j = 0; j < 5; ++j) {
2959             randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2960         }
2961     }
2962
2963     // Max of 10 segments, max 3 points per segment
2964     SkRandom rand(9876543);
2965     SkPoint          expectedPts[31]; // May have leading moveTo
2966     SkPath::Verb     expectedVerbs[22]; // May have leading moveTo
2967     SkPath::Verb     nextVerb;
2968
2969     for (int i = 0; i < 500; ++i) {
2970         p.reset();
2971         bool lastWasClose = true;
2972         bool haveMoveTo = false;
2973         SkPoint lastMoveToPt = { 0, 0 };
2974         int numPoints = 0;
2975         int numVerbs = (rand.nextU() >> 16) % 10;
2976         int numIterVerbs = 0;
2977         for (int j = 0; j < numVerbs; ++j) {
2978             do {
2979                 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2980             } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
2981             switch (nextVerb) {
2982                 case SkPath::kMove_Verb:
2983                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2984                     p.moveTo(expectedPts[numPoints]);
2985                     lastMoveToPt = expectedPts[numPoints];
2986                     numPoints += 1;
2987                     lastWasClose = false;
2988                     haveMoveTo = true;
2989                     break;
2990                 case SkPath::kLine_Verb:
2991                     if (!haveMoveTo) {
2992                         expectedPts[numPoints++] = lastMoveToPt;
2993                         expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2994                         haveMoveTo = true;
2995                     }
2996                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2997                     p.lineTo(expectedPts[numPoints]);
2998                     numPoints += 1;
2999                     lastWasClose = false;
3000                     break;
3001                 case SkPath::kQuad_Verb:
3002                     if (!haveMoveTo) {
3003                         expectedPts[numPoints++] = lastMoveToPt;
3004                         expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3005                         haveMoveTo = true;
3006                     }
3007                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3008                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3009                     p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3010                     numPoints += 2;
3011                     lastWasClose = false;
3012                     break;
3013                 case SkPath::kConic_Verb:
3014                     if (!haveMoveTo) {
3015                         expectedPts[numPoints++] = lastMoveToPt;
3016                         expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3017                         haveMoveTo = true;
3018                     }
3019                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3020                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3021                     p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3022                               rand.nextUScalar1() * 4);
3023                     numPoints += 2;
3024                     lastWasClose = false;
3025                     break;
3026                 case SkPath::kCubic_Verb:
3027                     if (!haveMoveTo) {
3028                         expectedPts[numPoints++] = lastMoveToPt;
3029                         expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3030                         haveMoveTo = true;
3031                     }
3032                     expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3033                     expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3034                     expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3035                     p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3036                               expectedPts[numPoints + 2]);
3037                     numPoints += 3;
3038                     lastWasClose = false;
3039                     break;
3040                 case SkPath::kClose_Verb:
3041                     p.close();
3042                     haveMoveTo = false;
3043                     lastWasClose = true;
3044                     break;
3045                 default:
3046                     SkDEBUGFAIL("unexpected verb");
3047             }
3048             expectedVerbs[numIterVerbs++] = nextVerb;
3049         }
3050
3051         iter.setPath(p);
3052         numVerbs = numIterVerbs;
3053         numIterVerbs = 0;
3054         int numIterPts = 0;
3055         SkPoint lastMoveTo;
3056         SkPoint lastPt;
3057         lastMoveTo.set(0, 0);
3058         lastPt.set(0, 0);
3059         while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3060             REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3061             numIterVerbs++;
3062             switch (nextVerb) {
3063                 case SkPath::kMove_Verb:
3064                     REPORTER_ASSERT(reporter, numIterPts < numPoints);
3065                     REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3066                     lastPt = lastMoveTo = pts[0];
3067                     numIterPts += 1;
3068                     break;
3069                 case SkPath::kLine_Verb:
3070                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3071                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3072                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3073                     lastPt = pts[1];
3074                     numIterPts += 1;
3075                     break;
3076                 case SkPath::kQuad_Verb:
3077                 case SkPath::kConic_Verb:
3078                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3079                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3080                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3081                     REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3082                     lastPt = pts[2];
3083                     numIterPts += 2;
3084                     break;
3085                 case SkPath::kCubic_Verb:
3086                     REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3087                     REPORTER_ASSERT(reporter, pts[0] == lastPt);
3088                     REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3089                     REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3090                     REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3091                     lastPt = pts[3];
3092                     numIterPts += 3;
3093                     break;
3094                 case SkPath::kClose_Verb:
3095                     lastPt = lastMoveTo;
3096                     break;
3097                 default:
3098                     SkDEBUGFAIL("unexpected verb");
3099             }
3100         }
3101         REPORTER_ASSERT(reporter, numIterPts == numPoints);
3102         REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3103     }
3104 }
3105
3106 static void check_for_circle(skiatest::Reporter* reporter,
3107                              const SkPath& path,
3108                              bool expectedCircle,
3109                              SkPathPriv::FirstDirection expectedDir) {
3110     SkRect rect = SkRect::MakeEmpty();
3111     REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
3112     SkPath::Direction isOvalDir;
3113     unsigned isOvalStart;
3114     if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
3115         REPORTER_ASSERT(reporter, rect.height() == rect.width());
3116         REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3117         SkPath tmpPath;
3118         tmpPath.addOval(rect, isOvalDir, isOvalStart);
3119         REPORTER_ASSERT(reporter, path == tmpPath);
3120     }
3121     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3122 }
3123
3124 static void test_circle_skew(skiatest::Reporter* reporter,
3125                              const SkPath& path,
3126                              SkPathPriv::FirstDirection dir) {
3127     SkPath tmp;
3128
3129     SkMatrix m;
3130     m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3131     path.transform(m, &tmp);
3132     // this matrix reverses the direction.
3133     if (SkPathPriv::kCCW_FirstDirection == dir) {
3134         dir = SkPathPriv::kCW_FirstDirection;
3135     } else {
3136         REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3137         dir = SkPathPriv::kCCW_FirstDirection;
3138     }
3139     check_for_circle(reporter, tmp, false, dir);
3140 }
3141
3142 static void test_circle_translate(skiatest::Reporter* reporter,
3143                                   const SkPath& path,
3144                                   SkPathPriv::FirstDirection dir) {
3145     SkPath tmp;
3146
3147     // translate at small offset
3148     SkMatrix m;
3149     m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3150     path.transform(m, &tmp);
3151     check_for_circle(reporter, tmp, true, dir);
3152
3153     tmp.reset();
3154     m.reset();
3155
3156     // translate at a relatively big offset
3157     m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3158     path.transform(m, &tmp);
3159     check_for_circle(reporter, tmp, true, dir);
3160 }
3161
3162 static void test_circle_rotate(skiatest::Reporter* reporter,
3163                                const SkPath& path,
3164                                SkPathPriv::FirstDirection dir) {
3165     for (int angle = 0; angle < 360; ++angle) {
3166         SkPath tmp;
3167         SkMatrix m;
3168         m.setRotate(SkIntToScalar(angle));
3169         path.transform(m, &tmp);
3170
3171         // TODO: a rotated circle whose rotated angle is not a multiple of 90
3172         // degrees is not an oval anymore, this can be improved.  we made this
3173         // for the simplicity of our implementation.
3174         if (angle % 90 == 0) {
3175             check_for_circle(reporter, tmp, true, dir);
3176         } else {
3177             check_for_circle(reporter, tmp, false, dir);
3178         }
3179     }
3180 }
3181
3182 static void test_circle_mirror_x(skiatest::Reporter* reporter,
3183                                  const SkPath& path,
3184                                  SkPathPriv::FirstDirection dir) {
3185     SkPath tmp;
3186     SkMatrix m;
3187     m.reset();
3188     m.setScaleX(-SK_Scalar1);
3189     path.transform(m, &tmp);
3190     if (SkPathPriv::kCW_FirstDirection == dir) {
3191         dir = SkPathPriv::kCCW_FirstDirection;
3192     } else {
3193         REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3194         dir = SkPathPriv::kCW_FirstDirection;
3195     }
3196     check_for_circle(reporter, tmp, true, dir);
3197 }
3198
3199 static void test_circle_mirror_y(skiatest::Reporter* reporter,
3200                                  const SkPath& path,
3201                                  SkPathPriv::FirstDirection dir) {
3202     SkPath tmp;
3203     SkMatrix m;
3204     m.reset();
3205     m.setScaleY(-SK_Scalar1);
3206     path.transform(m, &tmp);
3207
3208     if (SkPathPriv::kCW_FirstDirection == dir) {
3209         dir = SkPathPriv::kCCW_FirstDirection;
3210     } else {
3211         REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3212         dir = SkPathPriv::kCW_FirstDirection;
3213     }
3214
3215     check_for_circle(reporter, tmp, true, dir);
3216 }
3217
3218 static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3219                                  const SkPath& path,
3220                                  SkPathPriv::FirstDirection dir) {
3221     SkPath tmp;
3222     SkMatrix m;
3223     m.reset();
3224     m.setScaleX(-SK_Scalar1);
3225     m.setScaleY(-SK_Scalar1);
3226     path.transform(m, &tmp);
3227
3228     check_for_circle(reporter, tmp, true, dir);
3229 }
3230
3231 static void test_circle_with_direction(skiatest::Reporter* reporter,
3232                                        SkPath::Direction inDir) {
3233     const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3234     SkPath path;
3235
3236     // circle at origin
3237     path.addCircle(0, 0, SkIntToScalar(20), inDir);
3238
3239     check_for_circle(reporter, path, true, dir);
3240     test_circle_rotate(reporter, path, dir);
3241     test_circle_translate(reporter, path, dir);
3242     test_circle_skew(reporter, path, dir);
3243     test_circle_mirror_x(reporter, path, dir);
3244     test_circle_mirror_y(reporter, path, dir);
3245     test_circle_mirror_xy(reporter, path, dir);
3246
3247     // circle at an offset at (10, 10)
3248     path.reset();
3249     path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3250                    SkIntToScalar(20), inDir);
3251
3252     check_for_circle(reporter, path, true, dir);
3253     test_circle_rotate(reporter, path, dir);
3254     test_circle_translate(reporter, path, dir);
3255     test_circle_skew(reporter, path, dir);
3256     test_circle_mirror_x(reporter, path, dir);
3257     test_circle_mirror_y(reporter, path, dir);
3258     test_circle_mirror_xy(reporter, path, dir);
3259
3260     // Try different starting points for the contour.
3261     for (unsigned start = 0; start < 4; ++start) {
3262         path.reset();
3263         path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3264         test_circle_rotate(reporter, path, dir);
3265         test_circle_translate(reporter, path, dir);
3266         test_circle_skew(reporter, path, dir);
3267         test_circle_mirror_x(reporter, path, dir);
3268         test_circle_mirror_y(reporter, path, dir);
3269         test_circle_mirror_xy(reporter, path, dir);
3270     }
3271 }
3272
3273 static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3274     SkPath path;
3275     SkPath circle;
3276     SkPath rect;
3277     SkPath empty;
3278
3279     const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3280     const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
3281
3282     circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3283     rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3284                  SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3285
3286     SkMatrix translate;
3287     translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3288
3289     // Although all the path concatenation related operations leave
3290     // the path a circle, most mark it as a non-circle for simplicity
3291
3292     // empty + circle (translate)
3293     path = empty;
3294     path.addPath(circle, translate);
3295     check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3296
3297     // circle + empty (translate)
3298     path = circle;
3299     path.addPath(empty, translate);
3300
3301     check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3302
3303     // test reverseAddPath
3304     path = circle;
3305     path.reverseAddPath(rect);
3306     check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3307 }
3308
3309 static void test_circle(skiatest::Reporter* reporter) {
3310     test_circle_with_direction(reporter, SkPath::kCW_Direction);
3311     test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3312
3313     // multiple addCircle()
3314     SkPath path;
3315     path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3316     path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
3317     check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3318
3319     // some extra lineTo() would make isOval() fail
3320     path.reset();
3321     path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3322     path.lineTo(0, 0);
3323     check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3324
3325     // not back to the original point
3326     path.reset();
3327     path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3328     path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3329     check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3330
3331     test_circle_with_add_paths(reporter);
3332
3333     // test negative radius
3334     path.reset();
3335     path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3336     REPORTER_ASSERT(reporter, path.isEmpty());
3337 }
3338
3339 static void test_oval(skiatest::Reporter* reporter) {
3340     SkRect rect;
3341     SkMatrix m;
3342     SkPath path;
3343     unsigned start = 0;
3344     SkPath::Direction dir = SkPath::kCCW_Direction;
3345
3346     rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3347     path.addOval(rect);
3348
3349     // Defaults to dir = CW and start = 1
3350     REPORTER_ASSERT(reporter, path.isOval(nullptr));
3351
3352     m.setRotate(SkIntToScalar(90));
3353     SkPath tmp;
3354     path.transform(m, &tmp);
3355     // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3356     // is unchanged.
3357     REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3358     REPORTER_ASSERT(reporter, 2 == start);
3359     REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3360
3361     m.reset();
3362     m.setRotate(SkIntToScalar(30));
3363     tmp.reset();
3364     path.transform(m, &tmp);
3365     // an oval rotated 30 degrees is not an oval anymore.
3366     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3367
3368     // since empty path being transformed.
3369     path.reset();
3370     tmp.reset();
3371     m.reset();
3372     path.transform(m, &tmp);
3373     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3374
3375     // empty path is not an oval
3376     tmp.reset();
3377     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3378
3379     // only has moveTo()s
3380     tmp.reset();
3381     tmp.moveTo(0, 0);
3382     tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3383     REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3384
3385     // mimic WebKit's calling convention,
3386     // call moveTo() first and then call addOval()
3387     path.reset();
3388     path.moveTo(0, 0);
3389     path.addOval(rect);
3390     REPORTER_ASSERT(reporter, path.isOval(nullptr));
3391
3392     // copy path
3393     path.reset();
3394     tmp.reset();
3395     tmp.addOval(rect);
3396     path = tmp;
3397     REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3398     REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3399     REPORTER_ASSERT(reporter, 1 == start);
3400 }
3401
3402 static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3403     SkPath  empty;
3404
3405     REPORTER_ASSERT(reporter, p.isEmpty());
3406     REPORTER_ASSERT(reporter, 0 == p.countPoints());
3407     REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3408     REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3409     REPORTER_ASSERT(reporter, p.isConvex());
3410     REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3411     REPORTER_ASSERT(reporter, !p.isInverseFillType());
3412     REPORTER_ASSERT(reporter, p == empty);
3413     REPORTER_ASSERT(reporter, !(p != empty));
3414 }
3415
3416 static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3417                                  SkPath::Direction dir) {
3418     REPORTER_ASSERT(reporter, path->isConvex());
3419     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3420     path->setConvexity(SkPath::kUnknown_Convexity);
3421     REPORTER_ASSERT(reporter, path->isConvex());
3422     path->reset();
3423 }
3424
3425 static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3426                                  SkPath::Direction dir) {
3427     REPORTER_ASSERT(reporter, path->isConvex());
3428     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3429     path->setConvexity(SkPath::kUnknown_Convexity);
3430     REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3431     path->reset();
3432 }
3433
3434 static void test_rrect(skiatest::Reporter* reporter) {
3435     SkPath p;
3436     SkRRect rr;
3437     SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3438     SkRect r = {10, 20, 30, 40};
3439     rr.setRectRadii(r, radii);
3440     p.addRRect(rr);
3441     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3442     p.addRRect(rr, SkPath::kCCW_Direction);
3443     test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3444     p.addRoundRect(r, &radii[0].fX);
3445     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3446     p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
3447     test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3448     p.addRoundRect(r, radii[1].fX, radii[1].fY);
3449     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3450     p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
3451     test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3452     for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3453         SkVector save = radii[i];
3454         radii[i].set(0, 0);
3455         rr.setRectRadii(r, radii);
3456         p.addRRect(rr);
3457         test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3458         radii[i] = save;
3459     }
3460     p.addRoundRect(r, 0, 0);
3461     SkRect returnedRect;
3462     REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3463     REPORTER_ASSERT(reporter, returnedRect == r);
3464     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3465     SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3466     rr.setRectRadii(r, zeroRadii);
3467     p.addRRect(rr);
3468     bool closed;
3469     SkPath::Direction dir;
3470     REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3471     REPORTER_ASSERT(reporter, closed);
3472     REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3473     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3474     p.addRRect(rr, SkPath::kCW_Direction);
3475     p.addRRect(rr, SkPath::kCW_Direction);
3476     REPORTER_ASSERT(reporter, !p.isConvex());
3477     p.reset();
3478     p.addRRect(rr, SkPath::kCCW_Direction);
3479     p.addRRect(rr, SkPath::kCCW_Direction);
3480     REPORTER_ASSERT(reporter, !p.isConvex());
3481     p.reset();
3482     SkRect emptyR = {10, 20, 10, 30};
3483     rr.setRectRadii(emptyR, radii);
3484     p.addRRect(rr);
3485     REPORTER_ASSERT(reporter, p.isEmpty());
3486     SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3487     rr.setRectRadii(largeR, radii);
3488     p.addRRect(rr);
3489     test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
3490
3491     // we check for non-finites
3492     SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3493     rr.setRectRadii(infR, radii);
3494     REPORTER_ASSERT(reporter, rr.isEmpty());
3495
3496     SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3497     p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3498     test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3499 }
3500
3501 static void test_arc(skiatest::Reporter* reporter) {
3502     SkPath p;
3503     SkRect emptyOval = {10, 20, 30, 20};
3504     REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3505     p.addArc(emptyOval, 1, 2);
3506     REPORTER_ASSERT(reporter, p.isEmpty());
3507     p.reset();
3508     SkRect oval = {10, 20, 30, 40};
3509     p.addArc(oval, 1, 0);
3510     REPORTER_ASSERT(reporter, p.isEmpty());
3511     p.reset();
3512     SkPath cwOval;
3513     cwOval.addOval(oval);
3514     p.addArc(oval, 0, 360);
3515     REPORTER_ASSERT(reporter, p == cwOval);
3516     p.reset();
3517     SkPath ccwOval;
3518     ccwOval.addOval(oval, SkPath::kCCW_Direction);
3519     p.addArc(oval, 0, -360);
3520     REPORTER_ASSERT(reporter, p == ccwOval);
3521     p.reset();
3522     p.addArc(oval, 1, 180);
3523     REPORTER_ASSERT(reporter, p.isConvex());
3524     REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
3525     p.setConvexity(SkPath::kUnknown_Convexity);
3526     REPORTER_ASSERT(reporter, p.isConvex());
3527 }
3528
3529 static inline SkScalar oval_start_index_to_angle(unsigned start) {
3530     switch (start) {
3531         case 0:
3532             return 270.f;
3533         case 1:
3534             return 0.f;
3535         case 2:
3536             return 90.f;
3537         case 3:
3538             return 180.f;
3539         default:
3540             return -1.f;
3541     }
3542 }
3543
3544 static inline SkScalar canonical_start_angle(float angle) {
3545     while (angle < 0.f) {
3546         angle += 360.f;
3547     }
3548     while (angle >= 360.f) {
3549         angle -= 360.f;
3550     }
3551     return angle;
3552 }
3553
3554 static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3555                            const SkPath& path) {
3556     SkRect r = SkRect::MakeEmpty();
3557     SkPath::Direction d = SkPath::kCCW_Direction;
3558     unsigned s = ~0U;
3559     bool isOval = path.isOval(&r, &d, &s);
3560     REPORTER_ASSERT(reporter, isOval);
3561     SkPath recreatedPath;
3562     recreatedPath.addOval(r, d, s);
3563     REPORTER_ASSERT(reporter, path == recreatedPath);
3564     REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3565     REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3566 }
3567
3568 static void test_arc_ovals(skiatest::Reporter* reporter) {
3569     SkRect oval = SkRect::MakeWH(10, 20);
3570     for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3571         for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3572             SkPath path;
3573             path.addArc(oval, start, sweep);
3574             // SkPath's interfaces for inserting and extracting ovals only allow contours
3575             // to start at multiples of 90 degrees.
3576             if (std::fmod(start, 90.f) == 0) {
3577                 check_oval_arc(reporter, start, sweep, path);
3578             } else {
3579                 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3580             }
3581         }
3582         // Test start angles that are nearly at valid oval start angles.
3583         for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3584             for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3585                 SkPath path;
3586                 path.addArc(oval, start + delta, sweep);
3587                 check_oval_arc(reporter, start, sweep, path);
3588             }
3589         }
3590     }
3591 }
3592
3593 static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3594                        SkScalar x0, SkScalar y0) {
3595     SkPoint pts[4];
3596     SkPath::Verb v = iter->next(pts);
3597     REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3598     REPORTER_ASSERT(reporter, pts[0].fX == x0);
3599     REPORTER_ASSERT(reporter, pts[0].fY == y0);
3600 }
3601
3602 static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3603                        SkScalar x1, SkScalar y1) {
3604     SkPoint pts[4];
3605     SkPath::Verb v = iter->next(pts);
3606     REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3607     REPORTER_ASSERT(reporter, pts[1].fX == x1);
3608     REPORTER_ASSERT(reporter, pts[1].fY == y1);
3609 }
3610
3611 static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3612                        SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3613     SkPoint pts[4];
3614     SkPath::Verb v = iter->next(pts);
3615     REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3616     REPORTER_ASSERT(reporter, pts[1].fX == x1);
3617     REPORTER_ASSERT(reporter, pts[1].fY == y1);
3618     REPORTER_ASSERT(reporter, pts[2].fX == x2);
3619     REPORTER_ASSERT(reporter, pts[2].fY == y2);
3620 }
3621
3622 static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3623     SkPoint pts[4];
3624     SkPath::Verb v = iter->next(pts);
3625     REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3626 }
3627
3628 static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3629     check_done(reporter, p, iter);
3630     p->reset();
3631 }
3632
3633 static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3634                                          SkScalar x0, SkScalar y0) {
3635     SkPath::RawIter iter(*p);
3636     check_move(reporter, &iter, x0, y0);
3637     check_done_and_reset(reporter, p, &iter);
3638 }
3639
3640 static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3641                                          SkScalar x1, SkScalar y1) {
3642     SkPath::RawIter iter(*p);
3643     check_move(reporter, &iter, 0, 0);
3644     check_line(reporter, &iter, x1, y1);
3645     check_done_and_reset(reporter, p, &iter);
3646 }
3647
3648 static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3649                                          SkScalar x1, SkScalar y1) {
3650     SkPath::RawIter iter(*p);
3651     check_move(reporter, &iter, 0, 0);
3652     check_line(reporter, &iter, x1, y1);
3653     check_done(reporter, p, &iter);
3654 }
3655
3656 static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3657                                     SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3658     SkPath::RawIter iter(*p);
3659     check_move(reporter, &iter, 0, 0);
3660     check_line(reporter, &iter, x1, y1);
3661     check_line(reporter, &iter, x2, y2);
3662     check_done_and_reset(reporter, p, &iter);
3663 }
3664
3665 static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3666                                     SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3667     SkPath::RawIter iter(*p);
3668     check_move(reporter, &iter, 0, 0);
3669     check_quad(reporter, &iter, x1, y1, x2, y2);
3670     check_done_and_reset(reporter, p, &iter);
3671 }
3672
3673 static bool nearly_equal(const SkRect& a, const SkRect& b) {
3674     return  SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3675             SkScalarNearlyEqual(a.fTop, b.fTop) &&
3676             SkScalarNearlyEqual(a.fRight, b.fRight) &&
3677             SkScalarNearlyEqual(a.fBottom, b.fBottom);
3678 }
3679
3680 static void test_arcTo(skiatest::Reporter* reporter) {
3681     SkPath p;
3682     p.arcTo(0, 0, 1, 2, 1);
3683     check_path_is_line_and_reset(reporter, &p, 0, 0);
3684     p.arcTo(1, 2, 1, 2, 1);
3685     check_path_is_line_and_reset(reporter, &p, 1, 2);
3686     p.arcTo(1, 2, 3, 4, 0);
3687     check_path_is_line_and_reset(reporter, &p, 1, 2);
3688     p.arcTo(1, 2, 0, 0, 1);
3689     check_path_is_line_and_reset(reporter, &p, 1, 2);
3690     p.arcTo(1, 0, 1, 1, 1);
3691     SkPoint pt;
3692     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3693     p.reset();
3694     p.arcTo(1, 0, 1, -1, 1);
3695     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3696     p.reset();
3697     SkRect oval = {1, 2, 3, 4};
3698     p.arcTo(oval, 0, 0, true);
3699     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3700     p.arcTo(oval, 0, 0, false);
3701     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3702     p.arcTo(oval, 360, 0, true);
3703     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3704     p.arcTo(oval, 360, 0, false);
3705     check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3706
3707     for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3708         p.arcTo(oval, 0, sweep, false);
3709         REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3710         sweep += delta;
3711         delta /= 2;
3712     }
3713     for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3714         p.arcTo(oval, 0, sweep, false);
3715         REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3716         sweep -= delta;
3717         delta /= 2;
3718     }
3719     SkRect noOvalWidth = {1, 2, 0, 3};
3720     p.reset();
3721     p.arcTo(noOvalWidth, 0, 360, false);
3722     REPORTER_ASSERT(reporter, p.isEmpty());
3723
3724     SkRect noOvalHeight = {1, 2, 3, 1};
3725     p.reset();
3726     p.arcTo(noOvalHeight, 0, 360, false);
3727     REPORTER_ASSERT(reporter, p.isEmpty());
3728 }
3729
3730 static void test_addPath(skiatest::Reporter* reporter) {
3731     SkPath p, q;
3732     p.lineTo(1, 2);
3733     q.moveTo(4, 4);
3734     q.lineTo(7, 8);
3735     q.conicTo(8, 7, 6, 5, 0.5f);
3736     q.quadTo(6, 7, 8, 6);
3737     q.cubicTo(5, 6, 7, 8, 7, 5);
3738     q.close();
3739     p.addPath(q, -4, -4);
3740     SkRect expected = {0, 0, 4, 4};
3741     REPORTER_ASSERT(reporter, p.getBounds() == expected);
3742     p.reset();
3743     p.reverseAddPath(q);
3744     SkRect reverseExpected = {4, 4, 8, 8};
3745     REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3746 }
3747
3748 static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3749     SkPath p, q;
3750     if (explicitMoveTo) {
3751         p.moveTo(1, 1);
3752     }
3753     p.lineTo(1, 2);
3754     if (explicitMoveTo) {
3755         q.moveTo(2, 1);
3756     }
3757     q.lineTo(2, 2);
3758     p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3759     uint8_t verbs[4];
3760     int verbcount = p.getVerbs(verbs, 4);
3761     REPORTER_ASSERT(reporter, verbcount == 4);
3762     REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3763     REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3764     REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3765     REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3766 }
3767
3768 static void test_extendClosedPath(skiatest::Reporter* reporter) {
3769     SkPath p, q;
3770     p.moveTo(1, 1);
3771     p.lineTo(1, 2);
3772     p.lineTo(2, 2);
3773     p.close();
3774     q.moveTo(2, 1);
3775     q.lineTo(2, 3);
3776     p.addPath(q, SkPath::kExtend_AddPathMode);
3777     uint8_t verbs[7];
3778     int verbcount = p.getVerbs(verbs, 7);
3779     REPORTER_ASSERT(reporter, verbcount == 7);
3780     REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3781     REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3782     REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3783     REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3784     REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3785     REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3786     REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3787
3788     SkPoint pt;
3789     REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3790     REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3791     REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3792 }
3793
3794 static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3795     SkPath p, q, r;
3796     // case 1: dst is empty
3797     p.moveTo(2, 1);
3798     p.lineTo(2, 3);
3799     q.addPath(p, mode);
3800     REPORTER_ASSERT(reporter, q == p);
3801     // case 2: src is empty
3802     p.addPath(r, mode);
3803     REPORTER_ASSERT(reporter, q == p);
3804     // case 3: src and dst are empty
3805     q.reset();
3806     q.addPath(r, mode);
3807     REPORTER_ASSERT(reporter, q.isEmpty());
3808 }
3809
3810 static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3811     SkPath p;
3812     p.conicTo(1, 2, 3, 4, -1);
3813     check_path_is_line_and_reset(reporter, &p, 3, 4);
3814     p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3815     check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3816     p.conicTo(1, 2, 3, 4, 1);
3817     check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3818 }
3819
3820 static void test_get_point(skiatest::Reporter* reporter) {
3821     SkPath p;
3822     SkPoint pt = p.getPoint(0);
3823     REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3824     REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
3825     REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3826     p.setLastPt(10, 10);
3827     pt = p.getPoint(0);
3828     REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3829     REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
3830     p.rMoveTo(10, 10);
3831     REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3832 }
3833
3834 static void test_contains(skiatest::Reporter* reporter) {
3835     SkPath p;
3836     p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00));  // -7.7191e+19f, 1.50724e+19f
3837     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
3838     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
3839     p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8));  // 8.96947e+19f, -1.75139e+19f
3840     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
3841     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
3842     // this may return true or false, depending on the platform's numerics, but it should not crash
3843     (void) p.contains(-77.2027664f, 15.3066053f);
3844
3845     p.reset();
3846     p.setFillType(SkPath::kInverseWinding_FillType);
3847     REPORTER_ASSERT(reporter, p.contains(0, 0));
3848     p.setFillType(SkPath::kWinding_FillType);
3849     REPORTER_ASSERT(reporter, !p.contains(0, 0));
3850     p.moveTo(4, 4);
3851     p.lineTo(6, 8);
3852     p.lineTo(8, 4);
3853     // test on edge
3854     REPORTER_ASSERT(reporter, p.contains(6, 4));
3855     REPORTER_ASSERT(reporter, p.contains(5, 6));
3856     REPORTER_ASSERT(reporter, p.contains(7, 6));
3857     // test quick reject
3858     REPORTER_ASSERT(reporter, !p.contains(4, 0));
3859     REPORTER_ASSERT(reporter, !p.contains(0, 4));
3860     REPORTER_ASSERT(reporter, !p.contains(4, 10));
3861     REPORTER_ASSERT(reporter, !p.contains(10, 4));
3862     // test various crossings in x
3863     REPORTER_ASSERT(reporter, !p.contains(5, 7));
3864     REPORTER_ASSERT(reporter, p.contains(6, 7));
3865     REPORTER_ASSERT(reporter, !p.contains(7, 7));
3866     p.reset();
3867     p.moveTo(4, 4);
3868     p.lineTo(8, 6);
3869     p.lineTo(4, 8);
3870     // test on edge
3871     REPORTER_ASSERT(reporter, p.contains(4, 6));
3872     REPORTER_ASSERT(reporter, p.contains(6, 5));
3873     REPORTER_ASSERT(reporter, p.contains(6, 7));
3874     // test various crossings in y
3875     REPORTER_ASSERT(reporter, !p.contains(7, 5));
3876     REPORTER_ASSERT(reporter, p.contains(7, 6));
3877     REPORTER_ASSERT(reporter, !p.contains(7, 7));
3878     p.reset();
3879     p.moveTo(4, 4);
3880     p.lineTo(8, 4);
3881     p.lineTo(8, 8);
3882     p.lineTo(4, 8);
3883     // test on vertices
3884     REPORTER_ASSERT(reporter, p.contains(4, 4));
3885     REPORTER_ASSERT(reporter, p.contains(8, 4));
3886     REPORTER_ASSERT(reporter, p.contains(8, 8));
3887     REPORTER_ASSERT(reporter, p.contains(4, 8));
3888     p.reset();
3889     p.moveTo(4, 4);
3890     p.lineTo(6, 8);
3891     p.lineTo(2, 8);
3892     // test on edge
3893     REPORTER_ASSERT(reporter, p.contains(5, 6));
3894     REPORTER_ASSERT(reporter, p.contains(4, 8));
3895     REPORTER_ASSERT(reporter, p.contains(3, 6));
3896     p.reset();
3897     p.moveTo(4, 4);
3898     p.lineTo(0, 6);
3899     p.lineTo(4, 8);
3900     // test on edge
3901     REPORTER_ASSERT(reporter, p.contains(2, 5));
3902     REPORTER_ASSERT(reporter, p.contains(2, 7));
3903     REPORTER_ASSERT(reporter, p.contains(4, 6));
3904     // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3905     p.reset();
3906     p.moveTo(4, 0);
3907     p.lineTo(6, 4);
3908     p.lineTo(2, 4);
3909     p.moveTo(4, 0);
3910     p.lineTo(0, 8);
3911     p.lineTo(8, 8);
3912     REPORTER_ASSERT(reporter, !p.contains(1, 2));
3913     REPORTER_ASSERT(reporter, !p.contains(3, 2));
3914     REPORTER_ASSERT(reporter, !p.contains(4, 0));
3915     REPORTER_ASSERT(reporter, p.contains(4, 4));
3916
3917     // test quads
3918     p.reset();
3919     p.moveTo(4, 4);
3920     p.quadTo(6, 6, 8, 8);
3921     p.quadTo(6, 8, 4, 8);
3922     p.quadTo(4, 6, 4, 4);
3923     REPORTER_ASSERT(reporter, p.contains(5, 6));
3924     REPORTER_ASSERT(reporter, !p.contains(6, 5));
3925     // test quad edge
3926     REPORTER_ASSERT(reporter, p.contains(5, 5));
3927     REPORTER_ASSERT(reporter, p.contains(5, 8));
3928     REPORTER_ASSERT(reporter, p.contains(4, 5));
3929     // test quad endpoints
3930     REPORTER_ASSERT(reporter, p.contains(4, 4));
3931     REPORTER_ASSERT(reporter, p.contains(8, 8));
3932     REPORTER_ASSERT(reporter, p.contains(4, 8));
3933
3934     p.reset();
3935     const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3936     p.moveTo(qPts[0]);
3937     for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3938         p.quadTo(qPts[index], qPts[index + 1]);
3939     }
3940     REPORTER_ASSERT(reporter, p.contains(5, 6));
3941     REPORTER_ASSERT(reporter, !p.contains(6, 5));
3942     // test quad edge
3943     SkPoint halfway;
3944     for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3945         SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3946         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3947     }
3948
3949     // test conics
3950     p.reset();
3951     const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3952     p.moveTo(kPts[0]);
3953     for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3954         p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3955     }
3956     REPORTER_ASSERT(reporter, p.contains(5, 6));
3957     REPORTER_ASSERT(reporter, !p.contains(6, 5));
3958     // test conic edge
3959     for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3960         SkConic conic(&kPts[index], 0.5f);
3961         halfway = conic.evalAt(0.5f);
3962         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3963     }
3964     // test conic end points
3965     REPORTER_ASSERT(reporter, p.contains(4, 4));
3966     REPORTER_ASSERT(reporter, p.contains(8, 8));
3967     REPORTER_ASSERT(reporter, p.contains(4, 8));
3968
3969     // test cubics
3970     SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3971     for (int i = 0; i < 3; ++i) {
3972         p.reset();
3973         p.setFillType(SkPath::kEvenOdd_FillType);
3974         p.moveTo(pts[i].fX, pts[i].fY);
3975         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);
3976         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);
3977         p.close();
3978         REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3979         REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3980         // test cubic edge
3981         SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3982         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3983         SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3984         REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3985         // test cubic end points
3986         REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3987         REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3988         REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
3989     }
3990 }
3991
3992 class PathRefTest_Private {
3993 public:
3994     static void TestPathRef(skiatest::Reporter* reporter) {
3995         static const int kRepeatCnt = 10;
3996
3997         SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
3998
3999         SkPathRef::Editor ed(&pathRef);
4000
4001         {
4002             ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4003             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4004             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4005             REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4006             for (int i = 0; i < kRepeatCnt; ++i) {
4007                 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4008             }
4009             ed.resetToSize(0, 0, 0);
4010         }
4011
4012         {
4013             ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4014             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4015             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4016             REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4017             for (int i = 0; i < kRepeatCnt; ++i) {
4018                 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4019             }
4020             ed.resetToSize(0, 0, 0);
4021         }
4022
4023         {
4024             ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4025             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4026             REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4027             REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4028             for (int i = 0; i < kRepeatCnt; ++i) {
4029                 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4030             }
4031             ed.resetToSize(0, 0, 0);
4032         }
4033
4034         {
4035             SkScalar* weights = nullptr;
4036             ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4037             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4038             REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4039             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4040             REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4041             REPORTER_ASSERT(reporter, weights);
4042             for (int i = 0; i < kRepeatCnt; ++i) {
4043                 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4044             }
4045             ed.resetToSize(0, 0, 0);
4046         }
4047
4048         {
4049             ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4050             REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4051             REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4052             REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4053             for (int i = 0; i < kRepeatCnt; ++i) {
4054                 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4055             }
4056             ed.resetToSize(0, 0, 0);
4057         }
4058     }
4059 };
4060
4061 static void test_operatorEqual(skiatest::Reporter* reporter) {
4062     SkPath a;
4063     SkPath b;
4064     REPORTER_ASSERT(reporter, a == a);
4065     REPORTER_ASSERT(reporter, a == b);
4066     a.setFillType(SkPath::kInverseWinding_FillType);
4067     REPORTER_ASSERT(reporter, a != b);
4068     a.reset();
4069     REPORTER_ASSERT(reporter, a == b);
4070     a.lineTo(1, 1);
4071     REPORTER_ASSERT(reporter, a != b);
4072     a.reset();
4073     REPORTER_ASSERT(reporter, a == b);
4074     a.lineTo(1, 1);
4075     b.lineTo(1, 2);
4076     REPORTER_ASSERT(reporter, a != b);
4077     a.reset();
4078     a.lineTo(1, 2);
4079     REPORTER_ASSERT(reporter, a == b);
4080 }
4081
4082 static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4083         bool dumpAsHex, const char* str) {
4084     SkDynamicMemoryWStream wStream;
4085     path.dump(&wStream, force, dumpAsHex);
4086     sk_sp<SkData> data(wStream.copyToData());
4087     REPORTER_ASSERT(reporter, data->size() == strlen(str));
4088     if (strlen(str) > 0) {
4089         REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4090     } else {
4091         REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4092     }
4093 }
4094
4095 static void test_dump(skiatest::Reporter* reporter) {
4096     SkPath p;
4097     compare_dump(reporter, p, false, false, "");
4098     compare_dump(reporter, p, true, false, "");
4099     p.moveTo(1, 2);
4100     p.lineTo(3, 4);
4101     compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4102                                             "path.lineTo(3, 4);\n");
4103     compare_dump(reporter, p, true, false,  "path.moveTo(1, 2);\n"
4104                                             "path.lineTo(3, 4);\n"
4105                                             "path.lineTo(1, 2);\n"
4106                                             "path.close();\n");
4107     p.reset();
4108     p.moveTo(1, 2);
4109     p.quadTo(3, 4, 5, 6);
4110     compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4111                                             "path.quadTo(3, 4, 5, 6);\n");
4112     p.reset();
4113     p.moveTo(1, 2);
4114     p.conicTo(3, 4, 5, 6, 0.5f);
4115     compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4116                                             "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4117     p.reset();
4118     p.moveTo(1, 2);
4119     p.cubicTo(3, 4, 5, 6, 7, 8);
4120     compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4121                                             "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4122     p.reset();
4123     p.moveTo(1, 2);
4124     p.lineTo(3, 4);
4125     compare_dump(reporter, p, false, true,
4126                  "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));  // 1, 2\n"
4127                  "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));  // 3, 4\n");
4128     p.reset();
4129     p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4130     p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4131     compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4132                                             "path.lineTo(3, 4);\n");
4133 }
4134
4135 namespace {
4136
4137 class ChangeListener : public SkPathRef::GenIDChangeListener {
4138 public:
4139     ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4140     virtual ~ChangeListener() {}
4141     void onChange() override {
4142         *fChanged = true;
4143     }
4144 private:
4145     bool* fChanged;
4146 };
4147
4148 }
4149
4150 class PathTest_Private {
4151 public:
4152     static void TestPathTo(skiatest::Reporter* reporter) {
4153         SkPath p, q;
4154         p.lineTo(4, 4);
4155         p.reversePathTo(q);
4156         check_path_is_line(reporter, &p, 4, 4);
4157         q.moveTo(-4, -4);
4158         p.reversePathTo(q);
4159         check_path_is_line(reporter, &p, 4, 4);
4160         q.lineTo(7, 8);
4161         q.conicTo(8, 7, 6, 5, 0.5f);
4162         q.quadTo(6, 7, 8, 6);
4163         q.cubicTo(5, 6, 7, 8, 7, 5);
4164         q.close();
4165         p.reversePathTo(q);
4166         SkRect reverseExpected = {-4, -4, 8, 8};
4167         REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4168     }
4169
4170     static void TestPathrefListeners(skiatest::Reporter* reporter) {
4171         SkPath p;
4172
4173         bool changed = false;
4174         p.moveTo(0, 0);
4175
4176         // Check that listener is notified on moveTo().
4177
4178         SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
4179         REPORTER_ASSERT(reporter, !changed);
4180         p.moveTo(10, 0);
4181         REPORTER_ASSERT(reporter, changed);
4182
4183         // Check that listener is notified on lineTo().
4184         SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
4185         REPORTER_ASSERT(reporter, !changed);
4186         p.lineTo(20, 0);
4187         REPORTER_ASSERT(reporter, changed);
4188
4189         // Check that listener is notified on reset().
4190         SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
4191         REPORTER_ASSERT(reporter, !changed);
4192         p.reset();
4193         REPORTER_ASSERT(reporter, changed);
4194
4195         p.moveTo(0, 0);
4196
4197         // Check that listener is notified on rewind().
4198         SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
4199         REPORTER_ASSERT(reporter, !changed);
4200         p.rewind();
4201         REPORTER_ASSERT(reporter, changed);
4202
4203         // Check that listener is notified when pathref is deleted.
4204         {
4205             SkPath q;
4206             q.moveTo(10, 10);
4207             SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
4208             REPORTER_ASSERT(reporter, !changed);
4209         }
4210         // q went out of scope.
4211         REPORTER_ASSERT(reporter, changed);
4212     }
4213 };
4214
4215 static void test_crbug_629455(skiatest::Reporter* reporter) {
4216     SkPath path;
4217     path.moveTo(0, 0);
4218     path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4219                  SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4220                  SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4221 //  AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4222     path.lineTo(0, 0);
4223
4224     auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4225     SkPaint paint;
4226     paint.setAntiAlias(true);
4227     surface->getCanvas()->drawPath(path, paint);
4228 }
4229
4230 static void test_interp(skiatest::Reporter* reporter) {
4231     SkPath p1, p2, out;
4232     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4233     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4234     REPORTER_ASSERT(reporter, p1 == out);
4235     REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4236     REPORTER_ASSERT(reporter, p1 == out);
4237     p1.moveTo(0, 2);
4238     p1.lineTo(0, 4);
4239     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4240     REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4241     p2.moveTo(6, 0);
4242     p2.lineTo(8, 0);
4243     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4244     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4245     REPORTER_ASSERT(reporter, p2 == out);
4246     REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4247     REPORTER_ASSERT(reporter, p1 == out);
4248     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4249     REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4250     p1.reset();
4251     p1.moveTo(4, 4);
4252     p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4253     p2.reset();
4254     p2.moveTo(4, 2);
4255     p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4256     REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4257     REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4258     REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4259     p2.reset();
4260     p2.moveTo(4, 2);
4261     p2.conicTo(6, 3, 6, 5, 1);
4262     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4263     p2.reset();
4264     p2.moveTo(4, 4);
4265     p2.conicTo(5, 4, 5, 5, 0.5f);
4266     REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4267 }
4268
4269 DEF_TEST(PathInterp, reporter) {
4270     test_interp(reporter);
4271 }
4272
4273 DEF_TEST(PathContains, reporter) {
4274     test_contains(reporter);
4275 }
4276
4277 DEF_TEST(Paths, reporter) {
4278     test_fuzz_crbug_643933();
4279     test_sect_with_horizontal_needs_pinning();
4280     test_crbug_629455(reporter);
4281     test_fuzz_crbug_627414(reporter);
4282     test_path_crbug364224();
4283
4284     SkTSize<SkScalar>::Make(3,4);
4285
4286     SkPath  p, empty;
4287     SkRect  bounds, bounds2;
4288     test_empty(reporter, p);
4289
4290     REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4291
4292     // this triggers a code path in SkPath::operator= which is otherwise unexercised
4293     SkPath& self = p;
4294     p = self;
4295
4296     // this triggers a code path in SkPath::swap which is otherwise unexercised
4297     p.swap(self);
4298
4299     bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
4300
4301     p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4302     check_convex_bounds(reporter, p, bounds);
4303     // we have quads or cubics
4304     REPORTER_ASSERT(reporter,
4305                     p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4306     REPORTER_ASSERT(reporter, !p.isEmpty());
4307
4308     p.reset();
4309     test_empty(reporter, p);
4310
4311     p.addOval(bounds);
4312     check_convex_bounds(reporter, p, bounds);
4313     REPORTER_ASSERT(reporter, !p.isEmpty());
4314
4315     p.rewind();
4316     test_empty(reporter, p);
4317
4318     p.addRect(bounds);
4319     check_convex_bounds(reporter, p, bounds);
4320     // we have only lines
4321     REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4322     REPORTER_ASSERT(reporter, !p.isEmpty());
4323
4324     REPORTER_ASSERT(reporter, p != empty);
4325     REPORTER_ASSERT(reporter, !(p == empty));
4326
4327     // do getPoints and getVerbs return the right result
4328     REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4329     REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4330     SkPoint pts[4];
4331     int count = p.getPoints(pts, 4);
4332     REPORTER_ASSERT(reporter, count == 4);
4333     uint8_t verbs[6];
4334     verbs[5] = 0xff;
4335     p.getVerbs(verbs, 5);
4336     REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4337     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4338     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4339     REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4340     REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4341     REPORTER_ASSERT(reporter, 0xff == verbs[5]);
4342     bounds2.set(pts, 4);
4343     REPORTER_ASSERT(reporter, bounds == bounds2);
4344
4345     bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4346     p.offset(SK_Scalar1*3, SK_Scalar1*4);
4347     REPORTER_ASSERT(reporter, bounds == p.getBounds());
4348
4349     REPORTER_ASSERT(reporter, p.isRect(nullptr));
4350     bounds2.setEmpty();
4351     REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4352     REPORTER_ASSERT(reporter, bounds == bounds2);
4353
4354     // now force p to not be a rect
4355     bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4356     p.addRect(bounds);
4357     REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4358
4359     // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4360     // path has a trailing moveTo. Please see crbug.com\445368)
4361     {
4362         SkRect r;
4363         p.reset();
4364         p.addRect(bounds);
4365         REPORTER_ASSERT(reporter, p.isRect(&r));
4366         REPORTER_ASSERT(reporter, r == bounds);
4367         // add a moveTo outside of our bounds
4368         p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4369         REPORTER_ASSERT(reporter, p.isRect(&r));
4370         REPORTER_ASSERT(reporter, r == bounds);
4371     }
4372
4373     test_operatorEqual(reporter);
4374     test_isLine(reporter);
4375     test_isRect(reporter);
4376     test_is_simple_closed_rect(reporter);
4377     test_isNestedFillRects(reporter);
4378     test_zero_length_paths(reporter);
4379     test_direction(reporter);
4380     test_convexity(reporter);
4381     test_convexity2(reporter);
4382     test_conservativelyContains(reporter);
4383     test_close(reporter);
4384     test_segment_masks(reporter);
4385     test_flattening(reporter);
4386     test_transform(reporter);
4387     test_bounds(reporter);
4388     test_iter(reporter);
4389     test_raw_iter(reporter);
4390     test_circle(reporter);
4391     test_oval(reporter);
4392     test_strokerec(reporter);
4393     test_addPoly(reporter);
4394     test_isfinite(reporter);
4395     test_isfinite_after_transform(reporter);
4396     test_islastcontourclosed(reporter);
4397     test_arb_round_rect_is_convex(reporter);
4398     test_arb_zero_rad_round_rect_is_rect(reporter);
4399     test_addrect(reporter);
4400     test_addrect_isfinite(reporter);
4401     test_tricky_cubic();
4402     test_clipped_cubic();
4403     test_crbug_170666();
4404     test_crbug_493450(reporter);
4405     test_crbug_495894(reporter);
4406     test_crbug_613918();
4407     test_bad_cubic_crbug229478();
4408     test_bad_cubic_crbug234190();
4409     test_gen_id(reporter);
4410     test_path_close_issue1474(reporter);
4411     test_path_to_region(reporter);
4412     test_rrect(reporter);
4413     test_arc(reporter);
4414     test_arc_ovals(reporter);
4415     test_arcTo(reporter);
4416     test_addPath(reporter);
4417     test_addPathMode(reporter, false, false);
4418     test_addPathMode(reporter, true, false);
4419     test_addPathMode(reporter, false, true);
4420     test_addPathMode(reporter, true, true);
4421     test_extendClosedPath(reporter);
4422     test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4423     test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4424     test_conicTo_special_case(reporter);
4425     test_get_point(reporter);
4426     test_contains(reporter);
4427     PathTest_Private::TestPathTo(reporter);
4428     PathRefTest_Private::TestPathRef(reporter);
4429     PathTest_Private::TestPathrefListeners(reporter);
4430     test_dump(reporter);
4431     test_path_crbug389050(reporter);
4432     test_path_crbugskia2820(reporter);
4433     test_skbug_3469(reporter);
4434     test_skbug_3239(reporter);
4435     test_bounds_crbug_513799(reporter);
4436     test_fuzz_crbug_638223();
4437 }