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