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