blob: beaafd0a3b0f88a883bfae493479f129cf32d015 [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;
293 clip.setRect(0, 0, 1255, 1925);
294
295 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
296 SkPath path;
297 procs[i](&path);
298
299 SkRegion rgn;
300 rgn.setPath(path, clip);
301 path.toggleInverseFillType();
302 rgn.setPath(path, clip);
303 }
304}
305
306#ifdef SK_BUILD_FOR_WIN
307 #define SUPPRESS_VISIBILITY_WARNING
308#else
309 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
310#endif
311
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;
799 arcRect.set(-rx, -ry, rx, ry);
800 switch (startAngle) {
801 case 0:
802 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
803 break;
804 case 90:
805 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
806 break;
807 case 180:
808 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
809 break;
810 case 270:
811 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
812 break;
813 default:
814 break;
815 }
816
817 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
818}
819
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());
894 r.set(0, 0, inf, negInf);
895 REPORTER_ASSERT(reporter, !r.isFinite());
896 r.set(0, 0, nan, 0);
897 REPORTER_ASSERT(reporter, !r.isFinite());
898
899 SkPoint pts[] = {
900 { 0, 0 },
901 { SK_Scalar1, 0 },
902 { 0, SK_Scalar1 },
903 };
904
905 bool isFine = r.setBoundsCheck(pts, 3);
906 REPORTER_ASSERT(reporter, isFine);
907 REPORTER_ASSERT(reporter, !r.isEmpty());
908
909 pts[1].set(inf, 0);
910 isFine = r.setBoundsCheck(pts, 3);
911 REPORTER_ASSERT(reporter, !isFine);
912 REPORTER_ASSERT(reporter, r.isEmpty());
913
914 pts[1].set(nan, 0);
915 isFine = r.setBoundsCheck(pts, 3);
916 REPORTER_ASSERT(reporter, !isFine);
917 REPORTER_ASSERT(reporter, r.isEmpty());
918}
919
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();
1132 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1133 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
1134 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1135
1136 path.reset();
1137 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1138 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
1139 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1140
1141 // triangle with one point really far from the origin.
1142 path.reset();
1143 // the first point is roughly 1.05e10, 1.05e10
1144 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1145 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1146 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1147 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1148
1149 path.reset();
1150 path.conicTo(20, 0, 20, 20, 0.5f);
1151 path.close();
1152 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1153
1154 path.reset();
1155 path.lineTo(1, 1e7f);
1156 path.lineTo(1e7f, 2e7f);
1157 path.close();
1158 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
1159 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1160}
1161
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];
1329 while (SkPath::kMove_Verb == iter.next(pts, false, false)) {
1330 ++initialMoves;
1331 }
1332 if (initialMoves > 0) {
1333 std::vector<SkPoint> points;
1334 points.resize(path.getPoints(nullptr, 0));
1335 (void) path.getPoints(&points.front(), points.size());
1336 int skip = initialMoves - 1;
1337 bool isConvex = SkPathPriv::IsConvex(&points.front() + skip, points.size() - skip);
1338 REPORTER_ASSERT(reporter, isConvex == (SkPath::kConvex_Convexity == expected));
1339 }
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);
1509}
1510
Cary Clarkc9b7c722018-12-12 14:50:23 -05001511static void test_convexity_doubleback(skiatest::Reporter* reporter) {
1512 SkPath doubleback;
1513 doubleback.lineTo(1, 1);
1514 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1515 doubleback.lineTo(2, 2);
Brian Osman309de792019-04-19 09:39:56 -04001516 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001517 doubleback.reset();
1518 doubleback.lineTo(1, 0);
1519 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1520 doubleback.lineTo(2, 0);
1521 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1522 doubleback.lineTo(1, 0);
1523 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1524 doubleback.reset();
1525 doubleback.quadTo(1, 1, 2, 2);
Brian Osman309de792019-04-19 09:39:56 -04001526 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001527 doubleback.reset();
1528 doubleback.quadTo(1, 0, 2, 0);
1529 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1530 doubleback.quadTo(1, 0, 0, 0);
1531 check_convexity(reporter, doubleback, SkPath::kConvex_Convexity);
1532}
1533
Yuqian Li3154a532017-09-06 13:33:30 -04001534static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1535 const SkRect& bounds) {
1536 REPORTER_ASSERT(reporter, p.isConvex());
1537 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1538
1539 SkPath p2(p);
1540 REPORTER_ASSERT(reporter, p2.isConvex());
1541 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1542
1543 SkPath other;
1544 other.swap(p2);
1545 REPORTER_ASSERT(reporter, other.isConvex());
1546 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1547}
1548
1549static void setFromString(SkPath* path, const char str[]) {
1550 bool first = true;
1551 while (str) {
1552 SkScalar x, y;
1553 str = SkParse::FindScalar(str, &x);
1554 if (nullptr == str) {
1555 break;
1556 }
1557 str = SkParse::FindScalar(str, &y);
1558 SkASSERT(str);
1559 if (first) {
1560 path->moveTo(x, y);
1561 first = false;
1562 } else {
1563 path->lineTo(x, y);
1564 }
1565 }
1566}
1567
1568static void test_convexity(skiatest::Reporter* reporter) {
1569 SkPath path;
1570
1571 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1572 path.addCircle(0, 0, SkIntToScalar(10));
1573 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1574 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
1575 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1576
1577 path.reset();
1578 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
1579 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1580 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1581
1582 path.reset();
1583 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
1584 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1585 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1586
1587 path.reset();
Cary Clarkc9b7c722018-12-12 14:50:23 -05001588 path.quadTo(100, 100, 50, 50); // This from GM:convexpaths
Brian Osman309de792019-04-19 09:39:56 -04001589 check_convexity(reporter, path, SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04001590
1591 static const struct {
1592 const char* fPathStr;
1593 SkPath::Convexity fExpectedConvexity;
1594 SkPathPriv::FirstDirection fExpectedDirection;
1595 } gRec[] = {
1596 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1597 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1598 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1599 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1600 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1601 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
1602 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1603 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
1604 };
1605
1606 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001607 path.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04001608 setFromString(&path, gRec[i].fPathStr);
1609 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1610 check_direction(reporter, path, gRec[i].fExpectedDirection);
1611 // check after setting the initial convex and direction
1612 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1613 SkPath copy(path);
1614 SkPathPriv::FirstDirection dir;
1615 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1616 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
1617 ^ foundDir);
1618 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1619 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1620 }
1621 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1622 check_direction(reporter, path, gRec[i].fExpectedDirection);
1623 }
1624
1625 static const SkPoint nonFinitePts[] = {
1626 { SK_ScalarInfinity, 0 },
1627 { 0, SK_ScalarInfinity },
1628 { SK_ScalarInfinity, SK_ScalarInfinity },
1629 { SK_ScalarNegativeInfinity, 0},
1630 { 0, SK_ScalarNegativeInfinity },
1631 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1632 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1633 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1634 { SK_ScalarNaN, 0 },
1635 { 0, SK_ScalarNaN },
1636 { SK_ScalarNaN, SK_ScalarNaN },
1637 };
1638
1639 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1640
Cary Clarkc9b7c722018-12-12 14:50:23 -05001641 static const SkPoint axisAlignedPts[] = {
Yuqian Li3154a532017-09-06 13:33:30 -04001642 { SK_ScalarMax, 0 },
1643 { 0, SK_ScalarMax },
Yuqian Li3154a532017-09-06 13:33:30 -04001644 { SK_ScalarMin, 0 },
1645 { 0, SK_ScalarMin },
Yuqian Li3154a532017-09-06 13:33:30 -04001646 };
1647
Cary Clarkc9b7c722018-12-12 14:50:23 -05001648 const size_t axisAlignedPtsCount = sizeof(axisAlignedPts) / sizeof(axisAlignedPts[0]);
Yuqian Li3154a532017-09-06 13:33:30 -04001649
Cary Clarkc9b7c722018-12-12 14:50:23 -05001650 for (int index = 0; index < (int) (13 * nonFinitePtsCount * axisAlignedPtsCount); ++index) {
Yuqian Li3154a532017-09-06 13:33:30 -04001651 int i = (int) (index % nonFinitePtsCount);
Cary Clarkc9b7c722018-12-12 14:50:23 -05001652 int f = (int) (index % axisAlignedPtsCount);
1653 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001654 path.reset();
1655 switch (index % 13) {
1656 case 0: path.lineTo(nonFinitePts[i]); break;
1657 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001658 case 2: path.quadTo(nonFinitePts[i], axisAlignedPts[f]); break;
1659 case 3: path.quadTo(axisAlignedPts[f], nonFinitePts[i]); break;
1660 case 4: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[f]); break;
1661 case 5: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], axisAlignedPts[f]); break;
1662 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], nonFinitePts[i]); break;
1663 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], axisAlignedPts[f]); break;
1664 case 8: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], nonFinitePts[i]); break;
1665 case 9: path.cubicTo(axisAlignedPts[f], nonFinitePts[i], nonFinitePts[i]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001666 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
Cary Clarkc9b7c722018-12-12 14:50:23 -05001667 case 11: path.cubicTo(nonFinitePts[i], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001668 case 12: path.moveTo(nonFinitePts[i]); break;
1669 }
1670 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1671 }
1672
Cary Clarkc9b7c722018-12-12 14:50:23 -05001673 for (int index = 0; index < (int) (11 * axisAlignedPtsCount); ++index) {
1674 int f = (int) (index % axisAlignedPtsCount);
1675 int g = (int) ((f + 1) % axisAlignedPtsCount);
Yuqian Li3154a532017-09-06 13:33:30 -04001676 path.reset();
1677 int curveSelect = index % 11;
1678 switch (curveSelect) {
Cary Clarkc9b7c722018-12-12 14:50:23 -05001679 case 0: path.moveTo(axisAlignedPts[f]); break;
1680 case 1: path.lineTo(axisAlignedPts[f]); break;
1681 case 2: path.quadTo(axisAlignedPts[f], axisAlignedPts[f]); break;
1682 case 3: path.quadTo(axisAlignedPts[f], axisAlignedPts[g]); break;
1683 case 4: path.quadTo(axisAlignedPts[g], axisAlignedPts[f]); break;
1684 case 5: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[f]); break;
1685 case 6: path.cubicTo(axisAlignedPts[f], axisAlignedPts[f], axisAlignedPts[g]); break;
1686 case 7: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[f]); break;
1687 case 8: path.cubicTo(axisAlignedPts[f], axisAlignedPts[g], axisAlignedPts[g]); break;
1688 case 9: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[f]); break;
1689 case 10: path.cubicTo(axisAlignedPts[g], axisAlignedPts[f], axisAlignedPts[g]); break;
Yuqian Li3154a532017-09-06 13:33:30 -04001690 }
Cary Clarkc9b7c722018-12-12 14:50:23 -05001691 if (curveSelect == 0 || curveSelect == 1 || curveSelect == 2 || curveSelect == 5) {
1692 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1693 } else {
1694 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1695 SkPath::Convexity c = copy.getConvexity();
1696 REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
1697 || SkPath::kConcave_Convexity == c);
1698 }
Yuqian Li3154a532017-09-06 13:33:30 -04001699 }
1700
Cary Clarkc9b7c722018-12-12 14:50:23 -05001701 static const SkPoint diagonalPts[] = {
1702 { SK_ScalarMax, SK_ScalarMax },
1703 { SK_ScalarMin, SK_ScalarMin },
1704 };
1705
1706 const size_t diagonalPtsCount = sizeof(diagonalPts) / sizeof(diagonalPts[0]);
1707
1708 for (int index = 0; index < (int) (7 * diagonalPtsCount); ++index) {
1709 int f = (int) (index % diagonalPtsCount);
1710 int g = (int) ((f + 1) % diagonalPtsCount);
1711 path.reset();
1712 int curveSelect = index % 11;
1713 switch (curveSelect) {
1714 case 0: path.moveTo(diagonalPts[f]); break;
1715 case 1: path.lineTo(diagonalPts[f]); break;
1716 case 2: path.quadTo(diagonalPts[f], diagonalPts[f]); break;
1717 case 3: path.quadTo(axisAlignedPts[f], diagonalPts[g]); break;
1718 case 4: path.quadTo(diagonalPts[g], axisAlignedPts[f]); break;
1719 case 5: path.cubicTo(diagonalPts[f], diagonalPts[f], diagonalPts[f]); break;
1720 case 6: path.cubicTo(diagonalPts[f], diagonalPts[f], axisAlignedPts[g]); break;
1721 case 7: path.cubicTo(diagonalPts[f], axisAlignedPts[g], diagonalPts[f]); break;
1722 case 8: path.cubicTo(axisAlignedPts[f], diagonalPts[g], diagonalPts[g]); break;
1723 case 9: path.cubicTo(diagonalPts[g], diagonalPts[f], axisAlignedPts[f]); break;
1724 case 10: path.cubicTo(diagonalPts[g], axisAlignedPts[f], diagonalPts[g]); break;
1725 }
1726 if (curveSelect == 0) {
1727 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1728 } else {
1729 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1730 SkPath::Convexity c = copy.getConvexity();
1731 REPORTER_ASSERT(reporter, SkPath::kUnknown_Convexity == c
1732 || SkPath::kConcave_Convexity == c);
1733 }
1734 }
1735
1736
Yuqian Li3154a532017-09-06 13:33:30 -04001737 path.reset();
1738 path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1739 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
1740 path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
1741 path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
1742 path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
1743 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1744 path.close();
1745 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1746
1747}
1748
1749static void test_isLine(skiatest::Reporter* reporter) {
1750 SkPath path;
1751 SkPoint pts[2];
1752 const SkScalar value = SkIntToScalar(5);
1753
1754 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1755
1756 // set some non-zero values
1757 pts[0].set(value, value);
1758 pts[1].set(value, value);
1759 REPORTER_ASSERT(reporter, !path.isLine(pts));
1760 // check that pts was untouched
1761 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1762 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1763
1764 const SkScalar moveX = SkIntToScalar(1);
1765 const SkScalar moveY = SkIntToScalar(2);
1766 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1767
1768 path.moveTo(moveX, moveY);
1769 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1770 REPORTER_ASSERT(reporter, !path.isLine(pts));
1771 // check that pts was untouched
1772 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1773 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1774
1775 const SkScalar lineX = SkIntToScalar(2);
1776 const SkScalar lineY = SkIntToScalar(2);
1777 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1778
1779 path.lineTo(lineX, lineY);
1780 REPORTER_ASSERT(reporter, path.isLine(nullptr));
1781
1782 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1783 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1784 REPORTER_ASSERT(reporter, path.isLine(pts));
1785 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1786 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1787
1788 path.lineTo(0, 0); // too many points/verbs
1789 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1790 REPORTER_ASSERT(reporter, !path.isLine(pts));
1791 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1792 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1793
1794 path.reset();
1795 path.quadTo(1, 1, 2, 2);
1796 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1797}
1798
1799static void test_conservativelyContains(skiatest::Reporter* reporter) {
1800 SkPath path;
1801
1802 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1803 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1804
1805 // A circle that bounds kBaseRect (with a significant amount of slop)
1806 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1807 circleR *= 1.75f / 2;
1808 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1809
1810 // round-rect radii
1811 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1812
1813 static const struct SUPPRESS_VISIBILITY_WARNING {
1814 SkRect fQueryRect;
1815 bool fInRect;
1816 bool fInCircle;
1817 bool fInRR;
1818 bool fInCubicRR;
1819 } kQueries[] = {
1820 {kBaseRect, true, true, false, false},
1821
1822 // rect well inside of kBaseRect
1823 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1824 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1825 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1826 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1827 true, true, true, true},
1828
1829 // rects with edges off by one from kBaseRect's edges
1830 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1831 kBaseRect.width(), kBaseRect.height() + 1),
1832 false, true, false, false},
1833 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1834 kBaseRect.width() + 1, kBaseRect.height()),
1835 false, true, false, false},
1836 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1837 kBaseRect.width() + 1, kBaseRect.height() + 1),
1838 false, true, false, false},
1839 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1840 kBaseRect.width(), kBaseRect.height()),
1841 false, true, false, false},
1842 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1843 kBaseRect.width(), kBaseRect.height()),
1844 false, true, false, false},
1845 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1846 kBaseRect.width() + 2, kBaseRect.height()),
1847 false, true, false, false},
1848 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1849 kBaseRect.width() + 2, kBaseRect.height()),
1850 false, true, false, false},
1851
1852 // zero-w/h rects at each corner of kBaseRect
1853 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1854 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1855 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1856 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1857
1858 // far away rect
1859 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1860 SkIntToScalar(10), SkIntToScalar(10)),
1861 false, false, false, false},
1862
1863 // very large rect containing kBaseRect
1864 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1865 kBaseRect.fTop - 5 * kBaseRect.height(),
1866 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1867 false, false, false, false},
1868
1869 // skinny rect that spans same y-range as kBaseRect
1870 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1871 SkIntToScalar(1), kBaseRect.height()),
1872 true, true, true, true},
1873
1874 // short rect that spans same x-range as kBaseRect
1875 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1876 true, true, true, true},
1877
1878 // skinny rect that spans slightly larger y-range than kBaseRect
1879 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1880 SkIntToScalar(1), kBaseRect.height() + 1),
1881 false, true, false, false},
1882
1883 // short rect that spans slightly larger x-range than kBaseRect
1884 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1885 kBaseRect.width() + 1, SkScalar(1)),
1886 false, true, false, false},
1887 };
1888
1889 for (int inv = 0; inv < 4; ++inv) {
1890 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1891 SkRect qRect = kQueries[q].fQueryRect;
1892 if (inv & 0x1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001893 using std::swap;
1894 swap(qRect.fLeft, qRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04001895 }
1896 if (inv & 0x2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001897 using std::swap;
1898 swap(qRect.fTop, qRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04001899 }
1900 for (int d = 0; d < 2; ++d) {
1901 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1902 path.reset();
1903 path.addRect(kBaseRect, dir);
1904 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1905 path.conservativelyContainsRect(qRect));
1906
1907 path.reset();
1908 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1909 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1910 path.conservativelyContainsRect(qRect));
1911
1912 path.reset();
1913 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1914 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1915 path.conservativelyContainsRect(qRect));
1916
1917 path.reset();
1918 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1919 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1920 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1921 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1922 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1923 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1924 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1925 path.close();
1926 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1927 path.conservativelyContainsRect(qRect));
1928
1929 }
1930 // Slightly non-convex shape, shouldn't contain any rects.
1931 path.reset();
1932 path.moveTo(0, 0);
1933 path.lineTo(SkIntToScalar(50), 0.05f);
1934 path.lineTo(SkIntToScalar(100), 0);
1935 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1936 path.lineTo(0, SkIntToScalar(100));
1937 path.close();
1938 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1939 }
1940 }
1941
1942 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1943 path.reset();
1944 path.moveTo(0, 0);
1945 path.lineTo(SkIntToScalar(100), 0);
1946 path.lineTo(0, SkIntToScalar(100));
1947
1948 // inside, on along top edge
1949 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1950 SkIntToScalar(10),
1951 SkIntToScalar(10))));
1952 // above
1953 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1954 SkRect::MakeXYWH(SkIntToScalar(50),
1955 SkIntToScalar(-10),
1956 SkIntToScalar(10),
1957 SkIntToScalar(10))));
1958 // to the left
1959 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1960 SkIntToScalar(5),
1961 SkIntToScalar(5),
1962 SkIntToScalar(5))));
1963
1964 // outside the diagonal edge
1965 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1966 SkIntToScalar(200),
1967 SkIntToScalar(20),
1968 SkIntToScalar(5))));
1969
1970
1971 // Test that multiple move commands do not cause asserts.
Yuqian Li3154a532017-09-06 13:33:30 -04001972 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
Yuqian Li3154a532017-09-06 13:33:30 -04001973 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1974 SkIntToScalar(10),
1975 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001976
1977 // Same as above path and first test but with an extra moveTo.
1978 path.reset();
1979 path.moveTo(100, 100);
1980 path.moveTo(0, 0);
1981 path.lineTo(SkIntToScalar(100), 0);
1982 path.lineTo(0, SkIntToScalar(100));
Brian Osman205a1262017-09-18 13:13:48 +00001983 // Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
1984 // path non-convex.
1985 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1986 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1987 SkIntToScalar(10),
1988 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001989
1990 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
1991 // following the non-empty sub-path. Verifies that this does not trigger assertions.
1992 path.reset();
1993 path.moveTo(0, 0);
1994 path.lineTo(SkIntToScalar(100), 0);
1995 path.lineTo(0, SkIntToScalar(100));
1996 path.moveTo(100, 100);
1997
1998 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1999 SkIntToScalar(10),
2000 SkIntToScalar(10))));
2001
2002 // Test that multiple move commands do not cause asserts and that the function
2003 // is not confused by the multiple moves.
2004 path.reset();
2005 path.moveTo(0, 0);
2006 path.lineTo(SkIntToScalar(100), 0);
2007 path.lineTo(0, SkIntToScalar(100));
2008 path.moveTo(0, SkIntToScalar(200));
2009 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
2010 path.lineTo(0, SkIntToScalar(300));
2011
2012 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
2013 SkRect::MakeXYWH(SkIntToScalar(50), 0,
2014 SkIntToScalar(10),
2015 SkIntToScalar(10))));
2016
2017 path.reset();
2018 path.lineTo(100, 100);
2019 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
2020
2021 // An empty path should not contain any rectangle. It's questionable whether an empty path
2022 // contains an empty rectangle. However, since it is a conservative test it is ok to
2023 // return false.
2024 path.reset();
2025 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
2026 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
2027}
2028
2029static void test_isRect_open_close(skiatest::Reporter* reporter) {
2030 SkPath path;
2031 bool isClosed;
2032
2033 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
2034 path.close();
2035
2036 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
2037 REPORTER_ASSERT(reporter, isClosed);
2038}
2039
2040// Simple isRect test is inline TestPath, below.
2041// test_isRect provides more extensive testing.
2042static void test_isRect(skiatest::Reporter* reporter) {
2043 test_isRect_open_close(reporter);
2044
2045 // passing tests (all moveTo / lineTo...
2046 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2047 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2048 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2049 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2050 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
2051 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2052 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2053 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2054 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2055 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
2056 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
2057 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
2058 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
2059 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
2060 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
2061
2062 // failing tests
2063 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2064 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2065 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2066 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2067 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2068 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2069 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2070 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2071 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2072 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2073 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
2074
2075 // no close, but we should detect them as fillably the same as a rect
2076 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2077 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2078 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2079
2080 // like c2, but we double-back on ourselves
2081 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2082 // like c2, but we overshoot the start point
2083 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2084 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2085
2086 struct IsRectTest {
2087 SkPoint *fPoints;
2088 int fPointCount;
2089 bool fClose;
2090 bool fIsRect;
2091 } tests[] = {
2092 { r1, SK_ARRAY_COUNT(r1), true, true },
2093 { r2, SK_ARRAY_COUNT(r2), true, true },
2094 { r3, SK_ARRAY_COUNT(r3), true, true },
2095 { r4, SK_ARRAY_COUNT(r4), true, true },
2096 { r5, SK_ARRAY_COUNT(r5), true, true },
2097 { r6, SK_ARRAY_COUNT(r6), true, true },
2098 { r7, SK_ARRAY_COUNT(r7), true, true },
2099 { r8, SK_ARRAY_COUNT(r8), true, true },
2100 { r9, SK_ARRAY_COUNT(r9), true, true },
2101 { ra, SK_ARRAY_COUNT(ra), true, true },
2102 { rb, SK_ARRAY_COUNT(rb), true, true },
2103 { rc, SK_ARRAY_COUNT(rc), true, true },
2104 { rd, SK_ARRAY_COUNT(rd), true, true },
2105 { re, SK_ARRAY_COUNT(re), true, true },
2106 { rf, SK_ARRAY_COUNT(rf), true, true },
2107
2108 { f1, SK_ARRAY_COUNT(f1), true, false },
2109 { f2, SK_ARRAY_COUNT(f2), true, false },
2110 { f3, SK_ARRAY_COUNT(f3), true, false },
2111 { f4, SK_ARRAY_COUNT(f4), true, false },
2112 { f5, SK_ARRAY_COUNT(f5), true, false },
2113 { f6, SK_ARRAY_COUNT(f6), true, false },
2114 { f7, SK_ARRAY_COUNT(f7), true, false },
2115 { f8, SK_ARRAY_COUNT(f8), true, false },
2116 { f9, SK_ARRAY_COUNT(f9), true, false },
2117 { fa, SK_ARRAY_COUNT(fa), true, false },
2118 { fb, SK_ARRAY_COUNT(fb), true, false },
2119
2120 { c1, SK_ARRAY_COUNT(c1), false, true },
2121 { c2, SK_ARRAY_COUNT(c2), false, true },
2122 { c3, SK_ARRAY_COUNT(c3), false, true },
2123
2124 { d1, SK_ARRAY_COUNT(d1), false, false },
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002125 { d2, SK_ARRAY_COUNT(d2), false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002126 { d3, SK_ARRAY_COUNT(d3), false, false },
2127 };
2128
2129 const size_t testCount = SK_ARRAY_COUNT(tests);
2130 int index;
2131 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2132 SkPath path;
2133 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2134 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2135 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2136 }
2137 if (tests[testIndex].fClose) {
2138 path.close();
2139 }
2140 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2141
2142 if (tests[testIndex].fIsRect) {
2143 SkRect computed, expected;
2144 bool isClosed;
2145 SkPath::Direction direction;
2146 SkPathPriv::FirstDirection cheapDirection;
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002147 int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
2148 expected.set(tests[testIndex].fPoints, pointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002149 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
2150 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2151 REPORTER_ASSERT(reporter, expected == computed);
2152 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2153 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2154 } else {
2155 SkRect computed;
2156 computed.set(123, 456, 789, 1011);
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002157 for (auto c : {true, false})
2158 for (auto d : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) {
2159 bool isClosed = c;
2160 SkPath::Direction direction = d;
2161 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2162 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2163 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2164 REPORTER_ASSERT(reporter, isClosed == c);
2165 REPORTER_ASSERT(reporter, direction == d);
2166 }
Yuqian Li3154a532017-09-06 13:33:30 -04002167 }
2168 }
2169
2170 // fail, close then line
2171 SkPath path1;
2172 path1.moveTo(r1[0].fX, r1[0].fY);
2173 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2174 path1.lineTo(r1[index].fX, r1[index].fY);
2175 }
2176 path1.close();
2177 path1.lineTo(1, 0);
2178 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2179
2180 // fail, move in the middle
2181 path1.reset();
2182 path1.moveTo(r1[0].fX, r1[0].fY);
2183 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2184 if (index == 2) {
2185 path1.moveTo(1, .5f);
2186 }
2187 path1.lineTo(r1[index].fX, r1[index].fY);
2188 }
2189 path1.close();
2190 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2191
2192 // fail, move on the edge
2193 path1.reset();
2194 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2195 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2196 path1.lineTo(r1[index].fX, r1[index].fY);
2197 }
2198 path1.close();
2199 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2200
2201 // fail, quad
2202 path1.reset();
2203 path1.moveTo(r1[0].fX, r1[0].fY);
2204 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2205 if (index == 2) {
2206 path1.quadTo(1, .5f, 1, .5f);
2207 }
2208 path1.lineTo(r1[index].fX, r1[index].fY);
2209 }
2210 path1.close();
2211 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2212
2213 // fail, cubic
2214 path1.reset();
2215 path1.moveTo(r1[0].fX, r1[0].fY);
2216 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2217 if (index == 2) {
2218 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2219 }
2220 path1.lineTo(r1[index].fX, r1[index].fY);
2221 }
2222 path1.close();
2223 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2224}
2225
2226static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2227 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2228 SkRect r = SkRect::MakeEmpty();
2229 SkPath::Direction d = SkPath::kCCW_Direction;
2230 unsigned s = ~0U;
2231
2232 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2233 REPORTER_ASSERT(reporter, r == rect);
2234 REPORTER_ASSERT(reporter, d == dir);
2235 REPORTER_ASSERT(reporter, s == start);
2236}
2237
2238static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002239 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002240 SkRect r = SkRect::MakeEmpty();
2241 SkPath::Direction d = SkPath::kCCW_Direction;
2242 unsigned s = ~0U;
2243
2244 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2245 const SkRect emptyRect = SkRect::MakeEmpty();
2246 SkPath path;
2247 for (int start = 0; start < 4; ++start) {
2248 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2249 SkPath path;
2250 path.addRect(testRect, dir, start);
2251 check_simple_closed_rect(reporter, path, testRect, dir, start);
2252 path.close();
2253 check_simple_closed_rect(reporter, path, testRect, dir, start);
2254 SkPath path2 = path;
2255 path2.lineTo(10, 10);
2256 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2257 path2 = path;
2258 path2.moveTo(10, 10);
2259 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2260 path2 = path;
2261 path2.addRect(testRect, dir, start);
2262 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2263 // Make the path by hand, manually closing it.
2264 path2.reset();
2265 SkPath::RawIter iter(path);
2266 SkPath::Verb v;
2267 SkPoint verbPts[4];
2268 SkPoint firstPt = {0.f, 0.f};
2269 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2270 switch(v) {
2271 case SkPath::kMove_Verb:
2272 firstPt = verbPts[0];
2273 path2.moveTo(verbPts[0]);
2274 break;
2275 case SkPath::kLine_Verb:
2276 path2.lineTo(verbPts[1]);
2277 break;
2278 default:
2279 break;
2280 }
2281 }
2282 // We haven't closed it yet...
2283 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2284 // ... now we do and test again.
2285 path2.lineTo(firstPt);
2286 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2287 // A redundant close shouldn't cause a failure.
2288 path2.close();
2289 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2290 // Degenerate point and line rects are not allowed
2291 path2.reset();
2292 path2.addRect(emptyRect, dir, start);
2293 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2294 SkRect degenRect = testRect;
2295 degenRect.fLeft = degenRect.fRight;
2296 path2.reset();
2297 path2.addRect(degenRect, dir, start);
2298 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2299 degenRect = testRect;
2300 degenRect.fTop = degenRect.fBottom;
2301 path2.reset();
2302 path2.addRect(degenRect, dir, start);
2303 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2304 // An inverted rect makes a rect path, but changes the winding dir and start point.
2305 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2306 ? SkPath::kCCW_Direction
2307 : SkPath::kCW_Direction;
2308 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2309 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2310 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002311 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002312 path2.reset();
2313 path2.addRect(swapRect, dir, start);
2314 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2315 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002316 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002317 path2.reset();
2318 path2.addRect(swapRect, dir, start);
2319 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2320 }
2321 }
2322 // down, up, left, close
2323 path.reset();
2324 path.moveTo(1, 1);
2325 path.lineTo(1, 2);
2326 path.lineTo(1, 1);
2327 path.lineTo(0, 1);
2328 SkRect rect;
2329 SkPath::Direction dir;
2330 unsigned start;
2331 path.close();
2332 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2333 // right, left, up, close
2334 path.reset();
2335 path.moveTo(1, 1);
2336 path.lineTo(2, 1);
2337 path.lineTo(1, 1);
2338 path.lineTo(1, 0);
2339 path.close();
2340 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2341 // parallelogram with horizontal edges
2342 path.reset();
2343 path.moveTo(1, 0);
2344 path.lineTo(3, 0);
2345 path.lineTo(2, 1);
2346 path.lineTo(0, 1);
2347 path.close();
2348 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2349 // parallelogram with vertical edges
2350 path.reset();
2351 path.moveTo(0, 1);
2352 path.lineTo(0, 3);
2353 path.lineTo(1, 2);
2354 path.lineTo(1, 0);
2355 path.close();
2356 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2357
2358}
2359
2360static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2361 // passing tests (all moveTo / lineTo...
2362 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2363 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2364 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2365 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2366 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2367 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2368 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2369 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2370 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2371 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2372 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2373 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2374 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2375 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2376
2377 // failing tests
2378 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2379 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2380 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2381 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2382 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2383 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2384 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2385 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2386
2387 // success, no close is OK
2388 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2389 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2390
2391 struct IsNestedRectTest {
2392 SkPoint *fPoints;
2393 int fPointCount;
2394 SkPathPriv::FirstDirection fDirection;
2395 bool fClose;
2396 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2397 } tests[] = {
2398 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2399 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2400 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2401 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2402 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2403 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2404 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2405 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2406 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2407 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2408 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2409 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2410 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2411 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
2412
2413 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2414 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2415 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2416 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2417 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2418 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2419 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2420 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2421
2422 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2423 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2424 };
2425
2426 const size_t testCount = SK_ARRAY_COUNT(tests);
2427 int index;
2428 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2429 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2430 SkPath path;
2431 if (rectFirst) {
2432 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2433 }
2434 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2435 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2436 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2437 }
2438 if (tests[testIndex].fClose) {
2439 path.close();
2440 }
2441 if (!rectFirst) {
2442 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2443 }
2444 REPORTER_ASSERT(reporter,
2445 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
2446 if (tests[testIndex].fIsNestedRect) {
2447 SkRect expected[2], computed[2];
2448 SkPathPriv::FirstDirection expectedDirs[2];
2449 SkPath::Direction computedDirs[2];
2450 SkRect testBounds;
2451 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
2452 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2453 expected[1] = testBounds;
2454 if (rectFirst) {
2455 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2456 } else {
2457 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2458 }
2459 expectedDirs[1] = tests[testIndex].fDirection;
2460 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
2461 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2462 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2463 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2464 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2465 }
2466 }
2467
2468 // fail, close then line
2469 SkPath path1;
2470 if (rectFirst) {
2471 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2472 }
2473 path1.moveTo(r1[0].fX, r1[0].fY);
2474 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2475 path1.lineTo(r1[index].fX, r1[index].fY);
2476 }
2477 path1.close();
2478 path1.lineTo(1, 0);
2479 if (!rectFirst) {
2480 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2481 }
2482 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2483
2484 // fail, move in the middle
2485 path1.reset();
2486 if (rectFirst) {
2487 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2488 }
2489 path1.moveTo(r1[0].fX, r1[0].fY);
2490 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2491 if (index == 2) {
2492 path1.moveTo(1, .5f);
2493 }
2494 path1.lineTo(r1[index].fX, r1[index].fY);
2495 }
2496 path1.close();
2497 if (!rectFirst) {
2498 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2499 }
2500 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2501
2502 // fail, move on the edge
2503 path1.reset();
2504 if (rectFirst) {
2505 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2506 }
2507 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2508 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2509 path1.lineTo(r1[index].fX, r1[index].fY);
2510 }
2511 path1.close();
2512 if (!rectFirst) {
2513 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2514 }
2515 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2516
2517 // fail, quad
2518 path1.reset();
2519 if (rectFirst) {
2520 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2521 }
2522 path1.moveTo(r1[0].fX, r1[0].fY);
2523 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2524 if (index == 2) {
2525 path1.quadTo(1, .5f, 1, .5f);
2526 }
2527 path1.lineTo(r1[index].fX, r1[index].fY);
2528 }
2529 path1.close();
2530 if (!rectFirst) {
2531 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2532 }
2533 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2534
2535 // fail, cubic
2536 path1.reset();
2537 if (rectFirst) {
2538 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2539 }
2540 path1.moveTo(r1[0].fX, r1[0].fY);
2541 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2542 if (index == 2) {
2543 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2544 }
2545 path1.lineTo(r1[index].fX, r1[index].fY);
2546 }
2547 path1.close();
2548 if (!rectFirst) {
2549 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2550 }
2551 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2552
2553 // fail, not nested
2554 path1.reset();
2555 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2556 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
2557 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2558 }
2559
2560 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2561 SkPath path;
2562 path.moveTo(0, 0);
2563 path.lineTo(10, 0);
2564 path.lineTo(10, 10);
2565 path.lineTo(0, 10);
2566 path.lineTo(0, 0);
2567 path.moveTo(1, 1);
2568 path.lineTo(9, 1);
2569 path.lineTo(9, 9);
2570 path.lineTo(1, 9);
2571 path.lineTo(1, 1);
2572 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
2573
2574 // pass, stroke rect
2575 SkPath src, dst;
2576 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2577 SkPaint strokePaint;
2578 strokePaint.setStyle(SkPaint::kStroke_Style);
2579 strokePaint.setStrokeWidth(2);
2580 strokePaint.getFillPath(src, &dst);
2581 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
2582}
2583
2584static void write_and_read_back(skiatest::Reporter* reporter,
2585 const SkPath& p) {
2586 SkWriter32 writer;
2587 writer.writePath(p);
2588 size_t size = writer.bytesWritten();
2589 SkAutoMalloc storage(size);
2590 writer.flatten(storage.get());
2591 SkReader32 reader(storage.get(), size);
2592
2593 SkPath readBack;
2594 REPORTER_ASSERT(reporter, readBack != p);
2595 reader.readPath(&readBack);
2596 REPORTER_ASSERT(reporter, readBack == p);
2597
2598 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
2599 p.getConvexityOrUnknown());
2600
2601 SkRect oval0, oval1;
2602 SkPath::Direction dir0, dir1;
2603 unsigned start0, start1;
2604 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002605 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2606 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002607 REPORTER_ASSERT(reporter, oval0 == oval1);
2608 REPORTER_ASSERT(reporter, dir0 == dir1);
2609 REPORTER_ASSERT(reporter, start0 == start1);
2610 }
2611 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2612 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002613 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2614 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002615 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2616 REPORTER_ASSERT(reporter, dir0 == dir1);
2617 REPORTER_ASSERT(reporter, start0 == start1);
2618 }
2619 const SkRect& origBounds = p.getBounds();
2620 const SkRect& readBackBounds = readBack.getBounds();
2621
2622 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2623}
2624
Yuqian Li3154a532017-09-06 13:33:30 -04002625static void test_flattening(skiatest::Reporter* reporter) {
2626 SkPath p;
2627
2628 static const SkPoint pts[] = {
2629 { 0, 0 },
2630 { SkIntToScalar(10), SkIntToScalar(10) },
2631 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2632 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2633 };
2634 p.moveTo(pts[0]);
2635 p.lineTo(pts[1]);
2636 p.quadTo(pts[2], pts[3]);
2637 p.cubicTo(pts[4], pts[5], pts[6]);
2638
2639 write_and_read_back(reporter, p);
2640
2641 // create a buffer that should be much larger than the path so we don't
2642 // kill our stack if writer goes too far.
2643 char buffer[1024];
2644 size_t size1 = p.writeToMemory(nullptr);
2645 size_t size2 = p.writeToMemory(buffer);
2646 REPORTER_ASSERT(reporter, size1 == size2);
2647
2648 SkPath p2;
2649 size_t size3 = p2.readFromMemory(buffer, 1024);
2650 REPORTER_ASSERT(reporter, size1 == size3);
2651 REPORTER_ASSERT(reporter, p == p2);
2652
2653 size3 = p2.readFromMemory(buffer, 0);
2654 REPORTER_ASSERT(reporter, !size3);
2655
2656 SkPath tooShort;
2657 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2658 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2659
2660 char buffer2[1024];
2661 size3 = p2.writeToMemory(buffer2);
2662 REPORTER_ASSERT(reporter, size1 == size3);
2663 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2664
2665 // test persistence of the oval flag & convexity
2666 {
2667 SkPath oval;
2668 SkRect rect = SkRect::MakeWH(10, 10);
2669 oval.addOval(rect);
2670
2671 write_and_read_back(reporter, oval);
2672 }
Yuqian Li3154a532017-09-06 13:33:30 -04002673}
2674
2675static void test_transform(skiatest::Reporter* reporter) {
2676 SkPath p;
2677
2678#define CONIC_PERSPECTIVE_BUG_FIXED 0
2679 static const SkPoint pts[] = {
2680 { 0, 0 }, // move
2681 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2682 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2683 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2684#if CONIC_PERSPECTIVE_BUG_FIXED
2685 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2686#endif
2687 };
2688 const int kPtCount = SK_ARRAY_COUNT(pts);
2689
2690 p.moveTo(pts[0]);
2691 p.lineTo(pts[1]);
2692 p.quadTo(pts[2], pts[3]);
2693 p.cubicTo(pts[4], pts[5], pts[6]);
2694#if CONIC_PERSPECTIVE_BUG_FIXED
2695 p.conicTo(pts[4], pts[5], 0.5f);
2696#endif
2697 p.close();
2698
2699 {
2700 SkMatrix matrix;
2701 matrix.reset();
2702 SkPath p1;
2703 p.transform(matrix, &p1);
2704 REPORTER_ASSERT(reporter, p == p1);
2705 }
2706
2707
2708 {
2709 SkMatrix matrix;
2710 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2711
2712 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2713
2714 p.transform(matrix, &p1);
2715 SkPoint pts1[kPtCount];
2716 int count = p1.getPoints(pts1, kPtCount);
2717 REPORTER_ASSERT(reporter, kPtCount == count);
2718 for (int i = 0; i < count; ++i) {
2719 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2720 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2721 }
2722 }
2723
2724 {
2725 SkMatrix matrix;
2726 matrix.reset();
2727 matrix.setPerspX(4);
2728
2729 SkPath p1;
2730 p1.moveTo(SkPoint::Make(0, 0));
2731
2732 p.transform(matrix, &p1);
2733 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2734 p1.transform(matrix, nullptr);
2735 SkRect pBounds = p.getBounds();
2736 SkRect p1Bounds = p1.getBounds();
2737 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2738 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2739 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2740 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2741 }
2742
2743 p.reset();
2744 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
2745
2746 {
2747 SkMatrix matrix;
2748 matrix.reset();
2749 SkPath p1;
2750 p1.moveTo(SkPoint::Make(0, 0));
2751
2752 p.transform(matrix, &p1);
2753 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2754 }
2755
2756
2757 {
2758 SkMatrix matrix;
2759 matrix.reset();
2760 matrix.setScaleX(-1);
2761 SkPath p1;
2762 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2763
2764 p.transform(matrix, &p1);
2765 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2766 }
2767
2768 {
2769 SkMatrix matrix;
2770 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2771 SkPath p1;
2772 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2773
2774 p.transform(matrix, &p1);
2775 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2776 }
Brian Osmandbfcd922019-03-07 16:50:30 -05002777
2778 {
2779 SkPath p1;
2780 p1.addRect({ 10, 20, 30, 40 });
2781 SkPath p2;
2782 p2.addRect({ 10, 20, 30, 40 });
2783 uint32_t id1 = p1.getGenerationID();
2784 uint32_t id2 = p2.getGenerationID();
2785 SkMatrix matrix;
2786 matrix.setScale(2, 2);
2787 p1.transform(matrix, &p2);
2788 p1.transform(matrix);
2789 REPORTER_ASSERT(reporter, id1 != p1.getGenerationID());
2790 REPORTER_ASSERT(reporter, id2 != p2.getGenerationID());
2791 }
Yuqian Li3154a532017-09-06 13:33:30 -04002792}
2793
2794static void test_zero_length_paths(skiatest::Reporter* reporter) {
2795 SkPath p;
2796 uint8_t verbs[32];
2797
2798 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2799 const char* testPath;
2800 const size_t numResultPts;
2801 const SkRect resultBound;
2802 const SkPath::Verb* resultVerbs;
2803 const size_t numResultVerbs;
2804 };
2805
2806 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2807 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2808 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2809 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2810 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2811 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2812 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2813 static const SkPath::Verb resultVerbs8[] = {
2814 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2815 };
2816 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2817 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2818 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2819 static const SkPath::Verb resultVerbs12[] = {
2820 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2821 };
2822 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2823 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2824 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2825 static const SkPath::Verb resultVerbs16[] = {
2826 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2827 };
2828 static const struct zeroPathTestData gZeroLengthTests[] = {
2829 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2830 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2831 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2832 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2833 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2834 { "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) },
2835 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2836 { "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) },
2837 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2838 { "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) },
2839 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2840 { "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) },
2841 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2842 { "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,
2843 SK_ARRAY_COUNT(resultVerbs14)
2844 },
2845 { "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) },
2846 { "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,
2847 SK_ARRAY_COUNT(resultVerbs16)
2848 }
2849 };
2850
2851 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2852 p.reset();
2853 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2854 REPORTER_ASSERT(reporter, valid);
2855 REPORTER_ASSERT(reporter, !p.isEmpty());
2856 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2857 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2858 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2859 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2860 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2861 }
2862 }
2863}
2864
2865struct SegmentInfo {
2866 SkPath fPath;
2867 int fPointCount;
2868};
2869
2870#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2871
2872static void test_segment_masks(skiatest::Reporter* reporter) {
2873 SkPath p, p2;
2874
2875 p.moveTo(0, 0);
2876 p.quadTo(100, 100, 200, 200);
2877 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2878 REPORTER_ASSERT(reporter, !p.isEmpty());
2879 p2 = p;
2880 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2881 p.cubicTo(100, 100, 200, 200, 300, 300);
2882 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2883 REPORTER_ASSERT(reporter, !p.isEmpty());
2884 p2 = p;
2885 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2886
2887 p.reset();
2888 p.moveTo(0, 0);
2889 p.cubicTo(100, 100, 200, 200, 300, 300);
2890 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2891 p2 = p;
2892 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2893
2894 REPORTER_ASSERT(reporter, !p.isEmpty());
2895}
2896
2897static void test_iter(skiatest::Reporter* reporter) {
2898 SkPath p;
2899 SkPoint pts[4];
2900
2901 // Test an iterator with no path
2902 SkPath::Iter noPathIter;
2903 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2904
2905 // Test that setting an empty path works
2906 noPathIter.setPath(p, false);
2907 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2908
2909 // Test that close path makes no difference for an empty path
2910 noPathIter.setPath(p, true);
2911 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2912
2913 // Test an iterator with an initial empty path
2914 SkPath::Iter iter(p, false);
2915 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2916
2917 // Test that close path makes no difference
2918 iter.setPath(p, true);
2919 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2920
2921
2922 struct iterTestData {
2923 const char* testPath;
2924 const bool forceClose;
2925 const bool consumeDegenerates;
2926 const size_t* numResultPtsPerVerb;
2927 const SkPoint* resultPts;
2928 const SkPath::Verb* resultVerbs;
2929 const size_t numResultVerbs;
2930 };
2931
2932 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2933 static const SkPath::Verb resultVerbs2[] = {
2934 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2935 };
2936 static const SkPath::Verb resultVerbs3[] = {
2937 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2938 };
2939 static const SkPath::Verb resultVerbs4[] = {
2940 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2941 };
2942 static const SkPath::Verb resultVerbs5[] = {
2943 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2944 };
2945 static const size_t resultPtsSizes1[] = { 0 };
2946 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2947 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2948 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2949 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
2950 static const SkPoint* resultPts1 = nullptr;
2951 static const SkPoint resultPts2[] = {
2952 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2953 };
2954 static const SkPoint resultPts3[] = {
2955 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2956 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2957 };
2958 static const SkPoint resultPts4[] = {
2959 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2960 };
2961 static const SkPoint resultPts5[] = {
2962 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2963 };
2964 static const struct iterTestData gIterTests[] = {
2965 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2966 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2967 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2968 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2969 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2970 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2971 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2972 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2973 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2974 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2975 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2976 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2977 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
2978 };
2979
2980 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2981 p.reset();
2982 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2983 REPORTER_ASSERT(reporter, valid);
2984 iter.setPath(p, gIterTests[i].forceClose);
2985 int j = 0, l = 0;
2986 do {
2987 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2988 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2989 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2990 }
2991 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2992 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2993 }
2994
2995 p.reset();
2996 iter.setPath(p, false);
2997 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2998 p.lineTo(1, 1);
2999 p.close();
3000 iter.setPath(p, false);
3001 REPORTER_ASSERT(reporter, iter.isClosedContour());
3002 p.reset();
3003 iter.setPath(p, true);
3004 REPORTER_ASSERT(reporter, !iter.isClosedContour());
3005 p.lineTo(1, 1);
3006 iter.setPath(p, true);
3007 REPORTER_ASSERT(reporter, iter.isClosedContour());
3008 p.moveTo(0, 0);
3009 p.lineTo(2, 2);
3010 iter.setPath(p, false);
3011 REPORTER_ASSERT(reporter, !iter.isClosedContour());
3012
3013 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
3014 // check to see if the result is correct.
3015 for (int setNaN = 0; setNaN < 4; ++setNaN) {
3016 p.reset();
3017 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
3018 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
3019 iter.setPath(p, true);
3020 iter.next(pts, false);
3021 iter.next(pts, false);
3022 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
3023 }
3024
3025 p.reset();
3026 p.quadTo(0, 0, 0, 0);
3027 iter.setPath(p, false);
3028 iter.next(pts, false);
3029 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
3030 iter.setPath(p, false);
3031 iter.next(pts, false);
3032 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3033
3034 p.reset();
3035 p.conicTo(0, 0, 0, 0, 0.5f);
3036 iter.setPath(p, false);
3037 iter.next(pts, false);
3038 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
3039 iter.setPath(p, false);
3040 iter.next(pts, false);
3041 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3042
3043 p.reset();
3044 p.cubicTo(0, 0, 0, 0, 0, 0);
3045 iter.setPath(p, false);
3046 iter.next(pts, false);
3047 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3048 iter.setPath(p, false);
3049 iter.next(pts, false);
3050 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3051
3052 p.moveTo(1, 1); // add a trailing moveto
3053 iter.setPath(p, false);
3054 iter.next(pts, false);
3055 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3056 iter.setPath(p, false);
3057 iter.next(pts, false);
3058 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3059
3060 // The GM degeneratesegments.cpp test is more extensive
3061
3062 // Test out mixed degenerate and non-degenerate geometry with Conics
3063 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3064 SkRect r = SkRect::MakeWH(100, 100);
3065 SkRRect rr;
3066 rr.setRectRadii(r, radii);
3067 p.reset();
3068 p.addRRect(rr);
3069 iter.setPath(p, false);
3070 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3071 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3072 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3073 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3074 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
3075}
3076
3077static void test_raw_iter(skiatest::Reporter* reporter) {
3078 SkPath p;
3079 SkPoint pts[4];
3080
3081 // Test an iterator with no path
3082 SkPath::RawIter noPathIter;
3083 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3084 // Test that setting an empty path works
3085 noPathIter.setPath(p);
3086 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3087
3088 // Test an iterator with an initial empty path
3089 SkPath::RawIter iter(p);
3090 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3091
3092 // Test that a move-only path returns the move.
3093 p.moveTo(SK_Scalar1, 0);
3094 iter.setPath(p);
3095 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3096 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3097 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3098 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3099
3100 // No matter how many moves we add, we should get them all back
3101 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3102 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3103 iter.setPath(p);
3104 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3105 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3106 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3107 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3108 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3109 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3110 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3111 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3112 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3113 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3114
3115 // Initial close is never ever stored
3116 p.reset();
3117 p.close();
3118 iter.setPath(p);
3119 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3120
3121 // Move/close sequences
3122 p.reset();
3123 p.close(); // Not stored, no purpose
3124 p.moveTo(SK_Scalar1, 0);
3125 p.close();
3126 p.close(); // Not stored, no purpose
3127 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3128 p.close();
3129 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3130 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3131 p.close();
3132 iter.setPath(p);
3133 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3134 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3135 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3136 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3137 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3138 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3139 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3140 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3141 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3142 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3143 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3144 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3145 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3146 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3147 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3148 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3149
3150 // Generate random paths and verify
3151 SkPoint randomPts[25];
3152 for (int i = 0; i < 5; ++i) {
3153 for (int j = 0; j < 5; ++j) {
3154 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3155 }
3156 }
3157
3158 // Max of 10 segments, max 3 points per segment
3159 SkRandom rand(9876543);
3160 SkPoint expectedPts[31]; // May have leading moveTo
3161 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
3162 SkPath::Verb nextVerb;
3163
3164 for (int i = 0; i < 500; ++i) {
3165 p.reset();
3166 bool lastWasClose = true;
3167 bool haveMoveTo = false;
3168 SkPoint lastMoveToPt = { 0, 0 };
3169 int numPoints = 0;
3170 int numVerbs = (rand.nextU() >> 16) % 10;
3171 int numIterVerbs = 0;
3172 for (int j = 0; j < numVerbs; ++j) {
3173 do {
3174 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3175 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
3176 switch (nextVerb) {
3177 case SkPath::kMove_Verb:
3178 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3179 p.moveTo(expectedPts[numPoints]);
3180 lastMoveToPt = expectedPts[numPoints];
3181 numPoints += 1;
3182 lastWasClose = false;
3183 haveMoveTo = true;
3184 break;
3185 case SkPath::kLine_Verb:
3186 if (!haveMoveTo) {
3187 expectedPts[numPoints++] = lastMoveToPt;
3188 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3189 haveMoveTo = true;
3190 }
3191 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3192 p.lineTo(expectedPts[numPoints]);
3193 numPoints += 1;
3194 lastWasClose = false;
3195 break;
3196 case SkPath::kQuad_Verb:
3197 if (!haveMoveTo) {
3198 expectedPts[numPoints++] = lastMoveToPt;
3199 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3200 haveMoveTo = true;
3201 }
3202 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3203 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3204 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3205 numPoints += 2;
3206 lastWasClose = false;
3207 break;
3208 case SkPath::kConic_Verb:
3209 if (!haveMoveTo) {
3210 expectedPts[numPoints++] = lastMoveToPt;
3211 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3212 haveMoveTo = true;
3213 }
3214 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3215 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3216 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3217 rand.nextUScalar1() * 4);
3218 numPoints += 2;
3219 lastWasClose = false;
3220 break;
3221 case SkPath::kCubic_Verb:
3222 if (!haveMoveTo) {
3223 expectedPts[numPoints++] = lastMoveToPt;
3224 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3225 haveMoveTo = true;
3226 }
3227 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3228 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3229 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3230 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3231 expectedPts[numPoints + 2]);
3232 numPoints += 3;
3233 lastWasClose = false;
3234 break;
3235 case SkPath::kClose_Verb:
3236 p.close();
3237 haveMoveTo = false;
3238 lastWasClose = true;
3239 break;
3240 default:
3241 SkDEBUGFAIL("unexpected verb");
3242 }
3243 expectedVerbs[numIterVerbs++] = nextVerb;
3244 }
3245
3246 iter.setPath(p);
3247 numVerbs = numIterVerbs;
3248 numIterVerbs = 0;
3249 int numIterPts = 0;
3250 SkPoint lastMoveTo;
3251 SkPoint lastPt;
3252 lastMoveTo.set(0, 0);
3253 lastPt.set(0, 0);
3254 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3255 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3256 numIterVerbs++;
3257 switch (nextVerb) {
3258 case SkPath::kMove_Verb:
3259 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3260 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3261 lastPt = lastMoveTo = pts[0];
3262 numIterPts += 1;
3263 break;
3264 case SkPath::kLine_Verb:
3265 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3266 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3267 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3268 lastPt = pts[1];
3269 numIterPts += 1;
3270 break;
3271 case SkPath::kQuad_Verb:
3272 case SkPath::kConic_Verb:
3273 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3274 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3275 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3276 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3277 lastPt = pts[2];
3278 numIterPts += 2;
3279 break;
3280 case SkPath::kCubic_Verb:
3281 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3282 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3283 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3284 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3285 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3286 lastPt = pts[3];
3287 numIterPts += 3;
3288 break;
3289 case SkPath::kClose_Verb:
3290 lastPt = lastMoveTo;
3291 break;
3292 default:
3293 SkDEBUGFAIL("unexpected verb");
3294 }
3295 }
3296 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3297 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3298 }
3299}
3300
3301static void check_for_circle(skiatest::Reporter* reporter,
3302 const SkPath& path,
3303 bool expectedCircle,
3304 SkPathPriv::FirstDirection expectedDir) {
3305 SkRect rect = SkRect::MakeEmpty();
3306 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
3307 SkPath::Direction isOvalDir;
3308 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003309 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003310 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3311 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3312 SkPath tmpPath;
3313 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3314 REPORTER_ASSERT(reporter, path == tmpPath);
3315 }
3316 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3317}
3318
3319static void test_circle_skew(skiatest::Reporter* reporter,
3320 const SkPath& path,
3321 SkPathPriv::FirstDirection dir) {
3322 SkPath tmp;
3323
3324 SkMatrix m;
3325 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3326 path.transform(m, &tmp);
3327 // this matrix reverses the direction.
3328 if (SkPathPriv::kCCW_FirstDirection == dir) {
3329 dir = SkPathPriv::kCW_FirstDirection;
3330 } else {
3331 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3332 dir = SkPathPriv::kCCW_FirstDirection;
3333 }
3334 check_for_circle(reporter, tmp, false, dir);
3335}
3336
3337static void test_circle_translate(skiatest::Reporter* reporter,
3338 const SkPath& path,
3339 SkPathPriv::FirstDirection dir) {
3340 SkPath tmp;
3341
3342 // translate at small offset
3343 SkMatrix m;
3344 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3345 path.transform(m, &tmp);
3346 check_for_circle(reporter, tmp, true, dir);
3347
3348 tmp.reset();
3349 m.reset();
3350
3351 // translate at a relatively big offset
3352 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3353 path.transform(m, &tmp);
3354 check_for_circle(reporter, tmp, true, dir);
3355}
3356
3357static void test_circle_rotate(skiatest::Reporter* reporter,
3358 const SkPath& path,
3359 SkPathPriv::FirstDirection dir) {
3360 for (int angle = 0; angle < 360; ++angle) {
3361 SkPath tmp;
3362 SkMatrix m;
3363 m.setRotate(SkIntToScalar(angle));
3364 path.transform(m, &tmp);
3365
3366 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3367 // degrees is not an oval anymore, this can be improved. we made this
3368 // for the simplicity of our implementation.
3369 if (angle % 90 == 0) {
3370 check_for_circle(reporter, tmp, true, dir);
3371 } else {
3372 check_for_circle(reporter, tmp, false, dir);
3373 }
3374 }
3375}
3376
3377static void test_circle_mirror_x(skiatest::Reporter* reporter,
3378 const SkPath& path,
3379 SkPathPriv::FirstDirection dir) {
3380 SkPath tmp;
3381 SkMatrix m;
3382 m.reset();
3383 m.setScaleX(-SK_Scalar1);
3384 path.transform(m, &tmp);
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 check_for_circle(reporter, tmp, true, dir);
3392}
3393
3394static void test_circle_mirror_y(skiatest::Reporter* reporter,
3395 const SkPath& path,
3396 SkPathPriv::FirstDirection dir) {
3397 SkPath tmp;
3398 SkMatrix m;
3399 m.reset();
3400 m.setScaleY(-SK_Scalar1);
3401 path.transform(m, &tmp);
3402
3403 if (SkPathPriv::kCW_FirstDirection == dir) {
3404 dir = SkPathPriv::kCCW_FirstDirection;
3405 } else {
3406 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3407 dir = SkPathPriv::kCW_FirstDirection;
3408 }
3409
3410 check_for_circle(reporter, tmp, true, dir);
3411}
3412
3413static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3414 const SkPath& path,
3415 SkPathPriv::FirstDirection dir) {
3416 SkPath tmp;
3417 SkMatrix m;
3418 m.reset();
3419 m.setScaleX(-SK_Scalar1);
3420 m.setScaleY(-SK_Scalar1);
3421 path.transform(m, &tmp);
3422
3423 check_for_circle(reporter, tmp, true, dir);
3424}
3425
3426static void test_circle_with_direction(skiatest::Reporter* reporter,
3427 SkPath::Direction inDir) {
3428 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3429 SkPath path;
3430
3431 // circle at origin
3432 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3433
3434 check_for_circle(reporter, path, true, dir);
3435 test_circle_rotate(reporter, path, dir);
3436 test_circle_translate(reporter, path, dir);
3437 test_circle_skew(reporter, path, dir);
3438 test_circle_mirror_x(reporter, path, dir);
3439 test_circle_mirror_y(reporter, path, dir);
3440 test_circle_mirror_xy(reporter, path, dir);
3441
3442 // circle at an offset at (10, 10)
3443 path.reset();
3444 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3445 SkIntToScalar(20), inDir);
3446
3447 check_for_circle(reporter, path, true, dir);
3448 test_circle_rotate(reporter, path, dir);
3449 test_circle_translate(reporter, path, dir);
3450 test_circle_skew(reporter, path, dir);
3451 test_circle_mirror_x(reporter, path, dir);
3452 test_circle_mirror_y(reporter, path, dir);
3453 test_circle_mirror_xy(reporter, path, dir);
3454
3455 // Try different starting points for the contour.
3456 for (unsigned start = 0; start < 4; ++start) {
3457 path.reset();
3458 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3459 test_circle_rotate(reporter, path, dir);
3460 test_circle_translate(reporter, path, dir);
3461 test_circle_skew(reporter, path, dir);
3462 test_circle_mirror_x(reporter, path, dir);
3463 test_circle_mirror_y(reporter, path, dir);
3464 test_circle_mirror_xy(reporter, path, dir);
3465 }
3466}
3467
3468static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3469 SkPath path;
3470 SkPath circle;
3471 SkPath rect;
3472 SkPath empty;
3473
3474 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3475 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
3476
3477 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3478 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3479 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3480
3481 SkMatrix translate;
3482 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3483
3484 // Although all the path concatenation related operations leave
3485 // the path a circle, most mark it as a non-circle for simplicity
3486
3487 // empty + circle (translate)
3488 path = empty;
3489 path.addPath(circle, translate);
3490 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3491
3492 // circle + empty (translate)
3493 path = circle;
3494 path.addPath(empty, translate);
3495
3496 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3497
3498 // test reverseAddPath
3499 path = circle;
3500 path.reverseAddPath(rect);
3501 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3502}
3503
3504static void test_circle(skiatest::Reporter* reporter) {
3505 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3506 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3507
3508 // multiple addCircle()
3509 SkPath path;
3510 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3511 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
3512 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3513
3514 // some extra lineTo() would make isOval() fail
3515 path.reset();
3516 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3517 path.lineTo(0, 0);
3518 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3519
3520 // not back to the original point
3521 path.reset();
3522 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3523 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3524 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3525
3526 test_circle_with_add_paths(reporter);
3527
3528 // test negative radius
3529 path.reset();
3530 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3531 REPORTER_ASSERT(reporter, path.isEmpty());
3532}
3533
3534static void test_oval(skiatest::Reporter* reporter) {
3535 SkRect rect;
3536 SkMatrix m;
3537 SkPath path;
3538 unsigned start = 0;
3539 SkPath::Direction dir = SkPath::kCCW_Direction;
3540
3541 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3542 path.addOval(rect);
3543
3544 // Defaults to dir = CW and start = 1
3545 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3546
3547 m.setRotate(SkIntToScalar(90));
3548 SkPath tmp;
3549 path.transform(m, &tmp);
3550 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3551 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003552 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003553 REPORTER_ASSERT(reporter, 2 == start);
3554 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3555
3556 m.reset();
3557 m.setRotate(SkIntToScalar(30));
3558 tmp.reset();
3559 path.transform(m, &tmp);
3560 // an oval rotated 30 degrees is not an oval anymore.
3561 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3562
3563 // since empty path being transformed.
3564 path.reset();
3565 tmp.reset();
3566 m.reset();
3567 path.transform(m, &tmp);
3568 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3569
3570 // empty path is not an oval
3571 tmp.reset();
3572 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3573
3574 // only has moveTo()s
3575 tmp.reset();
3576 tmp.moveTo(0, 0);
3577 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3578 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3579
3580 // mimic WebKit's calling convention,
3581 // call moveTo() first and then call addOval()
3582 path.reset();
3583 path.moveTo(0, 0);
3584 path.addOval(rect);
3585 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3586
3587 // copy path
3588 path.reset();
3589 tmp.reset();
3590 tmp.addOval(rect);
3591 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003592 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003593 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3594 REPORTER_ASSERT(reporter, 1 == start);
3595}
3596
3597static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3598 SkPath empty;
3599
3600 REPORTER_ASSERT(reporter, p.isEmpty());
3601 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3602 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3603 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3604 REPORTER_ASSERT(reporter, p.isConvex());
3605 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3606 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3607 REPORTER_ASSERT(reporter, p == empty);
3608 REPORTER_ASSERT(reporter, !(p != empty));
3609}
3610
3611static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3612 SkPath::Direction dir) {
3613 REPORTER_ASSERT(reporter, path->isConvex());
3614 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3615 path->setConvexity(SkPath::kUnknown_Convexity);
3616 REPORTER_ASSERT(reporter, path->isConvex());
3617 path->reset();
3618}
3619
3620static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3621 SkPath::Direction dir) {
3622 REPORTER_ASSERT(reporter, path->isConvex());
3623 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3624 path->setConvexity(SkPath::kUnknown_Convexity);
Brian Osman14ac42b2019-04-18 12:17:04 -04003625 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConcave_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003626 path->reset();
3627}
3628
3629static void test_rrect(skiatest::Reporter* reporter) {
3630 SkPath p;
3631 SkRRect rr;
3632 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3633 SkRect r = {10, 20, 30, 40};
3634 rr.setRectRadii(r, radii);
3635 p.addRRect(rr);
3636 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3637 p.addRRect(rr, SkPath::kCCW_Direction);
3638 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3639 p.addRoundRect(r, &radii[0].fX);
3640 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3641 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
3642 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3643 p.addRoundRect(r, radii[1].fX, radii[1].fY);
3644 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3645 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
3646 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3647 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3648 SkVector save = radii[i];
3649 radii[i].set(0, 0);
3650 rr.setRectRadii(r, radii);
3651 p.addRRect(rr);
3652 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3653 radii[i] = save;
3654 }
3655 p.addRoundRect(r, 0, 0);
3656 SkRect returnedRect;
3657 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3658 REPORTER_ASSERT(reporter, returnedRect == r);
3659 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3660 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3661 rr.setRectRadii(r, zeroRadii);
3662 p.addRRect(rr);
3663 bool closed;
3664 SkPath::Direction dir;
3665 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3666 REPORTER_ASSERT(reporter, closed);
3667 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3668 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3669 p.addRRect(rr, SkPath::kCW_Direction);
3670 p.addRRect(rr, SkPath::kCW_Direction);
3671 REPORTER_ASSERT(reporter, !p.isConvex());
3672 p.reset();
3673 p.addRRect(rr, SkPath::kCCW_Direction);
3674 p.addRRect(rr, SkPath::kCCW_Direction);
3675 REPORTER_ASSERT(reporter, !p.isConvex());
3676 p.reset();
3677 SkRect emptyR = {10, 20, 10, 30};
3678 rr.setRectRadii(emptyR, radii);
3679 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003680 // The round rect is "empty" in that it has no fill area. However,
3681 // the path isn't "empty" in that it should have verbs and points.
3682 REPORTER_ASSERT(reporter, !p.isEmpty());
3683 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003684 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3685 rr.setRectRadii(largeR, radii);
3686 p.addRRect(rr);
3687 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
3688
3689 // we check for non-finites
3690 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3691 rr.setRectRadii(infR, radii);
3692 REPORTER_ASSERT(reporter, rr.isEmpty());
3693
3694 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3695 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3696 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3697}
3698
3699static void test_arc(skiatest::Reporter* reporter) {
3700 SkPath p;
3701 SkRect emptyOval = {10, 20, 30, 20};
3702 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3703 p.addArc(emptyOval, 1, 2);
3704 REPORTER_ASSERT(reporter, p.isEmpty());
3705 p.reset();
3706 SkRect oval = {10, 20, 30, 40};
3707 p.addArc(oval, 1, 0);
3708 REPORTER_ASSERT(reporter, p.isEmpty());
3709 p.reset();
3710 SkPath cwOval;
3711 cwOval.addOval(oval);
3712 p.addArc(oval, 0, 360);
3713 REPORTER_ASSERT(reporter, p == cwOval);
3714 p.reset();
3715 SkPath ccwOval;
3716 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3717 p.addArc(oval, 0, -360);
3718 REPORTER_ASSERT(reporter, p == ccwOval);
3719 p.reset();
3720 p.addArc(oval, 1, 180);
Cary Clarkc9b7c722018-12-12 14:50:23 -05003721 // diagonal colinear points make arc convex
3722 // TODO: one way to keep it concave would be to introduce interpolated on curve points
3723 // between control points and computing the on curve point at scan conversion time
Brian Osman309de792019-04-19 09:39:56 -04003724 REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003725 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
3726 p.setConvexity(SkPath::kUnknown_Convexity);
Brian Osman309de792019-04-19 09:39:56 -04003727 REPORTER_ASSERT(reporter, p.getConvexity() == SkPath::kConvex_Convexity);
Yuqian Li3154a532017-09-06 13:33:30 -04003728}
3729
3730static inline SkScalar oval_start_index_to_angle(unsigned start) {
3731 switch (start) {
3732 case 0:
3733 return 270.f;
3734 case 1:
3735 return 0.f;
3736 case 2:
3737 return 90.f;
3738 case 3:
3739 return 180.f;
3740 default:
3741 return -1.f;
3742 }
3743}
3744
3745static inline SkScalar canonical_start_angle(float angle) {
3746 while (angle < 0.f) {
3747 angle += 360.f;
3748 }
3749 while (angle >= 360.f) {
3750 angle -= 360.f;
3751 }
3752 return angle;
3753}
3754
3755static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3756 const SkPath& path) {
3757 SkRect r = SkRect::MakeEmpty();
3758 SkPath::Direction d = SkPath::kCCW_Direction;
3759 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003760 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003761 REPORTER_ASSERT(reporter, isOval);
3762 SkPath recreatedPath;
3763 recreatedPath.addOval(r, d, s);
3764 REPORTER_ASSERT(reporter, path == recreatedPath);
3765 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3766 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3767}
3768
3769static void test_arc_ovals(skiatest::Reporter* reporter) {
3770 SkRect oval = SkRect::MakeWH(10, 20);
3771 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3772 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3773 SkPath path;
3774 path.addArc(oval, start, sweep);
3775 // SkPath's interfaces for inserting and extracting ovals only allow contours
3776 // to start at multiples of 90 degrees.
3777 if (std::fmod(start, 90.f) == 0) {
3778 check_oval_arc(reporter, start, sweep, path);
3779 } else {
3780 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3781 }
3782 }
3783 // Test start angles that are nearly at valid oval start angles.
3784 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3785 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3786 SkPath path;
3787 path.addArc(oval, start + delta, sweep);
3788 check_oval_arc(reporter, start, sweep, path);
3789 }
3790 }
3791 }
3792}
3793
3794static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3795 SkScalar x0, SkScalar y0) {
3796 SkPoint pts[4];
3797 SkPath::Verb v = iter->next(pts);
3798 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3799 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3800 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3801}
3802
3803static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3804 SkScalar x1, SkScalar y1) {
3805 SkPoint pts[4];
3806 SkPath::Verb v = iter->next(pts);
3807 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3808 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3809 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3810}
3811
3812static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3813 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3814 SkPoint pts[4];
3815 SkPath::Verb v = iter->next(pts);
3816 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3817 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3818 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3819 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3820 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3821}
3822
3823static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3824 SkPoint pts[4];
3825 SkPath::Verb v = iter->next(pts);
3826 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3827}
3828
3829static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3830 check_done(reporter, p, iter);
3831 p->reset();
3832}
3833
3834static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3835 SkScalar x0, SkScalar y0) {
3836 SkPath::RawIter iter(*p);
3837 check_move(reporter, &iter, x0, y0);
3838 check_done_and_reset(reporter, p, &iter);
3839}
3840
3841static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3842 SkScalar x1, SkScalar y1) {
3843 SkPath::RawIter iter(*p);
3844 check_move(reporter, &iter, 0, 0);
3845 check_line(reporter, &iter, x1, y1);
3846 check_done_and_reset(reporter, p, &iter);
3847}
3848
3849static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3850 SkScalar x1, SkScalar y1) {
3851 SkPath::RawIter iter(*p);
3852 check_move(reporter, &iter, 0, 0);
3853 check_line(reporter, &iter, x1, y1);
3854 check_done(reporter, p, &iter);
3855}
3856
3857static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3858 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3859 SkPath::RawIter iter(*p);
3860 check_move(reporter, &iter, 0, 0);
3861 check_line(reporter, &iter, x1, y1);
3862 check_line(reporter, &iter, x2, y2);
3863 check_done_and_reset(reporter, p, &iter);
3864}
3865
3866static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3867 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3868 SkPath::RawIter iter(*p);
3869 check_move(reporter, &iter, 0, 0);
3870 check_quad(reporter, &iter, x1, y1, x2, y2);
3871 check_done_and_reset(reporter, p, &iter);
3872}
3873
3874static bool nearly_equal(const SkRect& a, const SkRect& b) {
3875 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3876 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3877 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3878 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3879}
3880
3881static void test_arcTo(skiatest::Reporter* reporter) {
3882 SkPath p;
3883 p.arcTo(0, 0, 1, 2, 1);
3884 check_path_is_line_and_reset(reporter, &p, 0, 0);
3885 p.arcTo(1, 2, 1, 2, 1);
3886 check_path_is_line_and_reset(reporter, &p, 1, 2);
3887 p.arcTo(1, 2, 3, 4, 0);
3888 check_path_is_line_and_reset(reporter, &p, 1, 2);
3889 p.arcTo(1, 2, 0, 0, 1);
3890 check_path_is_line_and_reset(reporter, &p, 1, 2);
3891 p.arcTo(1, 0, 1, 1, 1);
3892 SkPoint pt;
3893 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3894 p.reset();
3895 p.arcTo(1, 0, 1, -1, 1);
3896 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3897 p.reset();
3898 SkRect oval = {1, 2, 3, 4};
3899 p.arcTo(oval, 0, 0, true);
3900 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3901 p.arcTo(oval, 0, 0, false);
3902 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3903 p.arcTo(oval, 360, 0, true);
3904 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3905 p.arcTo(oval, 360, 0, false);
3906 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3907
3908 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3909 p.arcTo(oval, 0, sweep, false);
3910 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3911 sweep += delta;
3912 delta /= 2;
3913 }
3914 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3915 p.arcTo(oval, 0, sweep, false);
3916 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3917 sweep -= delta;
3918 delta /= 2;
3919 }
3920 SkRect noOvalWidth = {1, 2, 0, 3};
3921 p.reset();
3922 p.arcTo(noOvalWidth, 0, 360, false);
3923 REPORTER_ASSERT(reporter, p.isEmpty());
3924
3925 SkRect noOvalHeight = {1, 2, 3, 1};
3926 p.reset();
3927 p.arcTo(noOvalHeight, 0, 360, false);
3928 REPORTER_ASSERT(reporter, p.isEmpty());
3929}
3930
3931static void test_addPath(skiatest::Reporter* reporter) {
3932 SkPath p, q;
3933 p.lineTo(1, 2);
3934 q.moveTo(4, 4);
3935 q.lineTo(7, 8);
3936 q.conicTo(8, 7, 6, 5, 0.5f);
3937 q.quadTo(6, 7, 8, 6);
3938 q.cubicTo(5, 6, 7, 8, 7, 5);
3939 q.close();
3940 p.addPath(q, -4, -4);
3941 SkRect expected = {0, 0, 4, 4};
3942 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3943 p.reset();
3944 p.reverseAddPath(q);
3945 SkRect reverseExpected = {4, 4, 8, 8};
3946 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3947}
3948
3949static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3950 SkPath p, q;
3951 if (explicitMoveTo) {
3952 p.moveTo(1, 1);
3953 }
3954 p.lineTo(1, 2);
3955 if (explicitMoveTo) {
3956 q.moveTo(2, 1);
3957 }
3958 q.lineTo(2, 2);
3959 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3960 uint8_t verbs[4];
3961 int verbcount = p.getVerbs(verbs, 4);
3962 REPORTER_ASSERT(reporter, verbcount == 4);
3963 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3964 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3965 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3966 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3967}
3968
3969static void test_extendClosedPath(skiatest::Reporter* reporter) {
3970 SkPath p, q;
3971 p.moveTo(1, 1);
3972 p.lineTo(1, 2);
3973 p.lineTo(2, 2);
3974 p.close();
3975 q.moveTo(2, 1);
3976 q.lineTo(2, 3);
3977 p.addPath(q, SkPath::kExtend_AddPathMode);
3978 uint8_t verbs[7];
3979 int verbcount = p.getVerbs(verbs, 7);
3980 REPORTER_ASSERT(reporter, verbcount == 7);
3981 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3982 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3983 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3984 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3985 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3986 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3987 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3988
3989 SkPoint pt;
3990 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3991 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3992 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3993}
3994
3995static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3996 SkPath p, q, r;
3997 // case 1: dst is empty
3998 p.moveTo(2, 1);
3999 p.lineTo(2, 3);
4000 q.addPath(p, mode);
4001 REPORTER_ASSERT(reporter, q == p);
4002 // case 2: src is empty
4003 p.addPath(r, mode);
4004 REPORTER_ASSERT(reporter, q == p);
4005 // case 3: src and dst are empty
4006 q.reset();
4007 q.addPath(r, mode);
4008 REPORTER_ASSERT(reporter, q.isEmpty());
4009}
4010
4011static void test_conicTo_special_case(skiatest::Reporter* reporter) {
4012 SkPath p;
4013 p.conicTo(1, 2, 3, 4, -1);
4014 check_path_is_line_and_reset(reporter, &p, 3, 4);
4015 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
4016 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
4017 p.conicTo(1, 2, 3, 4, 1);
4018 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
4019}
4020
4021static void test_get_point(skiatest::Reporter* reporter) {
4022 SkPath p;
4023 SkPoint pt = p.getPoint(0);
4024 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
4025 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
4026 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
4027 p.setLastPt(10, 10);
4028 pt = p.getPoint(0);
4029 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
4030 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
4031 p.rMoveTo(10, 10);
4032 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4033}
4034
4035static void test_contains(skiatest::Reporter* reporter) {
4036 SkPath p;
4037 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4038 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
4039 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
4040 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4041 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
4042 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
4043 // this may return true or false, depending on the platform's numerics, but it should not crash
4044 (void) p.contains(-77.2027664f, 15.3066053f);
4045
4046 p.reset();
4047 p.setFillType(SkPath::kInverseWinding_FillType);
4048 REPORTER_ASSERT(reporter, p.contains(0, 0));
4049 p.setFillType(SkPath::kWinding_FillType);
4050 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4051 p.moveTo(4, 4);
4052 p.lineTo(6, 8);
4053 p.lineTo(8, 4);
4054 // test on edge
4055 REPORTER_ASSERT(reporter, p.contains(6, 4));
4056 REPORTER_ASSERT(reporter, p.contains(5, 6));
4057 REPORTER_ASSERT(reporter, p.contains(7, 6));
4058 // test quick reject
4059 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4060 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4061 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4062 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4063 // test various crossings in x
4064 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4065 REPORTER_ASSERT(reporter, p.contains(6, 7));
4066 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4067 p.reset();
4068 p.moveTo(4, 4);
4069 p.lineTo(8, 6);
4070 p.lineTo(4, 8);
4071 // test on edge
4072 REPORTER_ASSERT(reporter, p.contains(4, 6));
4073 REPORTER_ASSERT(reporter, p.contains(6, 5));
4074 REPORTER_ASSERT(reporter, p.contains(6, 7));
4075 // test various crossings in y
4076 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4077 REPORTER_ASSERT(reporter, p.contains(7, 6));
4078 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4079 p.reset();
4080 p.moveTo(4, 4);
4081 p.lineTo(8, 4);
4082 p.lineTo(8, 8);
4083 p.lineTo(4, 8);
4084 // test on vertices
4085 REPORTER_ASSERT(reporter, p.contains(4, 4));
4086 REPORTER_ASSERT(reporter, p.contains(8, 4));
4087 REPORTER_ASSERT(reporter, p.contains(8, 8));
4088 REPORTER_ASSERT(reporter, p.contains(4, 8));
4089 p.reset();
4090 p.moveTo(4, 4);
4091 p.lineTo(6, 8);
4092 p.lineTo(2, 8);
4093 // test on edge
4094 REPORTER_ASSERT(reporter, p.contains(5, 6));
4095 REPORTER_ASSERT(reporter, p.contains(4, 8));
4096 REPORTER_ASSERT(reporter, p.contains(3, 6));
4097 p.reset();
4098 p.moveTo(4, 4);
4099 p.lineTo(0, 6);
4100 p.lineTo(4, 8);
4101 // test on edge
4102 REPORTER_ASSERT(reporter, p.contains(2, 5));
4103 REPORTER_ASSERT(reporter, p.contains(2, 7));
4104 REPORTER_ASSERT(reporter, p.contains(4, 6));
4105 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4106 p.reset();
4107 p.moveTo(4, 0);
4108 p.lineTo(6, 4);
4109 p.lineTo(2, 4);
4110 p.moveTo(4, 0);
4111 p.lineTo(0, 8);
4112 p.lineTo(8, 8);
4113 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4114 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4115 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4116 REPORTER_ASSERT(reporter, p.contains(4, 4));
4117
4118 // test quads
4119 p.reset();
4120 p.moveTo(4, 4);
4121 p.quadTo(6, 6, 8, 8);
4122 p.quadTo(6, 8, 4, 8);
4123 p.quadTo(4, 6, 4, 4);
4124 REPORTER_ASSERT(reporter, p.contains(5, 6));
4125 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4126 // test quad edge
4127 REPORTER_ASSERT(reporter, p.contains(5, 5));
4128 REPORTER_ASSERT(reporter, p.contains(5, 8));
4129 REPORTER_ASSERT(reporter, p.contains(4, 5));
4130 // test quad endpoints
4131 REPORTER_ASSERT(reporter, p.contains(4, 4));
4132 REPORTER_ASSERT(reporter, p.contains(8, 8));
4133 REPORTER_ASSERT(reporter, p.contains(4, 8));
4134
4135 p.reset();
4136 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4137 p.moveTo(qPts[0]);
4138 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4139 p.quadTo(qPts[index], qPts[index + 1]);
4140 }
4141 REPORTER_ASSERT(reporter, p.contains(5, 6));
4142 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4143 // test quad edge
4144 SkPoint halfway;
4145 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4146 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4147 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4148 }
4149
4150 // test conics
4151 p.reset();
4152 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4153 p.moveTo(kPts[0]);
4154 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4155 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4156 }
4157 REPORTER_ASSERT(reporter, p.contains(5, 6));
4158 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4159 // test conic edge
4160 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4161 SkConic conic(&kPts[index], 0.5f);
4162 halfway = conic.evalAt(0.5f);
4163 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4164 }
4165 // test conic end points
4166 REPORTER_ASSERT(reporter, p.contains(4, 4));
4167 REPORTER_ASSERT(reporter, p.contains(8, 8));
4168 REPORTER_ASSERT(reporter, p.contains(4, 8));
4169
4170 // test cubics
4171 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4172 for (int i = 0; i < 3; ++i) {
4173 p.reset();
4174 p.setFillType(SkPath::kEvenOdd_FillType);
4175 p.moveTo(pts[i].fX, pts[i].fY);
4176 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);
4177 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);
4178 p.close();
4179 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4180 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4181 // test cubic edge
4182 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4183 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4184 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4185 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4186 // test cubic end points
4187 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4188 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4189 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4190 }
4191}
4192
4193class PathRefTest_Private {
4194public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004195 static size_t GetFreeSpace(const SkPathRef& ref) {
4196 return ref.fFreeSpace;
4197 }
4198
Yuqian Li3154a532017-09-06 13:33:30 -04004199 static void TestPathRef(skiatest::Reporter* reporter) {
4200 static const int kRepeatCnt = 10;
4201
4202 sk_sp<SkPathRef> pathRef(new SkPathRef);
4203
4204 SkPathRef::Editor ed(&pathRef);
4205
4206 {
4207 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4208 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4209 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4210 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4211 for (int i = 0; i < kRepeatCnt; ++i) {
4212 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4213 }
4214 ed.resetToSize(0, 0, 0);
4215 }
4216
4217 {
4218 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4219 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4220 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4221 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4222 for (int i = 0; i < kRepeatCnt; ++i) {
4223 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4224 }
4225 ed.resetToSize(0, 0, 0);
4226 }
4227
4228 {
4229 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4230 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4231 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4232 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4233 for (int i = 0; i < kRepeatCnt; ++i) {
4234 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4235 }
4236 ed.resetToSize(0, 0, 0);
4237 }
4238
4239 {
4240 SkScalar* weights = nullptr;
4241 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4242 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4243 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4244 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4245 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4246 REPORTER_ASSERT(reporter, weights);
4247 for (int i = 0; i < kRepeatCnt; ++i) {
4248 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4249 }
4250 ed.resetToSize(0, 0, 0);
4251 }
4252
4253 {
4254 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4255 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4256 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4257 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4258 for (int i = 0; i < kRepeatCnt; ++i) {
4259 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4260 }
4261 ed.resetToSize(0, 0, 0);
4262 }
4263 }
4264};
4265
4266static void test_operatorEqual(skiatest::Reporter* reporter) {
4267 SkPath a;
4268 SkPath b;
4269 REPORTER_ASSERT(reporter, a == a);
4270 REPORTER_ASSERT(reporter, a == b);
4271 a.setFillType(SkPath::kInverseWinding_FillType);
4272 REPORTER_ASSERT(reporter, a != b);
4273 a.reset();
4274 REPORTER_ASSERT(reporter, a == b);
4275 a.lineTo(1, 1);
4276 REPORTER_ASSERT(reporter, a != b);
4277 a.reset();
4278 REPORTER_ASSERT(reporter, a == b);
4279 a.lineTo(1, 1);
4280 b.lineTo(1, 2);
4281 REPORTER_ASSERT(reporter, a != b);
4282 a.reset();
4283 a.lineTo(1, 2);
4284 REPORTER_ASSERT(reporter, a == b);
4285}
4286
4287static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4288 bool dumpAsHex, const char* str) {
4289 SkDynamicMemoryWStream wStream;
4290 path.dump(&wStream, force, dumpAsHex);
4291 sk_sp<SkData> data = wStream.detachAsData();
4292 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4293 if (strlen(str) > 0) {
4294 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4295 } else {
4296 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4297 }
4298}
4299
4300static void test_dump(skiatest::Reporter* reporter) {
4301 SkPath p;
4302 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4303 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4304 p.moveTo(1, 2);
4305 p.lineTo(3, 4);
4306 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4307 "path.moveTo(1, 2);\n"
4308 "path.lineTo(3, 4);\n");
4309 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4310 "path.moveTo(1, 2);\n"
4311 "path.lineTo(3, 4);\n"
4312 "path.lineTo(1, 2);\n"
4313 "path.close();\n");
4314 p.reset();
4315 p.setFillType(SkPath::kEvenOdd_FillType);
4316 p.moveTo(1, 2);
4317 p.quadTo(3, 4, 5, 6);
4318 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4319 "path.moveTo(1, 2);\n"
4320 "path.quadTo(3, 4, 5, 6);\n");
4321 p.reset();
4322 p.setFillType(SkPath::kInverseWinding_FillType);
4323 p.moveTo(1, 2);
4324 p.conicTo(3, 4, 5, 6, 0.5f);
4325 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4326 "path.moveTo(1, 2);\n"
4327 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4328 p.reset();
4329 p.setFillType(SkPath::kInverseEvenOdd_FillType);
4330 p.moveTo(1, 2);
4331 p.cubicTo(3, 4, 5, 6, 7, 8);
4332 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4333 "path.moveTo(1, 2);\n"
4334 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4335 p.reset();
4336 p.setFillType(SkPath::kWinding_FillType);
4337 p.moveTo(1, 2);
4338 p.lineTo(3, 4);
4339 compare_dump(reporter, p, false, true,
4340 "path.setFillType(SkPath::kWinding_FillType);\n"
4341 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4342 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4343 p.reset();
4344 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4345 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4346 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4347 "path.moveTo(1, 2);\n"
4348 "path.lineTo(3, 4);\n");
4349}
4350
4351namespace {
4352
4353class ChangeListener : public SkPathRef::GenIDChangeListener {
4354public:
4355 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4356 ~ChangeListener() override {}
4357 void onChange() override {
4358 *fChanged = true;
4359 }
4360private:
4361 bool* fChanged;
4362};
4363
4364}
4365
4366class PathTest_Private {
4367public:
Florin Malita0e0f1a72018-09-11 16:16:19 -04004368 static size_t GetFreeSpace(const SkPath& path) {
4369 return PathRefTest_Private::GetFreeSpace(*path.fPathRef);
4370 }
4371
Yuqian Li3154a532017-09-06 13:33:30 -04004372 static void TestPathTo(skiatest::Reporter* reporter) {
4373 SkPath p, q;
4374 p.lineTo(4, 4);
4375 p.reversePathTo(q);
4376 check_path_is_line(reporter, &p, 4, 4);
4377 q.moveTo(-4, -4);
4378 p.reversePathTo(q);
4379 check_path_is_line(reporter, &p, 4, 4);
4380 q.lineTo(7, 8);
4381 q.conicTo(8, 7, 6, 5, 0.5f);
4382 q.quadTo(6, 7, 8, 6);
4383 q.cubicTo(5, 6, 7, 8, 7, 5);
4384 q.close();
4385 p.reversePathTo(q);
4386 SkRect reverseExpected = {-4, -4, 8, 8};
4387 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4388 }
4389
4390 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4391 SkPath p;
4392
4393 bool changed = false;
4394 p.moveTo(0, 0);
4395
4396 // Check that listener is notified on moveTo().
4397
Chris Daltonafa11582018-06-08 12:00:44 -06004398 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004399 REPORTER_ASSERT(reporter, !changed);
4400 p.moveTo(10, 0);
4401 REPORTER_ASSERT(reporter, changed);
4402
4403 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004404 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004405 REPORTER_ASSERT(reporter, !changed);
4406 p.lineTo(20, 0);
4407 REPORTER_ASSERT(reporter, changed);
4408
4409 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004410 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004411 REPORTER_ASSERT(reporter, !changed);
4412 p.reset();
4413 REPORTER_ASSERT(reporter, changed);
4414
4415 p.moveTo(0, 0);
4416
4417 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004418 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004419 REPORTER_ASSERT(reporter, !changed);
4420 p.rewind();
4421 REPORTER_ASSERT(reporter, changed);
4422
Brian Osmandbfcd922019-03-07 16:50:30 -05004423 // Check that listener is notified on transform().
4424 {
4425 SkPath q;
4426 q.moveTo(10, 10);
4427 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
4428 REPORTER_ASSERT(reporter, !changed);
4429 SkMatrix matrix;
4430 matrix.setScale(2, 2);
4431 p.transform(matrix, &q);
4432 REPORTER_ASSERT(reporter, changed);
4433 }
4434
Yuqian Li3154a532017-09-06 13:33:30 -04004435 // Check that listener is notified when pathref is deleted.
4436 {
4437 SkPath q;
4438 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004439 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004440 REPORTER_ASSERT(reporter, !changed);
4441 }
4442 // q went out of scope.
4443 REPORTER_ASSERT(reporter, changed);
4444 }
4445};
4446
4447static void test_crbug_629455(skiatest::Reporter* reporter) {
4448 SkPath path;
4449 path.moveTo(0, 0);
4450 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4451 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4452 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4453// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4454 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004455 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004456}
4457
4458static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4459 SkPath path;
4460 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4461 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4462 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4463 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4464 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4465 path.close();
4466
4467 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4468 SkPaint paint;
4469 paint.setAntiAlias(true);
4470 surface->getCanvas()->clipPath(path, true);
4471 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4472}
4473
4474static void test_path_crbugskia6003() {
4475 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4476 SkCanvas* canvas = surface->getCanvas();
4477 SkPaint paint;
4478 paint.setAntiAlias(true);
4479 SkPath path;
4480 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4481 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4482 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4483 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4484 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4485 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4486 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4487 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4488 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4489 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4490 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4491 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4492 path.close();
4493 canvas->clipPath(path, true);
4494 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4495}
4496
4497static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4498 SkPath path;
4499 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4500 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4501 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4502 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4503 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004504 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004505}
4506
Yuqian Li3154a532017-09-06 13:33:30 -04004507static void test_skbug_6947() {
4508 SkPath path;
4509 SkPoint points[] =
4510 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4511 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4512 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4513 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4514 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4515 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4516 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4517 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4518 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4519 {125.119476f, 1.50011659f}};
4520 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4521 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4522 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4523 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4524 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4525 int pointIndex = 0;
4526 for(auto verb : verbs) {
4527 switch (verb) {
4528 case kMove:
4529 path.moveTo(points[pointIndex++]);
4530 break;
4531 case kLine:
4532 path.lineTo(points[pointIndex++]);
4533 break;
4534 case kClose:
4535 default:
4536 path.close();
4537 break;
4538 }
4539 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004540 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004541}
Yuqian Lia81b6262017-09-06 17:10:05 -04004542
4543static void test_skbug_7015() {
4544 SkPath path;
4545 path.setFillType(SkPath::kWinding_FillType);
4546 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4547 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4548 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4549 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4550 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4551 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4552 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004553 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004554}
4555
Yuqian Lic5e4e742017-09-18 14:38:43 -04004556static void test_skbug_7051() {
4557 SkPath path;
4558 path.moveTo(10, 10);
4559 path.cubicTo(10, 20, 10, 30, 30, 30);
4560 path.lineTo(50, 20);
4561 path.lineTo(50, 10);
4562 path.close();
4563 test_draw_AA_path(100, 100, path);
4564}
4565
Yuqian Li3154a532017-09-06 13:33:30 -04004566static void test_interp(skiatest::Reporter* reporter) {
4567 SkPath p1, p2, out;
4568 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4569 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4570 REPORTER_ASSERT(reporter, p1 == out);
4571 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4572 REPORTER_ASSERT(reporter, p1 == out);
4573 p1.moveTo(0, 2);
4574 p1.lineTo(0, 4);
4575 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4576 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4577 p2.moveTo(6, 0);
4578 p2.lineTo(8, 0);
4579 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4580 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4581 REPORTER_ASSERT(reporter, p2 == out);
4582 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4583 REPORTER_ASSERT(reporter, p1 == out);
4584 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4585 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4586 p1.reset();
4587 p1.moveTo(4, 4);
4588 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4589 p2.reset();
4590 p2.moveTo(4, 2);
4591 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4592 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4593 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4594 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4595 p2.reset();
4596 p2.moveTo(4, 2);
4597 p2.conicTo(6, 3, 6, 5, 1);
4598 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4599 p2.reset();
4600 p2.moveTo(4, 4);
4601 p2.conicTo(5, 4, 5, 5, 0.5f);
4602 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4603}
4604
4605DEF_TEST(PathInterp, reporter) {
4606 test_interp(reporter);
4607}
4608
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004609#include "include/core/SkSurface.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004610DEF_TEST(PathBigCubic, reporter) {
4611 SkPath path;
4612 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4613 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4614 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
4615 path.moveTo(0, 512);
4616
4617 // this call should not assert
4618 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4619}
4620
4621DEF_TEST(PathContains, reporter) {
4622 test_contains(reporter);
4623}
4624
4625DEF_TEST(Paths, reporter) {
4626 test_fuzz_crbug_647922();
4627 test_fuzz_crbug_643933();
4628 test_sect_with_horizontal_needs_pinning();
4629 test_crbug_629455(reporter);
4630 test_fuzz_crbug_627414(reporter);
4631 test_path_crbug364224();
4632 test_fuzz_crbug_662952(reporter);
4633 test_fuzz_crbug_662730(reporter);
4634 test_fuzz_crbug_662780();
4635 test_mask_overflow();
4636 test_path_crbugskia6003();
4637 test_fuzz_crbug_668907();
Yuqian Li3154a532017-09-06 13:33:30 -04004638 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004639 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004640 test_skbug_7051();
Yuqian Li3154a532017-09-06 13:33:30 -04004641
4642 SkSize::Make(3, 4);
4643
4644 SkPath p, empty;
4645 SkRect bounds, bounds2;
4646 test_empty(reporter, p);
4647
4648 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4649
4650 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4651 SkPath& self = p;
4652 p = self;
4653
4654 // this triggers a code path in SkPath::swap which is otherwise unexercised
4655 p.swap(self);
4656
4657 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
4658
4659 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4660 check_convex_bounds(reporter, p, bounds);
4661 // we have quads or cubics
4662 REPORTER_ASSERT(reporter,
4663 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4664 REPORTER_ASSERT(reporter, !p.isEmpty());
4665
4666 p.reset();
4667 test_empty(reporter, p);
4668
4669 p.addOval(bounds);
4670 check_convex_bounds(reporter, p, bounds);
4671 REPORTER_ASSERT(reporter, !p.isEmpty());
4672
4673 p.rewind();
4674 test_empty(reporter, p);
4675
4676 p.addRect(bounds);
4677 check_convex_bounds(reporter, p, bounds);
4678 // we have only lines
4679 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4680 REPORTER_ASSERT(reporter, !p.isEmpty());
4681
4682 REPORTER_ASSERT(reporter, p != empty);
4683 REPORTER_ASSERT(reporter, !(p == empty));
4684
4685 // do getPoints and getVerbs return the right result
4686 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4687 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4688 SkPoint pts[4];
4689 int count = p.getPoints(pts, 4);
4690 REPORTER_ASSERT(reporter, count == 4);
4691 uint8_t verbs[6];
4692 verbs[5] = 0xff;
4693 p.getVerbs(verbs, 5);
4694 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4695 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4696 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4697 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4698 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4699 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
4700 bounds2.set(pts, 4);
4701 REPORTER_ASSERT(reporter, bounds == bounds2);
4702
4703 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4704 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4705 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4706
4707 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4708 bounds2.setEmpty();
4709 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4710 REPORTER_ASSERT(reporter, bounds == bounds2);
4711
4712 // now force p to not be a rect
4713 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4714 p.addRect(bounds);
4715 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4716
4717 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4718 // path has a trailing moveTo. Please see crbug.com\445368)
4719 {
4720 SkRect r;
4721 p.reset();
4722 p.addRect(bounds);
4723 REPORTER_ASSERT(reporter, p.isRect(&r));
4724 REPORTER_ASSERT(reporter, r == bounds);
4725 // add a moveTo outside of our bounds
4726 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4727 REPORTER_ASSERT(reporter, p.isRect(&r));
4728 REPORTER_ASSERT(reporter, r == bounds);
4729 }
4730
4731 test_operatorEqual(reporter);
4732 test_isLine(reporter);
4733 test_isRect(reporter);
4734 test_is_simple_closed_rect(reporter);
4735 test_isNestedFillRects(reporter);
4736 test_zero_length_paths(reporter);
4737 test_direction(reporter);
4738 test_convexity(reporter);
4739 test_convexity2(reporter);
Cary Clarkc9b7c722018-12-12 14:50:23 -05004740 test_convexity_doubleback(reporter);
Yuqian Li3154a532017-09-06 13:33:30 -04004741 test_conservativelyContains(reporter);
4742 test_close(reporter);
4743 test_segment_masks(reporter);
4744 test_flattening(reporter);
4745 test_transform(reporter);
4746 test_bounds(reporter);
4747 test_iter(reporter);
4748 test_raw_iter(reporter);
4749 test_circle(reporter);
4750 test_oval(reporter);
4751 test_strokerec(reporter);
4752 test_addPoly(reporter);
4753 test_isfinite(reporter);
4754 test_isfinite_after_transform(reporter);
4755 test_islastcontourclosed(reporter);
4756 test_arb_round_rect_is_convex(reporter);
4757 test_arb_zero_rad_round_rect_is_rect(reporter);
4758 test_addrect(reporter);
4759 test_addrect_isfinite(reporter);
4760 test_tricky_cubic();
4761 test_clipped_cubic();
4762 test_crbug_170666();
4763 test_crbug_493450(reporter);
4764 test_crbug_495894(reporter);
4765 test_crbug_613918();
4766 test_bad_cubic_crbug229478();
4767 test_bad_cubic_crbug234190();
4768 test_gen_id(reporter);
4769 test_path_close_issue1474(reporter);
4770 test_path_to_region(reporter);
4771 test_rrect(reporter);
4772 test_arc(reporter);
4773 test_arc_ovals(reporter);
4774 test_arcTo(reporter);
4775 test_addPath(reporter);
4776 test_addPathMode(reporter, false, false);
4777 test_addPathMode(reporter, true, false);
4778 test_addPathMode(reporter, false, true);
4779 test_addPathMode(reporter, true, true);
4780 test_extendClosedPath(reporter);
4781 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4782 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4783 test_conicTo_special_case(reporter);
4784 test_get_point(reporter);
4785 test_contains(reporter);
4786 PathTest_Private::TestPathTo(reporter);
4787 PathRefTest_Private::TestPathRef(reporter);
4788 PathTest_Private::TestPathrefListeners(reporter);
4789 test_dump(reporter);
4790 test_path_crbug389050(reporter);
4791 test_path_crbugskia2820(reporter);
4792 test_path_crbugskia5995();
4793 test_skbug_3469(reporter);
4794 test_skbug_3239(reporter);
4795 test_bounds_crbug_513799(reporter);
4796 test_fuzz_crbug_638223();
4797}
4798
4799DEF_TEST(conservatively_contains_rect, reporter) {
4800 SkPath path;
4801
4802 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4803 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4804 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4805 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4806 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4807 path.moveTo(0, 0);
4808
4809 // this guy should not assert
4810 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4811}
4812
4813///////////////////////////////////////////////////////////////////////////////////////////////////
4814
4815static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4816 for (int i = 0; i < n; ++i) {
4817 switch (verb) {
4818 case SkPath::kLine_Verb:
4819 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4820 break;
4821 case SkPath::kQuad_Verb:
4822 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4823 rand.nextF()*100, rand.nextF()*100);
4824 break;
4825 case SkPath::kConic_Verb:
4826 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4827 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4828 break;
4829 case SkPath::kCubic_Verb:
4830 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4831 rand.nextF()*100, rand.nextF()*100,
4832 rand.nextF()*100, rand.nextF()*100);
4833 break;
4834 default:
4835 SkASSERT(false);
4836 }
4837 }
4838}
4839
Mike Kleinc0bd9f92019-04-23 12:05:21 -05004840#include "include/pathops/SkPathOps.h"
Yuqian Li3154a532017-09-06 13:33:30 -04004841DEF_TEST(path_tight_bounds, reporter) {
4842 SkRandom rand;
4843
4844 const SkPath::Verb verbs[] = {
4845 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4846 };
4847 for (int i = 0; i < 1000; ++i) {
4848 for (int n = 1; n <= 10; n += 9) {
4849 for (SkPath::Verb verb : verbs) {
4850 SkPath path;
4851 rand_path(&path, rand, verb, n);
4852 SkRect bounds = path.getBounds();
4853 SkRect tight = path.computeTightBounds();
4854 REPORTER_ASSERT(reporter, bounds.contains(tight));
4855
4856 SkRect tight2;
4857 TightBounds(path, &tight2);
4858 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
4859 }
4860 }
4861 }
4862}
4863
4864DEF_TEST(skbug_6450, r) {
4865 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
4866 SkVector rdi[4] = {
4867 { 1.81159976e-09f, 7.58768801e-05f },
4868 { 0.000118725002f, 0.000118725002f },
4869 { 0.000118725002f, 0.000118725002f },
4870 { 0.000118725002f, 0.486297607f }
4871 };
4872 SkRRect irr;
4873 irr.setRectRadii(ri, rdi);
4874 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
4875 SkVector rdo[4] = {
4876 { 0, 0 },
4877 { 0.0103298295f, 0.185887396f },
4878 { 2.52999727e-29f, 169.001938f },
4879 { 195.262741f, 195.161255f }
4880 };
4881 SkRRect orr;
4882 orr.setRectRadii(ro, rdo);
4883 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
4884}
4885
4886DEF_TEST(PathRefSerialization, reporter) {
4887 SkPath path;
4888 const size_t numMoves = 5;
4889 const size_t numConics = 7;
4890 const size_t numPoints = numMoves + 2 * numConics;
4891 const size_t numVerbs = numMoves + numConics;
4892 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
4893 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
4894 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
4895 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
4896
4897 // Verify that path serializes/deserializes properly.
4898 sk_sp<SkData> data = path.serialize();
4899 size_t bytesWritten = data->size();
4900
4901 {
4902 SkPath readBack;
4903 REPORTER_ASSERT(reporter, readBack != path);
4904 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
4905 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
4906 REPORTER_ASSERT(reporter, readBack == path);
4907 }
4908
Yuqian Li3154a532017-09-06 13:33:30 -04004909 // One less byte (rounded down to alignment) than was written will also
4910 // fail to be deserialized.
4911 {
4912 SkPath readBack;
4913 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
4914 REPORTER_ASSERT(reporter, !bytesRead);
4915 }
4916}
Mike Klein1170a552017-09-08 15:00:25 -04004917
4918DEF_TEST(NonFinitePathIteration, reporter) {
4919 SkPath path;
4920 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
4921
4922 int verbs = 0;
4923
4924 SkPath::RawIter iter(path);
4925 SkPoint pts[4];
4926 while (iter.next(pts) != SkPath::kDone_Verb) {
4927 verbs++;
4928 }
4929
4930 REPORTER_ASSERT(reporter, verbs == 0);
4931}
Cary Clark1acd3c72017-12-08 11:37:01 -05004932
Cary Clark1acd3c72017-12-08 11:37:01 -05004933DEF_TEST(AndroidArc, reporter) {
4934 const char* tests[] = {
4935 "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",
4936 "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
4937 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
4938 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
4939 };
4940 for (auto test : tests) {
4941 SkPath aPath;
4942 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
4943 SkASSERT(aPath.isConvex());
4944 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
4945 SkPath scalePath = aPath;
4946 SkMatrix matrix;
4947 matrix.setScale(scale, scale);
4948 scalePath.transform(matrix);
4949 SkASSERT(scalePath.isConvex());
4950 }
4951 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
4952 SkPath scalePath = aPath;
4953 SkMatrix matrix;
4954 matrix.setScale(scale, scale);
4955 scalePath.transform(matrix);
4956 SkASSERT(scalePath.isConvex());
4957 }
4958 }
4959}
Mike Reed1fe0bcc2018-01-22 16:49:49 -05004960
4961/*
4962 * Try a range of crazy values, just to ensure that we don't assert/crash.
4963 */
4964DEF_TEST(HugeGeometry, reporter) {
4965 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
4966 auto canvas = surf->getCanvas();
4967
4968 const bool aas[] = { false, true };
4969 const SkPaint::Style styles[] = {
4970 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
4971 };
4972 const SkScalar values[] = {
4973 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
4974 SK_ScalarInfinity
4975 };
4976
4977 SkPaint paint;
4978 for (auto x : values) {
4979 SkRect r = { -x, -x, x, x };
4980 for (auto width : values) {
4981 paint.setStrokeWidth(width);
4982 for (auto aa : aas) {
4983 paint.setAntiAlias(aa);
4984 for (auto style : styles) {
4985 paint.setStyle(style);
4986 canvas->drawRect(r, paint);
4987 canvas->drawOval(r, paint);
4988 }
4989 }
4990 }
4991 }
4992
4993}
4994
Mike Reed1c6e7832018-01-30 11:29:36 -05004995// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
4996DEF_TEST(ClipPath_nonfinite, reporter) {
4997 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
4998 SkCanvas* canvas = surf->getCanvas();
4999
5000 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5001 for (bool aa : {false, true}) {
5002 for (SkPath::FillType ft : {SkPath::kWinding_FillType, SkPath::kInverseWinding_FillType}) {
5003 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
5004 for (int bits = 1; bits <= 15; ++bits) {
5005 SkPoint p0 = { 0, 0 };
5006 SkPoint p1 = { 0, 0 };
5007 if (bits & 1) p0.fX = -bad;
5008 if (bits & 2) p0.fY = -bad;
5009 if (bits & 4) p1.fX = bad;
5010 if (bits & 8) p1.fY = bad;
5011
5012 SkPath path;
5013 path.moveTo(p0);
5014 path.lineTo(p1);
5015 path.setFillType(ft);
5016 canvas->save();
5017 canvas->clipPath(path, aa);
5018 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
5019 canvas->restore();
5020 }
5021 }
5022 }
5023 }
5024 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
5025}
5026
Cary Clark5c715182018-04-09 16:07:11 -04005027// skbug.com/7792
5028DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04005029 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
5030 SkPath path;
5031 for (size_t index = 0; index < count; ++index) {
5032 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
5033 }
Cary Clark31608c02018-04-12 10:29:46 -04005034 if (close) {
5035 path.close();
5036 }
Cary Clark8540e112018-04-11 14:30:27 -04005037 return path;
5038 };
5039 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
5040 SkPath path;
5041 for (size_t index = 0; index < count; ++index) {
5042 switch (verbs[index]) {
5043 case SkPath::kMove_Verb:
5044 path.moveTo(*points++);
5045 break;
5046 case SkPath::kLine_Verb:
5047 path.lineTo(*points++);
5048 break;
5049 case SkPath::kClose_Verb:
5050 path.close();
5051 break;
5052 default:
5053 SkASSERT(0);
5054 }
5055 }
5056 return path;
5057 };
Cary Clarkd4228472018-04-13 07:07:04 -04005058 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04005059 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04005060 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5061 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04005062 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04005063 SkRect compare;
5064 compare.set(&points[1], SK_ARRAY_COUNT(points) - 1);
5065 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005066 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04005067 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
5068 path = makePath(points3, SK_ARRAY_COUNT(points3), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005069 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005070 // isolated from skbug.com/7792#c9
Cary Clark8540e112018-04-11 14:30:27 -04005071 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
5072 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005073 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04005074 compare.set(&points9[1], SK_ARRAY_COUNT(points9) - 1);
5075 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005076 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04005077 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5078 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
5079 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
5080 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04005081 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04005082 compare.set(&points11[0], SK_ARRAY_COUNT(points11));
5083 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005084 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04005085 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
5086 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5087 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
5088 SkPath::kLine_Verb, SkPath::kClose_Verb };
5089 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
5090 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
5091 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04005092 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005093 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04005094 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5095 SkPath::kLine_Verb, SkPath::kMove_Verb };
5096 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
5097 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04005098 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04005099 compare.set(&points15[0], SK_ARRAY_COUNT(points15) - 1);
5100 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04005101 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04005102 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
5103 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04005104 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005105 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04005106 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5107 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5108 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
5109 SkPath::kLine_Verb, SkPath::kLine_Verb };
5110 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5111 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
5112 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04005113 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005114 // isolated from skbug.com/7792#c23
5115 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5116 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5117 SkPath::kLine_Verb, SkPath::kClose_Verb };
5118 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5119 {75, 150} };
5120 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04005121 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005122 compare.set(&points23[0], SK_ARRAY_COUNT(points23));
5123 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005124 // isolated from skbug.com/7792#c29
5125 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5126 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5127 SkPath::kClose_Verb };
5128 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5129 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005130 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005131 // isolated from skbug.com/7792#c31
5132 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5133 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5134 SkPath::kClose_Verb };
5135 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5136 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005137 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005138 compare.set(&points31[0], 4);
5139 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005140 // isolated from skbug.com/7792#c36
5141 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5142 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5143 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5144 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005145 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005146 // isolated from skbug.com/7792#c39
5147 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005148 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005149 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5150 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005151 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005152 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005153 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5154 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5155 SkPath::kLine_Verb, SkPath::kClose_Verb };
5156 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5157 {32, 2} };
5158 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5159 REPORTER_ASSERT(reporter, path.isRect(&rect));
5160 compare.set(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
5161 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005162 // isolated from skbug.com/7792#c41
5163 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5164 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5165 SkPath::kClose_Verb };
5166 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5167 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5168 REPORTER_ASSERT(reporter, path.isRect(&rect));
5169 compare.set(&points41[1], 4);
5170 REPORTER_ASSERT(reporter, rect == compare);
5171 // isolated from skbug.com/7792#c53
5172 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5173 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5174 SkPath::kClose_Verb };
5175 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5176 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5177 REPORTER_ASSERT(reporter, path.isRect(&rect));
5178 compare.set(&points53[1], 4);
5179 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005180}
Mike Reedee430912018-05-23 12:12:21 -04005181
Mike Reedc3d8a482018-09-12 10:08:40 -04005182// Be sure we can safely add ourselves
5183DEF_TEST(Path_self_add, reporter) {
5184 // The possible problem is that during path.add() we may have to grow the dst buffers as
5185 // we append the src pts/verbs, but all the while we are iterating over the src. If src == dst
5186 // we could realloc the buffer's (on behalf of dst) leaving the src iterator pointing at
5187 // garbage.
5188 //
5189 // The test runs though verious sized src paths, since its not defined publicly what the
5190 // reserve allocation strategy is for SkPath, therefore we can't know when an append operation
5191 // will trigger a realloc. At the time of this writing, these loops were sufficient to trigger
5192 // an ASAN error w/o the fix to SkPath::addPath().
5193 //
5194 for (int count = 0; count < 10; ++count) {
5195 SkPath path;
5196 for (int add = 0; add < count; ++add) {
5197 // just add some stuff, so we have something to copy/append in addPath()
5198 path.moveTo(1, 2).lineTo(3, 4).cubicTo(1,2,3,4,5,6).conicTo(1,2,3,4,5);
5199 }
5200 path.addPath(path, 1, 2);
5201 path.addPath(path, 3, 4);
5202 }
5203}
5204
Mike Kleinc0bd9f92019-04-23 12:05:21 -05005205#include "include/core/SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005206static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5207 // draw in different ways, looking for an assert
5208
5209 {
5210 SkPath path;
5211 path.addPoly(pts, 3, false);
5212 canvas->drawPath(path, SkPaint());
5213 }
5214
5215 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5216 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5217 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5218}
5219
Mike Reedee430912018-05-23 12:12:21 -04005220DEF_TEST(triangle_onehalf, reporter) {
5221 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5222
5223 const SkPoint pts[] = {
5224 { 0.499069244f, 9.63295173f },
5225 { 0.499402374f, 7.88207579f },
5226 { 10.2363272f, 0.49999997f }
5227 };
Mike Reed50595412018-05-30 16:32:33 -04005228 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005229}
Mike Reed50595412018-05-30 16:32:33 -04005230
5231DEF_TEST(triangle_big, reporter) {
5232 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5233
5234 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5235 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5236 // be conservative, so we invoke clipping if we get in this situation.
5237 // This test was added to demonstrate the need for this conservative bounds calc.
5238 // (found by a fuzzer)
5239 const SkPoint pts[] = {
5240 { 0.327190518f, -114.945152f },
5241 { -0.5f, 1.00003874f },
5242 { 0.666425824f, 4304.26172f },
5243 };
5244 draw_triangle(surface->getCanvas(), pts);
5245}
5246
Florin Malita3d413c52018-09-11 14:01:42 -04005247static void add_verbs(SkPath* path, int count) {
5248 path->moveTo(0, 0);
5249 for (int i = 0; i < count; ++i) {
5250 switch (i & 3) {
5251 case 0: path->lineTo(10, 20); break;
5252 case 1: path->quadTo(5, 6, 7, 8); break;
5253 case 2: path->conicTo(1, 2, 3, 4, 0.5f); break;
5254 case 3: path->cubicTo(2, 4, 6, 8, 10, 12); break;
5255 }
5256 }
5257}
5258
5259// Make sure when we call shrinkToFit() that we always shrink (or stay the same)
5260// and that if we call twice, we stay the same.
5261DEF_TEST(Path_shrinkToFit, reporter) {
5262 size_t max_free = 0;
5263 for (int verbs = 0; verbs < 100; ++verbs) {
5264 SkPath unique_path, shared_path;
5265 add_verbs(&unique_path, verbs);
5266 add_verbs(&shared_path, verbs);
5267
5268 const SkPath copy = shared_path;
5269 REPORTER_ASSERT(reporter, shared_path == unique_path);
5270 REPORTER_ASSERT(reporter, shared_path == copy);
5271
5272#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005273 size_t before = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005274#endif
5275 unique_path.shrinkToFit();
5276 shared_path.shrinkToFit();
5277 REPORTER_ASSERT(reporter, shared_path == unique_path);
5278 REPORTER_ASSERT(reporter, shared_path == copy);
5279
5280#ifdef SK_DEBUG
Florin Malita0e0f1a72018-09-11 16:16:19 -04005281 size_t after = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005282 REPORTER_ASSERT(reporter, before >= after);
5283 max_free = std::max(max_free, before - after);
5284
Florin Malita0e0f1a72018-09-11 16:16:19 -04005285 size_t after2 = PathTest_Private::GetFreeSpace(unique_path);
Florin Malita3d413c52018-09-11 14:01:42 -04005286 REPORTER_ASSERT(reporter, after == after2);
5287#endif
5288 }
5289 if (false) {
5290 SkDebugf("max_free %zu\n", max_free);
5291 }
5292}
Mike Klein9de72162018-09-24 20:30:28 -04005293
5294DEF_TEST(Path_setLastPt, r) {
5295 // There was a time where SkPath::setLastPoint() didn't invalidate cached path bounds.
5296 SkPath p;
5297 p.moveTo(0,0);
5298 p.moveTo(20,01);
5299 p.moveTo(20,10);
5300 p.moveTo(20,61);
5301 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 20,61));
5302
5303 p.setLastPt(30,01);
5304 REPORTER_ASSERT(r, p.getBounds() == SkRect::MakeLTRB(0,0, 30,10)); // was {0,0, 20,61}
5305
5306 REPORTER_ASSERT(r, p.isValid());
Mike Klein9de72162018-09-24 20:30:28 -04005307}
Mike Reed6a388002018-10-16 13:13:09 -04005308
5309DEF_TEST(Path_increserve_handle_neg_crbug_883666, r) {
5310 SkPath path;
5311
5312 path.conicTo({0, 0}, {1, 1}, SK_FloatNegativeInfinity);
5313
5314 // <== use a copy path object to force SkPathRef::copy() and SkPathRef::resetToSize()
5315 SkPath shallowPath = path;
5316
5317 // make sure we don't assert/crash on this.
5318 shallowPath.incReserve(0xffffffff);
5319}
5320
Mike Reed07105bb2018-12-01 14:07:49 -05005321////////////////////////////////////////////////////////////////////////////////////////////////
5322
5323/*
5324 * For speed, we tried to preserve useful/expensive attributes about paths,
5325 * - convexity, isrect, isoval, ...
5326 * Axis-aligned shapes (rect, oval, rrect) should survive, including convexity if the matrix
5327 * is axis-aligned (e.g. scale+translate)
5328 */
5329
5330struct Xforms {
5331 SkMatrix fIM, fTM, fSM, fRM;
5332
5333 Xforms() {
5334 fIM.reset();
5335 fTM.setTranslate(10, 20);
5336 fSM.setScale(2, 3);
5337 fRM.setRotate(30);
5338 }
5339};
5340
Mike Reed07105bb2018-12-01 14:07:49 -05005341static bool conditional_convex(const SkPath& path, bool is_convex) {
5342 SkPath::Convexity c = path.getConvexityOrUnknown();
5343 return is_convex ? (c == SkPath::kConvex_Convexity) : (c != SkPath::kConvex_Convexity);
5344}
Mike Reed07105bb2018-12-01 14:07:49 -05005345
5346// expect axis-aligned shape to survive assignment, identity and scale/translate matrices
5347template <typename ISA>
5348void survive(SkPath* path, const Xforms& x, bool isAxisAligned, skiatest::Reporter* reporter,
5349 ISA isa_proc) {
5350 REPORTER_ASSERT(reporter, isa_proc(*path));
5351 // force the issue (computing convexity) the first time.
5352 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kConvex_Convexity);
5353
5354 SkPath path2;
5355
5356 // a path's isa and convexity should survive assignment
5357 path2 = *path;
5358 REPORTER_ASSERT(reporter, isa_proc(path2));
5359 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5360
5361 // a path's isa and convexity should identity transform
5362 path->transform(x.fIM, &path2);
5363 path->transform(x.fIM);
5364 REPORTER_ASSERT(reporter, isa_proc(path2));
5365 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5366 REPORTER_ASSERT(reporter, isa_proc(*path));
5367 REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() == SkPath::kConvex_Convexity);
5368
5369 // a path's isa should survive translation, convexity depends on axis alignment
5370 path->transform(x.fTM, &path2);
5371 path->transform(x.fTM);
5372 REPORTER_ASSERT(reporter, isa_proc(path2));
5373 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005374 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5375 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005376
5377 // a path's isa should survive scaling, convexity depends on axis alignment
5378 path->transform(x.fSM, &path2);
5379 path->transform(x.fSM);
5380 REPORTER_ASSERT(reporter, isa_proc(path2));
5381 REPORTER_ASSERT(reporter, isa_proc(*path));
Mike Reed07105bb2018-12-01 14:07:49 -05005382 REPORTER_ASSERT(reporter, conditional_convex(path2, isAxisAligned));
5383 REPORTER_ASSERT(reporter, conditional_convex(*path, isAxisAligned));
Mike Reed07105bb2018-12-01 14:07:49 -05005384
5385 // For security, post-rotation, we can't assume we're still convex. It might prove to be,
5386 // in fact, still be convex, be we can't have cached that setting, hence the call to
5387 // getConvexityOrUnknown() instead of getConvexity().
5388 path->transform(x.fRM, &path2);
5389 path->transform(x.fRM);
5390 if (isAxisAligned) {
5391 REPORTER_ASSERT(reporter, !isa_proc(path2));
5392 REPORTER_ASSERT(reporter, !isa_proc(*path));
5393 }
Mike Reed07105bb2018-12-01 14:07:49 -05005394 REPORTER_ASSERT(reporter, path2.getConvexityOrUnknown() != SkPath::kConvex_Convexity);
5395 REPORTER_ASSERT(reporter, path->getConvexityOrUnknown() != SkPath::kConvex_Convexity);
Mike Reed07105bb2018-12-01 14:07:49 -05005396}
5397
5398DEF_TEST(Path_survive_transform, r) {
5399 const Xforms x;
5400
5401 SkPath path;
5402 path.addRect({10, 10, 40, 50});
5403 survive(&path, x, true, r, [](const SkPath& p) { return p.isRect(nullptr); });
5404
5405 path.reset();
5406 path.addOval({10, 10, 40, 50});
5407 survive(&path, x, true, r, [](const SkPath& p) { return p.isOval(nullptr); });
5408
5409 path.reset();
5410 path.addRRect(SkRRect::MakeRectXY({10, 10, 40, 50}, 5, 5));
5411 survive(&path, x, true, r, [](const SkPath& p) { return p.isRRect(nullptr); });
5412
5413 // make a trapazoid; definitely convex, but not marked as axis-aligned (e.g. oval, rrect)
5414 path.reset();
5415 path.moveTo(0, 0).lineTo(100, 0).lineTo(70, 100).lineTo(30, 100);
5416 REPORTER_ASSERT(r, path.getConvexity() == SkPath::kConvex_Convexity);
5417 survive(&path, x, false, r, [](const SkPath& p) { return true; });
5418}
Mike Reedfb5f43b2018-12-18 12:17:57 -05005419
5420DEF_TEST(path_last_move_to_index, r) {
5421 // Make sure that copyPath is safe after the call to path.offset().
5422 // Previously, we would leave its fLastMoveToIndex alone after the copy, but now we should
5423 // set it to path's value inside SkPath::transform()
5424
Mike Reeddf3d2252018-12-20 17:10:27 -05005425 const char text[] = "hello";
5426 constexpr size_t len = sizeof(text) - 1;
5427 SkGlyphID glyphs[len];
Mike Reedfb5f43b2018-12-18 12:17:57 -05005428
Mike Reeddf3d2252018-12-20 17:10:27 -05005429 SkFont font;
5430 font.textToGlyphs(text, len, kUTF8_SkTextEncoding, glyphs, len);
5431
5432 SkPath copyPath;
5433 SkFont().getPaths(glyphs, len, [](const SkPath* src, const SkMatrix& mx, void* ctx) {
5434 if (src) {
5435 ((SkPath*)ctx)->addPath(*src, mx);
5436 }
5437 }, &copyPath);
Mike Reedfb5f43b2018-12-18 12:17:57 -05005438
Kevin Lubick13486cb2019-01-04 09:49:47 -05005439 SkScalar radii[] = { 80, 100, 0, 0, 40, 60, 0, 0 };
Mike Reeddf3d2252018-12-20 17:10:27 -05005440 SkPath path;
Mike Reedfb5f43b2018-12-18 12:17:57 -05005441 path.addRoundRect({10, 10, 110, 110}, radii);
5442 path.offset(0, 5, &(copyPath)); // <== change buffer copyPath.fPathRef->fPoints but not reset copyPath.fLastMoveToIndex lead to out of bound
5443
5444 copyPath.rConicTo(1, 1, 3, 3, 0.707107f);
5445}