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