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