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