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