blob: 54ef7671d67d63875036e405d090936b42f454f8 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkFont.h"
10#include "include/core/SkPaint.h"
11#include "include/core/SkPathEffect.h"
12#include "include/core/SkRRect.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkStream.h"
15#include "include/core/SkStrokeRec.h"
16#include "include/core/SkSurface.h"
17#include "include/private/SkTo.h"
18#include "include/utils/SkNullCanvas.h"
19#include "include/utils/SkParse.h"
20#include "include/utils/SkParsePath.h"
21#include "include/utils/SkRandom.h"
22#include "src/core/SkAutoMalloc.h"
23#include "src/core/SkGeometry.h"
24#include "src/core/SkPathPriv.h"
25#include "src/core/SkReader32.h"
26#include "src/core/SkWriter32.h"
27#include "tests/Test.h"
Yuqian Li3154a532017-09-06 13:33:30 -040028
Hal Canaryc640d0d2018-06-13 09:59:02 -040029#include <cmath>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040030#include <utility>
Cary Clarkc9b7c722018-12-12 14:50:23 -050031#include <vector>
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040032
Yuqian Li3154a532017-09-06 13:33:30 -040033static void set_radii(SkVector radii[4], int index, float rad) {
34 sk_bzero(radii, sizeof(SkVector) * 4);
35 radii[index].set(rad, rad);
36}
37
38static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
39 const SkVector radii[4]) {
40 SkRRect rrect;
41 rrect.setRectRadii(bounds, radii);
42 REPORTER_ASSERT(reporter, bounds == rrect.rect());
43
44 SkPath path;
45 // this line should not assert in the debug build (from validate)
46 path.addRRect(rrect);
47 REPORTER_ASSERT(reporter, bounds == path.getBounds());
48}
49
50static void test_skbug_3469(skiatest::Reporter* reporter) {
51 SkPath path;
52 path.moveTo(20, 20);
53 path.quadTo(20, 50, 80, 50);
54 path.quadTo(20, 50, 20, 80);
55 REPORTER_ASSERT(reporter, !path.isConvex());
56}
57
58static void test_skbug_3239(skiatest::Reporter* reporter) {
59 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
60 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
61 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
62
63 const float rad = 33436320;
64
65 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
66 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
67
68 SkVector radii[4];
69 for (int i = 0; i < 4; ++i) {
70 set_radii(radii, i, rad);
71 test_add_rrect(reporter, rectx, radii);
72 test_add_rrect(reporter, recty, radii);
73 }
74}
75
76static void make_path_crbug364224(SkPath* path) {
77 path->reset();
78 path->moveTo(3.747501373f, 2.724499941f);
79 path->lineTo(3.747501373f, 3.75f);
80 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
81 path->lineTo(0.7475013733f, 4.0f);
82 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
83 path->lineTo(0.4975013733f, 1.0f);
84 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
85 path->lineTo(3.497501373f, 0.75f);
86 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
87 path->lineTo(3.715001345f, 0.5512499809f);
88 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
89 path->lineTo(0.7475013733f, 0.4999999702f);
90 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
91 path->lineTo(0.2475013733f, 3.75f);
92 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
93 path->lineTo(3.497501373f, 4.25f);
94 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
95 path->lineTo(3.997501373f, 2.474750042f);
96 path->lineTo(3.747501373f, 2.724499941f);
97 path->close();
98}
99
100static void make_path_crbug364224_simplified(SkPath* path) {
101 path->moveTo(3.747501373f, 2.724499941f);
102 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
103 path->close();
104}
105
106static void test_sect_with_horizontal_needs_pinning() {
107 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
108 // intersection.
109 SkPath path;
110 path.reset();
111 path.moveTo(-540000, -720000);
112 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
113 path.lineTo(1, 1);
114
115 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
116 SkPaint paint;
117 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
118}
119
120static void test_path_crbug364224() {
121 SkPath path;
122 SkPaint paint;
123 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
124 SkCanvas* canvas = surface->getCanvas();
125
126 make_path_crbug364224_simplified(&path);
127 canvas->drawPath(path, paint);
128
129 make_path_crbug364224(&path);
130 canvas->drawPath(path, paint);
131}
132
Yuqian Lif13beef2017-09-14 17:15:04 -0400133static void test_draw_AA_path(int width, int height, const SkPath& path) {
134 auto surface(SkSurface::MakeRasterN32Premul(width, height));
135 SkCanvas* canvas = surface->getCanvas();
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
Yuqian Li3154a532017-09-06 13:33:30 -0400141// this is a unit test instead of a GM because it doesn't draw anything
142static void test_fuzz_crbug_638223() {
Yuqian Li3154a532017-09-06 13:33:30 -0400143 SkPath path;
144 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
145 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
146 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
147 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
Yuqian Lif13beef2017-09-14 17:15:04 -0400148 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400149}
150
151static void test_fuzz_crbug_643933() {
Yuqian Li3154a532017-09-06 13:33:30 -0400152 SkPath path;
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
155 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
156 SkBits2Float(0x721aee0c)); // 3.0687e+30f
Yuqian Lif13beef2017-09-14 17:15:04 -0400157 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400158 path.reset();
159 path.moveTo(0, 0);
160 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
161 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
162 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
Yuqian Lif13beef2017-09-14 17:15:04 -0400163 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400164}
165
166static void test_fuzz_crbug_647922() {
Yuqian Li3154a532017-09-06 13:33:30 -0400167 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
Yuqian Lif13beef2017-09-14 17:15:04 -0400172 test_draw_AA_path(250, 250, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400173}
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() {
Yuqian Li3154a532017-09-06 13:33:30 -0400204 SkPath path;
205 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
206 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
207 // 440.47f, 341, 437, 344.47f, 437, 348.75f
208 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
209 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
210 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
211 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
212 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
213 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
Yuqian Lif13beef2017-09-14 17:15:04 -0400214 test_draw_AA_path(500, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400215}
216
217static void test_fuzz_crbug_668907() {
Yuqian Li3154a532017-09-06 13:33:30 -0400218 SkPath path;
219 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
220 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
221 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
222 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
223 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -0400224 test_draw_AA_path(400, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400225}
226
227/**
228 * In debug mode, this path was causing an assertion to fail in
229 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
230 */
231static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
232 SkPoint orig, p1, p2, p3;
233 orig = SkPoint::Make(1.f, 1.f);
234 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
235 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
236 p3 = SkPoint::Make(2.f, 2.f);
237
238 path->reset();
239 path->moveTo(orig);
240 path->cubicTo(p1, p2, p3);
241 path->close();
242}
243
244static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
245 SkPath path;
246 make_path_crbugskia2820(&path, reporter);
247
248 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
249 stroke.setStrokeStyle(2 * SK_Scalar1);
250 stroke.applyToPath(&path, path);
251}
252
253static void test_path_crbugskia5995() {
Yuqian Li3154a532017-09-06 13:33:30 -0400254 SkPath path;
255 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
256 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
257 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
258 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
Yuqian Lif13beef2017-09-14 17:15:04 -0400259 test_draw_AA_path(500, 500, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400260}
261
262static void make_path0(SkPath* path) {
263 // from * https://code.google.com/p/skia/issues/detail?id=1706
264
265 path->moveTo(146.939f, 1012.84f);
266 path->lineTo(181.747f, 1009.18f);
267 path->lineTo(182.165f, 1013.16f);
268 path->lineTo(147.357f, 1016.82f);
269 path->lineTo(146.939f, 1012.84f);
270 path->close();
271}
272
273static void make_path1(SkPath* path) {
274 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
275}
276
277typedef void (*PathProc)(SkPath*);
278
279/*
280 * Regression test: we used to crash (overwrite internal storage) during
281 * construction of the region when the path was INVERSE. That is now fixed,
282 * so test these regions (which used to assert/crash).
283 *
284 * https://code.google.com/p/skia/issues/detail?id=1706
285 */
286static void test_path_to_region(skiatest::Reporter* reporter) {
287 PathProc procs[] = {
288 make_path0,
289 make_path1,
290 };
291
292 SkRegion clip;
Mike Reed92b33352019-08-24 19:39:13 -0400293 clip.setRect({0, 0, 1255, 1925});
Yuqian Li3154a532017-09-06 13:33:30 -0400294
295 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
296 SkPath path;
297 procs[i](&path);
298
299 SkRegion rgn;
300 rgn.setPath(path, clip);
301 path.toggleInverseFillType();
302 rgn.setPath(path, clip);
303 }
304}
305
306#ifdef SK_BUILD_FOR_WIN
307 #define SUPPRESS_VISIBILITY_WARNING
308#else
309 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
310#endif
311
312static void test_path_close_issue1474(skiatest::Reporter* reporter) {
313 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
314 // are relative to the point we close to, not relative to the point we close from.
315 SkPath path;
316 SkPoint last;
317
318 // Test rLineTo().
319 path.rLineTo(0, 100);
320 path.rLineTo(100, 0);
321 path.close(); // Returns us back to 0,0.
322 path.rLineTo(50, 50); // This should go to 50,50.
323
324 path.getLastPt(&last);
325 REPORTER_ASSERT(reporter, 50 == last.fX);
326 REPORTER_ASSERT(reporter, 50 == last.fY);
327
328 // Test rQuadTo().
329 path.rewind();
330 path.rLineTo(0, 100);
331 path.rLineTo(100, 0);
332 path.close();
333 path.rQuadTo(50, 50, 75, 75);
334
335 path.getLastPt(&last);
336 REPORTER_ASSERT(reporter, 75 == last.fX);
337 REPORTER_ASSERT(reporter, 75 == last.fY);
338
339 // Test rConicTo().
340 path.rewind();
341 path.rLineTo(0, 100);
342 path.rLineTo(100, 0);
343 path.close();
344 path.rConicTo(50, 50, 85, 85, 2);
345
346 path.getLastPt(&last);
347 REPORTER_ASSERT(reporter, 85 == last.fX);
348 REPORTER_ASSERT(reporter, 85 == last.fY);
349
350 // Test rCubicTo().
351 path.rewind();
352 path.rLineTo(0, 100);
353 path.rLineTo(100, 0);
354 path.close();
355 path.rCubicTo(50, 50, 85, 85, 95, 95);
356
357 path.getLastPt(&last);
358 REPORTER_ASSERT(reporter, 95 == last.fX);
359 REPORTER_ASSERT(reporter, 95 == last.fY);
360}
361
362static void test_gen_id(skiatest::Reporter* reporter) {
363 SkPath a, b;
364 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
365
366 a.moveTo(0, 0);
367 const uint32_t z = a.getGenerationID();
368 REPORTER_ASSERT(reporter, z != b.getGenerationID());
369
370 a.reset();
371 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
372
373 a.moveTo(1, 1);
374 const uint32_t y = a.getGenerationID();
375 REPORTER_ASSERT(reporter, z != y);
376
377 b.moveTo(2, 2);
378 const uint32_t x = b.getGenerationID();
379 REPORTER_ASSERT(reporter, x != y && x != z);
380
381 a.swap(b);
382 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
383
384 b = a;
385 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
386
387 SkPath c(a);
388 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
389
390 c.lineTo(3, 3);
391 const uint32_t w = c.getGenerationID();
392 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
393 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
394 REPORTER_ASSERT(reporter, w != x);
395
396#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
397 static bool kExpectGenIDToIgnoreFill = false;
398#else
399 static bool kExpectGenIDToIgnoreFill = true;
400#endif
401
402 c.toggleInverseFillType();
403 const uint32_t v = c.getGenerationID();
404 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
405
406 c.rewind();
407 REPORTER_ASSERT(reporter, v != c.getGenerationID());
408}
409
410// This used to assert in the debug build, as the edges did not all line-up.
411static void test_bad_cubic_crbug234190() {
412 SkPath path;
413 path.moveTo(13.8509f, 3.16858f);
414 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
415 -2.38991e+08f, -4.26573e+08f,
416 -2.41016e+08f, -4.30188e+08f);
Yuqian Lif13beef2017-09-14 17:15:04 -0400417 test_draw_AA_path(84, 88, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400418}
419
420static void test_bad_cubic_crbug229478() {
421 const SkPoint pts[] = {
422 { 4595.91064f, -11596.9873f },
423 { 4597.2168f, -11595.9414f },
424 { 4598.52344f, -11594.8955f },
425 { 4599.83008f, -11593.8496f },
426 };
427
428 SkPath path;
429 path.moveTo(pts[0]);
430 path.cubicTo(pts[1], pts[2], pts[3]);
431
432 SkPaint paint;
433 paint.setStyle(SkPaint::kStroke_Style);
434 paint.setStrokeWidth(20);
435
436 SkPath dst;
437 // Before the fix, this would infinite-recurse, and run out of stack
438 // because we would keep trying to subdivide a degenerate cubic segment.
439 paint.getFillPath(path, &dst, nullptr);
440}
441
442static void build_path_170666(SkPath& path) {
443 path.moveTo(17.9459f, 21.6344f);
444 path.lineTo(139.545f, -47.8105f);
445 path.lineTo(139.545f, -47.8105f);
446 path.lineTo(131.07f, -47.3888f);
447 path.lineTo(131.07f, -47.3888f);
448 path.lineTo(122.586f, -46.9532f);
449 path.lineTo(122.586f, -46.9532f);
450 path.lineTo(18076.6f, 31390.9f);
451 path.lineTo(18076.6f, 31390.9f);
452 path.lineTo(18085.1f, 31390.5f);
453 path.lineTo(18085.1f, 31390.5f);
454 path.lineTo(18076.6f, 31390.9f);
455 path.lineTo(18076.6f, 31390.9f);
456 path.lineTo(17955, 31460.3f);
457 path.lineTo(17955, 31460.3f);
458 path.lineTo(17963.5f, 31459.9f);
459 path.lineTo(17963.5f, 31459.9f);
460 path.lineTo(17971.9f, 31459.5f);
461 path.lineTo(17971.9f, 31459.5f);
462 path.lineTo(17.9551f, 21.6205f);
463 path.lineTo(17.9551f, 21.6205f);
464 path.lineTo(9.47091f, 22.0561f);
465 path.lineTo(9.47091f, 22.0561f);
466 path.lineTo(17.9459f, 21.6344f);
467 path.lineTo(17.9459f, 21.6344f);
468 path.close();path.moveTo(0.995934f, 22.4779f);
469 path.lineTo(0.986725f, 22.4918f);
470 path.lineTo(0.986725f, 22.4918f);
471 path.lineTo(17955, 31460.4f);
472 path.lineTo(17955, 31460.4f);
473 path.lineTo(17971.9f, 31459.5f);
474 path.lineTo(17971.9f, 31459.5f);
475 path.lineTo(18093.6f, 31390.1f);
476 path.lineTo(18093.6f, 31390.1f);
477 path.lineTo(18093.6f, 31390);
478 path.lineTo(18093.6f, 31390);
479 path.lineTo(139.555f, -47.8244f);
480 path.lineTo(139.555f, -47.8244f);
481 path.lineTo(122.595f, -46.9671f);
482 path.lineTo(122.595f, -46.9671f);
483 path.lineTo(0.995934f, 22.4779f);
484 path.lineTo(0.995934f, 22.4779f);
485 path.close();
486 path.moveTo(5.43941f, 25.5223f);
487 path.lineTo(798267, -28871.1f);
488 path.lineTo(798267, -28871.1f);
489 path.lineTo(3.12512e+06f, -113102);
490 path.lineTo(3.12512e+06f, -113102);
491 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
492 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
493 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
494 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
495 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
496 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
497 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
498 path.lineTo(2.78271e+08f, -1.00733e+07f);
499 path.lineTo(2.78271e+08f, -1.00733e+07f);
500 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
501 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
502 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
503 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
504 path.lineTo(2.77473e+08f, -1.00444e+07f);
505 path.lineTo(2.77473e+08f, -1.00444e+07f);
506 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
507 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
508 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
509 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
510 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
511 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
512 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
513 path.lineTo(798284, -28872);
514 path.lineTo(798284, -28872);
515 path.lineTo(22.4044f, 24.6677f);
516 path.lineTo(22.4044f, 24.6677f);
517 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
518 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
519 path.close();
520}
521
522static void build_path_simple_170666(SkPath& path) {
523 path.moveTo(126.677f, 24.1591f);
524 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
525}
526
527// This used to assert in the SK_DEBUG build, as the clip step would fail with
528// too-few interations in our cubic-line intersection code. That code now runs
529// 24 interations (instead of 16).
530static void test_crbug_170666() {
531 SkPath path;
Yuqian Li3154a532017-09-06 13:33:30 -0400532 build_path_simple_170666(path);
Yuqian Lif13beef2017-09-14 17:15:04 -0400533 test_draw_AA_path(1000, 1000, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400534
535 build_path_170666(path);
Yuqian Lif13beef2017-09-14 17:15:04 -0400536 test_draw_AA_path(1000, 1000, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400537}
538
539
540static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
541 SkScalar tx, SkScalar ty, SkScalar scale) {
542 SkPath smallPath;
543 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
544 bool smallConvex = smallPath.isConvex();
545 SkPath largePath;
546 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
547 SkMatrix matrix;
548 matrix.reset();
549 matrix.preTranslate(100, 100);
550 matrix.preScale(scale, scale);
551 largePath.transform(matrix);
552 bool largeConvex = largePath.isConvex();
553 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
554}
555
556static void test_crbug_493450(skiatest::Reporter* reporter) {
557 const char reducedCase[] =
558 "M0,0"
559 "L0.0002, 0"
560 "L0.0002, 0.0002"
561 "L0.0001, 0.0001"
562 "L0,0.0002"
563 "Z";
564 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
565 const char originalFiddleData[] =
566 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
567 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
568 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
569 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
570 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
571 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
572 826357.3384828606f);
573}
574
575static void test_crbug_495894(skiatest::Reporter* reporter) {
576 const char originalFiddleData[] =
577 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
578 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
579 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
580 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
581 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
582 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
583 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
584 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
585 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
586 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
587 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
588 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
589 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
590 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
591 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
592 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
593 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
594 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
595 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
596 "L-0.33993994441916414,-0.11288906492739594Z";
597 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
598 65536);
599}
600
601static void test_crbug_613918() {
602 SkPath path;
603 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
604 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
605 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
606
607 // This call could lead to an assert or uninitialized read due to a failure
608 // to check the return value from SkCubicClipper::ChopMonoAtY.
609 path.contains(-1.84817e-08f, 1.15465e-08f);
610}
611
612static void test_addrect(skiatest::Reporter* reporter) {
613 SkPath path;
614 path.lineTo(0, 0);
615 path.addRect(SkRect::MakeWH(50, 100));
616 REPORTER_ASSERT(reporter, path.isRect(nullptr));
617
618 path.reset();
619 path.lineTo(FLT_EPSILON, FLT_EPSILON);
620 path.addRect(SkRect::MakeWH(50, 100));
621 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
622
623 path.reset();
624 path.quadTo(0, 0, 0, 0);
625 path.addRect(SkRect::MakeWH(50, 100));
626 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
627
628 path.reset();
629 path.conicTo(0, 0, 0, 0, 0.5f);
630 path.addRect(SkRect::MakeWH(50, 100));
631 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
632
633 path.reset();
634 path.cubicTo(0, 0, 0, 0, 0, 0);
635 path.addRect(SkRect::MakeWH(50, 100));
636 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
637}
638
639// Make sure we stay non-finite once we get there (unless we reset or rewind).
640static void test_addrect_isfinite(skiatest::Reporter* reporter) {
641 SkPath path;
642
643 path.addRect(SkRect::MakeWH(50, 100));
644 REPORTER_ASSERT(reporter, path.isFinite());
645
646 path.moveTo(0, 0);
647 path.lineTo(SK_ScalarInfinity, 42);
648 REPORTER_ASSERT(reporter, !path.isFinite());
649
650 path.addRect(SkRect::MakeWH(50, 100));
651 REPORTER_ASSERT(reporter, !path.isFinite());
652
653 path.reset();
654 REPORTER_ASSERT(reporter, path.isFinite());
655
656 path.addRect(SkRect::MakeWH(50, 100));
657 REPORTER_ASSERT(reporter, path.isFinite());
658}
659
660static void build_big_path(SkPath* path, bool reducedCase) {
661 if (reducedCase) {
662 path->moveTo(577330, 1971.72f);
663 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
664 } else {
665 path->moveTo(60.1631f, 7.70567f);
666 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
667 path->lineTo(577379, 1977.77f);
668 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
669 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
670 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
671 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
672 path->quadTo(577198, 1972, 577238, 1971.31f);
673 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
674 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
675 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
676 path->moveTo(306.718f, -32.912f);
677 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
678 }
679}
680
681static void test_clipped_cubic() {
682 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
683
684 // This path used to assert, because our cubic-chopping code incorrectly
685 // moved control points after the chop. This test should be run in SK_DEBUG
686 // mode to ensure that we no long assert.
687 SkPath path;
688 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
689 build_big_path(&path, SkToBool(doReducedCase));
690
691 SkPaint paint;
692 for (int doAA = 0; doAA <= 1; ++doAA) {
693 paint.setAntiAlias(SkToBool(doAA));
694 surface->getCanvas()->drawPath(path, paint);
695 }
696 }
697}
698
699static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
700 if (expected != bounds) {
701 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
702 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
703 expected.left(), expected.top(), expected.right(), expected.bottom());
704 }
705}
706
707static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
708 SkPath path;
709#if 0
710 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
711 // rewritten them to avoid this (compiler-bug?).
712 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
713
714 path.moveTo(-5, -8);
715 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
716
717 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
718 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
719
720 path.moveTo(1, 2);
721 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
722#else
723 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
724
725 path.moveTo(-5, -8); // should set the bounds
726 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
727
728 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
729 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
730
731 path.moveTo(1, 2); // don't expect this to have changed the bounds
732 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
733#endif
734}
735
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500736#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -0400737static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
738 SkPath path;
739 path.moveTo(0, 0);
740 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
Yuqian Lif13beef2017-09-14 17:15:04 -0400741 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400742}
743
744// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
745// which triggered an assert, from a tricky cubic. This test replicates that
746// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
747// assert in the SK_DEBUG build.
748static void test_tricky_cubic() {
749 const SkPoint pts[] = {
750 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
751 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
752 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
753 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
754 };
755
756 SkPath path;
757 path.moveTo(pts[0]);
758 path.cubicTo(pts[1], pts[2], pts[3]);
Yuqian Lif13beef2017-09-14 17:15:04 -0400759 test_draw_AA_path(19, 130, path);
Yuqian Li3154a532017-09-06 13:33:30 -0400760}
761
762// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
763//
764static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
765 SkPath path;
766 path.quadTo(157, 366, 286, 208);
767 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
768
769 SkMatrix matrix;
770 matrix.setScale(1000*1000, 1000*1000);
771
772 // Be sure that path::transform correctly updates isFinite and the bounds
773 // if the transformation overflows. The previous bug was that isFinite was
774 // set to true in this case, but the bounds were not set to empty (which
775 // they should be).
776 while (path.isFinite()) {
777 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
778 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
779 path.transform(matrix);
780 }
781 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
782
783 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
784 path.transform(matrix);
785 // we need to still be non-finite
786 REPORTER_ASSERT(reporter, !path.isFinite());
787 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
788}
789
790static void add_corner_arc(SkPath* path, const SkRect& rect,
791 SkScalar xIn, SkScalar yIn,
792 int startAngle)
793{
794
795 SkScalar rx = SkMinScalar(rect.width(), xIn);
796 SkScalar ry = SkMinScalar(rect.height(), yIn);
797
798 SkRect arcRect;
Mike Reed92b33352019-08-24 19:39:13 -0400799 arcRect.setLTRB(-rx, -ry, rx, ry);
Yuqian Li3154a532017-09-06 13:33:30 -0400800 switch (startAngle) {
801 case 0:
802 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
803 break;
804 case 90:
805 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
806 break;
807 case 180:
808 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
809 break;
810 case 270:
811 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
812 break;
813 default:
814 break;
815 }
816
817 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
818}
819
820static void make_arb_round_rect(SkPath* path, const SkRect& r,
821 SkScalar xCorner, SkScalar yCorner) {
822 // we are lazy here and use the same x & y for each corner
823 add_corner_arc(path, r, xCorner, yCorner, 270);
824 add_corner_arc(path, r, xCorner, yCorner, 0);
825 add_corner_arc(path, r, xCorner, yCorner, 90);
826 add_corner_arc(path, r, xCorner, yCorner, 180);
827 path->close();
828}
829
830// Chrome creates its own round rects with each corner possibly being different.
831// Performance will suffer if they are not convex.
832// Note: PathBench::ArbRoundRectBench performs almost exactly
833// the same test (but with drawing)
834static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
835 SkRandom rand;
836 SkRect r;
837
838 for (int i = 0; i < 5000; ++i) {
839
840 SkScalar size = rand.nextUScalar1() * 30;
841 if (size < SK_Scalar1) {
842 continue;
843 }
844 r.fLeft = rand.nextUScalar1() * 300;
845 r.fTop = rand.nextUScalar1() * 300;
846 r.fRight = r.fLeft + 2 * size;
847 r.fBottom = r.fTop + 2 * size;
848
849 SkPath temp;
850
851 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
852
853 REPORTER_ASSERT(reporter, temp.isConvex());
854 }
855}
856
857// Chrome will sometimes create a 0 radius round rect. The degenerate
858// quads prevent the path from being converted to a rect
859// Note: PathBench::ArbRoundRectBench performs almost exactly
860// the same test (but with drawing)
861static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
862 SkRandom rand;
863 SkRect r;
864
865 for (int i = 0; i < 5000; ++i) {
866
867 SkScalar size = rand.nextUScalar1() * 30;
868 if (size < SK_Scalar1) {
869 continue;
870 }
871 r.fLeft = rand.nextUScalar1() * 300;
872 r.fTop = rand.nextUScalar1() * 300;
873 r.fRight = r.fLeft + 2 * size;
874 r.fBottom = r.fTop + 2 * size;
875
876 SkPath temp;
877
878 make_arb_round_rect(&temp, r, 0, 0);
879
880 SkRect result;
881 REPORTER_ASSERT(reporter, temp.isRect(&result));
882 REPORTER_ASSERT(reporter, r == result);
883 }
884}
885
886static void test_rect_isfinite(skiatest::Reporter* reporter) {
887 const SkScalar inf = SK_ScalarInfinity;
888 const SkScalar negInf = SK_ScalarNegativeInfinity;
889 const SkScalar nan = SK_ScalarNaN;
890
891 SkRect r;
892 r.setEmpty();
893 REPORTER_ASSERT(reporter, r.isFinite());
Mike Reed92b33352019-08-24 19:39:13 -0400894 r.setLTRB(0, 0, inf, negInf);
Yuqian Li3154a532017-09-06 13:33:30 -0400895 REPORTER_ASSERT(reporter, !r.isFinite());
Mike Reed92b33352019-08-24 19:39:13 -0400896 r.setLTRB(0, 0, nan, 0);
Yuqian Li3154a532017-09-06 13:33:30 -0400897 REPORTER_ASSERT(reporter, !r.isFinite());
898
899 SkPoint pts[] = {
900 { 0, 0 },
901 { SK_Scalar1, 0 },
902 { 0, SK_Scalar1 },
903 };
904
905 bool isFine = r.setBoundsCheck(pts, 3);
906 REPORTER_ASSERT(reporter, isFine);
907 REPORTER_ASSERT(reporter, !r.isEmpty());
908
909 pts[1].set(inf, 0);
910 isFine = r.setBoundsCheck(pts, 3);
911 REPORTER_ASSERT(reporter, !isFine);
912 REPORTER_ASSERT(reporter, r.isEmpty());
913
914 pts[1].set(nan, 0);
915 isFine = r.setBoundsCheck(pts, 3);
916 REPORTER_ASSERT(reporter, !isFine);
917 REPORTER_ASSERT(reporter, r.isEmpty());
918}
919
920static void test_path_isfinite(skiatest::Reporter* reporter) {
921 const SkScalar inf = SK_ScalarInfinity;
922 const SkScalar negInf = SK_ScalarNegativeInfinity;
923 const SkScalar nan = SK_ScalarNaN;
924
925 SkPath path;
926 REPORTER_ASSERT(reporter, path.isFinite());
927
928 path.reset();
929 REPORTER_ASSERT(reporter, path.isFinite());
930
931 path.reset();
932 path.moveTo(SK_Scalar1, 0);
933 REPORTER_ASSERT(reporter, path.isFinite());
934
935 path.reset();
936 path.moveTo(inf, negInf);
937 REPORTER_ASSERT(reporter, !path.isFinite());
938
939 path.reset();
940 path.moveTo(nan, 0);
941 REPORTER_ASSERT(reporter, !path.isFinite());
942}
943
944static void test_isfinite(skiatest::Reporter* reporter) {
945 test_rect_isfinite(reporter);
946 test_path_isfinite(reporter);
947}
948
949static void test_islastcontourclosed(skiatest::Reporter* reporter) {
950 SkPath path;
951 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
952 path.moveTo(0, 0);
953 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
954 path.close();
955 REPORTER_ASSERT(reporter, path.isLastContourClosed());
956 path.lineTo(100, 100);
957 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
958 path.moveTo(200, 200);
959 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
960 path.close();
961 REPORTER_ASSERT(reporter, path.isLastContourClosed());
962 path.moveTo(0, 0);
963 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
964}
965
966// assert that we always
967// start with a moveTo
968// only have 1 moveTo
969// only have Lines after that
970// end with a single close
971// only have (at most) 1 close
972//
973static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
974 const SkPoint srcPts[], bool expectClose) {
975 SkPath::RawIter iter(path);
976 SkPoint pts[4];
977
978 bool firstTime = true;
979 bool foundClose = false;
980 for (;;) {
981 switch (iter.next(pts)) {
982 case SkPath::kMove_Verb:
983 REPORTER_ASSERT(reporter, firstTime);
984 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
985 srcPts++;
986 firstTime = false;
987 break;
988 case SkPath::kLine_Verb:
989 REPORTER_ASSERT(reporter, !firstTime);
990 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
991 srcPts++;
992 break;
993 case SkPath::kQuad_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -0500994 REPORTER_ASSERT(reporter, false, "unexpected quad verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400995 break;
996 case SkPath::kConic_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -0500997 REPORTER_ASSERT(reporter, false, "unexpected conic verb");
Yuqian Li3154a532017-09-06 13:33:30 -0400998 break;
999 case SkPath::kCubic_Verb:
Brian Salomon1c80e992018-01-29 09:50:47 -05001000 REPORTER_ASSERT(reporter, false, "unexpected cubic verb");
Yuqian Li3154a532017-09-06 13:33:30 -04001001 break;
1002 case SkPath::kClose_Verb:
1003 REPORTER_ASSERT(reporter, !firstTime);
1004 REPORTER_ASSERT(reporter, !foundClose);
1005 REPORTER_ASSERT(reporter, expectClose);
1006 foundClose = true;
1007 break;
1008 case SkPath::kDone_Verb:
1009 goto DONE;
1010 }
1011 }
1012DONE:
1013 REPORTER_ASSERT(reporter, foundClose == expectClose);
1014}
1015
1016static void test_addPoly(skiatest::Reporter* reporter) {
1017 SkPoint pts[32];
1018 SkRandom rand;
1019
1020 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1021 pts[i].fX = rand.nextSScalar1();
1022 pts[i].fY = rand.nextSScalar1();
1023 }
1024
1025 for (int doClose = 0; doClose <= 1; ++doClose) {
1026 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1027 SkPath path;
1028 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
1029 test_poly(reporter, path, pts, SkToBool(doClose));
1030 }
1031 }
1032}
1033
1034static void test_strokerec(skiatest::Reporter* reporter) {
1035 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1036 REPORTER_ASSERT(reporter, rec.isFillStyle());
1037
1038 rec.setHairlineStyle();
1039 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
1040
1041 rec.setStrokeStyle(SK_Scalar1, false);
1042 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
1043
1044 rec.setStrokeStyle(SK_Scalar1, true);
1045 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
1046
1047 rec.setStrokeStyle(0, false);
1048 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
1049
1050 rec.setStrokeStyle(0, true);
1051 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1052}
1053
1054// Set this for paths that don't have a consistent direction such as a bowtie.
1055// (cheapComputeDirection is not expected to catch these.)
Mike Kleinf6c85402018-08-13 12:19:28 -04001056// Legal values are CW (0), CCW (1) and Unknown (2), leaving 3 as a convenient sentinel.
1057const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(3);
Yuqian Li3154a532017-09-06 13:33:30 -04001058
1059static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
1060 SkPathPriv::FirstDirection expected) {
1061 if (expected == kDontCheckDir) {
1062 return;
1063 }
1064 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1065
1066 SkPathPriv::FirstDirection dir;
1067 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
1068 REPORTER_ASSERT(reporter, dir == expected);
1069 } else {
1070 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
1071 }
1072}
1073
1074static void test_direction(skiatest::Reporter* reporter) {
1075 size_t i;
1076 SkPath path;
1077 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1078 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1079 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1080 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
1081
1082 static const char* gDegen[] = {
1083 "M 10 10",
1084 "M 10 10 M 20 20",
1085 "M 10 10 L 20 20",
1086 "M 10 10 L 10 10 L 10 10",
1087 "M 10 10 Q 10 10 10 10",
1088 "M 10 10 C 10 10 10 10 10 10",
1089 };
1090 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1091 path.reset();
1092 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1093 REPORTER_ASSERT(reporter, valid);
1094 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1095 }
1096
1097 static const char* gCW[] = {
1098 "M 10 10 L 10 10 Q 20 10 20 20",
1099 "M 10 10 C 20 10 20 20 20 20",
1100 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1101 // rect with top two corners replaced by cubics with identical middle
1102 // control points
1103 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1104 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
1105 };
1106 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1107 path.reset();
1108 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1109 REPORTER_ASSERT(reporter, valid);
1110 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1111 }
1112
1113 static const char* gCCW[] = {
1114 "M 10 10 L 10 10 Q 20 10 20 -20",
1115 "M 10 10 C 20 10 20 -20 20 -20",
1116 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1117 // rect with top two corners replaced by cubics with identical middle
1118 // control points
1119 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1120 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
1121 };
1122 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1123 path.reset();
1124 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1125 REPORTER_ASSERT(reporter, valid);
1126 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1127 }
1128
1129 // Test two donuts, each wound a different direction. Only the outer contour
1130 // determines the cheap direction
1131 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00001132 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1133 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04001134 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1135
1136 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00001137 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1138 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04001139 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1140
1141 // triangle with one point really far from the origin.
1142 path.reset();
1143 // the first point is roughly 1.05e10, 1.05e10
1144 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1145 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1146 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1147 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1148
1149 path.reset();
1150 path.conicTo(20, 0, 20, 20, 0.5f);
1151 path.close();
1152 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1153
1154 path.reset();
1155 path.lineTo(1, 1e7f);
1156 path.lineTo(1e7f, 2e7f);
1157 path.close();
Mike Reed4241f5e2019-09-14 19:13:23 +00001158 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
Yuqian Li3154a532017-09-06 13:33:30 -04001159 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1160}
1161
1162static void add_rect(SkPath* path, const SkRect& r) {
1163 path->moveTo(r.fLeft, r.fTop);
1164 path->lineTo(r.fRight, r.fTop);
1165 path->lineTo(r.fRight, r.fBottom);
1166 path->lineTo(r.fLeft, r.fBottom);
1167 path->close();
1168}
1169
1170static void test_bounds(skiatest::Reporter* reporter) {
1171 static const SkRect rects[] = {
1172 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1173 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1174 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1175 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1176 };
1177
1178 SkPath path0, path1;
1179 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1180 path0.addRect(rects[i]);
1181 add_rect(&path1, rects[i]);
1182 }
1183
1184 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1185}
1186
1187static void stroke_cubic(const SkPoint pts[4]) {
1188 SkPath path;
1189 path.moveTo(pts[0]);
1190 path.cubicTo(pts[1], pts[2], pts[3]);
1191
1192 SkPaint paint;
1193 paint.setStyle(SkPaint::kStroke_Style);
1194 paint.setStrokeWidth(SK_Scalar1 * 2);
1195
1196 SkPath fill;
1197 paint.getFillPath(path, &fill);
1198}
1199
1200// just ensure this can run w/o any SkASSERTS firing in the debug build
1201// we used to assert due to differences in how we determine a degenerate vector
1202// but that was fixed with the introduction of SkPoint::CanNormalize
1203static void stroke_tiny_cubic() {
1204 SkPoint p0[] = {
1205 { 372.0f, 92.0f },
1206 { 372.0f, 92.0f },
1207 { 372.0f, 92.0f },
1208 { 372.0f, 92.0f },
1209 };
1210
1211 stroke_cubic(p0);
1212
1213 SkPoint p1[] = {
1214 { 372.0f, 92.0f },
1215 { 372.0007f, 92.000755f },
1216 { 371.99927f, 92.003922f },
1217 { 371.99826f, 92.003899f },
1218 };
1219
1220 stroke_cubic(p1);
1221}
1222
1223static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1224 for (int i = 0; i < 2; ++i) {
1225 SkPath::Iter iter(path, SkToBool(i));
1226 SkPoint mv;
1227 SkPoint pts[4];
1228 SkPath::Verb v;
1229 int nMT = 0;
1230 int nCL = 0;
1231 mv.set(0, 0);
1232 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1233 switch (v) {
1234 case SkPath::kMove_Verb:
1235 mv = pts[0];
1236 ++nMT;
1237 break;
1238 case SkPath::kClose_Verb:
1239 REPORTER_ASSERT(reporter, mv == pts[0]);
1240 ++nCL;
1241 break;
1242 default:
1243 break;
1244 }
1245 }
1246 // if we force a close on the interator we should have a close
1247 // for every moveTo
1248 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1249 }
1250}
1251
1252static void test_close(skiatest::Reporter* reporter) {
1253 SkPath closePt;
1254 closePt.moveTo(0, 0);
1255 closePt.close();
1256 check_close(reporter, closePt);
1257
1258 SkPath openPt;
1259 openPt.moveTo(0, 0);
1260 check_close(reporter, openPt);
1261
1262 SkPath empty;
1263 check_close(reporter, empty);
1264 empty.close();
1265 check_close(reporter, empty);
1266
1267 SkPath rect;
1268 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1269 check_close(reporter, rect);
1270 rect.close();
1271 check_close(reporter, rect);
1272
1273 SkPath quad;
1274 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1275 check_close(reporter, quad);
1276 quad.close();
1277 check_close(reporter, quad);
1278
1279 SkPath cubic;
1280 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1281 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1282 check_close(reporter, cubic);
1283 cubic.close();
1284 check_close(reporter, cubic);
1285
1286 SkPath line;
1287 line.moveTo(SK_Scalar1, SK_Scalar1);
1288 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1289 check_close(reporter, line);
1290 line.close();
1291 check_close(reporter, line);
1292
1293 SkPath rect2;
1294 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1295 rect2.close();
1296 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1297 check_close(reporter, rect2);
1298 rect2.close();
1299 check_close(reporter, rect2);
1300
1301 SkPath oval3;
1302 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1303 oval3.close();
1304 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1305 check_close(reporter, oval3);
1306 oval3.close();
1307 check_close(reporter, oval3);
1308
1309 SkPath moves;
1310 moves.moveTo(SK_Scalar1, SK_Scalar1);
1311 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1312 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1313 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1314 check_close(reporter, moves);
1315
1316 stroke_tiny_cubic();
1317}
1318
1319static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1320 SkPath::Convexity expected) {
1321 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1322 SkPath::Convexity c = copy.getConvexity();
1323 REPORTER_ASSERT(reporter, c == expected);
Cary Clark21a7be02018-12-13 15:40:04 -05001324#ifndef SK_LEGACY_PATH_CONVEXITY
Cary Clarkc9b7c722018-12-12 14:50:23 -05001325 // test points-by-array interface
1326 SkPath::Iter iter(path, true);
1327 int initialMoves = 0;
1328 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -04001329 while (SkPath::kMove_Verb == iter.next(pts)) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001330 ++initialMoves;
1331 }
1332 if (initialMoves > 0) {
1333 std::vector<SkPoint> points;
1334 points.resize(path.getPoints(nullptr, 0));
1335 (void) path.getPoints(&points.front(), points.size());
1336 int skip = initialMoves - 1;
1337 bool isConvex = SkPathPriv::IsConvex(&points.front() + skip, points.size() - skip);
1338 REPORTER_ASSERT(reporter, isConvex == (SkPath::kConvex_Convexity == expected));
1339 }
Cary Clark21a7be02018-12-13 15:40:04 -05001340#endif
Yuqian Li3154a532017-09-06 13:33:30 -04001341}
1342
1343static void test_path_crbug389050(skiatest::Reporter* reporter) {
1344 SkPath tinyConvexPolygon;
1345 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1346 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1347 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1348 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1349 tinyConvexPolygon.close();
1350 tinyConvexPolygon.getConvexity();
Brian Osmancf4ec502019-04-19 12:58:21 -04001351 // This is convex, but so small that it fails many of our checks, and the three "backwards"
1352 // bends convince the checker that it's concave. That's okay though, we draw it correctly.
1353 check_convexity(reporter, tinyConvexPolygon, SkPath::kConcave_Convexity);
1354 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
Yuqian Li3154a532017-09-06 13:33:30 -04001355
1356 SkPath platTriangle;
1357 platTriangle.moveTo(0, 0);
1358 platTriangle.lineTo(200, 0);
1359 platTriangle.lineTo(100, 0.04f);
1360 platTriangle.close();
1361 platTriangle.getConvexity();
1362 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1363
1364 platTriangle.reset();
1365 platTriangle.moveTo(0, 0);
1366 platTriangle.lineTo(200, 0);
1367 platTriangle.lineTo(100, 0.03f);
1368 platTriangle.close();
1369 platTriangle.getConvexity();
1370 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1371}
1372
1373static void test_convexity2(skiatest::Reporter* reporter) {
1374 SkPath pt;
1375 pt.moveTo(0, 0);
1376 pt.close();
1377 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
1378 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
1379
1380 SkPath line;
1381 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1382 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1383 line.close();
1384 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1385 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
1386
1387 SkPath triLeft;
1388 triLeft.moveTo(0, 0);
1389 triLeft.lineTo(SK_Scalar1, 0);
1390 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1391 triLeft.close();
1392 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
1393 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
1394
1395 SkPath triRight;
1396 triRight.moveTo(0, 0);
1397 triRight.lineTo(-SK_Scalar1, 0);
1398 triRight.lineTo(SK_Scalar1, SK_Scalar1);
1399 triRight.close();
1400 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
1401 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
1402
1403 SkPath square;
1404 square.moveTo(0, 0);
1405 square.lineTo(SK_Scalar1, 0);
1406 square.lineTo(SK_Scalar1, SK_Scalar1);
1407 square.lineTo(0, SK_Scalar1);
1408 square.close();
1409 check_convexity(reporter, square, SkPath::kConvex_Convexity);
1410 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
1411
1412 SkPath redundantSquare;
1413 redundantSquare.moveTo(0, 0);
1414 redundantSquare.lineTo(0, 0);
1415 redundantSquare.lineTo(0, 0);
1416 redundantSquare.lineTo(SK_Scalar1, 0);
1417 redundantSquare.lineTo(SK_Scalar1, 0);
1418 redundantSquare.lineTo(SK_Scalar1, 0);
1419 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1420 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1421 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1422 redundantSquare.lineTo(0, SK_Scalar1);
1423 redundantSquare.lineTo(0, SK_Scalar1);
1424 redundantSquare.lineTo(0, SK_Scalar1);
1425 redundantSquare.close();
1426 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
1427 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
1428
1429 SkPath bowTie;
1430 bowTie.moveTo(0, 0);
1431 bowTie.lineTo(0, 0);
1432 bowTie.lineTo(0, 0);
1433 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1434 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1435 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1436 bowTie.lineTo(SK_Scalar1, 0);
1437 bowTie.lineTo(SK_Scalar1, 0);
1438 bowTie.lineTo(SK_Scalar1, 0);
1439 bowTie.lineTo(0, SK_Scalar1);
1440 bowTie.lineTo(0, SK_Scalar1);
1441 bowTie.lineTo(0, SK_Scalar1);
1442 bowTie.close();
1443 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
1444 check_direction(reporter, bowTie, kDontCheckDir);
1445
1446 SkPath spiral;
1447 spiral.moveTo(0, 0);
1448 spiral.lineTo(100*SK_Scalar1, 0);
1449 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1450 spiral.lineTo(0, 100*SK_Scalar1);
1451 spiral.lineTo(0, 50*SK_Scalar1);
1452 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1453 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1454 spiral.close();
1455 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
1456 check_direction(reporter, spiral, kDontCheckDir);
1457
1458 SkPath dent;
1459 dent.moveTo(0, 0);
1460 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1461 dent.lineTo(0, 100*SK_Scalar1);
1462 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1463 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1464 dent.close();
1465 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
1466 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
1467
1468 // https://bug.skia.org/2235
1469 SkPath strokedSin;
1470 for (int i = 0; i < 2000; i++) {
1471 SkScalar x = SkIntToScalar(i) / 2;
1472 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1473 if (0 == i) {
1474 strokedSin.moveTo(x, y);
1475 } else {
1476 strokedSin.lineTo(x, y);
1477 }
1478 }
1479 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1480 stroke.setStrokeStyle(2 * SK_Scalar1);
1481 stroke.applyToPath(&strokedSin, strokedSin);
Brian Osmancf4ec502019-04-19 12:58:21 -04001482 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04001483 check_direction(reporter, strokedSin, kDontCheckDir);
1484
1485 // http://crbug.com/412640
1486 SkPath degenerateConcave;
1487 degenerateConcave.moveTo(148.67912f, 191.875f);
1488 degenerateConcave.lineTo(470.37695f, 7.5f);
1489 degenerateConcave.lineTo(148.67912f, 191.875f);
1490 degenerateConcave.lineTo(41.446522f, 376.25f);
1491 degenerateConcave.lineTo(-55.971577f, 460.0f);
1492 degenerateConcave.lineTo(41.446522f, 376.25f);
1493 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
1494 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
1495
1496 // http://crbug.com/433683
1497 SkPath badFirstVector;
1498 badFirstVector.moveTo(501.087708f, 319.610352f);
1499 badFirstVector.lineTo(501.087708f, 319.610352f);
1500 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1501 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1502 badFirstVector.lineTo(301.557678f, 98.044601f);
1503 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1504 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1505 badFirstVector.lineTo(504.912292f, 316.389648f);
1506 badFirstVector.lineTo(501.087708f, 319.610352f);
1507 badFirstVector.close();
1508 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
Brian Osman4be31e22019-08-20 15:35:26 -04001509
1510 // http://crbug.com/993330
1511 SkPath falseBackEdge;
1512 falseBackEdge.moveTo(-217.83430557928145f, -382.14948768484857f);
1513 falseBackEdge.lineTo(-227.73867866614847f, -399.52485512718323f);
1514 falseBackEdge.cubicTo(-158.3541047666846f, -439.0757140459542f,
1515 -79.8654464485281f, -459.875f,
1516 -1.1368683772161603e-13f, -459.875f);
1517 falseBackEdge.lineTo(-8.08037266162413e-14f, -439.875f);
1518 falseBackEdge.lineTo(-8.526512829121202e-14f, -439.87499999999994f);
1519 falseBackEdge.cubicTo(-76.39209188702645f, -439.87499999999994f,
1520 -151.46727226799754f, -419.98027663161537f,
1521 -217.83430557928145f, -382.14948768484857f);
1522 falseBackEdge.close();
1523 check_convexity(reporter, falseBackEdge, SkPath::kConcave_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04001524}
1525
Cary Clarkc9b7c722018-12-12 14:50:23 -05001526static void test_convexity_doubleback(skiatest::Reporter* reporter) {
1527 SkPath doubleback;
1528 doubleback.lineTo(1, 1);
1529 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1530 doubleback.lineTo(2, 2);
Brian Osman309de792019-04-19 09:39:56 -04001531 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001532 doubleback.reset();
1533 doubleback.lineTo(1, 0);
1534 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1535 doubleback.lineTo(2, 0);
1536 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1537 doubleback.lineTo(1, 0);
1538 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1539 doubleback.reset();
1540 doubleback.quadTo(1, 1, 2, 2);
Brian Osman309de792019-04-19 09:39:56 -04001541 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001542 doubleback.reset();
1543 doubleback.quadTo(1, 0, 2, 0);
1544 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1545 doubleback.quadTo(1, 0, 0, 0);
1546 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1547}
1548
Yuqian Li3154a532017-09-06 13:33:30 -04001549static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1550 const SkRect& bounds) {
1551 REPORTER_ASSERT(reporter, p.isConvex());
1552 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1553
1554 SkPath p2(p);
1555 REPORTER_ASSERT(reporter, p2.isConvex());
1556 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1557
1558 SkPath other;
1559 other.swap(p2);
1560 REPORTER_ASSERT(reporter, other.isConvex());
1561 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1562}
1563
1564static void setFromString(SkPath* path, const char str[]) {
1565 bool first = true;
1566 while (str) {
1567 SkScalar x, y;
1568 str = SkParse::FindScalar(str, &x);
1569 if (nullptr == str) {
1570 break;
1571 }
1572 str = SkParse::FindScalar(str, &y);
1573 SkASSERT(str);
1574 if (first) {
1575 path->moveTo(x, y);
1576 first = false;
1577 } else {
1578 path->lineTo(x, y);
1579 }
1580 }
1581}
1582
1583static void test_convexity(skiatest::Reporter* reporter) {
1584 SkPath path;
1585
1586 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1587 path.addCircle(0, 0, SkIntToScalar(10));
1588 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1589 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
1590 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1591
1592 path.reset();
1593 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
1594 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1595 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1596
1597 path.reset();
1598 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
1599 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1600 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1601
1602 path.reset();
Cary Clarkc9b7c722018-12-12 14:50:23 -05001603 path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
Brian Osman309de792019-04-19 09:39:56 -04001604 check_convexity(reporter, path, SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04001605
1606 static const struct {
1607 const char* fPathStr;
1608 SkPath::Convexity fExpectedConvexity;
1609 SkPathPriv::FirstDirection fExpectedDirection;
1610 } gRec[] = {
1611 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1612 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1613 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1614 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1615 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1616 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
1617 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1618 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
1619 };
1620
1621 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001622 path.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04001623 setFromString(&path, gRec[i].fPathStr);
1624 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1625 check_direction(reporter, path, gRec[i].fExpectedDirection);
1626 // check after setting the initial convex and direction
1627 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1628 SkPath copy(path);
1629 SkPathPriv::FirstDirection dir;
1630 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1631 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
1632 ^ foundDir);
1633 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1634 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1635 }
1636 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1637 check_direction(reporter, path, gRec[i].fExpectedDirection);
1638 }
1639
1640 static const SkPoint nonFinitePts[] = {
1641 { SK_ScalarInfinity, 0 },
1642 { 0, SK_ScalarInfinity },
1643 { SK_ScalarInfinity, SK_ScalarInfinity },
1644 { SK_ScalarNegativeInfinity, 0},
1645 { 0, SK_ScalarNegativeInfinity },
1646 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1647 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1648 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1649 { SK_ScalarNaN, 0 },
1650 { 0, SK_ScalarNaN },
1651 { SK_ScalarNaN, SK_ScalarNaN },
1652 };
1653
1654 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1655
Cary Clarkc9b7c722018-12-12 14:50:23 -05001656 static const SkPoint axisAlignedPts[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04001657 { SK_ScalarMax, 0 },
1658 { 0, SK_ScalarMax },
Yuqian Li3154a532017-09-06 13:33:30 -04001659 { SK_ScalarMin, 0 },
1660 { 0, SK_ScalarMin },
Yuqian Li3154a532017-09-06 13:33:30 -04001661 };
1662
Cary Clarkc9b7c722018-12-12 14:50:23 -05001663 const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
Yuqian Li3154a532017-09-06 13:33:30 -04001664
Cary Clarkc9b7c722018-12-12 14:50:23 -05001665 for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
Yuqian Li3154a532017-09-06 13:33:30 -04001666 int i = (int) (index % nonFinitePtsCount);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001667 int f = (int) (index % axisAlignedPtsCount);
1668 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001669 path.reset();
1670 switch (index % 13) {
1671 case 0: path.lineTo(nonFinitePts[i]); break;
1672 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001673 case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
1674 case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
1675 case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
1676 case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
1677 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
1678 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
1679 case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
1680 case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001681 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001682 case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001683 case 12: path.moveTo(nonFinitePts[i]); break;
1684 }
1685 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1686 }
1687
Cary Clarkc9b7c722018-12-12 14:50:23 -05001688 for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
1689 int f = (int) (index % axisAlignedPtsCount);
1690 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001691 path.reset();
1692 int curveSelect = index % 11;
1693 switch (curveSelect) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001694 case 0: path.moveTo(axisAlignedPts[f]); break;
1695 case 1: path.lineTo(axisAlignedPts[f]); break;
1696 case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
1697 case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
1698 case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
1699 case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
1700 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
1701 case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
1702 case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
1703 case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
1704 case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001705 }
Cary Clarkc9b7c722018-12-12 14:50:23 -05001706 if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
1707 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1708 } else {
1709 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1710 SkPath::Convexity c = copy.getConvexity();
1711 REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
1712 || SkPath::kConcave_Convexity == c);
1713 }
Yuqian Li3154a532017-09-06 13:33:30 -04001714 }
1715
Cary Clarkc9b7c722018-12-12 14:50:23 -05001716 static const SkPoint diagonalPts[] = {
1717 { SK_ScalarMax, SK_ScalarMax },
1718 { SK_ScalarMin, SK_ScalarMin },
1719 };
1720
1721 const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
1722
1723 for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
1724 int f = (int) (index % diagonalPtsCount);
1725 int g = (int) ((f + 1) % diagonalPtsCount);
1726 path.reset();
1727 int curveSelect = index % 11;
1728 switch (curveSelect) {
1729 case 0: path.moveTo(diagonalPts[f]); break;
1730 case 1: path.lineTo(diagonalPts[f]); break;
1731 case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
1732 case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
1733 case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
1734 case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
1735 case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
1736 case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
1737 case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
1738 case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
1739 case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
1740 }
1741 if (curveSelect == 0) {
1742 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1743 } else {
1744 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1745 SkPath::Convexity c = copy.getConvexity();
1746 REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
1747 || SkPath::kConcave_Convexity == c);
1748 }
1749 }
1750
1751
Yuqian Li3154a532017-09-06 13:33:30 -04001752 path.reset();
1753 path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1754 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
1755 path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
1756 path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
1757 path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
1758 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1759 path.close();
1760 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1761
1762}
1763
1764static void test_isLine(skiatest::Reporter* reporter) {
1765 SkPath path;
1766 SkPoint pts[2];
1767 const SkScalar value = SkIntToScalar(5);
1768
1769 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1770
1771 // set some non-zero values
1772 pts[0].set(value, value);
1773 pts[1].set(value, value);
1774 REPORTER_ASSERT(reporter, !path.isLine(pts));
1775 // check that pts was untouched
1776 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1777 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1778
1779 const SkScalar moveX = SkIntToScalar(1);
1780 const SkScalar moveY = SkIntToScalar(2);
1781 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1782
1783 path.moveTo(moveX, moveY);
1784 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1785 REPORTER_ASSERT(reporter, !path.isLine(pts));
1786 // check that pts was untouched
1787 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1788 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1789
1790 const SkScalar lineX = SkIntToScalar(2);
1791 const SkScalar lineY = SkIntToScalar(2);
1792 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1793
1794 path.lineTo(lineX, lineY);
1795 REPORTER_ASSERT(reporter, path.isLine(nullptr));
1796
1797 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1798 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1799 REPORTER_ASSERT(reporter, path.isLine(pts));
1800 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1801 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1802
1803 path.lineTo(0, 0); // too many points/verbs
1804 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1805 REPORTER_ASSERT(reporter, !path.isLine(pts));
1806 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1807 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1808
1809 path.reset();
1810 path.quadTo(1, 1, 2, 2);
1811 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1812}
1813
1814static void test_conservativelyContains(skiatest::Reporter* reporter) {
1815 SkPath path;
1816
1817 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1818 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1819
1820 // A circle that bounds kBaseRect (with a significant amount of slop)
1821 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1822 circleR *= 1.75f / 2;
1823 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1824
1825 // round-rect radii
1826 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1827
1828 static const struct SUPPRESS_VISIBILITY_WARNING {
1829 SkRect fQueryRect;
1830 bool fInRect;
1831 bool fInCircle;
1832 bool fInRR;
1833 bool fInCubicRR;
1834 } kQueries[] = {
1835 {kBaseRect, true, true, false, false},
1836
1837 // rect well inside of kBaseRect
1838 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1839 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1840 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1841 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1842 true, true, true, true},
1843
1844 // rects with edges off by one from kBaseRect's edges
1845 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1846 kBaseRect.width(), kBaseRect.height() + 1),
1847 false, true, false, false},
1848 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1849 kBaseRect.width() + 1, kBaseRect.height()),
1850 false, true, false, false},
1851 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1852 kBaseRect.width() + 1, kBaseRect.height() + 1),
1853 false, true, false, false},
1854 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1855 kBaseRect.width(), kBaseRect.height()),
1856 false, true, false, false},
1857 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1858 kBaseRect.width(), kBaseRect.height()),
1859 false, true, false, false},
1860 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1861 kBaseRect.width() + 2, kBaseRect.height()),
1862 false, true, false, false},
1863 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1864 kBaseRect.width() + 2, kBaseRect.height()),
1865 false, true, false, false},
1866
1867 // zero-w/h rects at each corner of kBaseRect
1868 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1869 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1870 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1871 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1872
1873 // far away rect
1874 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1875 SkIntToScalar(10), SkIntToScalar(10)),
1876 false, false, false, false},
1877
1878 // very large rect containing kBaseRect
1879 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1880 kBaseRect.fTop - 5 * kBaseRect.height(),
1881 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1882 false, false, false, false},
1883
1884 // skinny rect that spans same y-range as kBaseRect
1885 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1886 SkIntToScalar(1), kBaseRect.height()),
1887 true, true, true, true},
1888
1889 // short rect that spans same x-range as kBaseRect
1890 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1891 true, true, true, true},
1892
1893 // skinny rect that spans slightly larger y-range than kBaseRect
1894 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1895 SkIntToScalar(1), kBaseRect.height() + 1),
1896 false, true, false, false},
1897
1898 // short rect that spans slightly larger x-range than kBaseRect
1899 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1900 kBaseRect.width() + 1, SkScalar(1)),
1901 false, true, false, false},
1902 };
1903
1904 for (int inv = 0; inv < 4; ++inv) {
1905 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1906 SkRect qRect = kQueries[q].fQueryRect;
1907 if (inv & 0x1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001908 using std::swap;
1909 swap(qRect.fLeft, qRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04001910 }
1911 if (inv & 0x2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001912 using std::swap;
1913 swap(qRect.fTop, qRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04001914 }
1915 for (int d = 0; d < 2; ++d) {
Mike Reed4241f5e2019-09-14 19:13:23 +00001916 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04001917 path.reset();
1918 path.addRect(kBaseRect, dir);
1919 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1920 path.conservativelyContainsRect(qRect));
1921
1922 path.reset();
1923 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1924 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1925 path.conservativelyContainsRect(qRect));
1926
1927 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00001928 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
Yuqian Li3154a532017-09-06 13:33:30 -04001929 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1930 path.conservativelyContainsRect(qRect));
1931
1932 path.reset();
1933 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1934 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1935 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1936 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1937 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1938 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1939 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1940 path.close();
1941 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1942 path.conservativelyContainsRect(qRect));
1943
1944 }
1945 // Slightly non-convex shape, shouldn't contain any rects.
1946 path.reset();
1947 path.moveTo(0, 0);
1948 path.lineTo(SkIntToScalar(50), 0.05f);
1949 path.lineTo(SkIntToScalar(100), 0);
1950 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1951 path.lineTo(0, SkIntToScalar(100));
1952 path.close();
1953 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1954 }
1955 }
1956
1957 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1958 path.reset();
1959 path.moveTo(0, 0);
1960 path.lineTo(SkIntToScalar(100), 0);
1961 path.lineTo(0, SkIntToScalar(100));
1962
1963 // inside, on along top edge
1964 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1965 SkIntToScalar(10),
1966 SkIntToScalar(10))));
1967 // above
1968 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1969 SkRect::MakeXYWH(SkIntToScalar(50),
1970 SkIntToScalar(-10),
1971 SkIntToScalar(10),
1972 SkIntToScalar(10))));
1973 // to the left
1974 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1975 SkIntToScalar(5),
1976 SkIntToScalar(5),
1977 SkIntToScalar(5))));
1978
1979 // outside the diagonal edge
1980 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1981 SkIntToScalar(200),
1982 SkIntToScalar(20),
1983 SkIntToScalar(5))));
1984
1985
1986 // Test that multiple move commands do not cause asserts.
Yuqian Li3154a532017-09-06 13:33:30 -04001987 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
Yuqian Li3154a532017-09-06 13:33:30 -04001988 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1989 SkIntToScalar(10),
1990 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001991
1992 // Same as above path and first test but with an extra moveTo.
1993 path.reset();
1994 path.moveTo(100, 100);
1995 path.moveTo(0, 0);
1996 path.lineTo(SkIntToScalar(100), 0);
1997 path.lineTo(0, SkIntToScalar(100));
Brian Osman205a1262017-09-18 13:13:48 +00001998 // Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
1999 // path non-convex.
2000 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2001 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2002 SkIntToScalar(10),
2003 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04002004
2005 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
2006 // following the non-empty sub-path. Verifies that this does not trigger assertions.
2007 path.reset();
2008 path.moveTo(0, 0);
2009 path.lineTo(SkIntToScalar(100), 0);
2010 path.lineTo(0, SkIntToScalar(100));
2011 path.moveTo(100, 100);
2012
2013 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
2014 SkIntToScalar(10),
2015 SkIntToScalar(10))));
2016
2017 // Test that multiple move commands do not cause asserts and that the function
2018 // is not confused by the multiple moves.
2019 path.reset();
2020 path.moveTo(0, 0);
2021 path.lineTo(SkIntToScalar(100), 0);
2022 path.lineTo(0, SkIntToScalar(100));
2023 path.moveTo(0, SkIntToScalar(200));
2024 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
2025 path.lineTo(0, SkIntToScalar(300));
2026
2027 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2028 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2029 SkIntToScalar(10),
2030 SkIntToScalar(10))));
2031
2032 path.reset();
2033 path.lineTo(100, 100);
2034 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
2035
2036 // An empty path should not contain any rectangle. It's questionable whether an empty path
2037 // contains an empty rectangle. However, since it is a conservative test it is ok to
2038 // return false.
2039 path.reset();
2040 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2041 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2042}
2043
2044static void test_isRect_open_close(skiatest::Reporter* reporter) {
2045 SkPath path;
2046 bool isClosed;
2047
2048 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
2049 path.close();
2050
2051 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
2052 REPORTER_ASSERT(reporter, isClosed);
2053}
2054
2055// Simple isRect test is inline TestPath, below.
2056// test_isRect provides more extensive testing.
2057static void test_isRect(skiatest::Reporter* reporter) {
2058 test_isRect_open_close(reporter);
2059
2060 // passing tests (all moveTo / lineTo...
2061 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2062 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2063 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2064 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2065 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
2066 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2067 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2068 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2069 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2070 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
2071 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
2072 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
2073 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
2074 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
2075 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
2076
2077 // failing tests
2078 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2079 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2080 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2081 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2082 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2083 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2084 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2085 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2086 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2087 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2088 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
2089
2090 // no close, but we should detect them as fillably the same as a rect
2091 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2092 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2093 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2094
2095 // like c2, but we double-back on ourselves
2096 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2097 // like c2, but we overshoot the start point
2098 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2099 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2100
2101 struct IsRectTest {
2102 SkPoint *fPoints;
2103 int fPointCount;
2104 bool fClose;
2105 bool fIsRect;
2106 } tests[] = {
2107 { r1, SK_ARRAY_COUNT(r1), true, true },
2108 { r2, SK_ARRAY_COUNT(r2), true, true },
2109 { r3, SK_ARRAY_COUNT(r3), true, true },
2110 { r4, SK_ARRAY_COUNT(r4), true, true },
2111 { r5, SK_ARRAY_COUNT(r5), true, true },
2112 { r6, SK_ARRAY_COUNT(r6), true, true },
2113 { r7, SK_ARRAY_COUNT(r7), true, true },
2114 { r8, SK_ARRAY_COUNT(r8), true, true },
2115 { r9, SK_ARRAY_COUNT(r9), true, true },
2116 { ra, SK_ARRAY_COUNT(ra), true, true },
2117 { rb, SK_ARRAY_COUNT(rb), true, true },
2118 { rc, SK_ARRAY_COUNT(rc), true, true },
2119 { rd, SK_ARRAY_COUNT(rd), true, true },
2120 { re, SK_ARRAY_COUNT(re), true, true },
2121 { rf, SK_ARRAY_COUNT(rf), true, true },
2122
2123 { f1, SK_ARRAY_COUNT(f1), true, false },
2124 { f2, SK_ARRAY_COUNT(f2), true, false },
2125 { f3, SK_ARRAY_COUNT(f3), true, false },
2126 { f4, SK_ARRAY_COUNT(f4), true, false },
2127 { f5, SK_ARRAY_COUNT(f5), true, false },
2128 { f6, SK_ARRAY_COUNT(f6), true, false },
2129 { f7, SK_ARRAY_COUNT(f7), true, false },
2130 { f8, SK_ARRAY_COUNT(f8), true, false },
2131 { f9, SK_ARRAY_COUNT(f9), true, false },
2132 { fa, SK_ARRAY_COUNT(fa), true, false },
2133 { fb, SK_ARRAY_COUNT(fb), true, false },
2134
2135 { c1, SK_ARRAY_COUNT(c1), false, true },
2136 { c2, SK_ARRAY_COUNT(c2), false, true },
2137 { c3, SK_ARRAY_COUNT(c3), false, true },
2138
2139 { d1, SK_ARRAY_COUNT(d1), false, false },
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002140 { d2, SK_ARRAY_COUNT(d2), false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002141 { d3, SK_ARRAY_COUNT(d3), false, false },
2142 };
2143
2144 const size_t testCount = SK_ARRAY_COUNT(tests);
2145 int index;
2146 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2147 SkPath path;
2148 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2149 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2150 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2151 }
2152 if (tests[testIndex].fClose) {
2153 path.close();
2154 }
2155 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2156
2157 if (tests[testIndex].fIsRect) {
2158 SkRect computed, expected;
2159 bool isClosed;
Mike Reed4241f5e2019-09-14 19:13:23 +00002160 SkPath::Direction direction;
Yuqian Li3154a532017-09-06 13:33:30 -04002161 SkPathPriv::FirstDirection cheapDirection;
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002162 int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
Mike Reed92b33352019-08-24 19:39:13 -04002163 expected.setBounds(tests[testIndex].fPoints, pointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002164 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
2165 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2166 REPORTER_ASSERT(reporter, expected == computed);
2167 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2168 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2169 } else {
2170 SkRect computed;
Mike Reed92b33352019-08-24 19:39:13 -04002171 computed.setLTRB(123, 456, 789, 1011);
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002172 for (auto c : {true, false})
Mike Reed4241f5e2019-09-14 19:13:23 +00002173 for (auto d : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) {
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002174 bool isClosed = c;
Mike Reed4241f5e2019-09-14 19:13:23 +00002175 SkPath::Direction direction = d;
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002176 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2177 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2178 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2179 REPORTER_ASSERT(reporter, isClosed == c);
2180 REPORTER_ASSERT(reporter, direction == d);
2181 }
Yuqian Li3154a532017-09-06 13:33:30 -04002182 }
2183 }
2184
2185 // fail, close then line
2186 SkPath path1;
2187 path1.moveTo(r1[0].fX, r1[0].fY);
2188 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2189 path1.lineTo(r1[index].fX, r1[index].fY);
2190 }
2191 path1.close();
2192 path1.lineTo(1, 0);
2193 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2194
2195 // fail, move in the middle
2196 path1.reset();
2197 path1.moveTo(r1[0].fX, r1[0].fY);
2198 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2199 if (index == 2) {
2200 path1.moveTo(1, .5f);
2201 }
2202 path1.lineTo(r1[index].fX, r1[index].fY);
2203 }
2204 path1.close();
2205 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2206
2207 // fail, move on the edge
2208 path1.reset();
2209 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2210 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2211 path1.lineTo(r1[index].fX, r1[index].fY);
2212 }
2213 path1.close();
2214 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2215
2216 // fail, quad
2217 path1.reset();
2218 path1.moveTo(r1[0].fX, r1[0].fY);
2219 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2220 if (index == 2) {
2221 path1.quadTo(1, .5f, 1, .5f);
2222 }
2223 path1.lineTo(r1[index].fX, r1[index].fY);
2224 }
2225 path1.close();
2226 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2227
2228 // fail, cubic
2229 path1.reset();
2230 path1.moveTo(r1[0].fX, r1[0].fY);
2231 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2232 if (index == 2) {
2233 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2234 }
2235 path1.lineTo(r1[index].fX, r1[index].fY);
2236 }
2237 path1.close();
2238 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2239}
2240
2241static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
Mike Reed4241f5e2019-09-14 19:13:23 +00002242 const SkRect& rect, SkPath::Direction dir, unsigned start) {
Yuqian Li3154a532017-09-06 13:33:30 -04002243 SkRect r = SkRect::MakeEmpty();
Mike Reed4241f5e2019-09-14 19:13:23 +00002244 SkPath::Direction d = SkPath::kCCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04002245 unsigned s = ~0U;
2246
2247 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2248 REPORTER_ASSERT(reporter, r == rect);
2249 REPORTER_ASSERT(reporter, d == dir);
2250 REPORTER_ASSERT(reporter, s == start);
2251}
2252
2253static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002254 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002255 SkRect r = SkRect::MakeEmpty();
Mike Reed4241f5e2019-09-14 19:13:23 +00002256 SkPath::Direction d = SkPath::kCCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04002257 unsigned s = ~0U;
2258
2259 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2260 const SkRect emptyRect = SkRect::MakeEmpty();
2261 SkPath path;
2262 for (int start = 0; start < 4; ++start) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002263 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
Yuqian Li3154a532017-09-06 13:33:30 -04002264 SkPath path;
2265 path.addRect(testRect, dir, start);
2266 check_simple_closed_rect(reporter, path, testRect, dir, start);
2267 path.close();
2268 check_simple_closed_rect(reporter, path, testRect, dir, start);
2269 SkPath path2 = path;
2270 path2.lineTo(10, 10);
2271 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2272 path2 = path;
2273 path2.moveTo(10, 10);
2274 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2275 path2 = path;
2276 path2.addRect(testRect, dir, start);
2277 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2278 // Make the path by hand, manually closing it.
2279 path2.reset();
2280 SkPath::RawIter iter(path);
2281 SkPath::Verb v;
2282 SkPoint verbPts[4];
2283 SkPoint firstPt = {0.f, 0.f};
2284 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2285 switch(v) {
2286 case SkPath::kMove_Verb:
2287 firstPt = verbPts[0];
2288 path2.moveTo(verbPts[0]);
2289 break;
2290 case SkPath::kLine_Verb:
2291 path2.lineTo(verbPts[1]);
2292 break;
2293 default:
2294 break;
2295 }
2296 }
2297 // We haven't closed it yet...
2298 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2299 // ... now we do and test again.
2300 path2.lineTo(firstPt);
2301 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2302 // A redundant close shouldn't cause a failure.
2303 path2.close();
2304 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2305 // Degenerate point and line rects are not allowed
2306 path2.reset();
2307 path2.addRect(emptyRect, dir, start);
2308 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2309 SkRect degenRect = testRect;
2310 degenRect.fLeft = degenRect.fRight;
2311 path2.reset();
2312 path2.addRect(degenRect, dir, start);
2313 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2314 degenRect = testRect;
2315 degenRect.fTop = degenRect.fBottom;
2316 path2.reset();
2317 path2.addRect(degenRect, dir, start);
2318 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2319 // An inverted rect makes a rect path, but changes the winding dir and start point.
Mike Reed4241f5e2019-09-14 19:13:23 +00002320 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2321 ? SkPath::kCCW_Direction
2322 : SkPath::kCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04002323 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2324 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2325 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002326 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002327 path2.reset();
2328 path2.addRect(swapRect, dir, start);
2329 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2330 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002331 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002332 path2.reset();
2333 path2.addRect(swapRect, dir, start);
2334 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2335 }
2336 }
2337 // down, up, left, close
2338 path.reset();
2339 path.moveTo(1, 1);
2340 path.lineTo(1, 2);
2341 path.lineTo(1, 1);
2342 path.lineTo(0, 1);
2343 SkRect rect;
Mike Reed4241f5e2019-09-14 19:13:23 +00002344 SkPath::Direction dir;
Yuqian Li3154a532017-09-06 13:33:30 -04002345 unsigned start;
2346 path.close();
2347 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2348 // right, left, up, close
2349 path.reset();
2350 path.moveTo(1, 1);
2351 path.lineTo(2, 1);
2352 path.lineTo(1, 1);
2353 path.lineTo(1, 0);
2354 path.close();
2355 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2356 // parallelogram with horizontal edges
2357 path.reset();
2358 path.moveTo(1, 0);
2359 path.lineTo(3, 0);
2360 path.lineTo(2, 1);
2361 path.lineTo(0, 1);
2362 path.close();
2363 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2364 // parallelogram with vertical edges
2365 path.reset();
2366 path.moveTo(0, 1);
2367 path.lineTo(0, 3);
2368 path.lineTo(1, 2);
2369 path.lineTo(1, 0);
2370 path.close();
2371 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2372
2373}
2374
2375static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2376 // passing tests (all moveTo / lineTo...
2377 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2378 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2379 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2380 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2381 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2382 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2383 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2384 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2385 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2386 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2387 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2388 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2389 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2390 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2391
2392 // failing tests
2393 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2394 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2395 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2396 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2397 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2398 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2399 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2400 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2401
2402 // success, no close is OK
2403 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2404 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2405
2406 struct IsNestedRectTest {
2407 SkPoint *fPoints;
2408 int fPointCount;
2409 SkPathPriv::FirstDirection fDirection;
2410 bool fClose;
2411 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2412 } tests[] = {
2413 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2414 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2415 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2416 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2417 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2418 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2419 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2420 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2421 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2422 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2423 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2424 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2425 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2426 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
2427
2428 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2429 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2430 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2431 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2432 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2433 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2434 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2435 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2436
2437 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2438 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2439 };
2440
2441 const size_t testCount = SK_ARRAY_COUNT(tests);
2442 int index;
2443 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2444 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2445 SkPath path;
2446 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002447 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002448 }
2449 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2450 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2451 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2452 }
2453 if (tests[testIndex].fClose) {
2454 path.close();
2455 }
2456 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002457 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002458 }
2459 REPORTER_ASSERT(reporter,
Mike Reed430470d2019-09-12 17:09:12 -04002460 tests[testIndex].fIsNestedRect == SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002461 if (tests[testIndex].fIsNestedRect) {
2462 SkRect expected[2], computed[2];
2463 SkPathPriv::FirstDirection expectedDirs[2];
Mike Reed4241f5e2019-09-14 19:13:23 +00002464 SkPath::Direction computedDirs[2];
Yuqian Li3154a532017-09-06 13:33:30 -04002465 SkRect testBounds;
Mike Reed92b33352019-08-24 19:39:13 -04002466 testBounds.setBounds(tests[testIndex].fPoints, tests[testIndex].fPointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002467 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2468 expected[1] = testBounds;
2469 if (rectFirst) {
2470 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2471 } else {
2472 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2473 }
2474 expectedDirs[1] = tests[testIndex].fDirection;
Mike Reed430470d2019-09-12 17:09:12 -04002475 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, computed, computedDirs));
Yuqian Li3154a532017-09-06 13:33:30 -04002476 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2477 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2478 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2479 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2480 }
2481 }
2482
2483 // fail, close then line
2484 SkPath path1;
2485 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002486 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002487 }
2488 path1.moveTo(r1[0].fX, r1[0].fY);
2489 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2490 path1.lineTo(r1[index].fX, r1[index].fY);
2491 }
2492 path1.close();
2493 path1.lineTo(1, 0);
2494 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002495 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002496 }
Mike Reed430470d2019-09-12 17:09:12 -04002497 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002498
2499 // fail, move in the middle
2500 path1.reset();
2501 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002502 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002503 }
2504 path1.moveTo(r1[0].fX, r1[0].fY);
2505 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2506 if (index == 2) {
2507 path1.moveTo(1, .5f);
2508 }
2509 path1.lineTo(r1[index].fX, r1[index].fY);
2510 }
2511 path1.close();
2512 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002513 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002514 }
Mike Reed430470d2019-09-12 17:09:12 -04002515 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002516
2517 // fail, move on the edge
2518 path1.reset();
2519 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002520 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002521 }
2522 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2523 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2524 path1.lineTo(r1[index].fX, r1[index].fY);
2525 }
2526 path1.close();
2527 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002528 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002529 }
Mike Reed430470d2019-09-12 17:09:12 -04002530 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002531
2532 // fail, quad
2533 path1.reset();
2534 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002535 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002536 }
2537 path1.moveTo(r1[0].fX, r1[0].fY);
2538 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2539 if (index == 2) {
2540 path1.quadTo(1, .5f, 1, .5f);
2541 }
2542 path1.lineTo(r1[index].fX, r1[index].fY);
2543 }
2544 path1.close();
2545 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002546 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002547 }
Mike Reed430470d2019-09-12 17:09:12 -04002548 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002549
2550 // fail, cubic
2551 path1.reset();
2552 if (rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002553 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002554 }
2555 path1.moveTo(r1[0].fX, r1[0].fY);
2556 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2557 if (index == 2) {
2558 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2559 }
2560 path1.lineTo(r1[index].fX, r1[index].fY);
2561 }
2562 path1.close();
2563 if (!rectFirst) {
Mike Reed4241f5e2019-09-14 19:13:23 +00002564 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002565 }
Mike Reed430470d2019-09-12 17:09:12 -04002566 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002567
2568 // fail, not nested
2569 path1.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00002570 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2571 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
Mike Reed430470d2019-09-12 17:09:12 -04002572 REPORTER_ASSERT(reporter, !SkPathPriv::IsNestedFillRects(path1, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002573 }
2574
2575 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2576 SkPath path;
2577 path.moveTo(0, 0);
2578 path.lineTo(10, 0);
2579 path.lineTo(10, 10);
2580 path.lineTo(0, 10);
2581 path.lineTo(0, 0);
2582 path.moveTo(1, 1);
2583 path.lineTo(9, 1);
2584 path.lineTo(9, 9);
2585 path.lineTo(1, 9);
2586 path.lineTo(1, 1);
Mike Reed430470d2019-09-12 17:09:12 -04002587 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(path, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002588
2589 // pass, stroke rect
2590 SkPath src, dst;
Mike Reed4241f5e2019-09-14 19:13:23 +00002591 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002592 SkPaint strokePaint;
2593 strokePaint.setStyle(SkPaint::kStroke_Style);
2594 strokePaint.setStrokeWidth(2);
2595 strokePaint.getFillPath(src, &dst);
Mike Reed430470d2019-09-12 17:09:12 -04002596 REPORTER_ASSERT(reporter, SkPathPriv::IsNestedFillRects(dst, nullptr));
Yuqian Li3154a532017-09-06 13:33:30 -04002597}
2598
2599static void write_and_read_back(skiatest::Reporter* reporter,
2600 const SkPath& p) {
2601 SkWriter32 writer;
2602 writer.writePath(p);
2603 size_t size = writer.bytesWritten();
2604 SkAutoMalloc storage(size);
2605 writer.flatten(storage.get());
2606 SkReader32 reader(storage.get(), size);
2607
2608 SkPath readBack;
2609 REPORTER_ASSERT(reporter, readBack != p);
2610 reader.readPath(&readBack);
2611 REPORTER_ASSERT(reporter, readBack == p);
2612
2613 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
2614 p.getConvexityOrUnknown());
2615
2616 SkRect oval0, oval1;
Mike Reed4241f5e2019-09-14 19:13:23 +00002617 SkPath::Direction dir0, dir1;
Yuqian Li3154a532017-09-06 13:33:30 -04002618 unsigned start0, start1;
2619 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002620 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2621 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002622 REPORTER_ASSERT(reporter, oval0 == oval1);
2623 REPORTER_ASSERT(reporter, dir0 == dir1);
2624 REPORTER_ASSERT(reporter, start0 == start1);
2625 }
2626 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2627 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002628 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2629 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002630 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2631 REPORTER_ASSERT(reporter, dir0 == dir1);
2632 REPORTER_ASSERT(reporter, start0 == start1);
2633 }
2634 const SkRect& origBounds = p.getBounds();
2635 const SkRect& readBackBounds = readBack.getBounds();
2636
2637 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2638}
2639
Yuqian Li3154a532017-09-06 13:33:30 -04002640static void test_flattening(skiatest::Reporter* reporter) {
2641 SkPath p;
2642
2643 static const SkPoint pts[] = {
2644 { 0, 0 },
2645 { SkIntToScalar(10), SkIntToScalar(10) },
2646 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2647 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2648 };
2649 p.moveTo(pts[0]);
2650 p.lineTo(pts[1]);
2651 p.quadTo(pts[2], pts[3]);
2652 p.cubicTo(pts[4], pts[5], pts[6]);
2653
2654 write_and_read_back(reporter, p);
2655
2656 // create a buffer that should be much larger than the path so we don't
2657 // kill our stack if writer goes too far.
2658 char buffer[1024];
2659 size_t size1 = p.writeToMemory(nullptr);
2660 size_t size2 = p.writeToMemory(buffer);
2661 REPORTER_ASSERT(reporter, size1 == size2);
2662
2663 SkPath p2;
2664 size_t size3 = p2.readFromMemory(buffer, 1024);
2665 REPORTER_ASSERT(reporter, size1 == size3);
2666 REPORTER_ASSERT(reporter, p == p2);
2667
2668 size3 = p2.readFromMemory(buffer, 0);
2669 REPORTER_ASSERT(reporter, !size3);
2670
2671 SkPath tooShort;
2672 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2673 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2674
2675 char buffer2[1024];
2676 size3 = p2.writeToMemory(buffer2);
2677 REPORTER_ASSERT(reporter, size1 == size3);
2678 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2679
2680 // test persistence of the oval flag & convexity
2681 {
2682 SkPath oval;
2683 SkRect rect = SkRect::MakeWH(10, 10);
2684 oval.addOval(rect);
2685
2686 write_and_read_back(reporter, oval);
2687 }
Yuqian Li3154a532017-09-06 13:33:30 -04002688}
2689
2690static void test_transform(skiatest::Reporter* reporter) {
2691 SkPath p;
2692
2693#define CONIC_PERSPECTIVE_BUG_FIXED 0
2694 static const SkPoint pts[] = {
2695 { 0, 0 }, // move
2696 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2697 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2698 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2699#if CONIC_PERSPECTIVE_BUG_FIXED
2700 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2701#endif
2702 };
2703 const int kPtCount = SK_ARRAY_COUNT(pts);
2704
2705 p.moveTo(pts[0]);
2706 p.lineTo(pts[1]);
2707 p.quadTo(pts[2], pts[3]);
2708 p.cubicTo(pts[4], pts[5], pts[6]);
2709#if CONIC_PERSPECTIVE_BUG_FIXED
2710 p.conicTo(pts[4], pts[5], 0.5f);
2711#endif
2712 p.close();
2713
2714 {
2715 SkMatrix matrix;
2716 matrix.reset();
2717 SkPath p1;
2718 p.transform(matrix, &p1);
2719 REPORTER_ASSERT(reporter, p == p1);
2720 }
2721
2722
2723 {
2724 SkMatrix matrix;
2725 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2726
2727 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2728
2729 p.transform(matrix, &p1);
2730 SkPoint pts1[kPtCount];
2731 int count = p1.getPoints(pts1, kPtCount);
2732 REPORTER_ASSERT(reporter, kPtCount == count);
2733 for (int i = 0; i < count; ++i) {
2734 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2735 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2736 }
2737 }
2738
2739 {
2740 SkMatrix matrix;
2741 matrix.reset();
2742 matrix.setPerspX(4);
2743
2744 SkPath p1;
2745 p1.moveTo(SkPoint::Make(0, 0));
2746
2747 p.transform(matrix, &p1);
2748 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2749 p1.transform(matrix, nullptr);
2750 SkRect pBounds = p.getBounds();
2751 SkRect p1Bounds = p1.getBounds();
2752 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2753 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2754 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2755 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2756 }
2757
2758 p.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00002759 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04002760
2761 {
2762 SkMatrix matrix;
2763 matrix.reset();
2764 SkPath p1;
2765 p1.moveTo(SkPoint::Make(0, 0));
2766
2767 p.transform(matrix, &p1);
2768 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2769 }
2770
2771
2772 {
2773 SkMatrix matrix;
2774 matrix.reset();
2775 matrix.setScaleX(-1);
2776 SkPath p1;
2777 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2778
2779 p.transform(matrix, &p1);
2780 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2781 }
2782
2783 {
2784 SkMatrix matrix;
2785 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2786 SkPath p1;
2787 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2788
2789 p.transform(matrix, &p1);
2790 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2791 }
Brian Osmandbfcd922019-03-07 16:50:30 -05002792
2793 {
2794 SkPath p1;
2795 p1.addRect({ 10, 20, 30, 40 });
2796 SkPath p2;
2797 p2.addRect({ 10, 20, 30, 40 });
2798 uint32_t id1 = p1.getGenerationID();
2799 uint32_t id2 = p2.getGenerationID();
Mike Reed8fda88e2019-09-05 14:14:38 -04002800 REPORTER_ASSERT(reporter, id1 != id2);
Brian Osmandbfcd922019-03-07 16:50:30 -05002801 SkMatrix matrix;
2802 matrix.setScale(2, 2);
2803 p1.transform(matrix, &p2);
Mike Reed8fda88e2019-09-05 14:14:38 -04002804 REPORTER_ASSERT(reporter, id1 == p1.getGenerationID());
2805 REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002806 p1.transform(matrix);
2807 REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
Brian Osmandbfcd922019-03-07 16:50:30 -05002808 }
Yuqian Li3154a532017-09-06 13:33:30 -04002809}
2810
2811static void test_zero_length_paths(skiatest::Reporter* reporter) {
2812 SkPath p;
2813 uint8_t verbs[32];
2814
2815 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2816 const char* testPath;
2817 const size_t numResultPts;
2818 const SkRect resultBound;
2819 const SkPath::Verb* resultVerbs;
2820 const size_t numResultVerbs;
2821 };
2822
2823 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2824 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2825 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2826 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2827 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2828 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2829 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2830 static const SkPath::Verb resultVerbs8[] = {
2831 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2832 };
2833 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2834 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2835 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2836 static const SkPath::Verb resultVerbs12[] = {
2837 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2838 };
2839 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2840 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2841 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2842 static const SkPath::Verb resultVerbs16[] = {
2843 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2844 };
2845 static const struct zeroPathTestData gZeroLengthTests[] = {
2846 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2847 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2848 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2849 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2850 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2851 { "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) },
2852 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2853 { "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) },
2854 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2855 { "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) },
2856 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2857 { "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) },
2858 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2859 { "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,
2860 SK_ARRAY_COUNT(resultVerbs14)
2861 },
2862 { "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) },
2863 { "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,
2864 SK_ARRAY_COUNT(resultVerbs16)
2865 }
2866 };
2867
2868 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2869 p.reset();
2870 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2871 REPORTER_ASSERT(reporter, valid);
2872 REPORTER_ASSERT(reporter, !p.isEmpty());
2873 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2874 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2875 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2876 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2877 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2878 }
2879 }
2880}
2881
2882struct SegmentInfo {
2883 SkPath fPath;
2884 int fPointCount;
2885};
2886
2887#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2888
2889static void test_segment_masks(skiatest::Reporter* reporter) {
2890 SkPath p, p2;
2891
2892 p.moveTo(0, 0);
2893 p.quadTo(100, 100, 200, 200);
2894 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2895 REPORTER_ASSERT(reporter, !p.isEmpty());
2896 p2 = p;
2897 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2898 p.cubicTo(100, 100, 200, 200, 300, 300);
2899 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2900 REPORTER_ASSERT(reporter, !p.isEmpty());
2901 p2 = p;
2902 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2903
2904 p.reset();
2905 p.moveTo(0, 0);
2906 p.cubicTo(100, 100, 200, 200, 300, 300);
2907 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2908 p2 = p;
2909 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2910
2911 REPORTER_ASSERT(reporter, !p.isEmpty());
2912}
2913
2914static void test_iter(skiatest::Reporter* reporter) {
2915 SkPath p;
2916 SkPoint pts[4];
2917
2918 // Test an iterator with no path
2919 SkPath::Iter noPathIter;
2920 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2921
2922 // Test that setting an empty path works
2923 noPathIter.setPath(p, false);
2924 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2925
2926 // Test that close path makes no difference for an empty path
2927 noPathIter.setPath(p, true);
2928 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2929
2930 // Test an iterator with an initial empty path
2931 SkPath::Iter iter(p, false);
2932 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2933
2934 // Test that close path makes no difference
2935 iter.setPath(p, true);
2936 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2937
2938
2939 struct iterTestData {
2940 const char* testPath;
2941 const bool forceClose;
Yuqian Li3154a532017-09-06 13:33:30 -04002942 const size_t* numResultPtsPerVerb;
2943 const SkPoint* resultPts;
2944 const SkPath::Verb* resultVerbs;
2945 const size_t numResultVerbs;
2946 };
2947
2948 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2949 static const SkPath::Verb resultVerbs2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002950 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2951 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002952 static const SkPath::Verb resultVerbs3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002953 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2954 };
2955 static const size_t resultPtsSizes1[] = { 0 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002956 static const size_t resultPtsSizes2[] = { 1, 2, 1, 1, 0 };
2957 static const size_t resultPtsSizes3[] = { 1, 2, 1, 1, 1, 0 };
Yuqian Li3154a532017-09-06 13:33:30 -04002958 static const SkPoint* resultPts1 = nullptr;
2959 static const SkPoint resultPts2[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002960 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2961 };
Mike Reedba7e9a62019-08-16 13:30:34 -04002962 static const SkPoint resultPts3[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04002963 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2964 };
2965 static const struct iterTestData gIterTests[] = {
Mike Reedba7e9a62019-08-16 13:30:34 -04002966 { "M 1 0", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2967 { "z", false, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2968 { "z", true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2969 { "M 1 0 L 1 0 M 0 0 z", false, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2970 { "M 1 0 L 1 0 M 0 0 z", true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }
Yuqian Li3154a532017-09-06 13:33:30 -04002971 };
2972
2973 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2974 p.reset();
2975 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2976 REPORTER_ASSERT(reporter, valid);
2977 iter.setPath(p, gIterTests[i].forceClose);
2978 int j = 0, l = 0;
2979 do {
Mike Reedba7e9a62019-08-16 13:30:34 -04002980 REPORTER_ASSERT(reporter, iter.next(pts) == gIterTests[i].resultVerbs[j]);
Yuqian Li3154a532017-09-06 13:33:30 -04002981 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2982 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2983 }
2984 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2985 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2986 }
2987
2988 p.reset();
2989 iter.setPath(p, false);
2990 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2991 p.lineTo(1, 1);
2992 p.close();
2993 iter.setPath(p, false);
2994 REPORTER_ASSERT(reporter, iter.isClosedContour());
2995 p.reset();
2996 iter.setPath(p, true);
2997 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2998 p.lineTo(1, 1);
2999 iter.setPath(p, true);
3000 REPORTER_ASSERT(reporter, iter.isClosedContour());
3001 p.moveTo(0, 0);
3002 p.lineTo(2, 2);
3003 iter.setPath(p, false);
3004 REPORTER_ASSERT(reporter, !iter.isClosedContour());
3005
3006 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
3007 // check to see if the result is correct.
3008 for (int setNaN = 0; setNaN < 4; ++setNaN) {
3009 p.reset();
3010 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
3011 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
3012 iter.setPath(p, true);
Mike Reedba7e9a62019-08-16 13:30:34 -04003013 iter.next(pts);
3014 iter.next(pts);
3015 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003016 }
3017
3018 p.reset();
3019 p.quadTo(0, 0, 0, 0);
3020 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003021 iter.next(pts);
3022 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003023
3024 p.reset();
3025 p.conicTo(0, 0, 0, 0, 0.5f);
3026 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003027 iter.next(pts);
3028 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003029
3030 p.reset();
3031 p.cubicTo(0, 0, 0, 0, 0, 0);
3032 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003033 iter.next(pts);
3034 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003035
3036 p.moveTo(1, 1); // add a trailing moveto
3037 iter.setPath(p, false);
Mike Reedba7e9a62019-08-16 13:30:34 -04003038 iter.next(pts);
3039 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts));
Yuqian Li3154a532017-09-06 13:33:30 -04003040
3041 // The GM degeneratesegments.cpp test is more extensive
3042
3043 // Test out mixed degenerate and non-degenerate geometry with Conics
3044 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3045 SkRect r = SkRect::MakeWH(100, 100);
3046 SkRRect rr;
3047 rr.setRectRadii(r, radii);
3048 p.reset();
3049 p.addRRect(rr);
3050 iter.setPath(p, false);
3051 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3052 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
Mike Reedba7e9a62019-08-16 13:30:34 -04003053 return;
Yuqian Li3154a532017-09-06 13:33:30 -04003054 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3055 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3056 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
3057}
3058
3059static void test_raw_iter(skiatest::Reporter* reporter) {
3060 SkPath p;
3061 SkPoint pts[4];
3062
3063 // Test an iterator with no path
3064 SkPath::RawIter noPathIter;
3065 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3066 // Test that setting an empty path works
3067 noPathIter.setPath(p);
3068 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3069
3070 // Test an iterator with an initial empty path
3071 SkPath::RawIter iter(p);
3072 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3073
3074 // Test that a move-only path returns the move.
3075 p.moveTo(SK_Scalar1, 0);
3076 iter.setPath(p);
3077 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3078 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3079 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3080 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3081
3082 // No matter how many moves we add, we should get them all back
3083 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3084 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
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::kMove_Verb);
3090 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3091 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3092 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3093 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3094 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3095 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3096
3097 // Initial close is never ever stored
3098 p.reset();
3099 p.close();
3100 iter.setPath(p);
3101 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3102
3103 // Move/close sequences
3104 p.reset();
3105 p.close(); // Not stored, no purpose
3106 p.moveTo(SK_Scalar1, 0);
3107 p.close();
3108 p.close(); // Not stored, no purpose
3109 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3110 p.close();
3111 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3112 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3113 p.close();
3114 iter.setPath(p);
3115 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3116 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3117 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3118 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3119 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3120 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3121 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3122 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3123 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3124 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3125 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3126 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3127 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3128 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3129 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3130 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3131
3132 // Generate random paths and verify
3133 SkPoint randomPts[25];
3134 for (int i = 0; i < 5; ++i) {
3135 for (int j = 0; j < 5; ++j) {
3136 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3137 }
3138 }
3139
3140 // Max of 10 segments, max 3 points per segment
3141 SkRandom rand(9876543);
3142 SkPoint expectedPts[31]; // May have leading moveTo
3143 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
3144 SkPath::Verb nextVerb;
3145
3146 for (int i = 0; i < 500; ++i) {
3147 p.reset();
3148 bool lastWasClose = true;
3149 bool haveMoveTo = false;
3150 SkPoint lastMoveToPt = { 0, 0 };
3151 int numPoints = 0;
3152 int numVerbs = (rand.nextU() >> 16) % 10;
3153 int numIterVerbs = 0;
3154 for (int j = 0; j < numVerbs; ++j) {
3155 do {
3156 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3157 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
3158 switch (nextVerb) {
3159 case SkPath::kMove_Verb:
3160 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3161 p.moveTo(expectedPts[numPoints]);
3162 lastMoveToPt = expectedPts[numPoints];
3163 numPoints += 1;
3164 lastWasClose = false;
3165 haveMoveTo = true;
3166 break;
3167 case SkPath::kLine_Verb:
3168 if (!haveMoveTo) {
3169 expectedPts[numPoints++] = lastMoveToPt;
3170 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3171 haveMoveTo = true;
3172 }
3173 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3174 p.lineTo(expectedPts[numPoints]);
3175 numPoints += 1;
3176 lastWasClose = false;
3177 break;
3178 case SkPath::kQuad_Verb:
3179 if (!haveMoveTo) {
3180 expectedPts[numPoints++] = lastMoveToPt;
3181 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3182 haveMoveTo = true;
3183 }
3184 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3185 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3186 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3187 numPoints += 2;
3188 lastWasClose = false;
3189 break;
3190 case SkPath::kConic_Verb:
3191 if (!haveMoveTo) {
3192 expectedPts[numPoints++] = lastMoveToPt;
3193 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3194 haveMoveTo = true;
3195 }
3196 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3197 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3198 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3199 rand.nextUScalar1() * 4);
3200 numPoints += 2;
3201 lastWasClose = false;
3202 break;
3203 case SkPath::kCubic_Verb:
3204 if (!haveMoveTo) {
3205 expectedPts[numPoints++] = lastMoveToPt;
3206 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3207 haveMoveTo = true;
3208 }
3209 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3210 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3211 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3212 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3213 expectedPts[numPoints + 2]);
3214 numPoints += 3;
3215 lastWasClose = false;
3216 break;
3217 case SkPath::kClose_Verb:
3218 p.close();
3219 haveMoveTo = false;
3220 lastWasClose = true;
3221 break;
3222 default:
3223 SkDEBUGFAIL("unexpected verb");
3224 }
3225 expectedVerbs[numIterVerbs++] = nextVerb;
3226 }
3227
3228 iter.setPath(p);
3229 numVerbs = numIterVerbs;
3230 numIterVerbs = 0;
3231 int numIterPts = 0;
3232 SkPoint lastMoveTo;
3233 SkPoint lastPt;
3234 lastMoveTo.set(0, 0);
3235 lastPt.set(0, 0);
3236 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3237 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3238 numIterVerbs++;
3239 switch (nextVerb) {
3240 case SkPath::kMove_Verb:
3241 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3242 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3243 lastPt = lastMoveTo = pts[0];
3244 numIterPts += 1;
3245 break;
3246 case SkPath::kLine_Verb:
3247 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3248 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3249 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3250 lastPt = pts[1];
3251 numIterPts += 1;
3252 break;
3253 case SkPath::kQuad_Verb:
3254 case SkPath::kConic_Verb:
3255 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3256 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3257 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3258 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3259 lastPt = pts[2];
3260 numIterPts += 2;
3261 break;
3262 case SkPath::kCubic_Verb:
3263 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3264 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3265 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3266 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3267 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3268 lastPt = pts[3];
3269 numIterPts += 3;
3270 break;
3271 case SkPath::kClose_Verb:
3272 lastPt = lastMoveTo;
3273 break;
3274 default:
3275 SkDEBUGFAIL("unexpected verb");
3276 }
3277 }
3278 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3279 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3280 }
3281}
3282
3283static void check_for_circle(skiatest::Reporter* reporter,
3284 const SkPath& path,
3285 bool expectedCircle,
3286 SkPathPriv::FirstDirection expectedDir) {
3287 SkRect rect = SkRect::MakeEmpty();
3288 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
Mike Reed4241f5e2019-09-14 19:13:23 +00003289 SkPath::Direction isOvalDir;
Yuqian Li3154a532017-09-06 13:33:30 -04003290 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003291 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003292 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3293 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3294 SkPath tmpPath;
3295 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3296 REPORTER_ASSERT(reporter, path == tmpPath);
3297 }
3298 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3299}
3300
3301static void test_circle_skew(skiatest::Reporter* reporter,
3302 const SkPath& path,
3303 SkPathPriv::FirstDirection dir) {
3304 SkPath tmp;
3305
3306 SkMatrix m;
3307 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3308 path.transform(m, &tmp);
3309 // this matrix reverses the direction.
3310 if (SkPathPriv::kCCW_FirstDirection == dir) {
3311 dir = SkPathPriv::kCW_FirstDirection;
3312 } else {
3313 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3314 dir = SkPathPriv::kCCW_FirstDirection;
3315 }
3316 check_for_circle(reporter, tmp, false, dir);
3317}
3318
3319static void test_circle_translate(skiatest::Reporter* reporter,
3320 const SkPath& path,
3321 SkPathPriv::FirstDirection dir) {
3322 SkPath tmp;
3323
3324 // translate at small offset
3325 SkMatrix m;
3326 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3327 path.transform(m, &tmp);
3328 check_for_circle(reporter, tmp, true, dir);
3329
3330 tmp.reset();
3331 m.reset();
3332
3333 // translate at a relatively big offset
3334 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3335 path.transform(m, &tmp);
3336 check_for_circle(reporter, tmp, true, dir);
3337}
3338
3339static void test_circle_rotate(skiatest::Reporter* reporter,
3340 const SkPath& path,
3341 SkPathPriv::FirstDirection dir) {
3342 for (int angle = 0; angle < 360; ++angle) {
3343 SkPath tmp;
3344 SkMatrix m;
3345 m.setRotate(SkIntToScalar(angle));
3346 path.transform(m, &tmp);
3347
3348 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3349 // degrees is not an oval anymore, this can be improved. we made this
3350 // for the simplicity of our implementation.
3351 if (angle % 90 == 0) {
3352 check_for_circle(reporter, tmp, true, dir);
3353 } else {
3354 check_for_circle(reporter, tmp, false, dir);
3355 }
3356 }
3357}
3358
3359static void test_circle_mirror_x(skiatest::Reporter* reporter,
3360 const SkPath& path,
3361 SkPathPriv::FirstDirection dir) {
3362 SkPath tmp;
3363 SkMatrix m;
3364 m.reset();
3365 m.setScaleX(-SK_Scalar1);
3366 path.transform(m, &tmp);
3367 if (SkPathPriv::kCW_FirstDirection == dir) {
3368 dir = SkPathPriv::kCCW_FirstDirection;
3369 } else {
3370 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3371 dir = SkPathPriv::kCW_FirstDirection;
3372 }
3373 check_for_circle(reporter, tmp, true, dir);
3374}
3375
3376static void test_circle_mirror_y(skiatest::Reporter* reporter,
3377 const SkPath& path,
3378 SkPathPriv::FirstDirection dir) {
3379 SkPath tmp;
3380 SkMatrix m;
3381 m.reset();
3382 m.setScaleY(-SK_Scalar1);
3383 path.transform(m, &tmp);
3384
3385 if (SkPathPriv::kCW_FirstDirection == dir) {
3386 dir = SkPathPriv::kCCW_FirstDirection;
3387 } else {
3388 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3389 dir = SkPathPriv::kCW_FirstDirection;
3390 }
3391
3392 check_for_circle(reporter, tmp, true, dir);
3393}
3394
3395static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3396 const SkPath& path,
3397 SkPathPriv::FirstDirection dir) {
3398 SkPath tmp;
3399 SkMatrix m;
3400 m.reset();
3401 m.setScaleX(-SK_Scalar1);
3402 m.setScaleY(-SK_Scalar1);
3403 path.transform(m, &tmp);
3404
3405 check_for_circle(reporter, tmp, true, dir);
3406}
3407
3408static void test_circle_with_direction(skiatest::Reporter* reporter,
Mike Reed4241f5e2019-09-14 19:13:23 +00003409 SkPath::Direction inDir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003410 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3411 SkPath path;
3412
3413 // circle at origin
3414 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3415
3416 check_for_circle(reporter, path, true, dir);
3417 test_circle_rotate(reporter, path, dir);
3418 test_circle_translate(reporter, path, dir);
3419 test_circle_skew(reporter, path, dir);
3420 test_circle_mirror_x(reporter, path, dir);
3421 test_circle_mirror_y(reporter, path, dir);
3422 test_circle_mirror_xy(reporter, path, dir);
3423
3424 // circle at an offset at (10, 10)
3425 path.reset();
3426 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3427 SkIntToScalar(20), inDir);
3428
3429 check_for_circle(reporter, path, true, dir);
3430 test_circle_rotate(reporter, path, dir);
3431 test_circle_translate(reporter, path, dir);
3432 test_circle_skew(reporter, path, dir);
3433 test_circle_mirror_x(reporter, path, dir);
3434 test_circle_mirror_y(reporter, path, dir);
3435 test_circle_mirror_xy(reporter, path, dir);
3436
3437 // Try different starting points for the contour.
3438 for (unsigned start = 0; start < 4; ++start) {
3439 path.reset();
3440 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3441 test_circle_rotate(reporter, path, dir);
3442 test_circle_translate(reporter, path, dir);
3443 test_circle_skew(reporter, path, dir);
3444 test_circle_mirror_x(reporter, path, dir);
3445 test_circle_mirror_y(reporter, path, dir);
3446 test_circle_mirror_xy(reporter, path, dir);
3447 }
3448}
3449
3450static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3451 SkPath path;
3452 SkPath circle;
3453 SkPath rect;
3454 SkPath empty;
3455
Mike Reed4241f5e2019-09-14 19:13:23 +00003456 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3457 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04003458
3459 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3460 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3461 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3462
3463 SkMatrix translate;
3464 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3465
3466 // Although all the path concatenation related operations leave
3467 // the path a circle, most mark it as a non-circle for simplicity
3468
3469 // empty + circle (translate)
3470 path = empty;
3471 path.addPath(circle, translate);
3472 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3473
3474 // circle + empty (translate)
3475 path = circle;
3476 path.addPath(empty, translate);
3477
3478 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3479
3480 // test reverseAddPath
3481 path = circle;
3482 path.reverseAddPath(rect);
3483 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3484}
3485
3486static void test_circle(skiatest::Reporter* reporter) {
Mike Reed4241f5e2019-09-14 19:13:23 +00003487 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3488 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003489
3490 // multiple addCircle()
3491 SkPath path;
Mike Reed4241f5e2019-09-14 19:13:23 +00003492 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3493 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003494 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3495
3496 // some extra lineTo() would make isOval() fail
3497 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00003498 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003499 path.lineTo(0, 0);
3500 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3501
3502 // not back to the original point
3503 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00003504 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003505 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3506 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3507
3508 test_circle_with_add_paths(reporter);
3509
3510 // test negative radius
3511 path.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +00003512 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003513 REPORTER_ASSERT(reporter, path.isEmpty());
3514}
3515
3516static void test_oval(skiatest::Reporter* reporter) {
3517 SkRect rect;
3518 SkMatrix m;
3519 SkPath path;
3520 unsigned start = 0;
Mike Reed4241f5e2019-09-14 19:13:23 +00003521 SkPath::Direction dir = SkPath::kCCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04003522
3523 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3524 path.addOval(rect);
3525
3526 // Defaults to dir = CW and start = 1
3527 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3528
3529 m.setRotate(SkIntToScalar(90));
3530 SkPath tmp;
3531 path.transform(m, &tmp);
3532 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3533 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003534 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003535 REPORTER_ASSERT(reporter, 2 == start);
Mike Reed4241f5e2019-09-14 19:13:23 +00003536 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003537
3538 m.reset();
3539 m.setRotate(SkIntToScalar(30));
3540 tmp.reset();
3541 path.transform(m, &tmp);
3542 // an oval rotated 30 degrees is not an oval anymore.
3543 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3544
3545 // since empty path being transformed.
3546 path.reset();
3547 tmp.reset();
3548 m.reset();
3549 path.transform(m, &tmp);
3550 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3551
3552 // empty path is not an oval
3553 tmp.reset();
3554 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3555
3556 // only has moveTo()s
3557 tmp.reset();
3558 tmp.moveTo(0, 0);
3559 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3560 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3561
3562 // mimic WebKit's calling convention,
3563 // call moveTo() first and then call addOval()
3564 path.reset();
3565 path.moveTo(0, 0);
3566 path.addOval(rect);
3567 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3568
3569 // copy path
3570 path.reset();
3571 tmp.reset();
3572 tmp.addOval(rect);
3573 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003574 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Mike Reed4241f5e2019-09-14 19:13:23 +00003575 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
Yuqian Li3154a532017-09-06 13:33:30 -04003576 REPORTER_ASSERT(reporter, 1 == start);
3577}
3578
3579static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3580 SkPath empty;
3581
3582 REPORTER_ASSERT(reporter, p.isEmpty());
3583 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3584 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3585 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3586 REPORTER_ASSERT(reporter, p.isConvex());
3587 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3588 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3589 REPORTER_ASSERT(reporter, p == empty);
3590 REPORTER_ASSERT(reporter, !(p != empty));
3591}
3592
3593static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
Mike Reed4241f5e2019-09-14 19:13:23 +00003594 SkPath::Direction dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003595 REPORTER_ASSERT(reporter, path->isConvex());
3596 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3597 path->setConvexity(SkPath::kUnknown_Convexity);
3598 REPORTER_ASSERT(reporter, path->isConvex());
3599 path->reset();
3600}
3601
3602static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
Mike Reed4241f5e2019-09-14 19:13:23 +00003603 SkPath::Direction dir) {
Yuqian Li3154a532017-09-06 13:33:30 -04003604 REPORTER_ASSERT(reporter, path->isConvex());
3605 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3606 path->setConvexity(SkPath::kUnknown_Convexity);
Brian Osman14ac42b2019-04-18 12:17:04 -04003607 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConcave_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003608 path->reset();
3609}
3610
3611static void test_rrect(skiatest::Reporter* reporter) {
3612 SkPath p;
3613 SkRRect rr;
3614 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3615 SkRect r = {10, 20, 30, 40};
3616 rr.setRectRadii(r, radii);
3617 p.addRRect(rr);
Mike Reed4241f5e2019-09-14 19:13:23 +00003618 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3619 p.addRRect(rr, SkPath::kCCW_Direction);
3620 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003621 p.addRoundRect(r, &radii[0].fX);
Mike Reed4241f5e2019-09-14 19:13:23 +00003622 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003623 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
Mike Reed4241f5e2019-09-14 19:13:23 +00003624 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003625 p.addRoundRect(r, radii[1].fX, radii[1].fY);
Mike Reed4241f5e2019-09-14 19:13:23 +00003626 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003627 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
Mike Reed4241f5e2019-09-14 19:13:23 +00003628 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003629 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3630 SkVector save = radii[i];
3631 radii[i].set(0, 0);
3632 rr.setRectRadii(r, radii);
3633 p.addRRect(rr);
Mike Reed4241f5e2019-09-14 19:13:23 +00003634 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003635 radii[i] = save;
3636 }
3637 p.addRoundRect(r, 0, 0);
3638 SkRect returnedRect;
3639 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3640 REPORTER_ASSERT(reporter, returnedRect == r);
Mike Reed4241f5e2019-09-14 19:13:23 +00003641 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003642 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3643 rr.setRectRadii(r, zeroRadii);
3644 p.addRRect(rr);
3645 bool closed;
Mike Reed4241f5e2019-09-14 19:13:23 +00003646 SkPath::Direction dir;
Yuqian Li3154a532017-09-06 13:33:30 -04003647 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3648 REPORTER_ASSERT(reporter, closed);
Mike Reed4241f5e2019-09-14 19:13:23 +00003649 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3650 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003651 p.addRRect(rr, SkPath::kCW_Direction);
3652 p.addRRect(rr, SkPath::kCW_Direction);
3653 REPORTER_ASSERT(reporter, !p.isConvex());
3654 p.reset();
3655 p.addRRect(rr, SkPath::kCCW_Direction);
3656 p.addRRect(rr, SkPath::kCCW_Direction);
3657 REPORTER_ASSERT(reporter, !p.isConvex());
3658 p.reset();
3659 SkRect emptyR = {10, 20, 10, 30};
3660 rr.setRectRadii(emptyR, radii);
3661 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003662 // The round rect is "empty" in that it has no fill area. However,
3663 // the path isn't "empty" in that it should have verbs and points.
3664 REPORTER_ASSERT(reporter, !p.isEmpty());
3665 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003666 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3667 rr.setRectRadii(largeR, radii);
3668 p.addRRect(rr);
Mike Reed4241f5e2019-09-14 19:13:23 +00003669 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003670
3671 // we check for non-finites
3672 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3673 rr.setRectRadii(infR, radii);
3674 REPORTER_ASSERT(reporter, rr.isEmpty());
3675
Brian Osman4be31e22019-08-20 15:35:26 -04003676 // We consider any path with very small (numerically unstable) edges to be concave.
Yuqian Li3154a532017-09-06 13:33:30 -04003677 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3678 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
Mike Reed4241f5e2019-09-14 19:13:23 +00003679 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
Yuqian Li3154a532017-09-06 13:33:30 -04003680}
3681
3682static void test_arc(skiatest::Reporter* reporter) {
3683 SkPath p;
3684 SkRect emptyOval = {10, 20, 30, 20};
3685 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3686 p.addArc(emptyOval, 1, 2);
3687 REPORTER_ASSERT(reporter, p.isEmpty());
3688 p.reset();
3689 SkRect oval = {10, 20, 30, 40};
3690 p.addArc(oval, 1, 0);
3691 REPORTER_ASSERT(reporter, p.isEmpty());
3692 p.reset();
3693 SkPath cwOval;
3694 cwOval.addOval(oval);
3695 p.addArc(oval, 0, 360);
3696 REPORTER_ASSERT(reporter, p == cwOval);
3697 p.reset();
3698 SkPath ccwOval;
3699 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3700 p.addArc(oval, 0, -360);
3701 REPORTER_ASSERT(reporter, p == ccwOval);
3702 p.reset();
3703 p.addArc(oval, 1, 180);
Cary Clarkc9b7c722018-12-12 14:50:23 -05003704 // diagonal colinear points make arc convex
3705 // TODO: one way to keep it concave would be to introduce interpolated on curve points
3706 // between control points and computing the on curve point at scan conversion time
Brian Osman309de792019-04-19 09:39:56 -04003707 REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003708 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
3709 p.setConvexity(SkPath::kUnknown_Convexity);
Brian Osman309de792019-04-19 09:39:56 -04003710 REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003711}
3712
3713static inline SkScalar oval_start_index_to_angle(unsigned start) {
3714 switch (start) {
3715 case 0:
3716 return 270.f;
3717 case 1:
3718 return 0.f;
3719 case 2:
3720 return 90.f;
3721 case 3:
3722 return 180.f;
3723 default:
3724 return -1.f;
3725 }
3726}
3727
3728static inline SkScalar canonical_start_angle(float angle) {
3729 while (angle < 0.f) {
3730 angle += 360.f;
3731 }
3732 while (angle >= 360.f) {
3733 angle -= 360.f;
3734 }
3735 return angle;
3736}
3737
3738static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3739 const SkPath& path) {
3740 SkRect r = SkRect::MakeEmpty();
Mike Reed4241f5e2019-09-14 19:13:23 +00003741 SkPath::Direction d = SkPath::kCCW_Direction;
Yuqian Li3154a532017-09-06 13:33:30 -04003742 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003743 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003744 REPORTER_ASSERT(reporter, isOval);
3745 SkPath recreatedPath;
3746 recreatedPath.addOval(r, d, s);
3747 REPORTER_ASSERT(reporter, path == recreatedPath);
3748 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
Mike Reed4241f5e2019-09-14 19:13:23 +00003749 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
Yuqian Li3154a532017-09-06 13:33:30 -04003750}
3751
3752static void test_arc_ovals(skiatest::Reporter* reporter) {
3753 SkRect oval = SkRect::MakeWH(10, 20);
3754 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3755 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3756 SkPath path;
3757 path.addArc(oval, start, sweep);
3758 // SkPath's interfaces for inserting and extracting ovals only allow contours
3759 // to start at multiples of 90 degrees.
3760 if (std::fmod(start, 90.f) == 0) {
3761 check_oval_arc(reporter, start, sweep, path);
3762 } else {
3763 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3764 }
3765 }
3766 // Test start angles that are nearly at valid oval start angles.
3767 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3768 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3769 SkPath path;
3770 path.addArc(oval, start + delta, sweep);
3771 check_oval_arc(reporter, start, sweep, path);
3772 }
3773 }
3774 }
3775}
3776
3777static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3778 SkScalar x0, SkScalar y0) {
3779 SkPoint pts[4];
3780 SkPath::Verb v = iter->next(pts);
3781 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3782 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3783 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3784}
3785
3786static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3787 SkScalar x1, SkScalar y1) {
3788 SkPoint pts[4];
3789 SkPath::Verb v = iter->next(pts);
3790 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3791 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3792 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3793}
3794
3795static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3796 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3797 SkPoint pts[4];
3798 SkPath::Verb v = iter->next(pts);
3799 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3800 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3801 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3802 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3803 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3804}
3805
3806static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3807 SkPoint pts[4];
3808 SkPath::Verb v = iter->next(pts);
3809 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3810}
3811
3812static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3813 check_done(reporter, p, iter);
3814 p->reset();
3815}
3816
3817static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3818 SkScalar x0, SkScalar y0) {
3819 SkPath::RawIter iter(*p);
3820 check_move(reporter, &iter, x0, y0);
3821 check_done_and_reset(reporter, p, &iter);
3822}
3823
3824static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3825 SkScalar x1, SkScalar y1) {
3826 SkPath::RawIter iter(*p);
3827 check_move(reporter, &iter, 0, 0);
3828 check_line(reporter, &iter, x1, y1);
3829 check_done_and_reset(reporter, p, &iter);
3830}
3831
3832static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3833 SkScalar x1, SkScalar y1) {
3834 SkPath::RawIter iter(*p);
3835 check_move(reporter, &iter, 0, 0);
3836 check_line(reporter, &iter, x1, y1);
3837 check_done(reporter, p, &iter);
3838}
3839
3840static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3841 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3842 SkPath::RawIter iter(*p);
3843 check_move(reporter, &iter, 0, 0);
3844 check_line(reporter, &iter, x1, y1);
3845 check_line(reporter, &iter, x2, y2);
3846 check_done_and_reset(reporter, p, &iter);
3847}
3848
3849static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3850 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3851 SkPath::RawIter iter(*p);
3852 check_move(reporter, &iter, 0, 0);
3853 check_quad(reporter, &iter, x1, y1, x2, y2);
3854 check_done_and_reset(reporter, p, &iter);
3855}
3856
3857static bool nearly_equal(const SkRect& a, const SkRect& b) {
3858 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3859 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3860 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3861 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3862}
3863
3864static void test_arcTo(skiatest::Reporter* reporter) {
3865 SkPath p;
3866 p.arcTo(0, 0, 1, 2, 1);
3867 check_path_is_line_and_reset(reporter, &p, 0, 0);
3868 p.arcTo(1, 2, 1, 2, 1);
3869 check_path_is_line_and_reset(reporter, &p, 1, 2);
3870 p.arcTo(1, 2, 3, 4, 0);
3871 check_path_is_line_and_reset(reporter, &p, 1, 2);
3872 p.arcTo(1, 2, 0, 0, 1);
3873 check_path_is_line_and_reset(reporter, &p, 1, 2);
3874 p.arcTo(1, 0, 1, 1, 1);
3875 SkPoint pt;
3876 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3877 p.reset();
3878 p.arcTo(1, 0, 1, -1, 1);
3879 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3880 p.reset();
3881 SkRect oval = {1, 2, 3, 4};
3882 p.arcTo(oval, 0, 0, true);
3883 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3884 p.arcTo(oval, 0, 0, false);
3885 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3886 p.arcTo(oval, 360, 0, true);
3887 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3888 p.arcTo(oval, 360, 0, false);
3889 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3890
3891 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3892 p.arcTo(oval, 0, sweep, false);
3893 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3894 sweep += delta;
3895 delta /= 2;
3896 }
3897 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3898 p.arcTo(oval, 0, sweep, false);
3899 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3900 sweep -= delta;
3901 delta /= 2;
3902 }
3903 SkRect noOvalWidth = {1, 2, 0, 3};
3904 p.reset();
3905 p.arcTo(noOvalWidth, 0, 360, false);
3906 REPORTER_ASSERT(reporter, p.isEmpty());
3907
3908 SkRect noOvalHeight = {1, 2, 3, 1};
3909 p.reset();
3910 p.arcTo(noOvalHeight, 0, 360, false);
3911 REPORTER_ASSERT(reporter, p.isEmpty());
3912}
3913
3914static void test_addPath(skiatest::Reporter* reporter) {
3915 SkPath p, q;
3916 p.lineTo(1, 2);
3917 q.moveTo(4, 4);
3918 q.lineTo(7, 8);
3919 q.conicTo(8, 7, 6, 5, 0.5f);
3920 q.quadTo(6, 7, 8, 6);
3921 q.cubicTo(5, 6, 7, 8, 7, 5);
3922 q.close();
3923 p.addPath(q, -4, -4);
3924 SkRect expected = {0, 0, 4, 4};
3925 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3926 p.reset();
3927 p.reverseAddPath(q);
3928 SkRect reverseExpected = {4, 4, 8, 8};
3929 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3930}
3931
3932static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3933 SkPath p, q;
3934 if (explicitMoveTo) {
3935 p.moveTo(1, 1);
3936 }
3937 p.lineTo(1, 2);
3938 if (explicitMoveTo) {
3939 q.moveTo(2, 1);
3940 }
3941 q.lineTo(2, 2);
3942 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3943 uint8_t verbs[4];
3944 int verbcount = p.getVerbs(verbs, 4);
3945 REPORTER_ASSERT(reporter, verbcount == 4);
3946 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3947 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3948 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3949 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3950}
3951
3952static void test_extendClosedPath(skiatest::Reporter* reporter) {
3953 SkPath p, q;
3954 p.moveTo(1, 1);
3955 p.lineTo(1, 2);
3956 p.lineTo(2, 2);
3957 p.close();
3958 q.moveTo(2, 1);
3959 q.lineTo(2, 3);
3960 p.addPath(q, SkPath::kExtend_AddPathMode);
3961 uint8_t verbs[7];
3962 int verbcount = p.getVerbs(verbs, 7);
3963 REPORTER_ASSERT(reporter, verbcount == 7);
3964 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3965 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3966 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3967 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3968 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3969 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3970 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3971
3972 SkPoint pt;
3973 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3974 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3975 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3976}
3977
3978static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3979 SkPath p, q, r;
3980 // case 1: dst is empty
3981 p.moveTo(2, 1);
3982 p.lineTo(2, 3);
3983 q.addPath(p, mode);
3984 REPORTER_ASSERT(reporter, q == p);
3985 // case 2: src is empty
3986 p.addPath(r, mode);
3987 REPORTER_ASSERT(reporter, q == p);
3988 // case 3: src and dst are empty
3989 q.reset();
3990 q.addPath(r, mode);
3991 REPORTER_ASSERT(reporter, q.isEmpty());
3992}
3993
3994static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3995 SkPath p;
3996 p.conicTo(1, 2, 3, 4, -1);
3997 check_path_is_line_and_reset(reporter, &p, 3, 4);
3998 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3999 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
4000 p.conicTo(1, 2, 3, 4, 1);
4001 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
4002}
4003
4004static void test_get_point(skiatest::Reporter* reporter) {
4005 SkPath p;
4006 SkPoint pt = p.getPoint(0);
4007 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
4008 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
4009 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
4010 p.setLastPt(10, 10);
4011 pt = p.getPoint(0);
4012 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
4013 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
4014 p.rMoveTo(10, 10);
4015 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4016}
4017
4018static void test_contains(skiatest::Reporter* reporter) {
4019 SkPath p;
4020 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4021 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
4022 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
4023 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4024 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
4025 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
4026 // this may return true or false, depending on the platform's numerics, but it should not crash
4027 (void) p.contains(-77.2027664f, 15.3066053f);
4028
4029 p.reset();
4030 p.setFillType(SkPath::kInverseWinding_FillType);
4031 REPORTER_ASSERT(reporter, p.contains(0, 0));
4032 p.setFillType(SkPath::kWinding_FillType);
4033 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4034 p.moveTo(4, 4);
4035 p.lineTo(6, 8);
4036 p.lineTo(8, 4);
4037 // test on edge
4038 REPORTER_ASSERT(reporter, p.contains(6, 4));
4039 REPORTER_ASSERT(reporter, p.contains(5, 6));
4040 REPORTER_ASSERT(reporter, p.contains(7, 6));
4041 // test quick reject
4042 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4043 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4044 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4045 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4046 // test various crossings in x
4047 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4048 REPORTER_ASSERT(reporter, p.contains(6, 7));
4049 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4050 p.reset();
4051 p.moveTo(4, 4);
4052 p.lineTo(8, 6);
4053 p.lineTo(4, 8);
4054 // test on edge
4055 REPORTER_ASSERT(reporter, p.contains(4, 6));
4056 REPORTER_ASSERT(reporter, p.contains(6, 5));
4057 REPORTER_ASSERT(reporter, p.contains(6, 7));
4058 // test various crossings in y
4059 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4060 REPORTER_ASSERT(reporter, p.contains(7, 6));
4061 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4062 p.reset();
4063 p.moveTo(4, 4);
4064 p.lineTo(8, 4);
4065 p.lineTo(8, 8);
4066 p.lineTo(4, 8);
4067 // test on vertices
4068 REPORTER_ASSERT(reporter, p.contains(4, 4));
4069 REPORTER_ASSERT(reporter, p.contains(8, 4));
4070 REPORTER_ASSERT(reporter, p.contains(8, 8));
4071 REPORTER_ASSERT(reporter, p.contains(4, 8));
4072 p.reset();
4073 p.moveTo(4, 4);
4074 p.lineTo(6, 8);
4075 p.lineTo(2, 8);
4076 // test on edge
4077 REPORTER_ASSERT(reporter, p.contains(5, 6));
4078 REPORTER_ASSERT(reporter, p.contains(4, 8));
4079 REPORTER_ASSERT(reporter, p.contains(3, 6));
4080 p.reset();
4081 p.moveTo(4, 4);
4082 p.lineTo(0, 6);
4083 p.lineTo(4, 8);
4084 // test on edge
4085 REPORTER_ASSERT(reporter, p.contains(2, 5));
4086 REPORTER_ASSERT(reporter, p.contains(2, 7));
4087 REPORTER_ASSERT(reporter, p.contains(4, 6));
4088 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4089 p.reset();
4090 p.moveTo(4, 0);
4091 p.lineTo(6, 4);
4092 p.lineTo(2, 4);
4093 p.moveTo(4, 0);
4094 p.lineTo(0, 8);
4095 p.lineTo(8, 8);
4096 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4097 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4098 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4099 REPORTER_ASSERT(reporter, p.contains(4, 4));
4100
4101 // test quads
4102 p.reset();
4103 p.moveTo(4, 4);
4104 p.quadTo(6, 6, 8, 8);
4105 p.quadTo(6, 8, 4, 8);
4106 p.quadTo(4, 6, 4, 4);
4107 REPORTER_ASSERT(reporter, p.contains(5, 6));
4108 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4109 // test quad edge
4110 REPORTER_ASSERT(reporter, p.contains(5, 5));
4111 REPORTER_ASSERT(reporter, p.contains(5, 8));
4112 REPORTER_ASSERT(reporter, p.contains(4, 5));
4113 // test quad endpoints
4114 REPORTER_ASSERT(reporter, p.contains(4, 4));
4115 REPORTER_ASSERT(reporter, p.contains(8, 8));
4116 REPORTER_ASSERT(reporter, p.contains(4, 8));
4117
4118 p.reset();
4119 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4120 p.moveTo(qPts[0]);
4121 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4122 p.quadTo(qPts[index], qPts[index + 1]);
4123 }
4124 REPORTER_ASSERT(reporter, p.contains(5, 6));
4125 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4126 // test quad edge
4127 SkPoint halfway;
4128 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4129 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4130 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4131 }
4132
4133 // test conics
4134 p.reset();
4135 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4136 p.moveTo(kPts[0]);
4137 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4138 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4139 }
4140 REPORTER_ASSERT(reporter, p.contains(5, 6));
4141 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4142 // test conic edge
4143 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4144 SkConic conic(&kPts[index], 0.5f);
4145 halfway = conic.evalAt(0.5f);
4146 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4147 }
4148 // test conic end points
4149 REPORTER_ASSERT(reporter, p.contains(4, 4));
4150 REPORTER_ASSERT(reporter, p.contains(8, 8));
4151 REPORTER_ASSERT(reporter, p.contains(4, 8));
4152
4153 // test cubics
4154 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4155 for (int i = 0; i < 3; ++i) {
4156 p.reset();
4157 p.setFillType(SkPath::kEvenOdd_FillType);
4158 p.moveTo(pts[i].fX, pts[i].fY);
4159 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);
4160 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);
4161 p.close();
4162 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4163 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4164 // test cubic edge
4165 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4166 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4167 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4168 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4169 // test cubic end points
4170 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4171 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4172 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4173 }
4174}
4175
4176class PathRefTest_Private {
4177public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004178 static size_t GetFreeSpace(const SkPathRef& ref) {
Mike Reed8fda88e2019-09-05 14:14:38 -04004179 return (ref.fPoints.reserved() - ref.fPoints.count()) * sizeof(SkPoint)
4180 + (ref.fVerbs.reserved() - ref.fVerbs.count()) * sizeof(uint8_t);
Florin Malita0e0f1a72018-09-11 16:16:19 -04004181 }
4182
Yuqian Li3154a532017-09-06 13:33:30 -04004183 static void TestPathRef(skiatest::Reporter* reporter) {
4184 static const int kRepeatCnt = 10;
4185
4186 sk_sp<SkPathRef> pathRef(new SkPathRef);
4187
4188 SkPathRef::Editor ed(&pathRef);
4189
4190 {
4191 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4192 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4193 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4194 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4195 for (int i = 0; i < kRepeatCnt; ++i) {
4196 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4197 }
4198 ed.resetToSize(0, 0, 0);
4199 }
4200
4201 {
4202 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4203 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4204 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4205 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4206 for (int i = 0; i < kRepeatCnt; ++i) {
4207 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4208 }
4209 ed.resetToSize(0, 0, 0);
4210 }
4211
4212 {
4213 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4214 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4215 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4216 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4217 for (int i = 0; i < kRepeatCnt; ++i) {
4218 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4219 }
4220 ed.resetToSize(0, 0, 0);
4221 }
4222
4223 {
4224 SkScalar* weights = nullptr;
4225 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4226 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4227 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4228 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4229 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4230 REPORTER_ASSERT(reporter, weights);
4231 for (int i = 0; i < kRepeatCnt; ++i) {
4232 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4233 }
4234 ed.resetToSize(0, 0, 0);
4235 }
4236
4237 {
4238 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4239 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4240 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4241 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4242 for (int i = 0; i < kRepeatCnt; ++i) {
4243 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4244 }
4245 ed.resetToSize(0, 0, 0);
4246 }
4247 }
4248};
4249
4250static void test_operatorEqual(skiatest::Reporter* reporter) {
4251 SkPath a;
4252 SkPath b;
4253 REPORTER_ASSERT(reporter, a == a);
4254 REPORTER_ASSERT(reporter, a == b);
4255 a.setFillType(SkPath::kInverseWinding_FillType);
4256 REPORTER_ASSERT(reporter, a != b);
4257 a.reset();
4258 REPORTER_ASSERT(reporter, a == b);
4259 a.lineTo(1, 1);
4260 REPORTER_ASSERT(reporter, a != b);
4261 a.reset();
4262 REPORTER_ASSERT(reporter, a == b);
4263 a.lineTo(1, 1);
4264 b.lineTo(1, 2);
4265 REPORTER_ASSERT(reporter, a != b);
4266 a.reset();
4267 a.lineTo(1, 2);
4268 REPORTER_ASSERT(reporter, a == b);
4269}
4270
4271static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4272 bool dumpAsHex, const char* str) {
4273 SkDynamicMemoryWStream wStream;
4274 path.dump(&wStream, force, dumpAsHex);
4275 sk_sp<SkData> data = wStream.detachAsData();
4276 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4277 if (strlen(str) > 0) {
4278 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4279 } else {
4280 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4281 }
4282}
4283
4284static void test_dump(skiatest::Reporter* reporter) {
4285 SkPath p;
4286 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4287 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4288 p.moveTo(1, 2);
4289 p.lineTo(3, 4);
4290 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4291 "path.moveTo(1, 2);\n"
4292 "path.lineTo(3, 4);\n");
4293 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4294 "path.moveTo(1, 2);\n"
4295 "path.lineTo(3, 4);\n"
4296 "path.lineTo(1, 2);\n"
4297 "path.close();\n");
4298 p.reset();
4299 p.setFillType(SkPath::kEvenOdd_FillType);
4300 p.moveTo(1, 2);
4301 p.quadTo(3, 4, 5, 6);
4302 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4303 "path.moveTo(1, 2);\n"
4304 "path.quadTo(3, 4, 5, 6);\n");
4305 p.reset();
4306 p.setFillType(SkPath::kInverseWinding_FillType);
4307 p.moveTo(1, 2);
4308 p.conicTo(3, 4, 5, 6, 0.5f);
4309 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4310 "path.moveTo(1, 2);\n"
4311 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4312 p.reset();
4313 p.setFillType(SkPath::kInverseEvenOdd_FillType);
4314 p.moveTo(1, 2);
4315 p.cubicTo(3, 4, 5, 6, 7, 8);
4316 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4317 "path.moveTo(1, 2);\n"
4318 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4319 p.reset();
4320 p.setFillType(SkPath::kWinding_FillType);
4321 p.moveTo(1, 2);
4322 p.lineTo(3, 4);
4323 compare_dump(reporter, p, false, true,
4324 "path.setFillType(SkPath::kWinding_FillType);\n"
4325 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4326 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4327 p.reset();
4328 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4329 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4330 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4331 "path.moveTo(1, 2);\n"
4332 "path.lineTo(3, 4);\n");
4333}
4334
4335namespace {
4336
4337class ChangeListener : public SkPathRef::GenIDChangeListener {
4338public:
4339 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4340 ~ChangeListener() override {}
4341 void onChange() override {
4342 *fChanged = true;
4343 }
4344private:
4345 bool* fChanged;
4346};
4347
4348}
4349
4350class PathTest_Private {
4351public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004352 static size_t GetFreeSpace(const SkPath& path) {
4353 return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
4354 }
4355
Yuqian Li3154a532017-09-06 13:33:30 -04004356 static void TestPathTo(skiatest::Reporter* reporter) {
4357 SkPath p, q;
4358 p.lineTo(4, 4);
4359 p.reversePathTo(q);
4360 check_path_is_line(reporter, &p, 4, 4);
4361 q.moveTo(-4, -4);
4362 p.reversePathTo(q);
4363 check_path_is_line(reporter, &p, 4, 4);
4364 q.lineTo(7, 8);
4365 q.conicTo(8, 7, 6, 5, 0.5f);
4366 q.quadTo(6, 7, 8, 6);
4367 q.cubicTo(5, 6, 7, 8, 7, 5);
4368 q.close();
4369 p.reversePathTo(q);
4370 SkRect reverseExpected = {-4, -4, 8, 8};
4371 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4372 }
4373
4374 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4375 SkPath p;
4376
4377 bool changed = false;
4378 p.moveTo(0, 0);
4379
4380 // Check that listener is notified on moveTo().
4381
Chris Daltonafa11582018-06-08 12:00:44 -06004382 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004383 REPORTER_ASSERT(reporter, !changed);
4384 p.moveTo(10, 0);
4385 REPORTER_ASSERT(reporter, changed);
4386
4387 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004388 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004389 REPORTER_ASSERT(reporter, !changed);
4390 p.lineTo(20, 0);
4391 REPORTER_ASSERT(reporter, changed);
4392
4393 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004394 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004395 REPORTER_ASSERT(reporter, !changed);
4396 p.reset();
4397 REPORTER_ASSERT(reporter, changed);
4398
4399 p.moveTo(0, 0);
4400
4401 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004402 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004403 REPORTER_ASSERT(reporter, !changed);
4404 p.rewind();
4405 REPORTER_ASSERT(reporter, changed);
4406
Brian Osmandbfcd922019-03-07 16:50:30 -05004407 // Check that listener is notified on transform().
4408 {
4409 SkPath q;
4410 q.moveTo(10, 10);
4411 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4412 REPORTER_ASSERT(reporter, !changed);
4413 SkMatrix matrix;
4414 matrix.setScale(2, 2);
4415 p.transform(matrix, &q);
4416 REPORTER_ASSERT(reporter, changed);
4417 }
4418
Yuqian Li3154a532017-09-06 13:33:30 -04004419 // Check that listener is notified when pathref is deleted.
4420 {
4421 SkPath q;
4422 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004423 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004424 REPORTER_ASSERT(reporter, !changed);
4425 }
4426 // q went out of scope.
4427 REPORTER_ASSERT(reporter, changed);
4428 }
4429};
4430
4431static void test_crbug_629455(skiatest::Reporter* reporter) {
4432 SkPath path;
4433 path.moveTo(0, 0);
4434 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4435 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4436 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4437// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4438 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004439 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004440}
4441
4442static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4443 SkPath path;
4444 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4445 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4446 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4447 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4448 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4449 path.close();
4450
4451 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4452 SkPaint paint;
4453 paint.setAntiAlias(true);
4454 surface->getCanvas()->clipPath(path, true);
4455 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4456}
4457
4458static void test_path_crbugskia6003() {
4459 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4460 SkCanvas* canvas = surface->getCanvas();
4461 SkPaint paint;
4462 paint.setAntiAlias(true);
4463 SkPath path;
4464 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4465 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4466 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4467 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4468 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4469 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4470 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4471 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4472 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4473 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4474 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4475 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4476 path.close();
4477 canvas->clipPath(path, true);
4478 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4479}
4480
4481static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4482 SkPath path;
4483 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4484 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4485 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4486 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4487 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004488 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004489}
4490
Yuqian Li3154a532017-09-06 13:33:30 -04004491static void test_skbug_6947() {
4492 SkPath path;
4493 SkPoint points[] =
4494 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4495 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4496 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4497 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4498 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4499 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4500 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4501 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4502 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4503 {125.119476f, 1.50011659f}};
4504 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4505 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4506 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4507 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4508 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4509 int pointIndex = 0;
4510 for(auto verb : verbs) {
4511 switch (verb) {
4512 case kMove:
4513 path.moveTo(points[pointIndex++]);
4514 break;
4515 case kLine:
4516 path.lineTo(points[pointIndex++]);
4517 break;
4518 case kClose:
4519 default:
4520 path.close();
4521 break;
4522 }
4523 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004524 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004525}
Yuqian Lia81b6262017-09-06 17:10:05 -04004526
4527static void test_skbug_7015() {
4528 SkPath path;
4529 path.setFillType(SkPath::kWinding_FillType);
4530 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4531 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4532 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4533 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4534 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4535 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4536 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004537 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004538}
4539
Yuqian Lic5e4e742017-09-18 14:38:43 -04004540static void test_skbug_7051() {
4541 SkPath path;
4542 path.moveTo(10, 10);
4543 path.cubicTo(10, 20, 10, 30, 30, 30);
4544 path.lineTo(50, 20);
4545 path.lineTo(50, 10);
4546 path.close();
4547 test_draw_AA_path(100, 100, path);
4548}
4549
Ben Wagner2e9f7722019-08-14 15:45:37 -04004550static void test_skbug_7435() {
4551 SkPaint paint;
4552 SkPath path;
4553 path.setFillType(SkPath::kWinding_FillType);
4554 path.moveTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4555 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4556 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4557 path.lineTo(SkBits2Float(0x7ef348e9), SkBits2Float(0xfef403c6)); // 1.6169e+38f, -1.62176e+38f
4558 path.lineTo(SkBits2Float(0x7ef74c4e), SkBits2Float(0xfef803cb)); // 1.64358e+38f, -1.64834e+38f
4559 path.conicTo(SkBits2Float(0x7ef74f23), SkBits2Float(0xfef8069e), SkBits2Float(0x7ef751f6), SkBits2Float(0xfef803c9), SkBits2Float(0x3f3504f3)); // 1.64365e+38f, -1.64841e+38f, 1.64372e+38f, -1.64834e+38f, 0.707107f
4560 path.conicTo(SkBits2Float(0x7ef754c8), SkBits2Float(0xfef800f5), SkBits2Float(0x7ef751f5), SkBits2Float(0xfef7fe22), SkBits2Float(0x3f353472)); // 1.6438e+38f, -1.64827e+38f, 1.64372e+38f, -1.64819e+38f, 0.707832f
4561 path.lineTo(SkBits2Float(0x7edb57a9), SkBits2Float(0xfedbfe06)); // 1.45778e+38f, -1.4621e+38f
4562 path.lineTo(SkBits2Float(0x7e875976), SkBits2Float(0xfe87fdb3)); // 8.99551e+37f, -9.03815e+37f
4563 path.lineTo(SkBits2Float(0x7ded5c2b), SkBits2Float(0xfdeff59e)); // 3.94382e+37f, -3.98701e+37f
4564 path.lineTo(SkBits2Float(0x7d7a78a7), SkBits2Float(0xfd7fda0f)); // 2.08083e+37f, -2.12553e+37f
4565 path.lineTo(SkBits2Float(0x7d7a6403), SkBits2Float(0xfd7fe461)); // 2.08016e+37f, -2.12587e+37f
4566 path.conicTo(SkBits2Float(0x7d7a4764), SkBits2Float(0xfd7ff2b0), SkBits2Float(0x7d7a55b4), SkBits2Float(0xfd8007a8), SkBits2Float(0x3f3504f3)); // 2.07924e+37f, -2.12633e+37f, 2.0797e+37f, -2.12726e+37f, 0.707107f
4567 path.conicTo(SkBits2Float(0x7d7a5803), SkBits2Float(0xfd8009f7), SkBits2Float(0x7d7a5ba9), SkBits2Float(0xfd800bcc), SkBits2Float(0x3f7cba66)); // 2.07977e+37f, -2.12741e+37f, 2.07989e+37f, -2.12753e+37f, 0.987219f
4568 path.lineTo(SkBits2Float(0x7d8d2067), SkBits2Float(0xfd900bdb)); // 2.34487e+37f, -2.39338e+37f
4569 path.lineTo(SkBits2Float(0x7ddd137a), SkBits2Float(0xfde00c2d)); // 3.67326e+37f, -3.72263e+37f
4570 path.lineTo(SkBits2Float(0x7ddd2a1b), SkBits2Float(0xfddff58e)); // 3.67473e+37f, -3.72116e+37f
4571 path.lineTo(SkBits2Float(0x7c694ae5), SkBits2Float(0xfc7fa67c)); // 4.8453e+36f, -5.30965e+36f
4572 path.lineTo(SkBits2Float(0xfc164a8b), SkBits2Float(0x7c005af5)); // -3.12143e+36f, 2.66584e+36f
4573 path.lineTo(SkBits2Float(0xfc8ae983), SkBits2Float(0x7c802da7)); // -5.77019e+36f, 5.32432e+36f
4574 path.lineTo(SkBits2Float(0xfc8b16d9), SkBits2Float(0x7c80007b)); // -5.77754e+36f, 5.31699e+36f
4575 path.lineTo(SkBits2Float(0xfc8b029c), SkBits2Float(0x7c7f8788)); // -5.77426e+36f, 5.30714e+36f
4576 path.lineTo(SkBits2Float(0xfc8b0290), SkBits2Float(0x7c7f8790)); // -5.77425e+36f, 5.30714e+36f
4577 path.lineTo(SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.77753e+36f, 5.31699e+36f
4578 path.lineTo(SkBits2Float(0xfc8b4409), SkBits2Float(0x7c7fa672)); // -5.78487e+36f, 5.30965e+36f
4579 path.lineTo(SkBits2Float(0x7d7aa2ba), SkBits2Float(0xfd800bd1)); // 2.0822e+37f, -2.12753e+37f
4580 path.lineTo(SkBits2Float(0x7e8757ee), SkBits2Float(0xfe88035b)); // 8.99512e+37f, -9.03962e+37f
4581 path.lineTo(SkBits2Float(0x7ef7552d), SkBits2Float(0xfef803ca)); // 1.64381e+38f, -1.64834e+38f
4582 path.lineTo(SkBits2Float(0x7f0fa653), SkBits2Float(0xff1001f9)); // 1.90943e+38f, -1.91419e+38f
4583 path.lineTo(SkBits2Float(0x7f0fa926), SkBits2Float(0xff0fff24)); // 1.90958e+38f, -1.91404e+38f
4584 path.lineTo(SkBits2Float(0x7f0da75c), SkBits2Float(0xff0dff22)); // 1.8829e+38f, -1.88746e+38f
4585 path.lineTo(SkBits2Float(0x7f07a5af), SkBits2Float(0xff07ff1d)); // 1.80306e+38f, -1.8077e+38f
4586 path.close();
4587 path.moveTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4588 path.lineTo(SkBits2Float(0x7f0da48a), SkBits2Float(0xff0e01f8)); // 1.88275e+38f, -1.88761e+38f
4589 path.lineTo(SkBits2Float(0x7f0fa654), SkBits2Float(0xff1001fa)); // 1.90943e+38f, -1.91419e+38f
4590 path.lineTo(SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90951e+38f, -1.91412e+38f
4591 path.lineTo(SkBits2Float(0x7f0fa927), SkBits2Float(0xff0fff25)); // 1.90958e+38f, -1.91404e+38f
4592 path.lineTo(SkBits2Float(0x7ef75ad5), SkBits2Float(0xfef7fe22)); // 1.64395e+38f, -1.64819e+38f
4593 path.lineTo(SkBits2Float(0x7e875d96), SkBits2Float(0xfe87fdb3)); // 8.99659e+37f, -9.03815e+37f
4594 path.lineTo(SkBits2Float(0x7d7acff6), SkBits2Float(0xfd7fea5b)); // 2.08367e+37f, -2.12606e+37f
4595 path.lineTo(SkBits2Float(0xfc8b0588), SkBits2Float(0x7c8049b7)); // -5.77473e+36f, 5.32887e+36f
4596 path.lineTo(SkBits2Float(0xfc8b2b16), SkBits2Float(0x7c803d32)); // -5.78083e+36f, 5.32684e+36f
4597 path.conicTo(SkBits2Float(0xfc8b395c), SkBits2Float(0x7c803870), SkBits2Float(0xfc8b4405), SkBits2Float(0x7c802dd1), SkBits2Float(0x3f79349d)); // -5.78314e+36f, 5.32607e+36f, -5.78487e+36f, 5.32435e+36f, 0.973459f
4598 path.conicTo(SkBits2Float(0xfc8b715b), SkBits2Float(0x7c8000a5), SkBits2Float(0xfc8b442f), SkBits2Float(0x7c7fa69e), SkBits2Float(0x3f3504f3)); // -5.79223e+36f, 5.31702e+36f, -5.7849e+36f, 5.30966e+36f, 0.707107f
4599 path.lineTo(SkBits2Float(0xfc16ffaa), SkBits2Float(0x7bff4c12)); // -3.13612e+36f, 2.65116e+36f
4600 path.lineTo(SkBits2Float(0x7c6895e0), SkBits2Float(0xfc802dc0)); // 4.83061e+36f, -5.32434e+36f
4601 path.lineTo(SkBits2Float(0x7ddd137b), SkBits2Float(0xfde00c2e)); // 3.67326e+37f, -3.72263e+37f
4602 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4603 path.lineTo(SkBits2Float(0x7ddd2a1c), SkBits2Float(0xfddff58f)); // 3.67473e+37f, -3.72116e+37f
4604 path.lineTo(SkBits2Float(0x7d8d3711), SkBits2Float(0xfd8ff543)); // 2.34634e+37f, -2.39191e+37f
4605 path.lineTo(SkBits2Float(0x7d7a88fe), SkBits2Float(0xfd7fea69)); // 2.08136e+37f, -2.12606e+37f
4606 path.lineTo(SkBits2Float(0x7d7a7254), SkBits2Float(0xfd800080)); // 2.08063e+37f, -2.1268e+37f
4607 path.lineTo(SkBits2Float(0x7d7a80a4), SkBits2Float(0xfd800ed0)); // 2.08109e+37f, -2.12773e+37f
4608 path.lineTo(SkBits2Float(0x7d7a80a8), SkBits2Float(0xfd800ecf)); // 2.08109e+37f, -2.12773e+37f
4609 path.lineTo(SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08063e+37f, -2.1268e+37f
4610 path.lineTo(SkBits2Float(0x7d7a5bb9), SkBits2Float(0xfd800bd0)); // 2.0799e+37f, -2.12753e+37f
4611 path.lineTo(SkBits2Float(0x7ded458b), SkBits2Float(0xfdf00c3e)); // 3.94235e+37f, -3.98848e+37f
4612 path.lineTo(SkBits2Float(0x7e8753ce), SkBits2Float(0xfe88035b)); // 8.99405e+37f, -9.03962e+37f
4613 path.lineTo(SkBits2Float(0x7edb5201), SkBits2Float(0xfedc03ae)); // 1.45763e+38f, -1.46225e+38f
4614 path.lineTo(SkBits2Float(0x7ef74c4d), SkBits2Float(0xfef803ca)); // 1.64358e+38f, -1.64834e+38f
4615 path.lineTo(SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64365e+38f, -1.64827e+38f
4616 path.lineTo(SkBits2Float(0x7ef751f4), SkBits2Float(0xfef7fe21)); // 1.64372e+38f, -1.64819e+38f
4617 path.lineTo(SkBits2Float(0x7ef34e91), SkBits2Float(0xfef3fe1e)); // 1.61705e+38f, -1.62161e+38f
4618 path.lineTo(SkBits2Float(0x7edf4b2d), SkBits2Float(0xfedffe0a)); // 1.48404e+38f, -1.48868e+38f
4619 path.lineTo(SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.48397e+38f, -1.48876e+38f
4620 path.lineTo(SkBits2Float(0x7edf4585), SkBits2Float(0xfee003b2)); // 1.48389e+38f, -1.48883e+38f
4621 path.lineTo(SkBits2Float(0x7f07a2db), SkBits2Float(0xff0801f1)); // 1.80291e+38f, -1.80785e+38f
4622 path.close();
4623 path.moveTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4624 path.lineTo(SkBits2Float(0xfd6597e5), SkBits2Float(0x7d60177f)); // -1.90739e+37f, 1.86168e+37f
4625 path.lineTo(SkBits2Float(0xfde2cea1), SkBits2Float(0x7de00c2e)); // -3.76848e+37f, 3.72263e+37f
4626 path.lineTo(SkBits2Float(0xfe316511), SkBits2Float(0x7e300657)); // -5.89495e+37f, 5.84943e+37f
4627 path.lineTo(SkBits2Float(0xfe415da1), SkBits2Float(0x7e400666)); // -6.42568e+37f, 6.38112e+37f
4628 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4000be)); // -6.42641e+37f, 6.38039e+37f
4629 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4630 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e3ff8be)); // -6.42641e+37f, 6.37935e+37f
4631 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e3ff8be)); // -6.42591e+37f, 6.37935e+37f
4632 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e3ff8be)); // -6.42544e+37f, 6.37935e+37f
4633 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4634 path.lineTo(SkBits2Float(0xfe416171), SkBits2Float(0x7e3ffb16)); // -6.42617e+37f, 6.37966e+37f
4635 path.lineTo(SkBits2Float(0xfe016131), SkBits2Float(0x7dfff5ae)); // -4.29938e+37f, 4.25286e+37f
4636 path.lineTo(SkBits2Float(0xfe0155e2), SkBits2Float(0x7e000628)); // -4.29791e+37f, 4.25433e+37f
4637 path.lineTo(SkBits2Float(0xfe0958ea), SkBits2Float(0x7e080630)); // -4.56415e+37f, 4.52018e+37f
4638 path.lineTo(SkBits2Float(0xfe115c92), SkBits2Float(0x7e100638)); // -4.83047e+37f, 4.78603e+37f
4639 path.conicTo(SkBits2Float(0xfe11623c), SkBits2Float(0x7e100bdf), SkBits2Float(0xfe1167e2), SkBits2Float(0x7e100636), SkBits2Float(0x3f3504f3)); // -4.8312e+37f, 4.78676e+37f, -4.83194e+37f, 4.78603e+37f, 0.707107f
4640 path.conicTo(SkBits2Float(0xfe116d87), SkBits2Float(0x7e10008e), SkBits2Float(0xfe1167e2), SkBits2Float(0x7e0ffae8), SkBits2Float(0x3f35240a)); // -4.83267e+37f, 4.78529e+37f, -4.83194e+37f, 4.78456e+37f, 0.707581f
4641 path.lineTo(SkBits2Float(0xfe016b92), SkBits2Float(0x7dfff5af)); // -4.30072e+37f, 4.25286e+37f
4642 path.lineTo(SkBits2Float(0xfdc2d963), SkBits2Float(0x7dbff56e)); // -3.23749e+37f, 3.18946e+37f
4643 path.lineTo(SkBits2Float(0xfd65ae25), SkBits2Float(0x7d5fea3d)); // -1.90811e+37f, 1.86021e+37f
4644 path.lineTo(SkBits2Float(0xfab448de), SkBits2Float(0xf7b50a19)); // -4.68046e+35f, -7.34383e+33f
4645 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4646 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x7800007f)); // -4.60703e+35f, 1.03848e+34f
4647 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x7800007f)); // -4.67194e+35f, 1.03848e+34f
4648 path.lineTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4649 path.lineTo(SkBits2Float(0xfab120db), SkBits2Float(0x77b50b4f)); // -4.59851e+35f, 7.34402e+33f
4650 path.close();
4651 path.moveTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4652 path.lineTo(SkBits2Float(0xfaa7cc52), SkBits2Float(0xf800007f)); // -4.35629e+35f, -1.03848e+34f
4653 path.lineTo(SkBits2Float(0xfd6580e5), SkBits2Float(0x7d60177f)); // -1.90664e+37f, 1.86168e+37f
4654 path.lineTo(SkBits2Float(0xfdc2c2c1), SkBits2Float(0x7dc00c0f)); // -3.23602e+37f, 3.19093e+37f
4655 path.lineTo(SkBits2Float(0xfe016040), SkBits2Float(0x7e000626)); // -4.29925e+37f, 4.25433e+37f
4656 path.lineTo(SkBits2Float(0xfe115c90), SkBits2Float(0x7e100636)); // -4.83047e+37f, 4.78603e+37f
4657 path.lineTo(SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.8312e+37f, 4.78529e+37f
4658 path.lineTo(SkBits2Float(0xfe1167e0), SkBits2Float(0x7e0ffae6)); // -4.83194e+37f, 4.78456e+37f
4659 path.lineTo(SkBits2Float(0xfe096438), SkBits2Float(0x7e07fade)); // -4.56562e+37f, 4.51871e+37f
4660 path.lineTo(SkBits2Float(0xfe016130), SkBits2Float(0x7dfff5ac)); // -4.29938e+37f, 4.25286e+37f
4661 path.lineTo(SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29864e+37f, 4.25359e+37f
4662 path.lineTo(SkBits2Float(0xfe0155e1), SkBits2Float(0x7e000627)); // -4.29791e+37f, 4.25433e+37f
4663 path.lineTo(SkBits2Float(0xfe415879), SkBits2Float(0x7e4008bf)); // -6.42501e+37f, 6.38143e+37f
4664 path.lineTo(SkBits2Float(0xfe415f69), SkBits2Float(0x7e4008bf)); // -6.42591e+37f, 6.38143e+37f
4665 path.lineTo(SkBits2Float(0xfe416349), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4666 path.lineTo(SkBits2Float(0xfe41634a), SkBits2Float(0x7e4008bf)); // -6.42641e+37f, 6.38143e+37f
4667 path.conicTo(SkBits2Float(0xfe416699), SkBits2Float(0x7e4008bf), SkBits2Float(0xfe4168f1), SkBits2Float(0x7e400668), SkBits2Float(0x3f6c8ed9)); // -6.42684e+37f, 6.38143e+37f, -6.42715e+37f, 6.38113e+37f, 0.924055f
4668 path.conicTo(SkBits2Float(0xfe416e9a), SkBits2Float(0x7e4000c2), SkBits2Float(0xfe4168f3), SkBits2Float(0x7e3ffb17), SkBits2Float(0x3f3504f3)); // -6.42788e+37f, 6.38039e+37f, -6.42715e+37f, 6.37966e+37f, 0.707107f
4669 path.lineTo(SkBits2Float(0xfe317061), SkBits2Float(0x7e2ffb07)); // -5.89642e+37f, 5.84796e+37f
4670 path.lineTo(SkBits2Float(0xfde2e542), SkBits2Float(0x7ddff58e)); // -3.76995e+37f, 3.72116e+37f
4671 path.lineTo(SkBits2Float(0xfd65c525), SkBits2Float(0x7d5fea3d)); // -1.90886e+37f, 1.86021e+37f
4672 path.lineTo(SkBits2Float(0xfab6c8db), SkBits2Float(0xf7b50b4f)); // -4.74536e+35f, -7.34402e+33f
4673 path.lineTo(SkBits2Float(0xfab59cf2), SkBits2Float(0xf800007e)); // -4.71494e+35f, -1.03847e+34f
4674 path.close();
4675 path.moveTo(SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -4.67194e+35f, 200
4676 path.lineTo(SkBits2Float(0xfab174d9), SkBits2Float(0x43480000)); // -4.60703e+35f, 200
4677 path.quadTo(SkBits2Float(0xfd0593a5), SkBits2Float(0x7d00007f), SkBits2Float(0xfd659785), SkBits2Float(0x7d6000de)); // -1.10971e+37f, 1.0634e+37f, -1.90737e+37f, 1.86095e+37f
4678 path.quadTo(SkBits2Float(0xfda2cdf2), SkBits2Float(0x7da0009f), SkBits2Float(0xfdc2ce12), SkBits2Float(0x7dc000be)); // -2.70505e+37f, 2.6585e+37f, -3.23675e+37f, 3.1902e+37f
4679 path.quadTo(SkBits2Float(0xfde2ce31), SkBits2Float(0x7de000de), SkBits2Float(0xfe0165e9), SkBits2Float(0x7e00007f)); // -3.76845e+37f, 3.72189e+37f, -4.29999e+37f, 4.25359e+37f
4680 path.quadTo(SkBits2Float(0xfe1164b9), SkBits2Float(0x7e10008f), SkBits2Float(0xfe116239), SkBits2Float(0x7e10008f)); // -4.83153e+37f, 4.78529e+37f, -4.8312e+37f, 4.78529e+37f
4681 path.quadTo(SkBits2Float(0xfe116039), SkBits2Float(0x7e10008f), SkBits2Float(0xfe095e91), SkBits2Float(0x7e080087)); // -4.83094e+37f, 4.78529e+37f, -4.56488e+37f, 4.51944e+37f
4682 path.quadTo(SkBits2Float(0xfe015d09), SkBits2Float(0x7e00007f), SkBits2Float(0xfe015b89), SkBits2Float(0x7e00007f)); // -4.29884e+37f, 4.25359e+37f, -4.29864e+37f, 4.25359e+37f
4683 path.lineTo(SkBits2Float(0xfe415bc9), SkBits2Float(0x7e4000be)); // -6.42544e+37f, 6.38039e+37f
4684 path.quadTo(SkBits2Float(0xfe415da9), SkBits2Float(0x7e4000be), SkBits2Float(0xfe415f69), SkBits2Float(0x7e4000be)); // -6.42568e+37f, 6.38039e+37f, -6.42591e+37f, 6.38039e+37f
4685 path.quadTo(SkBits2Float(0xfe416149), SkBits2Float(0x7e4000be), SkBits2Float(0xfe416349), SkBits2Float(0x7e4000be)); // -6.42615e+37f, 6.38039e+37f, -6.42641e+37f, 6.38039e+37f
4686 path.quadTo(SkBits2Float(0xfe416849), SkBits2Float(0x7e4000be), SkBits2Float(0xfe316ab9), SkBits2Float(0x7e3000af)); // -6.42706e+37f, 6.38039e+37f, -5.89569e+37f, 5.84869e+37f
4687 path.quadTo(SkBits2Float(0xfe216d29), SkBits2Float(0x7e20009f), SkBits2Float(0xfde2d9f2), SkBits2Float(0x7de000de)); // -5.36431e+37f, 5.31699e+37f, -3.76921e+37f, 3.72189e+37f
4688 path.quadTo(SkBits2Float(0xfda2d9b2), SkBits2Float(0x7da0009f), SkBits2Float(0xfd65ae85), SkBits2Float(0x7d6000de)); // -2.70582e+37f, 2.6585e+37f, -1.90812e+37f, 1.86095e+37f
4689 path.quadTo(SkBits2Float(0xfd05a9a6), SkBits2Float(0x7d00007f), SkBits2Float(0xfab3f4db), SkBits2Float(0x43480000)); // -1.11043e+37f, 1.0634e+37f, -4.67194e+35f, 200
4690 path.close();
4691 path.moveTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4692 path.quadTo(SkBits2Float(0x7f0ba519), SkBits2Float(0xff0c008b), SkBits2Float(0x7f0da5f3), SkBits2Float(0xff0e008d)); // 1.8562e+38f, -1.86095e+38f, 1.88283e+38f, -1.88753e+38f
4693 path.quadTo(SkBits2Float(0x7f0fa6d5), SkBits2Float(0xff10008f), SkBits2Float(0x7f0fa7bd), SkBits2Float(0xff10008f)); // 1.90946e+38f, -1.91412e+38f, 1.90951e+38f, -1.91412e+38f
4694 path.quadTo(SkBits2Float(0x7f0faa7d), SkBits2Float(0xff10008f), SkBits2Float(0x7ef75801), SkBits2Float(0xfef800f6)); // 1.90965e+38f, -1.91412e+38f, 1.64388e+38f, -1.64827e+38f
4695 path.quadTo(SkBits2Float(0x7ecf5b09), SkBits2Float(0xfed000ce), SkBits2Float(0x7e875ac2), SkBits2Float(0xfe880087)); // 1.37811e+38f, -1.38242e+38f, 8.99585e+37f, -9.03889e+37f
4696 path.quadTo(SkBits2Float(0x7e0eb505), SkBits2Float(0xfe10008f), SkBits2Float(0x7d7ab958), SkBits2Float(0xfd80007f)); // 4.74226e+37f, -4.78529e+37f, 2.08293e+37f, -2.1268e+37f
4697 path.quadTo(SkBits2Float(0xfc8ac1cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc8b16cd), SkBits2Float(0x7c80007f)); // -5.76374e+36f, 5.31699e+36f, -5.77753e+36f, 5.31699e+36f
4698 path.quadTo(SkBits2Float(0xfc8b36cd), SkBits2Float(0x7c80007f), SkBits2Float(0xfc16a51a), SkBits2Float(0x7c00007f)); // -5.78273e+36f, 5.31699e+36f, -3.12877e+36f, 2.6585e+36f
4699 path.quadTo(SkBits2Float(0xfab6e4de), SkBits2Float(0x43480000), SkBits2Float(0x7c68f062), SkBits2Float(0xfc80007f)); // -4.7482e+35f, 200, 4.83795e+36f, -5.31699e+36f
4700 path.lineTo(SkBits2Float(0x7ddd1ecb), SkBits2Float(0xfde000de)); // 3.67399e+37f, -3.72189e+37f
4701 path.quadTo(SkBits2Float(0x7d9d254b), SkBits2Float(0xfda0009f), SkBits2Float(0x7d8d2bbc), SkBits2Float(0xfd90008f)); // 2.61103e+37f, -2.6585e+37f, 2.3456e+37f, -2.39265e+37f
4702 path.quadTo(SkBits2Float(0x7d7a64d8), SkBits2Float(0xfd80007f), SkBits2Float(0x7d7a7258), SkBits2Float(0xfd80007f)); // 2.08019e+37f, -2.1268e+37f, 2.08063e+37f, -2.1268e+37f
4703 path.quadTo(SkBits2Float(0x7d7a9058), SkBits2Float(0xfd80007f), SkBits2Float(0x7ded50db), SkBits2Float(0xfdf000ee)); // 2.0816e+37f, -2.1268e+37f, 3.94309e+37f, -3.98774e+37f
4704 path.quadTo(SkBits2Float(0x7e2eace5), SkBits2Float(0xfe3000af), SkBits2Float(0x7e8756a2), SkBits2Float(0xfe880087)); // 5.80458e+37f, -5.84869e+37f, 8.99478e+37f, -9.03889e+37f
4705 path.quadTo(SkBits2Float(0x7ebf56d9), SkBits2Float(0xfec000be), SkBits2Float(0x7edb54d5), SkBits2Float(0xfedc00da)); // 1.27167e+38f, -1.27608e+38f, 1.45771e+38f, -1.46217e+38f
4706 path.quadTo(SkBits2Float(0x7ef752e1), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef74f21), SkBits2Float(0xfef800f6)); // 1.64375e+38f, -1.64827e+38f, 1.64365e+38f, -1.64827e+38f
4707 path.quadTo(SkBits2Float(0x7ef74d71), SkBits2Float(0xfef800f6), SkBits2Float(0x7ef34bbd), SkBits2Float(0xfef400f2)); // 1.64361e+38f, -1.64827e+38f, 1.61698e+38f, -1.62168e+38f
4708 path.quadTo(SkBits2Float(0x7eef4a19), SkBits2Float(0xfef000ee), SkBits2Float(0x7edf4859), SkBits2Float(0xfee000de)); // 1.59035e+38f, -1.5951e+38f, 1.48397e+38f, -1.48876e+38f
4709 path.lineTo(SkBits2Float(0x7f07a445), SkBits2Float(0xff080087)); // 1.80299e+38f, -1.80778e+38f
4710 path.close();
4711 SkSurface::MakeRasterN32Premul(250, 250, nullptr)->getCanvas()->drawPath(path, paint);
4712}
4713
Yuqian Li3154a532017-09-06 13:33:30 -04004714static void test_interp(skiatest::Reporter* reporter) {
4715 SkPath p1, p2, out;
4716 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4717 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4718 REPORTER_ASSERT(reporter, p1 == out);
4719 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4720 REPORTER_ASSERT(reporter, p1 == out);
4721 p1.moveTo(0, 2);
4722 p1.lineTo(0, 4);
4723 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4724 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4725 p2.moveTo(6, 0);
4726 p2.lineTo(8, 0);
4727 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4728 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4729 REPORTER_ASSERT(reporter, p2 == out);
4730 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4731 REPORTER_ASSERT(reporter, p1 == out);
4732 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4733 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4734 p1.reset();
4735 p1.moveTo(4, 4);
4736 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4737 p2.reset();
4738 p2.moveTo(4, 2);
4739 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4740 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4741 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4742 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4743 p2.reset();
4744 p2.moveTo(4, 2);
4745 p2.conicTo(6, 3, 6, 5, 1);
4746 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4747 p2.reset();
4748 p2.moveTo(4, 4);
4749 p2.conicTo(5, 4, 5, 5, 0.5f);
4750 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4751}
4752
4753DEF_TEST(PathInterp, reporter) {
4754 test_interp(reporter);
4755}
4756
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004757#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004758DEF_TEST(PathBigCubic, reporter) {
4759 SkPath path;
4760 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4761 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4762 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
4763 path.moveTo(0, 512);
4764
4765 // this call should not assert
4766 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4767}
4768
4769DEF_TEST(PathContains, reporter) {
4770 test_contains(reporter);
4771}
4772
4773DEF_TEST(Paths, reporter) {
4774 test_fuzz_crbug_647922();
4775 test_fuzz_crbug_643933();
4776 test_sect_with_horizontal_needs_pinning();
4777 test_crbug_629455(reporter);
4778 test_fuzz_crbug_627414(reporter);
4779 test_path_crbug364224();
4780 test_fuzz_crbug_662952(reporter);
4781 test_fuzz_crbug_662730(reporter);
4782 test_fuzz_crbug_662780();
4783 test_mask_overflow();
4784 test_path_crbugskia6003();
4785 test_fuzz_crbug_668907();
Yuqian Li3154a532017-09-06 13:33:30 -04004786 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004787 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004788 test_skbug_7051();
Ben Wagner2e9f7722019-08-14 15:45:37 -04004789 test_skbug_7435();
Yuqian Li3154a532017-09-06 13:33:30 -04004790
4791 SkSize::Make(3, 4);
4792
4793 SkPath p, empty;
4794 SkRect bounds, bounds2;
4795 test_empty(reporter, p);
4796
4797 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4798
4799 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4800 SkPath& self = p;
4801 p = self;
4802
4803 // this triggers a code path in SkPath::swap which is otherwise unexercised
4804 p.swap(self);
4805
Mike Reed92b33352019-08-24 19:39:13 -04004806 bounds.setLTRB(0, 0, SK_Scalar1, SK_Scalar1);
Yuqian Li3154a532017-09-06 13:33:30 -04004807
4808 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4809 check_convex_bounds(reporter, p, bounds);
4810 // we have quads or cubics
4811 REPORTER_ASSERT(reporter,
4812 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4813 REPORTER_ASSERT(reporter, !p.isEmpty());
4814
4815 p.reset();
4816 test_empty(reporter, p);
4817
4818 p.addOval(bounds);
4819 check_convex_bounds(reporter, p, bounds);
4820 REPORTER_ASSERT(reporter, !p.isEmpty());
4821
4822 p.rewind();
4823 test_empty(reporter, p);
4824
4825 p.addRect(bounds);
4826 check_convex_bounds(reporter, p, bounds);
4827 // we have only lines
4828 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4829 REPORTER_ASSERT(reporter, !p.isEmpty());
4830
4831 REPORTER_ASSERT(reporter, p != empty);
4832 REPORTER_ASSERT(reporter, !(p == empty));
4833
4834 // do getPoints and getVerbs return the right result
4835 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4836 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4837 SkPoint pts[4];
4838 int count = p.getPoints(pts, 4);
4839 REPORTER_ASSERT(reporter, count == 4);
4840 uint8_t verbs[6];
4841 verbs[5] = 0xff;
4842 p.getVerbs(verbs, 5);
4843 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4844 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4845 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4846 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4847 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4848 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
Mike Reed92b33352019-08-24 19:39:13 -04004849 bounds2.setBounds(pts, 4);
Yuqian Li3154a532017-09-06 13:33:30 -04004850 REPORTER_ASSERT(reporter, bounds == bounds2);
4851
4852 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4853 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4854 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4855
4856 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4857 bounds2.setEmpty();
4858 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4859 REPORTER_ASSERT(reporter, bounds == bounds2);
4860
4861 // now force p to not be a rect
Mike Reed92b33352019-08-24 19:39:13 -04004862 bounds.setWH(SK_Scalar1/2, SK_Scalar1/2);
Yuqian Li3154a532017-09-06 13:33:30 -04004863 p.addRect(bounds);
4864 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4865
4866 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4867 // path has a trailing moveTo. Please see crbug.com\445368)
4868 {
4869 SkRect r;
4870 p.reset();
4871 p.addRect(bounds);
4872 REPORTER_ASSERT(reporter, p.isRect(&r));
4873 REPORTER_ASSERT(reporter, r == bounds);
4874 // add a moveTo outside of our bounds
4875 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4876 REPORTER_ASSERT(reporter, p.isRect(&r));
4877 REPORTER_ASSERT(reporter, r == bounds);
4878 }
4879
4880 test_operatorEqual(reporter);
4881 test_isLine(reporter);
4882 test_isRect(reporter);
4883 test_is_simple_closed_rect(reporter);
4884 test_isNestedFillRects(reporter);
4885 test_zero_length_paths(reporter);
4886 test_direction(reporter);
4887 test_convexity(reporter);
4888 test_convexity2(reporter);
Cary Clarkc9b7c722018-12-12 14:50:23 -05004889 test_convexity_doubleback(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004890 test_conservativelyContains(reporter);
4891 test_close(reporter);
4892 test_segment_masks(reporter);
4893 test_flattening(reporter);
4894 test_transform(reporter);
4895 test_bounds(reporter);
4896 test_iter(reporter);
4897 test_raw_iter(reporter);
4898 test_circle(reporter);
4899 test_oval(reporter);
4900 test_strokerec(reporter);
4901 test_addPoly(reporter);
4902 test_isfinite(reporter);
4903 test_isfinite_after_transform(reporter);
4904 test_islastcontourclosed(reporter);
4905 test_arb_round_rect_is_convex(reporter);
4906 test_arb_zero_rad_round_rect_is_rect(reporter);
4907 test_addrect(reporter);
4908 test_addrect_isfinite(reporter);
4909 test_tricky_cubic();
4910 test_clipped_cubic();
4911 test_crbug_170666();
4912 test_crbug_493450(reporter);
4913 test_crbug_495894(reporter);
4914 test_crbug_613918();
4915 test_bad_cubic_crbug229478();
4916 test_bad_cubic_crbug234190();
4917 test_gen_id(reporter);
4918 test_path_close_issue1474(reporter);
4919 test_path_to_region(reporter);
4920 test_rrect(reporter);
4921 test_arc(reporter);
4922 test_arc_ovals(reporter);
4923 test_arcTo(reporter);
4924 test_addPath(reporter);
4925 test_addPathMode(reporter, false, false);
4926 test_addPathMode(reporter, true, false);
4927 test_addPathMode(reporter, false, true);
4928 test_addPathMode(reporter, true, true);
4929 test_extendClosedPath(reporter);
4930 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4931 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4932 test_conicTo_special_case(reporter);
4933 test_get_point(reporter);
4934 test_contains(reporter);
4935 PathTest_Private::TestPathTo(reporter);
4936 PathRefTest_Private::TestPathRef(reporter);
4937 PathTest_Private::TestPathrefListeners(reporter);
4938 test_dump(reporter);
4939 test_path_crbug389050(reporter);
4940 test_path_crbugskia2820(reporter);
4941 test_path_crbugskia5995();
4942 test_skbug_3469(reporter);
4943 test_skbug_3239(reporter);
4944 test_bounds_crbug_513799(reporter);
4945 test_fuzz_crbug_638223();
4946}
4947
4948DEF_TEST(conservatively_contains_rect, reporter) {
4949 SkPath path;
4950
4951 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4952 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4953 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4954 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4955 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4956 path.moveTo(0, 0);
4957
4958 // this guy should not assert
4959 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4960}
4961
4962///////////////////////////////////////////////////////////////////////////////////////////////////
4963
4964static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4965 for (int i = 0; i < n; ++i) {
4966 switch (verb) {
4967 case SkPath::kLine_Verb:
4968 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4969 break;
4970 case SkPath::kQuad_Verb:
4971 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4972 rand.nextF()*100, rand.nextF()*100);
4973 break;
4974 case SkPath::kConic_Verb:
4975 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4976 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4977 break;
4978 case SkPath::kCubic_Verb:
4979 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4980 rand.nextF()*100, rand.nextF()*100,
4981 rand.nextF()*100, rand.nextF()*100);
4982 break;
4983 default:
4984 SkASSERT(false);
4985 }
4986 }
4987}
4988
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004989#include "include/pathops/SkPathOps.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004990DEF_TEST(path_tight_bounds, reporter) {
4991 SkRandom rand;
4992
4993 const SkPath::Verb verbs[] = {
4994 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4995 };
4996 for (int i = 0; i < 1000; ++i) {
4997 for (int n = 1; n <= 10; n += 9) {
4998 for (SkPath::Verb verb : verbs) {
4999 SkPath path;
5000 rand_path(&path, rand, verb, n);
5001 SkRect bounds = path.getBounds();
5002 SkRect tight = path.computeTightBounds();
5003 REPORTER_ASSERT(reporter, bounds.contains(tight));
5004
5005 SkRect tight2;
5006 TightBounds(path, &tight2);
5007 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
5008 }
5009 }
5010 }
5011}
5012
5013DEF_TEST(skbug_6450, r) {
5014 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
5015 SkVector rdi[4] = {
5016 { 1.81159976e-09f, 7.58768801e-05f },
5017 { 0.000118725002f, 0.000118725002f },
5018 { 0.000118725002f, 0.000118725002f },
5019 { 0.000118725002f, 0.486297607f }
5020 };
5021 SkRRect irr;
5022 irr.setRectRadii(ri, rdi);
5023 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
5024 SkVector rdo[4] = {
5025 { 0, 0 },
5026 { 0.0103298295f, 0.185887396f },
5027 { 2.52999727e-29f, 169.001938f },
5028 { 195.262741f, 195.161255f }
5029 };
5030 SkRRect orr;
5031 orr.setRectRadii(ro, rdo);
5032 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
5033}
5034
5035DEF_TEST(PathRefSerialization, reporter) {
5036 SkPath path;
5037 const size_t numMoves = 5;
5038 const size_t numConics = 7;
5039 const size_t numPoints = numMoves + 2 * numConics;
5040 const size_t numVerbs = numMoves + numConics;
5041 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
5042 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
5043 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
5044 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
5045
5046 // Verify that path serializes/deserializes properly.
5047 sk_sp<SkData> data = path.serialize();
5048 size_t bytesWritten = data->size();
5049
5050 {
5051 SkPath readBack;
5052 REPORTER_ASSERT(reporter, readBack != path);
5053 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
5054 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
5055 REPORTER_ASSERT(reporter, readBack == path);
5056 }
5057
Yuqian Li3154a532017-09-06 13:33:30 -04005058 // One less byte (rounded down to alignment) than was written will also
5059 // fail to be deserialized.
5060 {
5061 SkPath readBack;
5062 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
5063 REPORTER_ASSERT(reporter, !bytesRead);
5064 }
5065}
Mike Klein1170a552017-09-08 15:00:25 -04005066
5067DEF_TEST(NonFinitePathIteration, reporter) {
5068 SkPath path;
5069 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
5070
5071 int verbs = 0;
5072
5073 SkPath::RawIter iter(path);
5074 SkPoint pts[4];
5075 while (iter.next(pts) != SkPath::kDone_Verb) {
5076 verbs++;
5077 }
5078
5079 REPORTER_ASSERT(reporter, verbs == 0);
5080}
Cary Clark1acd3c72017-12-08 11:37:01 -05005081
Cary Clark1acd3c72017-12-08 11:37:01 -05005082DEF_TEST(AndroidArc, reporter) {
5083 const char* tests[] = {
5084 "M50,0A50,50,0,0 1 100,50 L100,85 A15,15,0,0 1 85,100 L50,100 A50,50,0,0 1 50,0z",
5085 "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
5086 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
5087 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
5088 };
5089 for (auto test : tests) {
5090 SkPath aPath;
5091 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
5092 SkASSERT(aPath.isConvex());
5093 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
5094 SkPath scalePath = aPath;
5095 SkMatrix matrix;
5096 matrix.setScale(scale, scale);
5097 scalePath.transform(matrix);
5098 SkASSERT(scalePath.isConvex());
5099 }
5100 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
5101 SkPath scalePath = aPath;
5102 SkMatrix matrix;
5103 matrix.setScale(scale, scale);
5104 scalePath.transform(matrix);
5105 SkASSERT(scalePath.isConvex());
5106 }
5107 }
5108}
Mike Reed1fe0bcc2018-01-22 16:49:49 -05005109
5110/*
5111 * Try a range of crazy values, just to ensure that we don't assert/crash.
5112 */
5113DEF_TEST(HugeGeometry, reporter) {
5114 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
5115 auto canvas = surf->getCanvas();
5116
5117 const bool aas[] = { false, true };
5118 const SkPaint::Style styles[] = {
5119 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
5120 };
5121 const SkScalar values[] = {
5122 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
5123 SK_ScalarInfinity
5124 };
5125
5126 SkPaint paint;
5127 for (auto x : values) {
5128 SkRect r = { -x, -x, x, x };
5129 for (auto width : values) {
5130 paint.setStrokeWidth(width);
5131 for (auto aa : aas) {
5132 paint.setAntiAlias(aa);
5133 for (auto style : styles) {
5134 paint.setStyle(style);
5135 canvas->drawRect(r, paint);
5136 canvas->drawOval(r, paint);
5137 }
5138 }
5139 }
5140 }
5141
5142}
5143
Mike Reed1c6e7832018-01-30 11:29:36 -05005144// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
5145DEF_TEST(ClipPath_nonfinite, reporter) {
5146 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
5147 SkCanvas* canvas = surf->getCanvas();
5148
5149 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5150 for (bool aa : {false, true}) {
5151 for (SkPath::FillType ft : {SkPath::kWinding_FillType, SkPath::kInverseWinding_FillType}) {
5152 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
5153 for (int bits = 1; bits <= 15; ++bits) {
5154 SkPoint p0 = { 0, 0 };
5155 SkPoint p1 = { 0, 0 };
5156 if (bits & 1) p0.fX = -bad;
5157 if (bits & 2) p0.fY = -bad;
5158 if (bits & 4) p1.fX = bad;
5159 if (bits & 8) p1.fY = bad;
5160
5161 SkPath path;
5162 path.moveTo(p0);
5163 path.lineTo(p1);
5164 path.setFillType(ft);
5165 canvas->save();
5166 canvas->clipPath(path, aa);
5167 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
5168 canvas->restore();
5169 }
5170 }
5171 }
5172 }
5173 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5174}
5175
Cary Clark5c715182018-04-09 16:07:11 -04005176// skbug.com/7792
5177DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04005178 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
5179 SkPath path;
5180 for (size_t index = 0; index < count; ++index) {
5181 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
5182 }
Cary Clark31608c02018-04-12 10:29:46 -04005183 if (close) {
5184 path.close();
5185 }
Cary Clark8540e112018-04-11 14:30:27 -04005186 return path;
5187 };
5188 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
5189 SkPath path;
5190 for (size_t index = 0; index < count; ++index) {
5191 switch (verbs[index]) {
5192 case SkPath::kMove_Verb:
5193 path.moveTo(*points++);
5194 break;
5195 case SkPath::kLine_Verb:
5196 path.lineTo(*points++);
5197 break;
5198 case SkPath::kClose_Verb:
5199 path.close();
5200 break;
5201 default:
5202 SkASSERT(0);
5203 }
5204 }
5205 return path;
5206 };
Cary Clarkd4228472018-04-13 07:07:04 -04005207 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04005208 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04005209 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5210 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04005211 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04005212 SkRect compare;
Mike Reed92b33352019-08-24 19:39:13 -04005213 compare.setBounds(&points[1], SK_ARRAY_COUNT(points) - 1);
Cary Clark5c715182018-04-09 16:07:11 -04005214 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005215 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04005216 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
5217 path = makePath(points3, SK_ARRAY_COUNT(points3), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005218 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005219 // isolated from skbug.com/7792#c9
Cary Clark8540e112018-04-11 14:30:27 -04005220 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5221 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005222 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005223 compare.setBounds(&points9[1], SK_ARRAY_COUNT(points9) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005224 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005225 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04005226 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5227 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
5228 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
5229 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04005230 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005231 compare.setBounds(&points11[0], SK_ARRAY_COUNT(points11));
Cary Clark8540e112018-04-11 14:30:27 -04005232 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005233 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04005234 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
5235 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5236 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
5237 SkPath::kLine_Verb, SkPath::kClose_Verb };
5238 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
5239 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
5240 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04005241 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005242 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04005243 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5244 SkPath::kLine_Verb, SkPath::kMove_Verb };
5245 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
5246 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04005247 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005248 compare.setBounds(&points15[0], SK_ARRAY_COUNT(points15) - 1);
Cary Clark8540e112018-04-11 14:30:27 -04005249 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005250 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04005251 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
5252 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005253 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005254 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04005255 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5256 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5257 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
5258 SkPath::kLine_Verb, SkPath::kLine_Verb };
5259 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5260 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
5261 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04005262 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005263 // isolated from skbug.com/7792#c23
5264 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5265 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5266 SkPath::kLine_Verb, SkPath::kClose_Verb };
5267 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5268 {75, 150} };
5269 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04005270 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005271 compare.setBounds(&points23[0], SK_ARRAY_COUNT(points23));
Cary Clarkd4228472018-04-13 07:07:04 -04005272 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005273 // isolated from skbug.com/7792#c29
5274 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5275 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5276 SkPath::kClose_Verb };
5277 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5278 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005279 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005280 // isolated from skbug.com/7792#c31
5281 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5282 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5283 SkPath::kClose_Verb };
5284 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5285 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005286 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005287 compare.setBounds(&points31[0], 4);
Cary Clarka7651562018-04-17 09:30:14 -04005288 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005289 // isolated from skbug.com/7792#c36
5290 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5291 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5292 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5293 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005294 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005295 // isolated from skbug.com/7792#c39
5296 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005297 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005298 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5299 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005300 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005301 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005302 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5303 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5304 SkPath::kLine_Verb, SkPath::kClose_Verb };
5305 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5306 {32, 2} };
5307 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5308 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005309 compare.setBounds(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
Cary Clarkb120e922018-04-18 12:25:08 -04005310 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005311 // isolated from skbug.com/7792#c41
5312 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5313 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5314 SkPath::kClose_Verb };
5315 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5316 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5317 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005318 compare.setBounds(&points41[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005319 REPORTER_ASSERT(reporter, rect == compare);
5320 // isolated from skbug.com/7792#c53
5321 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5322 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5323 SkPath::kClose_Verb };
5324 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5325 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5326 REPORTER_ASSERT(reporter, path.isRect(&rect));
Mike Reed92b33352019-08-24 19:39:13 -04005327 compare.setBounds(&points53[1], 4);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005328 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005329}
Mike Reedee430912018-05-23 12:12:21 -04005330
Mike Reedc3d8a482018-09-12 10:08:40 -04005331// Be sure we can safely add ourselves
5332DEF_TEST(Path_self_add, reporter) {
5333 // The possible problem is that during path.add() we may have to grow the dst buffers as
5334 // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
5335 // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
5336 // garbage.
5337 //
5338 // The test runs though verious sized src paths, since its not defined publicly what the
5339 // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
5340 // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
5341 // an ASAN error w/o the fix to SkPath::addPath().
5342 //
5343 for (int count = 0; count < 10; ++count) {
5344 SkPath path;
5345 for (int add = 0; add < count; ++add) {
5346 // just add some stuff, so we have something to copy/append in addPath()
5347 path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
5348 }
5349 path.addPath(path, 1, 2);
5350 path.addPath(path, 3, 4);
5351 }
5352}
5353
Mike Kleinc0bd9f92019-04-23 12:05:21 -05005354#include "include/core/SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005355static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5356 // draw in different ways, looking for an assert
5357
5358 {
5359 SkPath path;
5360 path.addPoly(pts, 3, false);
5361 canvas->drawPath(path, SkPaint());
5362 }
5363
5364 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5365 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5366 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5367}
5368
Mike Reedee430912018-05-23 12:12:21 -04005369DEF_TEST(triangle_onehalf, reporter) {
5370 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5371
5372 const SkPoint pts[] = {
5373 { 0.499069244f, 9.63295173f },
5374 { 0.499402374f, 7.88207579f },
5375 { 10.2363272f, 0.49999997f }
5376 };
Mike Reed50595412018-05-30 16:32:33 -04005377 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005378}
Mike Reed50595412018-05-30 16:32:33 -04005379
5380DEF_TEST(triangle_big, reporter) {
5381 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5382
5383 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5384 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5385 // be conservative, so we invoke clipping if we get in this situation.
5386 // This test was added to demonstrate the need for this conservative bounds calc.
5387 // (found by a fuzzer)
5388 const SkPoint pts[] = {
5389 { 0.327190518f, -114.945152f },
5390 { -0.5f, 1.00003874f },
5391 { 0.666425824f, 4304.26172f },
5392 };
5393 draw_triangle(surface->getCanvas(), pts);
5394}
5395
Florin Malita3d413c52018-09-11 14:01:42 -04005396static void add_verbs(SkPath* path, int count) {
5397 path->moveTo(0, 0);
5398 for (int i = 0; i < count; ++i) {
5399 switch (i & 3) {
5400 case 0: path->lineTo(10, 20); break;
5401 case 1: path->quadTo(5, 6, 7, 8); break;
5402 case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
5403 case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
5404 }
5405 }
5406}
5407
5408// Make sure when we call shrinkToFit() that we always shrink (or stay the same)
5409// and that if we call twice, we stay the same.
5410DEF_TEST(Path_shrinkToFit, reporter) {
5411 size_t max_free = 0;
5412 for (int verbs = 0; verbs < 100; ++verbs) {
5413 SkPath unique_path, shared_path;
5414 add_verbs(&unique_path, verbs);
5415 add_verbs(&shared_path, verbs);
5416
5417 const SkPath copy = shared_path;
5418 REPORTER_ASSERT(reporter, shared_path == unique_path);
5419 REPORTER_ASSERT(reporter, shared_path == copy);
5420
5421#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005422 size_t before = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005423#endif
5424 unique_path.shrinkToFit();
5425 shared_path.shrinkToFit();
5426 REPORTER_ASSERT(reporter, shared_path == unique_path);
5427 REPORTER_ASSERT(reporter, shared_path == copy);
5428
5429#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005430 size_t after = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005431 REPORTER_ASSERT(reporter, before >= after);
5432 max_free = std::max(max_free, before - after);
5433
Florin Malita0e0f1a72018-09-11 16:16:19 -04005434 size_t after2 = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005435 REPORTER_ASSERT(reporter, after == after2);
5436#endif
5437 }
5438 if (false) {
5439 SkDebugf("max_free %zu\n", max_free);
5440 }
5441}
Mike Klein9de72162018-09-24 20:30:28 -04005442
5443DEF_TEST(Path_setLastPt, r) {
5444 // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
5445 SkPath p;
5446 p.moveTo(0,0);
5447 p.moveTo(20,01);
5448 p.moveTo(20,10);
5449 p.moveTo(20,61);
5450 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
5451
5452 p.setLastPt(30,01);
5453 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10)); // was {0,0, 20,61}
5454
5455 REPORTER_ASSERT(r, p.isValid());
Mike Klein9de72162018-09-24 20:30:28 -04005456}
Mike Reed6a388002018-10-16 13:13:09 -04005457
5458DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
5459 SkPath path;
5460
5461 path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
5462
5463 // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
5464 SkPath shallowPath = path;
5465
5466 // make sure we don't assert/crash on this.
5467 shallowPath.incReserve(0xffffffff);
5468}
5469
Mike Reed07105bb2018-12-01 14:07:49 -05005470////////////////////////////////////////////////////////////////////////////////////////////////
5471
5472/*
5473 * For speed, we tried to preserve useful/expensive attributes about paths,
5474 * - convexity, isrect, isoval, ...
5475 * Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
5476 * is axis-aligned (e.g. scale+translate)
5477 */
5478
5479struct Xforms {
5480 SkMatrix fIM, fTM, fSM, fRM;
5481
5482 Xforms() {
5483 fIM.reset();
5484 fTM.setTranslate(10, 20);
5485 fSM.setScale(2, 3);
5486 fRM.setRotate(30);
5487 }
5488};
5489
Mike Reed07105bb2018-12-01 14:07:49 -05005490static bool conditional_convex(const SkPath& path, bool is_convex) {
5491 SkPath::Convexity c = path.getConvexityOrUnknown();
5492 return is_convex ? (c == SkPath::kConvex_Convexity) : (c != SkPath::kConvex_Convexity);
5493}
Mike Reed07105bb2018-12-01 14:07:49 -05005494
5495// expect axis-aligned shape to survive assignment, identity and scale/translate matrices
5496template <typename ISA>
5497void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
5498 ISA isa_proc) {
5499 REPORTER_ASSERT(reporter, isa_proc(*path));
5500 // force the issue (computing convexity) the first time.
5501 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConvex_Convexity);
5502
5503 SkPath path2;
5504
5505 // a path's isa and convexity should survive assignment
5506 path2 = *path;
5507 REPORTER_ASSERT(reporter, isa_proc(path2));
5508 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5509
5510 // a path's isa and convexity should identity transform
5511 path->transform(x.fIM, &path2);
5512 path->transform(x.fIM);
5513 REPORTER_ASSERT(reporter, isa_proc(path2));
5514 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5515 REPORTER_ASSERT(reporter, isa_proc(*path));
5516 REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5517
5518 // a path's isa should survive translation, convexity depends on axis alignment
5519 path->transform(x.fTM, &path2);
5520 path->transform(x.fTM);
5521 REPORTER_ASSERT(reporter, isa_proc(path2));
5522 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005523 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5524 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005525
5526 // a path's isa should survive scaling, convexity depends on axis alignment
5527 path->transform(x.fSM, &path2);
5528 path->transform(x.fSM);
5529 REPORTER_ASSERT(reporter, isa_proc(path2));
5530 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005531 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5532 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005533
5534 // For security, post-rotation, we can't assume we're still convex. It might prove to be,
5535 // in fact, still be convex, be we can't have cached that setting, hence the call to
5536 // getConvexityOrUnknown() instead of getConvexity().
5537 path->transform(x.fRM, &path2);
5538 path->transform(x.fRM);
5539 if (isAxisAligned) {
5540 REPORTER_ASSERT(reporter, !isa_proc(path2));
5541 REPORTER_ASSERT(reporter, !isa_proc(*path));
5542 }
Mike Reed07105bb2018-12-01 14:07:49 -05005543 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() != SkPath::kConvex_Convexity);
5544 REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() != SkPath::kConvex_Convexity);
Mike Reed07105bb2018-12-01 14:07:49 -05005545}
5546
5547DEF_TEST(Path_survive_transform, r) {
5548 const Xforms x;
5549
5550 SkPath path;
5551 path.addRect({10, 10, 40, 50});
5552 survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
5553
5554 path.reset();
5555 path.addOval({10, 10, 40, 50});
5556 survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
5557
5558 path.reset();
5559 path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
5560 survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
5561
5562 // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
5563 path.reset();
5564 path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
5565 REPORTER_ASSERT(r, path.getConvexity() == SkPath::kConvex_Convexity);
5566 survive(&path, x, false, r, [](const SkPath& p) { return true; });
5567}
Mike Reedfb5f43b2018-12-18 12:17:57 -05005568
5569DEF_TEST(path_last_move_to_index, r) {
5570 // Make sure that copyPath is safe after the call to path.offset().
5571 // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
5572 // set it to path's value inside SkPath::transform()
5573
Mike Reeddf3d2252018-12-20 17:10:27 -05005574 const char text[] = "hello";
5575 constexpr size_t len = sizeof(text) - 1;
5576 SkGlyphID glyphs[len];
Mike Reedfb5f43b2018-12-18 12:17:57 -05005577
Mike Reeddf3d2252018-12-20 17:10:27 -05005578 SkFont font;
Ben Wagner51e15a62019-05-07 15:38:46 -04005579 font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
Mike Reeddf3d2252018-12-20 17:10:27 -05005580
5581 SkPath copyPath;
5582 SkFont().getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
5583 if (src) {
5584 ((SkPath*)ctx)->addPath(*src, mx);
5585 }
5586 }, &copyPath);
Mike Reedfb5f43b2018-12-18 12:17:57 -05005587
Kevin Lubick13486cb2019-01-04 09:49:47 -05005588 SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
Mike Reeddf3d2252018-12-20 17:10:27 -05005589 SkPath path;
Mike Reedfb5f43b2018-12-18 12:17:57 -05005590 path.addRoundRect({10, 10, 110, 110}, radii);
5591 path.offset(0, 5, &(copyPath)); // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
5592
5593 copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
5594}
Mike Reed5f152f02019-08-19 14:40:16 -04005595
5596static void test_edger(skiatest::Reporter* r,
5597 const std::initializer_list<SkPath::Verb>& in,
5598 const std::initializer_list<SkPath::Verb>& expected) {
5599 SkPath path;
5600 SkScalar x = 0, y = 0;
5601 for (auto v : in) {
5602 switch (v) {
5603 case SkPath::kMove_Verb: path.moveTo(x++, y++); break;
5604 case SkPath::kLine_Verb: path.lineTo(x++, y++); break;
5605 case SkPath::kClose_Verb: path.close(); break;
5606 default: SkASSERT(false);
5607 }
5608 }
5609
5610 SkPathEdgeIter iter(path);
5611 for (auto v : expected) {
5612 auto e = iter.next();
5613 REPORTER_ASSERT(r, e);
5614 REPORTER_ASSERT(r, SkPathEdgeIter::EdgeToVerb(e.fEdge) == v);
5615 }
5616 auto e = iter.next();
5617 REPORTER_ASSERT(r, !e);
5618}
5619
5620DEF_TEST(pathedger, r) {
5621 auto M = SkPath::kMove_Verb;
5622 auto L = SkPath::kLine_Verb;
5623 auto C = SkPath::kClose_Verb;
5624
5625 test_edger(r, { M }, {});
5626 test_edger(r, { M, M }, {});
5627 test_edger(r, { M, C }, {});
5628 test_edger(r, { M, M, C }, {});
5629 test_edger(r, { M, L }, { L, L });
5630 test_edger(r, { M, L, C }, { L, L });
5631 test_edger(r, { M, L, L }, { L, L, L });
5632 test_edger(r, { M, L, L, C }, { L, L, L });
5633
5634 test_edger(r, { M, L, L, M, L, L }, { L, L, L, L, L, L });
5635}