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