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