blob: 5f6aa3d8291776e2798b931bf4f96249417049ac [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.)
1054const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
1055
1056static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
1057 SkPathPriv::FirstDirection expected) {
1058 if (expected == kDontCheckDir) {
1059 return;
1060 }
1061 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1062
1063 SkPathPriv::FirstDirection dir;
1064 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
1065 REPORTER_ASSERT(reporter, dir == expected);
1066 } else {
1067 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
1068 }
1069}
1070
1071static void test_direction(skiatest::Reporter* reporter) {
1072 size_t i;
1073 SkPath path;
1074 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1075 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1076 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1077 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
1078
1079 static const char* gDegen[] = {
1080 "M 10 10",
1081 "M 10 10 M 20 20",
1082 "M 10 10 L 20 20",
1083 "M 10 10 L 10 10 L 10 10",
1084 "M 10 10 Q 10 10 10 10",
1085 "M 10 10 C 10 10 10 10 10 10",
1086 };
1087 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1088 path.reset();
1089 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1090 REPORTER_ASSERT(reporter, valid);
1091 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
1092 }
1093
1094 static const char* gCW[] = {
1095 "M 10 10 L 10 10 Q 20 10 20 20",
1096 "M 10 10 C 20 10 20 20 20 20",
1097 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
1098 // rect with top two corners replaced by cubics with identical middle
1099 // control points
1100 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1101 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
1102 };
1103 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1104 path.reset();
1105 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1106 REPORTER_ASSERT(reporter, valid);
1107 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1108 }
1109
1110 static const char* gCCW[] = {
1111 "M 10 10 L 10 10 Q 20 10 20 -20",
1112 "M 10 10 C 20 10 20 -20 20 -20",
1113 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
1114 // rect with top two corners replaced by cubics with identical middle
1115 // control points
1116 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1117 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
1118 };
1119 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1120 path.reset();
1121 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1122 REPORTER_ASSERT(reporter, valid);
1123 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1124 }
1125
1126 // Test two donuts, each wound a different direction. Only the outer contour
1127 // determines the cheap direction
1128 path.reset();
1129 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1130 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
1131 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1132
1133 path.reset();
1134 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1135 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
1136 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1137
1138 // triangle with one point really far from the origin.
1139 path.reset();
1140 // the first point is roughly 1.05e10, 1.05e10
1141 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
1142 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1143 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
1144 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1145
1146 path.reset();
1147 path.conicTo(20, 0, 20, 20, 0.5f);
1148 path.close();
1149 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
1150
1151 path.reset();
1152 path.lineTo(1, 1e7f);
1153 path.lineTo(1e7f, 2e7f);
1154 path.close();
1155 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
1156 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
1157}
1158
1159static void add_rect(SkPath* path, const SkRect& r) {
1160 path->moveTo(r.fLeft, r.fTop);
1161 path->lineTo(r.fRight, r.fTop);
1162 path->lineTo(r.fRight, r.fBottom);
1163 path->lineTo(r.fLeft, r.fBottom);
1164 path->close();
1165}
1166
1167static void test_bounds(skiatest::Reporter* reporter) {
1168 static const SkRect rects[] = {
1169 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1170 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1171 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1172 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
1173 };
1174
1175 SkPath path0, path1;
1176 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1177 path0.addRect(rects[i]);
1178 add_rect(&path1, rects[i]);
1179 }
1180
1181 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1182}
1183
1184static void stroke_cubic(const SkPoint pts[4]) {
1185 SkPath path;
1186 path.moveTo(pts[0]);
1187 path.cubicTo(pts[1], pts[2], pts[3]);
1188
1189 SkPaint paint;
1190 paint.setStyle(SkPaint::kStroke_Style);
1191 paint.setStrokeWidth(SK_Scalar1 * 2);
1192
1193 SkPath fill;
1194 paint.getFillPath(path, &fill);
1195}
1196
1197// just ensure this can run w/o any SkASSERTS firing in the debug build
1198// we used to assert due to differences in how we determine a degenerate vector
1199// but that was fixed with the introduction of SkPoint::CanNormalize
1200static void stroke_tiny_cubic() {
1201 SkPoint p0[] = {
1202 { 372.0f, 92.0f },
1203 { 372.0f, 92.0f },
1204 { 372.0f, 92.0f },
1205 { 372.0f, 92.0f },
1206 };
1207
1208 stroke_cubic(p0);
1209
1210 SkPoint p1[] = {
1211 { 372.0f, 92.0f },
1212 { 372.0007f, 92.000755f },
1213 { 371.99927f, 92.003922f },
1214 { 371.99826f, 92.003899f },
1215 };
1216
1217 stroke_cubic(p1);
1218}
1219
1220static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1221 for (int i = 0; i < 2; ++i) {
1222 SkPath::Iter iter(path, SkToBool(i));
1223 SkPoint mv;
1224 SkPoint pts[4];
1225 SkPath::Verb v;
1226 int nMT = 0;
1227 int nCL = 0;
1228 mv.set(0, 0);
1229 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1230 switch (v) {
1231 case SkPath::kMove_Verb:
1232 mv = pts[0];
1233 ++nMT;
1234 break;
1235 case SkPath::kClose_Verb:
1236 REPORTER_ASSERT(reporter, mv == pts[0]);
1237 ++nCL;
1238 break;
1239 default:
1240 break;
1241 }
1242 }
1243 // if we force a close on the interator we should have a close
1244 // for every moveTo
1245 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1246 }
1247}
1248
1249static void test_close(skiatest::Reporter* reporter) {
1250 SkPath closePt;
1251 closePt.moveTo(0, 0);
1252 closePt.close();
1253 check_close(reporter, closePt);
1254
1255 SkPath openPt;
1256 openPt.moveTo(0, 0);
1257 check_close(reporter, openPt);
1258
1259 SkPath empty;
1260 check_close(reporter, empty);
1261 empty.close();
1262 check_close(reporter, empty);
1263
1264 SkPath rect;
1265 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1266 check_close(reporter, rect);
1267 rect.close();
1268 check_close(reporter, rect);
1269
1270 SkPath quad;
1271 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1272 check_close(reporter, quad);
1273 quad.close();
1274 check_close(reporter, quad);
1275
1276 SkPath cubic;
1277 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
1278 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1279 check_close(reporter, cubic);
1280 cubic.close();
1281 check_close(reporter, cubic);
1282
1283 SkPath line;
1284 line.moveTo(SK_Scalar1, SK_Scalar1);
1285 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1286 check_close(reporter, line);
1287 line.close();
1288 check_close(reporter, line);
1289
1290 SkPath rect2;
1291 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1292 rect2.close();
1293 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1294 check_close(reporter, rect2);
1295 rect2.close();
1296 check_close(reporter, rect2);
1297
1298 SkPath oval3;
1299 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1300 oval3.close();
1301 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1302 check_close(reporter, oval3);
1303 oval3.close();
1304 check_close(reporter, oval3);
1305
1306 SkPath moves;
1307 moves.moveTo(SK_Scalar1, SK_Scalar1);
1308 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1309 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1310 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1311 check_close(reporter, moves);
1312
1313 stroke_tiny_cubic();
1314}
1315
1316static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1317 SkPath::Convexity expected) {
1318 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1319 SkPath::Convexity c = copy.getConvexity();
1320 REPORTER_ASSERT(reporter, c == expected);
1321}
1322
1323static void test_path_crbug389050(skiatest::Reporter* reporter) {
1324 SkPath tinyConvexPolygon;
1325 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1326 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1327 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1328 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1329 tinyConvexPolygon.close();
1330 tinyConvexPolygon.getConvexity();
1331 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
1332 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
1333
1334 SkPath platTriangle;
1335 platTriangle.moveTo(0, 0);
1336 platTriangle.lineTo(200, 0);
1337 platTriangle.lineTo(100, 0.04f);
1338 platTriangle.close();
1339 platTriangle.getConvexity();
1340 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1341
1342 platTriangle.reset();
1343 platTriangle.moveTo(0, 0);
1344 platTriangle.lineTo(200, 0);
1345 platTriangle.lineTo(100, 0.03f);
1346 platTriangle.close();
1347 platTriangle.getConvexity();
1348 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
1349}
1350
1351static void test_convexity2(skiatest::Reporter* reporter) {
1352 SkPath pt;
1353 pt.moveTo(0, 0);
1354 pt.close();
1355 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
1356 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
1357
1358 SkPath line;
1359 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1360 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
1361 line.close();
1362 check_convexity(reporter, line, SkPath::kConvex_Convexity);
1363 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
1364
1365 SkPath triLeft;
1366 triLeft.moveTo(0, 0);
1367 triLeft.lineTo(SK_Scalar1, 0);
1368 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
1369 triLeft.close();
1370 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
1371 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
1372
1373 SkPath triRight;
1374 triRight.moveTo(0, 0);
1375 triRight.lineTo(-SK_Scalar1, 0);
1376 triRight.lineTo(SK_Scalar1, SK_Scalar1);
1377 triRight.close();
1378 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
1379 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
1380
1381 SkPath square;
1382 square.moveTo(0, 0);
1383 square.lineTo(SK_Scalar1, 0);
1384 square.lineTo(SK_Scalar1, SK_Scalar1);
1385 square.lineTo(0, SK_Scalar1);
1386 square.close();
1387 check_convexity(reporter, square, SkPath::kConvex_Convexity);
1388 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
1389
1390 SkPath redundantSquare;
1391 redundantSquare.moveTo(0, 0);
1392 redundantSquare.lineTo(0, 0);
1393 redundantSquare.lineTo(0, 0);
1394 redundantSquare.lineTo(SK_Scalar1, 0);
1395 redundantSquare.lineTo(SK_Scalar1, 0);
1396 redundantSquare.lineTo(SK_Scalar1, 0);
1397 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1398 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1399 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1400 redundantSquare.lineTo(0, SK_Scalar1);
1401 redundantSquare.lineTo(0, SK_Scalar1);
1402 redundantSquare.lineTo(0, SK_Scalar1);
1403 redundantSquare.close();
1404 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
1405 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
1406
1407 SkPath bowTie;
1408 bowTie.moveTo(0, 0);
1409 bowTie.lineTo(0, 0);
1410 bowTie.lineTo(0, 0);
1411 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1412 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1413 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1414 bowTie.lineTo(SK_Scalar1, 0);
1415 bowTie.lineTo(SK_Scalar1, 0);
1416 bowTie.lineTo(SK_Scalar1, 0);
1417 bowTie.lineTo(0, SK_Scalar1);
1418 bowTie.lineTo(0, SK_Scalar1);
1419 bowTie.lineTo(0, SK_Scalar1);
1420 bowTie.close();
1421 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
1422 check_direction(reporter, bowTie, kDontCheckDir);
1423
1424 SkPath spiral;
1425 spiral.moveTo(0, 0);
1426 spiral.lineTo(100*SK_Scalar1, 0);
1427 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1428 spiral.lineTo(0, 100*SK_Scalar1);
1429 spiral.lineTo(0, 50*SK_Scalar1);
1430 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1431 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
1432 spiral.close();
1433 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
1434 check_direction(reporter, spiral, kDontCheckDir);
1435
1436 SkPath dent;
1437 dent.moveTo(0, 0);
1438 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1439 dent.lineTo(0, 100*SK_Scalar1);
1440 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1441 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
1442 dent.close();
1443 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
1444 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
1445
1446 // https://bug.skia.org/2235
1447 SkPath strokedSin;
1448 for (int i = 0; i < 2000; i++) {
1449 SkScalar x = SkIntToScalar(i) / 2;
1450 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1451 if (0 == i) {
1452 strokedSin.moveTo(x, y);
1453 } else {
1454 strokedSin.lineTo(x, y);
1455 }
1456 }
1457 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1458 stroke.setStrokeStyle(2 * SK_Scalar1);
1459 stroke.applyToPath(&strokedSin, strokedSin);
1460 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1461 check_direction(reporter, strokedSin, kDontCheckDir);
1462
1463 // http://crbug.com/412640
1464 SkPath degenerateConcave;
1465 degenerateConcave.moveTo(148.67912f, 191.875f);
1466 degenerateConcave.lineTo(470.37695f, 7.5f);
1467 degenerateConcave.lineTo(148.67912f, 191.875f);
1468 degenerateConcave.lineTo(41.446522f, 376.25f);
1469 degenerateConcave.lineTo(-55.971577f, 460.0f);
1470 degenerateConcave.lineTo(41.446522f, 376.25f);
1471 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
1472 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
1473
1474 // http://crbug.com/433683
1475 SkPath badFirstVector;
1476 badFirstVector.moveTo(501.087708f, 319.610352f);
1477 badFirstVector.lineTo(501.087708f, 319.610352f);
1478 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1479 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1480 badFirstVector.lineTo(301.557678f, 98.044601f);
1481 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1482 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1483 badFirstVector.lineTo(504.912292f, 316.389648f);
1484 badFirstVector.lineTo(501.087708f, 319.610352f);
1485 badFirstVector.close();
1486 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
1487}
1488
1489static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1490 const SkRect& bounds) {
1491 REPORTER_ASSERT(reporter, p.isConvex());
1492 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
1493
1494 SkPath p2(p);
1495 REPORTER_ASSERT(reporter, p2.isConvex());
1496 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1497
1498 SkPath other;
1499 other.swap(p2);
1500 REPORTER_ASSERT(reporter, other.isConvex());
1501 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1502}
1503
1504static void setFromString(SkPath* path, const char str[]) {
1505 bool first = true;
1506 while (str) {
1507 SkScalar x, y;
1508 str = SkParse::FindScalar(str, &x);
1509 if (nullptr == str) {
1510 break;
1511 }
1512 str = SkParse::FindScalar(str, &y);
1513 SkASSERT(str);
1514 if (first) {
1515 path->moveTo(x, y);
1516 first = false;
1517 } else {
1518 path->lineTo(x, y);
1519 }
1520 }
1521}
1522
1523static void test_convexity(skiatest::Reporter* reporter) {
1524 SkPath path;
1525
1526 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1527 path.addCircle(0, 0, SkIntToScalar(10));
1528 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1529 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
1530 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1531
1532 path.reset();
1533 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
1534 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1535 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1536
1537 path.reset();
1538 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
1539 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1540 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1541
1542 path.reset();
1543 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1544 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1545
1546 static const struct {
1547 const char* fPathStr;
1548 SkPath::Convexity fExpectedConvexity;
1549 SkPathPriv::FirstDirection fExpectedDirection;
1550 } gRec[] = {
1551 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1552 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1553 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1554 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1555 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1556 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
1557 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
1558 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
1559 };
1560
1561 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1562 SkPath path;
1563 setFromString(&path, gRec[i].fPathStr);
1564 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1565 check_direction(reporter, path, gRec[i].fExpectedDirection);
1566 // check after setting the initial convex and direction
1567 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1568 SkPath copy(path);
1569 SkPathPriv::FirstDirection dir;
1570 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1571 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
1572 ^ foundDir);
1573 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1574 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1575 }
1576 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1577 check_direction(reporter, path, gRec[i].fExpectedDirection);
1578 }
1579
1580 static const SkPoint nonFinitePts[] = {
1581 { SK_ScalarInfinity, 0 },
1582 { 0, SK_ScalarInfinity },
1583 { SK_ScalarInfinity, SK_ScalarInfinity },
1584 { SK_ScalarNegativeInfinity, 0},
1585 { 0, SK_ScalarNegativeInfinity },
1586 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1587 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1588 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1589 { SK_ScalarNaN, 0 },
1590 { 0, SK_ScalarNaN },
1591 { SK_ScalarNaN, SK_ScalarNaN },
1592 };
1593
1594 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1595
1596 static const SkPoint finitePts[] = {
1597 { SK_ScalarMax, 0 },
1598 { 0, SK_ScalarMax },
1599 { SK_ScalarMax, SK_ScalarMax },
1600 { SK_ScalarMin, 0 },
1601 { 0, SK_ScalarMin },
1602 { SK_ScalarMin, SK_ScalarMin },
1603 };
1604
1605 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1606
1607 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1608 int i = (int) (index % nonFinitePtsCount);
1609 int f = (int) (index % finitePtsCount);
1610 int g = (int) ((f + 1) % finitePtsCount);
1611 path.reset();
1612 switch (index % 13) {
1613 case 0: path.lineTo(nonFinitePts[i]); break;
1614 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1615 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1616 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1617 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1618 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1619 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1620 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1621 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1622 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1623 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1624 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1625 case 12: path.moveTo(nonFinitePts[i]); break;
1626 }
1627 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1628 }
1629
1630 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1631 int f = (int) (index % finitePtsCount);
1632 int g = (int) ((f + 1) % finitePtsCount);
1633 path.reset();
1634 int curveSelect = index % 11;
1635 switch (curveSelect) {
1636 case 0: path.moveTo(finitePts[f]); break;
1637 case 1: path.lineTo(finitePts[f]); break;
1638 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1639 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1640 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1641 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1642 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1643 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1644 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1645 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1646 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1647 }
1648 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1649 : SkPath::kUnknown_Convexity);
1650 }
1651
1652 path.reset();
1653 path.moveTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1654 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eea38)); // -0.284072f, -0.0622351f
1655 path.lineTo(SkBits2Float(0xbe9171a0), SkBits2Float(0xbd7ee5a7)); // -0.28407f, -0.0622307f
1656 path.lineTo(SkBits2Float(0xbe917147), SkBits2Float(0xbd7ed886)); // -0.284067f, -0.0622182f
1657 path.lineTo(SkBits2Float(0xbe917378), SkBits2Float(0xbd7ee1a9)); // -0.284084f, -0.0622269f
1658 path.lineTo(SkBits2Float(0xbe9171db), SkBits2Float(0xbd7eeb5d)); // -0.284072f, -0.0622362f
1659 path.close();
1660 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1661
1662}
1663
1664static void test_isLine(skiatest::Reporter* reporter) {
1665 SkPath path;
1666 SkPoint pts[2];
1667 const SkScalar value = SkIntToScalar(5);
1668
1669 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1670
1671 // set some non-zero values
1672 pts[0].set(value, value);
1673 pts[1].set(value, value);
1674 REPORTER_ASSERT(reporter, !path.isLine(pts));
1675 // check that pts was untouched
1676 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1677 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1678
1679 const SkScalar moveX = SkIntToScalar(1);
1680 const SkScalar moveY = SkIntToScalar(2);
1681 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
1682
1683 path.moveTo(moveX, moveY);
1684 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1685 REPORTER_ASSERT(reporter, !path.isLine(pts));
1686 // check that pts was untouched
1687 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1688 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1689
1690 const SkScalar lineX = SkIntToScalar(2);
1691 const SkScalar lineY = SkIntToScalar(2);
1692 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
1693
1694 path.lineTo(lineX, lineY);
1695 REPORTER_ASSERT(reporter, path.isLine(nullptr));
1696
1697 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1698 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1699 REPORTER_ASSERT(reporter, path.isLine(pts));
1700 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1701 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1702
1703 path.lineTo(0, 0); // too many points/verbs
1704 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1705 REPORTER_ASSERT(reporter, !path.isLine(pts));
1706 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1707 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1708
1709 path.reset();
1710 path.quadTo(1, 1, 2, 2);
1711 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
1712}
1713
1714static void test_conservativelyContains(skiatest::Reporter* reporter) {
1715 SkPath path;
1716
1717 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1718 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1719
1720 // A circle that bounds kBaseRect (with a significant amount of slop)
1721 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
1722 circleR *= 1.75f / 2;
1723 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1724
1725 // round-rect radii
1726 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
1727
1728 static const struct SUPPRESS_VISIBILITY_WARNING {
1729 SkRect fQueryRect;
1730 bool fInRect;
1731 bool fInCircle;
1732 bool fInRR;
1733 bool fInCubicRR;
1734 } kQueries[] = {
1735 {kBaseRect, true, true, false, false},
1736
1737 // rect well inside of kBaseRect
1738 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1739 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1740 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1741 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
1742 true, true, true, true},
1743
1744 // rects with edges off by one from kBaseRect's edges
1745 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1746 kBaseRect.width(), kBaseRect.height() + 1),
1747 false, true, false, false},
1748 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1749 kBaseRect.width() + 1, kBaseRect.height()),
1750 false, true, false, false},
1751 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1752 kBaseRect.width() + 1, kBaseRect.height() + 1),
1753 false, true, false, false},
1754 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1755 kBaseRect.width(), kBaseRect.height()),
1756 false, true, false, false},
1757 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1758 kBaseRect.width(), kBaseRect.height()),
1759 false, true, false, false},
1760 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1761 kBaseRect.width() + 2, kBaseRect.height()),
1762 false, true, false, false},
1763 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1764 kBaseRect.width() + 2, kBaseRect.height()),
1765 false, true, false, false},
1766
1767 // zero-w/h rects at each corner of kBaseRect
1768 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1769 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1770 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1771 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
1772
1773 // far away rect
1774 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1775 SkIntToScalar(10), SkIntToScalar(10)),
1776 false, false, false, false},
1777
1778 // very large rect containing kBaseRect
1779 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1780 kBaseRect.fTop - 5 * kBaseRect.height(),
1781 11 * kBaseRect.width(), 11 * kBaseRect.height()),
1782 false, false, false, false},
1783
1784 // skinny rect that spans same y-range as kBaseRect
1785 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1786 SkIntToScalar(1), kBaseRect.height()),
1787 true, true, true, true},
1788
1789 // short rect that spans same x-range as kBaseRect
1790 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
1791 true, true, true, true},
1792
1793 // skinny rect that spans slightly larger y-range than kBaseRect
1794 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1795 SkIntToScalar(1), kBaseRect.height() + 1),
1796 false, true, false, false},
1797
1798 // short rect that spans slightly larger x-range than kBaseRect
1799 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1800 kBaseRect.width() + 1, SkScalar(1)),
1801 false, true, false, false},
1802 };
1803
1804 for (int inv = 0; inv < 4; ++inv) {
1805 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
1806 SkRect qRect = kQueries[q].fQueryRect;
1807 if (inv & 0x1) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001808 using std::swap;
1809 swap(qRect.fLeft, qRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04001810 }
1811 if (inv & 0x2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001812 using std::swap;
1813 swap(qRect.fTop, qRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04001814 }
1815 for (int d = 0; d < 2; ++d) {
1816 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1817 path.reset();
1818 path.addRect(kBaseRect, dir);
1819 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1820 path.conservativelyContainsRect(qRect));
1821
1822 path.reset();
1823 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1824 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1825 path.conservativelyContainsRect(qRect));
1826
1827 path.reset();
1828 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1829 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1830 path.conservativelyContainsRect(qRect));
1831
1832 path.reset();
1833 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1834 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1835 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1836 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1837 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1838 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1839 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1840 path.close();
1841 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1842 path.conservativelyContainsRect(qRect));
1843
1844 }
1845 // Slightly non-convex shape, shouldn't contain any rects.
1846 path.reset();
1847 path.moveTo(0, 0);
1848 path.lineTo(SkIntToScalar(50), 0.05f);
1849 path.lineTo(SkIntToScalar(100), 0);
1850 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1851 path.lineTo(0, SkIntToScalar(100));
1852 path.close();
1853 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1854 }
1855 }
1856
1857 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1858 path.reset();
1859 path.moveTo(0, 0);
1860 path.lineTo(SkIntToScalar(100), 0);
1861 path.lineTo(0, SkIntToScalar(100));
1862
1863 // inside, on along top edge
1864 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1865 SkIntToScalar(10),
1866 SkIntToScalar(10))));
1867 // above
1868 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1869 SkRect::MakeXYWH(SkIntToScalar(50),
1870 SkIntToScalar(-10),
1871 SkIntToScalar(10),
1872 SkIntToScalar(10))));
1873 // to the left
1874 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1875 SkIntToScalar(5),
1876 SkIntToScalar(5),
1877 SkIntToScalar(5))));
1878
1879 // outside the diagonal edge
1880 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1881 SkIntToScalar(200),
1882 SkIntToScalar(20),
1883 SkIntToScalar(5))));
1884
1885
1886 // Test that multiple move commands do not cause asserts.
Yuqian Li3154a532017-09-06 13:33:30 -04001887 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
Yuqian Li3154a532017-09-06 13:33:30 -04001888 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1889 SkIntToScalar(10),
1890 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001891
1892 // Same as above path and first test but with an extra moveTo.
1893 path.reset();
1894 path.moveTo(100, 100);
1895 path.moveTo(0, 0);
1896 path.lineTo(SkIntToScalar(100), 0);
1897 path.lineTo(0, SkIntToScalar(100));
Brian Osman205a1262017-09-18 13:13:48 +00001898 // Convexity logic is now more conservative, so that multiple (non-trailing) moveTos make a
1899 // path non-convex.
1900 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1901 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1902 SkIntToScalar(10),
1903 SkIntToScalar(10))));
Yuqian Li3154a532017-09-06 13:33:30 -04001904
1905 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
1906 // following the non-empty sub-path. Verifies that this does not trigger assertions.
1907 path.reset();
1908 path.moveTo(0, 0);
1909 path.lineTo(SkIntToScalar(100), 0);
1910 path.lineTo(0, SkIntToScalar(100));
1911 path.moveTo(100, 100);
1912
1913 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1914 SkIntToScalar(10),
1915 SkIntToScalar(10))));
1916
1917 // Test that multiple move commands do not cause asserts and that the function
1918 // is not confused by the multiple moves.
1919 path.reset();
1920 path.moveTo(0, 0);
1921 path.lineTo(SkIntToScalar(100), 0);
1922 path.lineTo(0, SkIntToScalar(100));
1923 path.moveTo(0, SkIntToScalar(200));
1924 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1925 path.lineTo(0, SkIntToScalar(300));
1926
1927 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1928 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1929 SkIntToScalar(10),
1930 SkIntToScalar(10))));
1931
1932 path.reset();
1933 path.lineTo(100, 100);
1934 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
1935
1936 // An empty path should not contain any rectangle. It's questionable whether an empty path
1937 // contains an empty rectangle. However, since it is a conservative test it is ok to
1938 // return false.
1939 path.reset();
1940 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(1,1)));
1941 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeWH(0,0)));
1942}
1943
1944static void test_isRect_open_close(skiatest::Reporter* reporter) {
1945 SkPath path;
1946 bool isClosed;
1947
1948 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
1949 path.close();
1950
1951 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
1952 REPORTER_ASSERT(reporter, isClosed);
1953}
1954
1955// Simple isRect test is inline TestPath, below.
1956// test_isRect provides more extensive testing.
1957static void test_isRect(skiatest::Reporter* reporter) {
1958 test_isRect_open_close(reporter);
1959
1960 // passing tests (all moveTo / lineTo...
1961 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1962 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1963 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1964 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1965 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1966 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1967 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1968 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1969 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1970 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1971 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
1972 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1973 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1974 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
1975 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
1976
1977 // failing tests
1978 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1979 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1980 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1981 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1982 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1983 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1984 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1985 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1986 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1987 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1988 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
1989
1990 // no close, but we should detect them as fillably the same as a rect
1991 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1992 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1993 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1994
1995 // like c2, but we double-back on ourselves
1996 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1997 // like c2, but we overshoot the start point
1998 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1999 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
2000
2001 struct IsRectTest {
2002 SkPoint *fPoints;
2003 int fPointCount;
2004 bool fClose;
2005 bool fIsRect;
2006 } tests[] = {
2007 { r1, SK_ARRAY_COUNT(r1), true, true },
2008 { r2, SK_ARRAY_COUNT(r2), true, true },
2009 { r3, SK_ARRAY_COUNT(r3), true, true },
2010 { r4, SK_ARRAY_COUNT(r4), true, true },
2011 { r5, SK_ARRAY_COUNT(r5), true, true },
2012 { r6, SK_ARRAY_COUNT(r6), true, true },
2013 { r7, SK_ARRAY_COUNT(r7), true, true },
2014 { r8, SK_ARRAY_COUNT(r8), true, true },
2015 { r9, SK_ARRAY_COUNT(r9), true, true },
2016 { ra, SK_ARRAY_COUNT(ra), true, true },
2017 { rb, SK_ARRAY_COUNT(rb), true, true },
2018 { rc, SK_ARRAY_COUNT(rc), true, true },
2019 { rd, SK_ARRAY_COUNT(rd), true, true },
2020 { re, SK_ARRAY_COUNT(re), true, true },
2021 { rf, SK_ARRAY_COUNT(rf), true, true },
2022
2023 { f1, SK_ARRAY_COUNT(f1), true, false },
2024 { f2, SK_ARRAY_COUNT(f2), true, false },
2025 { f3, SK_ARRAY_COUNT(f3), true, false },
2026 { f4, SK_ARRAY_COUNT(f4), true, false },
2027 { f5, SK_ARRAY_COUNT(f5), true, false },
2028 { f6, SK_ARRAY_COUNT(f6), true, false },
2029 { f7, SK_ARRAY_COUNT(f7), true, false },
2030 { f8, SK_ARRAY_COUNT(f8), true, false },
2031 { f9, SK_ARRAY_COUNT(f9), true, false },
2032 { fa, SK_ARRAY_COUNT(fa), true, false },
2033 { fb, SK_ARRAY_COUNT(fb), true, false },
2034
2035 { c1, SK_ARRAY_COUNT(c1), false, true },
2036 { c2, SK_ARRAY_COUNT(c2), false, true },
2037 { c3, SK_ARRAY_COUNT(c3), false, true },
2038
2039 { d1, SK_ARRAY_COUNT(d1), false, false },
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002040 { d2, SK_ARRAY_COUNT(d2), false, true },
Yuqian Li3154a532017-09-06 13:33:30 -04002041 { d3, SK_ARRAY_COUNT(d3), false, false },
2042 };
2043
2044 const size_t testCount = SK_ARRAY_COUNT(tests);
2045 int index;
2046 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2047 SkPath path;
2048 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2049 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2050 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2051 }
2052 if (tests[testIndex].fClose) {
2053 path.close();
2054 }
2055 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
2056
2057 if (tests[testIndex].fIsRect) {
2058 SkRect computed, expected;
2059 bool isClosed;
2060 SkPath::Direction direction;
2061 SkPathPriv::FirstDirection cheapDirection;
Cary Clarkdbc59ba2018-04-19 07:37:29 -04002062 int pointCount = tests[testIndex].fPointCount - (d2 == tests[testIndex].fPoints);
2063 expected.set(tests[testIndex].fPoints, pointCount);
Yuqian Li3154a532017-09-06 13:33:30 -04002064 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
2065 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2066 REPORTER_ASSERT(reporter, expected == computed);
2067 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
2068 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
2069 } else {
2070 SkRect computed;
2071 computed.set(123, 456, 789, 1011);
Mike Kleinde5d6eb2018-08-13 12:09:41 -04002072 for (auto c : {true, false})
2073 for (auto d : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) {
2074 bool isClosed = c;
2075 SkPath::Direction direction = d;
2076 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2077 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2078 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2079 REPORTER_ASSERT(reporter, isClosed == c);
2080 REPORTER_ASSERT(reporter, direction == d);
2081 }
Yuqian Li3154a532017-09-06 13:33:30 -04002082 }
2083 }
2084
2085 // fail, close then line
2086 SkPath path1;
2087 path1.moveTo(r1[0].fX, r1[0].fY);
2088 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2089 path1.lineTo(r1[index].fX, r1[index].fY);
2090 }
2091 path1.close();
2092 path1.lineTo(1, 0);
2093 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2094
2095 // fail, move in the middle
2096 path1.reset();
2097 path1.moveTo(r1[0].fX, r1[0].fY);
2098 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2099 if (index == 2) {
2100 path1.moveTo(1, .5f);
2101 }
2102 path1.lineTo(r1[index].fX, r1[index].fY);
2103 }
2104 path1.close();
2105 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2106
2107 // fail, move on the edge
2108 path1.reset();
2109 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2110 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2111 path1.lineTo(r1[index].fX, r1[index].fY);
2112 }
2113 path1.close();
2114 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2115
2116 // fail, quad
2117 path1.reset();
2118 path1.moveTo(r1[0].fX, r1[0].fY);
2119 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2120 if (index == 2) {
2121 path1.quadTo(1, .5f, 1, .5f);
2122 }
2123 path1.lineTo(r1[index].fX, r1[index].fY);
2124 }
2125 path1.close();
2126 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2127
2128 // fail, cubic
2129 path1.reset();
2130 path1.moveTo(r1[0].fX, r1[0].fY);
2131 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2132 if (index == 2) {
2133 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2134 }
2135 path1.lineTo(r1[index].fX, r1[index].fY);
2136 }
2137 path1.close();
2138 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2139}
2140
2141static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2142 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2143 SkRect r = SkRect::MakeEmpty();
2144 SkPath::Direction d = SkPath::kCCW_Direction;
2145 unsigned s = ~0U;
2146
2147 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2148 REPORTER_ASSERT(reporter, r == rect);
2149 REPORTER_ASSERT(reporter, d == dir);
2150 REPORTER_ASSERT(reporter, s == start);
2151}
2152
2153static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002154 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002155 SkRect r = SkRect::MakeEmpty();
2156 SkPath::Direction d = SkPath::kCCW_Direction;
2157 unsigned s = ~0U;
2158
2159 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2160 const SkRect emptyRect = SkRect::MakeEmpty();
2161 SkPath path;
2162 for (int start = 0; start < 4; ++start) {
2163 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2164 SkPath path;
2165 path.addRect(testRect, dir, start);
2166 check_simple_closed_rect(reporter, path, testRect, dir, start);
2167 path.close();
2168 check_simple_closed_rect(reporter, path, testRect, dir, start);
2169 SkPath path2 = path;
2170 path2.lineTo(10, 10);
2171 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2172 path2 = path;
2173 path2.moveTo(10, 10);
2174 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2175 path2 = path;
2176 path2.addRect(testRect, dir, start);
2177 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2178 // Make the path by hand, manually closing it.
2179 path2.reset();
2180 SkPath::RawIter iter(path);
2181 SkPath::Verb v;
2182 SkPoint verbPts[4];
2183 SkPoint firstPt = {0.f, 0.f};
2184 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2185 switch(v) {
2186 case SkPath::kMove_Verb:
2187 firstPt = verbPts[0];
2188 path2.moveTo(verbPts[0]);
2189 break;
2190 case SkPath::kLine_Verb:
2191 path2.lineTo(verbPts[1]);
2192 break;
2193 default:
2194 break;
2195 }
2196 }
2197 // We haven't closed it yet...
2198 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2199 // ... now we do and test again.
2200 path2.lineTo(firstPt);
2201 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2202 // A redundant close shouldn't cause a failure.
2203 path2.close();
2204 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2205 // Degenerate point and line rects are not allowed
2206 path2.reset();
2207 path2.addRect(emptyRect, dir, start);
2208 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2209 SkRect degenRect = testRect;
2210 degenRect.fLeft = degenRect.fRight;
2211 path2.reset();
2212 path2.addRect(degenRect, dir, start);
2213 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2214 degenRect = testRect;
2215 degenRect.fTop = degenRect.fBottom;
2216 path2.reset();
2217 path2.addRect(degenRect, dir, start);
2218 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2219 // An inverted rect makes a rect path, but changes the winding dir and start point.
2220 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2221 ? SkPath::kCCW_Direction
2222 : SkPath::kCW_Direction;
2223 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2224 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2225 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002226 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002227 path2.reset();
2228 path2.addRect(swapRect, dir, start);
2229 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2230 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002231 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002232 path2.reset();
2233 path2.addRect(swapRect, dir, start);
2234 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2235 }
2236 }
2237 // down, up, left, close
2238 path.reset();
2239 path.moveTo(1, 1);
2240 path.lineTo(1, 2);
2241 path.lineTo(1, 1);
2242 path.lineTo(0, 1);
2243 SkRect rect;
2244 SkPath::Direction dir;
2245 unsigned start;
2246 path.close();
2247 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2248 // right, left, up, close
2249 path.reset();
2250 path.moveTo(1, 1);
2251 path.lineTo(2, 1);
2252 path.lineTo(1, 1);
2253 path.lineTo(1, 0);
2254 path.close();
2255 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2256 // parallelogram with horizontal edges
2257 path.reset();
2258 path.moveTo(1, 0);
2259 path.lineTo(3, 0);
2260 path.lineTo(2, 1);
2261 path.lineTo(0, 1);
2262 path.close();
2263 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2264 // parallelogram with vertical edges
2265 path.reset();
2266 path.moveTo(0, 1);
2267 path.lineTo(0, 3);
2268 path.lineTo(1, 2);
2269 path.lineTo(1, 0);
2270 path.close();
2271 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2272
2273}
2274
2275static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2276 // passing tests (all moveTo / lineTo...
2277 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2278 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2279 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2280 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2281 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2282 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2283 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2284 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2285 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2286 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2287 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2288 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2289 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2290 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2291
2292 // failing tests
2293 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2294 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2295 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2296 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2297 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2298 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2299 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2300 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2301
2302 // success, no close is OK
2303 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2304 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2305
2306 struct IsNestedRectTest {
2307 SkPoint *fPoints;
2308 int fPointCount;
2309 SkPathPriv::FirstDirection fDirection;
2310 bool fClose;
2311 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2312 } tests[] = {
2313 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2314 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2315 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2316 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2317 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2318 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2319 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2320 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2321 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2322 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2323 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2324 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2325 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2326 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
2327
2328 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2329 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2330 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2331 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2332 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2333 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2334 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2335 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2336
2337 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2338 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2339 };
2340
2341 const size_t testCount = SK_ARRAY_COUNT(tests);
2342 int index;
2343 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2344 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2345 SkPath path;
2346 if (rectFirst) {
2347 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2348 }
2349 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2350 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2351 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2352 }
2353 if (tests[testIndex].fClose) {
2354 path.close();
2355 }
2356 if (!rectFirst) {
2357 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2358 }
2359 REPORTER_ASSERT(reporter,
2360 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
2361 if (tests[testIndex].fIsNestedRect) {
2362 SkRect expected[2], computed[2];
2363 SkPathPriv::FirstDirection expectedDirs[2];
2364 SkPath::Direction computedDirs[2];
2365 SkRect testBounds;
2366 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
2367 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2368 expected[1] = testBounds;
2369 if (rectFirst) {
2370 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2371 } else {
2372 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2373 }
2374 expectedDirs[1] = tests[testIndex].fDirection;
2375 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
2376 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2377 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2378 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2379 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2380 }
2381 }
2382
2383 // fail, close then line
2384 SkPath path1;
2385 if (rectFirst) {
2386 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2387 }
2388 path1.moveTo(r1[0].fX, r1[0].fY);
2389 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2390 path1.lineTo(r1[index].fX, r1[index].fY);
2391 }
2392 path1.close();
2393 path1.lineTo(1, 0);
2394 if (!rectFirst) {
2395 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2396 }
2397 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2398
2399 // fail, move in the middle
2400 path1.reset();
2401 if (rectFirst) {
2402 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2403 }
2404 path1.moveTo(r1[0].fX, r1[0].fY);
2405 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2406 if (index == 2) {
2407 path1.moveTo(1, .5f);
2408 }
2409 path1.lineTo(r1[index].fX, r1[index].fY);
2410 }
2411 path1.close();
2412 if (!rectFirst) {
2413 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2414 }
2415 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2416
2417 // fail, move on the edge
2418 path1.reset();
2419 if (rectFirst) {
2420 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2421 }
2422 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2423 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2424 path1.lineTo(r1[index].fX, r1[index].fY);
2425 }
2426 path1.close();
2427 if (!rectFirst) {
2428 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2429 }
2430 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2431
2432 // fail, quad
2433 path1.reset();
2434 if (rectFirst) {
2435 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2436 }
2437 path1.moveTo(r1[0].fX, r1[0].fY);
2438 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2439 if (index == 2) {
2440 path1.quadTo(1, .5f, 1, .5f);
2441 }
2442 path1.lineTo(r1[index].fX, r1[index].fY);
2443 }
2444 path1.close();
2445 if (!rectFirst) {
2446 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2447 }
2448 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2449
2450 // fail, cubic
2451 path1.reset();
2452 if (rectFirst) {
2453 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2454 }
2455 path1.moveTo(r1[0].fX, r1[0].fY);
2456 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2457 if (index == 2) {
2458 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2459 }
2460 path1.lineTo(r1[index].fX, r1[index].fY);
2461 }
2462 path1.close();
2463 if (!rectFirst) {
2464 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2465 }
2466 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2467
2468 // fail, not nested
2469 path1.reset();
2470 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2471 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
2472 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2473 }
2474
2475 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2476 SkPath path;
2477 path.moveTo(0, 0);
2478 path.lineTo(10, 0);
2479 path.lineTo(10, 10);
2480 path.lineTo(0, 10);
2481 path.lineTo(0, 0);
2482 path.moveTo(1, 1);
2483 path.lineTo(9, 1);
2484 path.lineTo(9, 9);
2485 path.lineTo(1, 9);
2486 path.lineTo(1, 1);
2487 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
2488
2489 // pass, stroke rect
2490 SkPath src, dst;
2491 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2492 SkPaint strokePaint;
2493 strokePaint.setStyle(SkPaint::kStroke_Style);
2494 strokePaint.setStrokeWidth(2);
2495 strokePaint.getFillPath(src, &dst);
2496 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
2497}
2498
2499static void write_and_read_back(skiatest::Reporter* reporter,
2500 const SkPath& p) {
2501 SkWriter32 writer;
2502 writer.writePath(p);
2503 size_t size = writer.bytesWritten();
2504 SkAutoMalloc storage(size);
2505 writer.flatten(storage.get());
2506 SkReader32 reader(storage.get(), size);
2507
2508 SkPath readBack;
2509 REPORTER_ASSERT(reporter, readBack != p);
2510 reader.readPath(&readBack);
2511 REPORTER_ASSERT(reporter, readBack == p);
2512
2513 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
2514 p.getConvexityOrUnknown());
2515
2516 SkRect oval0, oval1;
2517 SkPath::Direction dir0, dir1;
2518 unsigned start0, start1;
2519 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002520 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2521 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002522 REPORTER_ASSERT(reporter, oval0 == oval1);
2523 REPORTER_ASSERT(reporter, dir0 == dir1);
2524 REPORTER_ASSERT(reporter, start0 == start1);
2525 }
2526 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2527 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002528 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2529 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002530 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2531 REPORTER_ASSERT(reporter, dir0 == dir1);
2532 REPORTER_ASSERT(reporter, start0 == start1);
2533 }
2534 const SkRect& origBounds = p.getBounds();
2535 const SkRect& readBackBounds = readBack.getBounds();
2536
2537 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2538}
2539
Yuqian Li3154a532017-09-06 13:33:30 -04002540static void test_flattening(skiatest::Reporter* reporter) {
2541 SkPath p;
2542
2543 static const SkPoint pts[] = {
2544 { 0, 0 },
2545 { SkIntToScalar(10), SkIntToScalar(10) },
2546 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2547 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2548 };
2549 p.moveTo(pts[0]);
2550 p.lineTo(pts[1]);
2551 p.quadTo(pts[2], pts[3]);
2552 p.cubicTo(pts[4], pts[5], pts[6]);
2553
2554 write_and_read_back(reporter, p);
2555
2556 // create a buffer that should be much larger than the path so we don't
2557 // kill our stack if writer goes too far.
2558 char buffer[1024];
2559 size_t size1 = p.writeToMemory(nullptr);
2560 size_t size2 = p.writeToMemory(buffer);
2561 REPORTER_ASSERT(reporter, size1 == size2);
2562
2563 SkPath p2;
2564 size_t size3 = p2.readFromMemory(buffer, 1024);
2565 REPORTER_ASSERT(reporter, size1 == size3);
2566 REPORTER_ASSERT(reporter, p == p2);
2567
2568 size3 = p2.readFromMemory(buffer, 0);
2569 REPORTER_ASSERT(reporter, !size3);
2570
2571 SkPath tooShort;
2572 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2573 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2574
2575 char buffer2[1024];
2576 size3 = p2.writeToMemory(buffer2);
2577 REPORTER_ASSERT(reporter, size1 == size3);
2578 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2579
2580 // test persistence of the oval flag & convexity
2581 {
2582 SkPath oval;
2583 SkRect rect = SkRect::MakeWH(10, 10);
2584 oval.addOval(rect);
2585
2586 write_and_read_back(reporter, oval);
2587 }
Yuqian Li3154a532017-09-06 13:33:30 -04002588}
2589
2590static void test_transform(skiatest::Reporter* reporter) {
2591 SkPath p;
2592
2593#define CONIC_PERSPECTIVE_BUG_FIXED 0
2594 static const SkPoint pts[] = {
2595 { 0, 0 }, // move
2596 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2597 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2598 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2599#if CONIC_PERSPECTIVE_BUG_FIXED
2600 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2601#endif
2602 };
2603 const int kPtCount = SK_ARRAY_COUNT(pts);
2604
2605 p.moveTo(pts[0]);
2606 p.lineTo(pts[1]);
2607 p.quadTo(pts[2], pts[3]);
2608 p.cubicTo(pts[4], pts[5], pts[6]);
2609#if CONIC_PERSPECTIVE_BUG_FIXED
2610 p.conicTo(pts[4], pts[5], 0.5f);
2611#endif
2612 p.close();
2613
2614 {
2615 SkMatrix matrix;
2616 matrix.reset();
2617 SkPath p1;
2618 p.transform(matrix, &p1);
2619 REPORTER_ASSERT(reporter, p == p1);
2620 }
2621
2622
2623 {
2624 SkMatrix matrix;
2625 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2626
2627 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2628
2629 p.transform(matrix, &p1);
2630 SkPoint pts1[kPtCount];
2631 int count = p1.getPoints(pts1, kPtCount);
2632 REPORTER_ASSERT(reporter, kPtCount == count);
2633 for (int i = 0; i < count; ++i) {
2634 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2635 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2636 }
2637 }
2638
2639 {
2640 SkMatrix matrix;
2641 matrix.reset();
2642 matrix.setPerspX(4);
2643
2644 SkPath p1;
2645 p1.moveTo(SkPoint::Make(0, 0));
2646
2647 p.transform(matrix, &p1);
2648 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2649 p1.transform(matrix, nullptr);
2650 SkRect pBounds = p.getBounds();
2651 SkRect p1Bounds = p1.getBounds();
2652 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2653 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2654 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2655 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2656 }
2657
2658 p.reset();
2659 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
2660
2661 {
2662 SkMatrix matrix;
2663 matrix.reset();
2664 SkPath p1;
2665 p1.moveTo(SkPoint::Make(0, 0));
2666
2667 p.transform(matrix, &p1);
2668 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2669 }
2670
2671
2672 {
2673 SkMatrix matrix;
2674 matrix.reset();
2675 matrix.setScaleX(-1);
2676 SkPath p1;
2677 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2678
2679 p.transform(matrix, &p1);
2680 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2681 }
2682
2683 {
2684 SkMatrix matrix;
2685 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2686 SkPath p1;
2687 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2688
2689 p.transform(matrix, &p1);
2690 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2691 }
2692}
2693
2694static void test_zero_length_paths(skiatest::Reporter* reporter) {
2695 SkPath p;
2696 uint8_t verbs[32];
2697
2698 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2699 const char* testPath;
2700 const size_t numResultPts;
2701 const SkRect resultBound;
2702 const SkPath::Verb* resultVerbs;
2703 const size_t numResultVerbs;
2704 };
2705
2706 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2707 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2708 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2709 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2710 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2711 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2712 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2713 static const SkPath::Verb resultVerbs8[] = {
2714 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2715 };
2716 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2717 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2718 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2719 static const SkPath::Verb resultVerbs12[] = {
2720 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2721 };
2722 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2723 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2724 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2725 static const SkPath::Verb resultVerbs16[] = {
2726 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2727 };
2728 static const struct zeroPathTestData gZeroLengthTests[] = {
2729 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2730 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2731 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2732 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2733 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2734 { "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) },
2735 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2736 { "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) },
2737 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2738 { "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) },
2739 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2740 { "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) },
2741 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2742 { "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,
2743 SK_ARRAY_COUNT(resultVerbs14)
2744 },
2745 { "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) },
2746 { "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,
2747 SK_ARRAY_COUNT(resultVerbs16)
2748 }
2749 };
2750
2751 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2752 p.reset();
2753 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2754 REPORTER_ASSERT(reporter, valid);
2755 REPORTER_ASSERT(reporter, !p.isEmpty());
2756 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2757 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2758 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2759 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2760 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2761 }
2762 }
2763}
2764
2765struct SegmentInfo {
2766 SkPath fPath;
2767 int fPointCount;
2768};
2769
2770#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2771
2772static void test_segment_masks(skiatest::Reporter* reporter) {
2773 SkPath p, p2;
2774
2775 p.moveTo(0, 0);
2776 p.quadTo(100, 100, 200, 200);
2777 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2778 REPORTER_ASSERT(reporter, !p.isEmpty());
2779 p2 = p;
2780 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2781 p.cubicTo(100, 100, 200, 200, 300, 300);
2782 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2783 REPORTER_ASSERT(reporter, !p.isEmpty());
2784 p2 = p;
2785 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2786
2787 p.reset();
2788 p.moveTo(0, 0);
2789 p.cubicTo(100, 100, 200, 200, 300, 300);
2790 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2791 p2 = p;
2792 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2793
2794 REPORTER_ASSERT(reporter, !p.isEmpty());
2795}
2796
2797static void test_iter(skiatest::Reporter* reporter) {
2798 SkPath p;
2799 SkPoint pts[4];
2800
2801 // Test an iterator with no path
2802 SkPath::Iter noPathIter;
2803 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2804
2805 // Test that setting an empty path works
2806 noPathIter.setPath(p, false);
2807 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2808
2809 // Test that close path makes no difference for an empty path
2810 noPathIter.setPath(p, true);
2811 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2812
2813 // Test an iterator with an initial empty path
2814 SkPath::Iter iter(p, false);
2815 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2816
2817 // Test that close path makes no difference
2818 iter.setPath(p, true);
2819 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2820
2821
2822 struct iterTestData {
2823 const char* testPath;
2824 const bool forceClose;
2825 const bool consumeDegenerates;
2826 const size_t* numResultPtsPerVerb;
2827 const SkPoint* resultPts;
2828 const SkPath::Verb* resultVerbs;
2829 const size_t numResultVerbs;
2830 };
2831
2832 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2833 static const SkPath::Verb resultVerbs2[] = {
2834 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2835 };
2836 static const SkPath::Verb resultVerbs3[] = {
2837 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2838 };
2839 static const SkPath::Verb resultVerbs4[] = {
2840 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2841 };
2842 static const SkPath::Verb resultVerbs5[] = {
2843 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2844 };
2845 static const size_t resultPtsSizes1[] = { 0 };
2846 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2847 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2848 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2849 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
2850 static const SkPoint* resultPts1 = nullptr;
2851 static const SkPoint resultPts2[] = {
2852 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2853 };
2854 static const SkPoint resultPts3[] = {
2855 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2856 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2857 };
2858 static const SkPoint resultPts4[] = {
2859 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2860 };
2861 static const SkPoint resultPts5[] = {
2862 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2863 };
2864 static const struct iterTestData gIterTests[] = {
2865 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2866 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2867 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2868 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2869 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2870 { "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) },
2871 { "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) },
2872 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2873 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2874 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2875 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2876 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2877 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
2878 };
2879
2880 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2881 p.reset();
2882 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2883 REPORTER_ASSERT(reporter, valid);
2884 iter.setPath(p, gIterTests[i].forceClose);
2885 int j = 0, l = 0;
2886 do {
2887 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2888 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2889 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2890 }
2891 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2892 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2893 }
2894
2895 p.reset();
2896 iter.setPath(p, false);
2897 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2898 p.lineTo(1, 1);
2899 p.close();
2900 iter.setPath(p, false);
2901 REPORTER_ASSERT(reporter, iter.isClosedContour());
2902 p.reset();
2903 iter.setPath(p, true);
2904 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2905 p.lineTo(1, 1);
2906 iter.setPath(p, true);
2907 REPORTER_ASSERT(reporter, iter.isClosedContour());
2908 p.moveTo(0, 0);
2909 p.lineTo(2, 2);
2910 iter.setPath(p, false);
2911 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2912
2913 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2914 // check to see if the result is correct.
2915 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2916 p.reset();
2917 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2918 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2919 iter.setPath(p, true);
2920 iter.next(pts, false);
2921 iter.next(pts, false);
2922 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2923 }
2924
2925 p.reset();
2926 p.quadTo(0, 0, 0, 0);
2927 iter.setPath(p, false);
2928 iter.next(pts, false);
2929 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2930 iter.setPath(p, false);
2931 iter.next(pts, false);
2932 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2933
2934 p.reset();
2935 p.conicTo(0, 0, 0, 0, 0.5f);
2936 iter.setPath(p, false);
2937 iter.next(pts, false);
2938 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2939 iter.setPath(p, false);
2940 iter.next(pts, false);
2941 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2942
2943 p.reset();
2944 p.cubicTo(0, 0, 0, 0, 0, 0);
2945 iter.setPath(p, false);
2946 iter.next(pts, false);
2947 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2948 iter.setPath(p, false);
2949 iter.next(pts, false);
2950 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2951
2952 p.moveTo(1, 1); // add a trailing moveto
2953 iter.setPath(p, false);
2954 iter.next(pts, false);
2955 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2956 iter.setPath(p, false);
2957 iter.next(pts, false);
2958 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2959
2960 // The GM degeneratesegments.cpp test is more extensive
2961
2962 // Test out mixed degenerate and non-degenerate geometry with Conics
2963 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2964 SkRect r = SkRect::MakeWH(100, 100);
2965 SkRRect rr;
2966 rr.setRectRadii(r, radii);
2967 p.reset();
2968 p.addRRect(rr);
2969 iter.setPath(p, false);
2970 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2971 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2972 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2973 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2974 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
2975}
2976
2977static void test_raw_iter(skiatest::Reporter* reporter) {
2978 SkPath p;
2979 SkPoint pts[4];
2980
2981 // Test an iterator with no path
2982 SkPath::RawIter noPathIter;
2983 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2984 // Test that setting an empty path works
2985 noPathIter.setPath(p);
2986 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2987
2988 // Test an iterator with an initial empty path
2989 SkPath::RawIter iter(p);
2990 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2991
2992 // Test that a move-only path returns the move.
2993 p.moveTo(SK_Scalar1, 0);
2994 iter.setPath(p);
2995 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2996 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2997 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2998 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2999
3000 // No matter how many moves we add, we should get them all back
3001 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3002 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3003 iter.setPath(p);
3004 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3005 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3006 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3007 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3008 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3009 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3010 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3011 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3012 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3013 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3014
3015 // Initial close is never ever stored
3016 p.reset();
3017 p.close();
3018 iter.setPath(p);
3019 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3020
3021 // Move/close sequences
3022 p.reset();
3023 p.close(); // Not stored, no purpose
3024 p.moveTo(SK_Scalar1, 0);
3025 p.close();
3026 p.close(); // Not stored, no purpose
3027 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3028 p.close();
3029 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3030 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3031 p.close();
3032 iter.setPath(p);
3033 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3034 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3035 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3036 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3037 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3038 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3039 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3040 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3041 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3042 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3043 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3044 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3045 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3046 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3047 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3048 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3049
3050 // Generate random paths and verify
3051 SkPoint randomPts[25];
3052 for (int i = 0; i < 5; ++i) {
3053 for (int j = 0; j < 5; ++j) {
3054 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3055 }
3056 }
3057
3058 // Max of 10 segments, max 3 points per segment
3059 SkRandom rand(9876543);
3060 SkPoint expectedPts[31]; // May have leading moveTo
3061 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
3062 SkPath::Verb nextVerb;
3063
3064 for (int i = 0; i < 500; ++i) {
3065 p.reset();
3066 bool lastWasClose = true;
3067 bool haveMoveTo = false;
3068 SkPoint lastMoveToPt = { 0, 0 };
3069 int numPoints = 0;
3070 int numVerbs = (rand.nextU() >> 16) % 10;
3071 int numIterVerbs = 0;
3072 for (int j = 0; j < numVerbs; ++j) {
3073 do {
3074 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3075 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
3076 switch (nextVerb) {
3077 case SkPath::kMove_Verb:
3078 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3079 p.moveTo(expectedPts[numPoints]);
3080 lastMoveToPt = expectedPts[numPoints];
3081 numPoints += 1;
3082 lastWasClose = false;
3083 haveMoveTo = true;
3084 break;
3085 case SkPath::kLine_Verb:
3086 if (!haveMoveTo) {
3087 expectedPts[numPoints++] = lastMoveToPt;
3088 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3089 haveMoveTo = true;
3090 }
3091 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3092 p.lineTo(expectedPts[numPoints]);
3093 numPoints += 1;
3094 lastWasClose = false;
3095 break;
3096 case SkPath::kQuad_Verb:
3097 if (!haveMoveTo) {
3098 expectedPts[numPoints++] = lastMoveToPt;
3099 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3100 haveMoveTo = true;
3101 }
3102 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3103 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3104 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3105 numPoints += 2;
3106 lastWasClose = false;
3107 break;
3108 case SkPath::kConic_Verb:
3109 if (!haveMoveTo) {
3110 expectedPts[numPoints++] = lastMoveToPt;
3111 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3112 haveMoveTo = true;
3113 }
3114 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3115 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3116 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3117 rand.nextUScalar1() * 4);
3118 numPoints += 2;
3119 lastWasClose = false;
3120 break;
3121 case SkPath::kCubic_Verb:
3122 if (!haveMoveTo) {
3123 expectedPts[numPoints++] = lastMoveToPt;
3124 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3125 haveMoveTo = true;
3126 }
3127 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3128 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3129 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3130 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3131 expectedPts[numPoints + 2]);
3132 numPoints += 3;
3133 lastWasClose = false;
3134 break;
3135 case SkPath::kClose_Verb:
3136 p.close();
3137 haveMoveTo = false;
3138 lastWasClose = true;
3139 break;
3140 default:
3141 SkDEBUGFAIL("unexpected verb");
3142 }
3143 expectedVerbs[numIterVerbs++] = nextVerb;
3144 }
3145
3146 iter.setPath(p);
3147 numVerbs = numIterVerbs;
3148 numIterVerbs = 0;
3149 int numIterPts = 0;
3150 SkPoint lastMoveTo;
3151 SkPoint lastPt;
3152 lastMoveTo.set(0, 0);
3153 lastPt.set(0, 0);
3154 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3155 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3156 numIterVerbs++;
3157 switch (nextVerb) {
3158 case SkPath::kMove_Verb:
3159 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3160 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3161 lastPt = lastMoveTo = pts[0];
3162 numIterPts += 1;
3163 break;
3164 case SkPath::kLine_Verb:
3165 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3166 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3167 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3168 lastPt = pts[1];
3169 numIterPts += 1;
3170 break;
3171 case SkPath::kQuad_Verb:
3172 case SkPath::kConic_Verb:
3173 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3174 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3175 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3176 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3177 lastPt = pts[2];
3178 numIterPts += 2;
3179 break;
3180 case SkPath::kCubic_Verb:
3181 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3182 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3183 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3184 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3185 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3186 lastPt = pts[3];
3187 numIterPts += 3;
3188 break;
3189 case SkPath::kClose_Verb:
3190 lastPt = lastMoveTo;
3191 break;
3192 default:
3193 SkDEBUGFAIL("unexpected verb");
3194 }
3195 }
3196 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3197 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3198 }
3199}
3200
3201static void check_for_circle(skiatest::Reporter* reporter,
3202 const SkPath& path,
3203 bool expectedCircle,
3204 SkPathPriv::FirstDirection expectedDir) {
3205 SkRect rect = SkRect::MakeEmpty();
3206 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
3207 SkPath::Direction isOvalDir;
3208 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003209 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003210 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3211 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3212 SkPath tmpPath;
3213 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3214 REPORTER_ASSERT(reporter, path == tmpPath);
3215 }
3216 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3217}
3218
3219static void test_circle_skew(skiatest::Reporter* reporter,
3220 const SkPath& path,
3221 SkPathPriv::FirstDirection dir) {
3222 SkPath tmp;
3223
3224 SkMatrix m;
3225 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3226 path.transform(m, &tmp);
3227 // this matrix reverses the direction.
3228 if (SkPathPriv::kCCW_FirstDirection == dir) {
3229 dir = SkPathPriv::kCW_FirstDirection;
3230 } else {
3231 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3232 dir = SkPathPriv::kCCW_FirstDirection;
3233 }
3234 check_for_circle(reporter, tmp, false, dir);
3235}
3236
3237static void test_circle_translate(skiatest::Reporter* reporter,
3238 const SkPath& path,
3239 SkPathPriv::FirstDirection dir) {
3240 SkPath tmp;
3241
3242 // translate at small offset
3243 SkMatrix m;
3244 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3245 path.transform(m, &tmp);
3246 check_for_circle(reporter, tmp, true, dir);
3247
3248 tmp.reset();
3249 m.reset();
3250
3251 // translate at a relatively big offset
3252 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3253 path.transform(m, &tmp);
3254 check_for_circle(reporter, tmp, true, dir);
3255}
3256
3257static void test_circle_rotate(skiatest::Reporter* reporter,
3258 const SkPath& path,
3259 SkPathPriv::FirstDirection dir) {
3260 for (int angle = 0; angle < 360; ++angle) {
3261 SkPath tmp;
3262 SkMatrix m;
3263 m.setRotate(SkIntToScalar(angle));
3264 path.transform(m, &tmp);
3265
3266 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3267 // degrees is not an oval anymore, this can be improved. we made this
3268 // for the simplicity of our implementation.
3269 if (angle % 90 == 0) {
3270 check_for_circle(reporter, tmp, true, dir);
3271 } else {
3272 check_for_circle(reporter, tmp, false, dir);
3273 }
3274 }
3275}
3276
3277static void test_circle_mirror_x(skiatest::Reporter* reporter,
3278 const SkPath& path,
3279 SkPathPriv::FirstDirection dir) {
3280 SkPath tmp;
3281 SkMatrix m;
3282 m.reset();
3283 m.setScaleX(-SK_Scalar1);
3284 path.transform(m, &tmp);
3285 if (SkPathPriv::kCW_FirstDirection == dir) {
3286 dir = SkPathPriv::kCCW_FirstDirection;
3287 } else {
3288 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3289 dir = SkPathPriv::kCW_FirstDirection;
3290 }
3291 check_for_circle(reporter, tmp, true, dir);
3292}
3293
3294static void test_circle_mirror_y(skiatest::Reporter* reporter,
3295 const SkPath& path,
3296 SkPathPriv::FirstDirection dir) {
3297 SkPath tmp;
3298 SkMatrix m;
3299 m.reset();
3300 m.setScaleY(-SK_Scalar1);
3301 path.transform(m, &tmp);
3302
3303 if (SkPathPriv::kCW_FirstDirection == dir) {
3304 dir = SkPathPriv::kCCW_FirstDirection;
3305 } else {
3306 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3307 dir = SkPathPriv::kCW_FirstDirection;
3308 }
3309
3310 check_for_circle(reporter, tmp, true, dir);
3311}
3312
3313static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3314 const SkPath& path,
3315 SkPathPriv::FirstDirection dir) {
3316 SkPath tmp;
3317 SkMatrix m;
3318 m.reset();
3319 m.setScaleX(-SK_Scalar1);
3320 m.setScaleY(-SK_Scalar1);
3321 path.transform(m, &tmp);
3322
3323 check_for_circle(reporter, tmp, true, dir);
3324}
3325
3326static void test_circle_with_direction(skiatest::Reporter* reporter,
3327 SkPath::Direction inDir) {
3328 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3329 SkPath path;
3330
3331 // circle at origin
3332 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3333
3334 check_for_circle(reporter, path, true, dir);
3335 test_circle_rotate(reporter, path, dir);
3336 test_circle_translate(reporter, path, dir);
3337 test_circle_skew(reporter, path, dir);
3338 test_circle_mirror_x(reporter, path, dir);
3339 test_circle_mirror_y(reporter, path, dir);
3340 test_circle_mirror_xy(reporter, path, dir);
3341
3342 // circle at an offset at (10, 10)
3343 path.reset();
3344 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3345 SkIntToScalar(20), inDir);
3346
3347 check_for_circle(reporter, path, true, dir);
3348 test_circle_rotate(reporter, path, dir);
3349 test_circle_translate(reporter, path, dir);
3350 test_circle_skew(reporter, path, dir);
3351 test_circle_mirror_x(reporter, path, dir);
3352 test_circle_mirror_y(reporter, path, dir);
3353 test_circle_mirror_xy(reporter, path, dir);
3354
3355 // Try different starting points for the contour.
3356 for (unsigned start = 0; start < 4; ++start) {
3357 path.reset();
3358 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3359 test_circle_rotate(reporter, path, dir);
3360 test_circle_translate(reporter, path, dir);
3361 test_circle_skew(reporter, path, dir);
3362 test_circle_mirror_x(reporter, path, dir);
3363 test_circle_mirror_y(reporter, path, dir);
3364 test_circle_mirror_xy(reporter, path, dir);
3365 }
3366}
3367
3368static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3369 SkPath path;
3370 SkPath circle;
3371 SkPath rect;
3372 SkPath empty;
3373
3374 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3375 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
3376
3377 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3378 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3379 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3380
3381 SkMatrix translate;
3382 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3383
3384 // Although all the path concatenation related operations leave
3385 // the path a circle, most mark it as a non-circle for simplicity
3386
3387 // empty + circle (translate)
3388 path = empty;
3389 path.addPath(circle, translate);
3390 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3391
3392 // circle + empty (translate)
3393 path = circle;
3394 path.addPath(empty, translate);
3395
3396 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3397
3398 // test reverseAddPath
3399 path = circle;
3400 path.reverseAddPath(rect);
3401 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3402}
3403
3404static void test_circle(skiatest::Reporter* reporter) {
3405 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3406 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3407
3408 // multiple addCircle()
3409 SkPath path;
3410 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3411 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
3412 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3413
3414 // some extra lineTo() would make isOval() fail
3415 path.reset();
3416 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3417 path.lineTo(0, 0);
3418 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3419
3420 // not back to the original point
3421 path.reset();
3422 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3423 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3424 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3425
3426 test_circle_with_add_paths(reporter);
3427
3428 // test negative radius
3429 path.reset();
3430 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3431 REPORTER_ASSERT(reporter, path.isEmpty());
3432}
3433
3434static void test_oval(skiatest::Reporter* reporter) {
3435 SkRect rect;
3436 SkMatrix m;
3437 SkPath path;
3438 unsigned start = 0;
3439 SkPath::Direction dir = SkPath::kCCW_Direction;
3440
3441 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3442 path.addOval(rect);
3443
3444 // Defaults to dir = CW and start = 1
3445 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3446
3447 m.setRotate(SkIntToScalar(90));
3448 SkPath tmp;
3449 path.transform(m, &tmp);
3450 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3451 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003452 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003453 REPORTER_ASSERT(reporter, 2 == start);
3454 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3455
3456 m.reset();
3457 m.setRotate(SkIntToScalar(30));
3458 tmp.reset();
3459 path.transform(m, &tmp);
3460 // an oval rotated 30 degrees is not an oval anymore.
3461 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3462
3463 // since empty path being transformed.
3464 path.reset();
3465 tmp.reset();
3466 m.reset();
3467 path.transform(m, &tmp);
3468 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3469
3470 // empty path is not an oval
3471 tmp.reset();
3472 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3473
3474 // only has moveTo()s
3475 tmp.reset();
3476 tmp.moveTo(0, 0);
3477 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3478 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3479
3480 // mimic WebKit's calling convention,
3481 // call moveTo() first and then call addOval()
3482 path.reset();
3483 path.moveTo(0, 0);
3484 path.addOval(rect);
3485 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3486
3487 // copy path
3488 path.reset();
3489 tmp.reset();
3490 tmp.addOval(rect);
3491 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003492 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003493 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3494 REPORTER_ASSERT(reporter, 1 == start);
3495}
3496
3497static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3498 SkPath empty;
3499
3500 REPORTER_ASSERT(reporter, p.isEmpty());
3501 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3502 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3503 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3504 REPORTER_ASSERT(reporter, p.isConvex());
3505 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3506 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3507 REPORTER_ASSERT(reporter, p == empty);
3508 REPORTER_ASSERT(reporter, !(p != empty));
3509}
3510
3511static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3512 SkPath::Direction dir) {
3513 REPORTER_ASSERT(reporter, path->isConvex());
3514 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3515 path->setConvexity(SkPath::kUnknown_Convexity);
3516 REPORTER_ASSERT(reporter, path->isConvex());
3517 path->reset();
3518}
3519
3520static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3521 SkPath::Direction dir) {
3522 REPORTER_ASSERT(reporter, path->isConvex());
3523 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3524 path->setConvexity(SkPath::kUnknown_Convexity);
3525 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3526 path->reset();
3527}
3528
3529static void test_rrect(skiatest::Reporter* reporter) {
3530 SkPath p;
3531 SkRRect rr;
3532 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3533 SkRect r = {10, 20, 30, 40};
3534 rr.setRectRadii(r, radii);
3535 p.addRRect(rr);
3536 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3537 p.addRRect(rr, SkPath::kCCW_Direction);
3538 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3539 p.addRoundRect(r, &radii[0].fX);
3540 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3541 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
3542 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3543 p.addRoundRect(r, radii[1].fX, radii[1].fY);
3544 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3545 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
3546 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3547 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3548 SkVector save = radii[i];
3549 radii[i].set(0, 0);
3550 rr.setRectRadii(r, radii);
3551 p.addRRect(rr);
3552 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3553 radii[i] = save;
3554 }
3555 p.addRoundRect(r, 0, 0);
3556 SkRect returnedRect;
3557 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3558 REPORTER_ASSERT(reporter, returnedRect == r);
3559 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3560 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3561 rr.setRectRadii(r, zeroRadii);
3562 p.addRRect(rr);
3563 bool closed;
3564 SkPath::Direction dir;
3565 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3566 REPORTER_ASSERT(reporter, closed);
3567 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3568 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3569 p.addRRect(rr, SkPath::kCW_Direction);
3570 p.addRRect(rr, SkPath::kCW_Direction);
3571 REPORTER_ASSERT(reporter, !p.isConvex());
3572 p.reset();
3573 p.addRRect(rr, SkPath::kCCW_Direction);
3574 p.addRRect(rr, SkPath::kCCW_Direction);
3575 REPORTER_ASSERT(reporter, !p.isConvex());
3576 p.reset();
3577 SkRect emptyR = {10, 20, 10, 30};
3578 rr.setRectRadii(emptyR, radii);
3579 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003580 // The round rect is "empty" in that it has no fill area. However,
3581 // the path isn't "empty" in that it should have verbs and points.
3582 REPORTER_ASSERT(reporter, !p.isEmpty());
3583 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003584 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3585 rr.setRectRadii(largeR, radii);
3586 p.addRRect(rr);
3587 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
3588
3589 // we check for non-finites
3590 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3591 rr.setRectRadii(infR, radii);
3592 REPORTER_ASSERT(reporter, rr.isEmpty());
3593
3594 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3595 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3596 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3597}
3598
3599static void test_arc(skiatest::Reporter* reporter) {
3600 SkPath p;
3601 SkRect emptyOval = {10, 20, 30, 20};
3602 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3603 p.addArc(emptyOval, 1, 2);
3604 REPORTER_ASSERT(reporter, p.isEmpty());
3605 p.reset();
3606 SkRect oval = {10, 20, 30, 40};
3607 p.addArc(oval, 1, 0);
3608 REPORTER_ASSERT(reporter, p.isEmpty());
3609 p.reset();
3610 SkPath cwOval;
3611 cwOval.addOval(oval);
3612 p.addArc(oval, 0, 360);
3613 REPORTER_ASSERT(reporter, p == cwOval);
3614 p.reset();
3615 SkPath ccwOval;
3616 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3617 p.addArc(oval, 0, -360);
3618 REPORTER_ASSERT(reporter, p == ccwOval);
3619 p.reset();
3620 p.addArc(oval, 1, 180);
3621 REPORTER_ASSERT(reporter, p.isConvex());
3622 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
3623 p.setConvexity(SkPath::kUnknown_Convexity);
3624 REPORTER_ASSERT(reporter, p.isConvex());
3625}
3626
3627static inline SkScalar oval_start_index_to_angle(unsigned start) {
3628 switch (start) {
3629 case 0:
3630 return 270.f;
3631 case 1:
3632 return 0.f;
3633 case 2:
3634 return 90.f;
3635 case 3:
3636 return 180.f;
3637 default:
3638 return -1.f;
3639 }
3640}
3641
3642static inline SkScalar canonical_start_angle(float angle) {
3643 while (angle < 0.f) {
3644 angle += 360.f;
3645 }
3646 while (angle >= 360.f) {
3647 angle -= 360.f;
3648 }
3649 return angle;
3650}
3651
3652static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3653 const SkPath& path) {
3654 SkRect r = SkRect::MakeEmpty();
3655 SkPath::Direction d = SkPath::kCCW_Direction;
3656 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003657 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003658 REPORTER_ASSERT(reporter, isOval);
3659 SkPath recreatedPath;
3660 recreatedPath.addOval(r, d, s);
3661 REPORTER_ASSERT(reporter, path == recreatedPath);
3662 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3663 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3664}
3665
3666static void test_arc_ovals(skiatest::Reporter* reporter) {
3667 SkRect oval = SkRect::MakeWH(10, 20);
3668 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3669 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3670 SkPath path;
3671 path.addArc(oval, start, sweep);
3672 // SkPath's interfaces for inserting and extracting ovals only allow contours
3673 // to start at multiples of 90 degrees.
3674 if (std::fmod(start, 90.f) == 0) {
3675 check_oval_arc(reporter, start, sweep, path);
3676 } else {
3677 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3678 }
3679 }
3680 // Test start angles that are nearly at valid oval start angles.
3681 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3682 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3683 SkPath path;
3684 path.addArc(oval, start + delta, sweep);
3685 check_oval_arc(reporter, start, sweep, path);
3686 }
3687 }
3688 }
3689}
3690
3691static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3692 SkScalar x0, SkScalar y0) {
3693 SkPoint pts[4];
3694 SkPath::Verb v = iter->next(pts);
3695 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3696 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3697 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3698}
3699
3700static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3701 SkScalar x1, SkScalar y1) {
3702 SkPoint pts[4];
3703 SkPath::Verb v = iter->next(pts);
3704 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3705 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3706 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3707}
3708
3709static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3710 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3711 SkPoint pts[4];
3712 SkPath::Verb v = iter->next(pts);
3713 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3714 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3715 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3716 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3717 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3718}
3719
3720static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3721 SkPoint pts[4];
3722 SkPath::Verb v = iter->next(pts);
3723 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3724}
3725
3726static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3727 check_done(reporter, p, iter);
3728 p->reset();
3729}
3730
3731static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3732 SkScalar x0, SkScalar y0) {
3733 SkPath::RawIter iter(*p);
3734 check_move(reporter, &iter, x0, y0);
3735 check_done_and_reset(reporter, p, &iter);
3736}
3737
3738static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3739 SkScalar x1, SkScalar y1) {
3740 SkPath::RawIter iter(*p);
3741 check_move(reporter, &iter, 0, 0);
3742 check_line(reporter, &iter, x1, y1);
3743 check_done_and_reset(reporter, p, &iter);
3744}
3745
3746static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3747 SkScalar x1, SkScalar y1) {
3748 SkPath::RawIter iter(*p);
3749 check_move(reporter, &iter, 0, 0);
3750 check_line(reporter, &iter, x1, y1);
3751 check_done(reporter, p, &iter);
3752}
3753
3754static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3755 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3756 SkPath::RawIter iter(*p);
3757 check_move(reporter, &iter, 0, 0);
3758 check_line(reporter, &iter, x1, y1);
3759 check_line(reporter, &iter, x2, y2);
3760 check_done_and_reset(reporter, p, &iter);
3761}
3762
3763static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3764 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3765 SkPath::RawIter iter(*p);
3766 check_move(reporter, &iter, 0, 0);
3767 check_quad(reporter, &iter, x1, y1, x2, y2);
3768 check_done_and_reset(reporter, p, &iter);
3769}
3770
3771static bool nearly_equal(const SkRect& a, const SkRect& b) {
3772 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3773 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3774 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3775 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3776}
3777
3778static void test_arcTo(skiatest::Reporter* reporter) {
3779 SkPath p;
3780 p.arcTo(0, 0, 1, 2, 1);
3781 check_path_is_line_and_reset(reporter, &p, 0, 0);
3782 p.arcTo(1, 2, 1, 2, 1);
3783 check_path_is_line_and_reset(reporter, &p, 1, 2);
3784 p.arcTo(1, 2, 3, 4, 0);
3785 check_path_is_line_and_reset(reporter, &p, 1, 2);
3786 p.arcTo(1, 2, 0, 0, 1);
3787 check_path_is_line_and_reset(reporter, &p, 1, 2);
3788 p.arcTo(1, 0, 1, 1, 1);
3789 SkPoint pt;
3790 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3791 p.reset();
3792 p.arcTo(1, 0, 1, -1, 1);
3793 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3794 p.reset();
3795 SkRect oval = {1, 2, 3, 4};
3796 p.arcTo(oval, 0, 0, true);
3797 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3798 p.arcTo(oval, 0, 0, false);
3799 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3800 p.arcTo(oval, 360, 0, true);
3801 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3802 p.arcTo(oval, 360, 0, false);
3803 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3804
3805 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3806 p.arcTo(oval, 0, sweep, false);
3807 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3808 sweep += delta;
3809 delta /= 2;
3810 }
3811 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3812 p.arcTo(oval, 0, sweep, false);
3813 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3814 sweep -= delta;
3815 delta /= 2;
3816 }
3817 SkRect noOvalWidth = {1, 2, 0, 3};
3818 p.reset();
3819 p.arcTo(noOvalWidth, 0, 360, false);
3820 REPORTER_ASSERT(reporter, p.isEmpty());
3821
3822 SkRect noOvalHeight = {1, 2, 3, 1};
3823 p.reset();
3824 p.arcTo(noOvalHeight, 0, 360, false);
3825 REPORTER_ASSERT(reporter, p.isEmpty());
3826}
3827
3828static void test_addPath(skiatest::Reporter* reporter) {
3829 SkPath p, q;
3830 p.lineTo(1, 2);
3831 q.moveTo(4, 4);
3832 q.lineTo(7, 8);
3833 q.conicTo(8, 7, 6, 5, 0.5f);
3834 q.quadTo(6, 7, 8, 6);
3835 q.cubicTo(5, 6, 7, 8, 7, 5);
3836 q.close();
3837 p.addPath(q, -4, -4);
3838 SkRect expected = {0, 0, 4, 4};
3839 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3840 p.reset();
3841 p.reverseAddPath(q);
3842 SkRect reverseExpected = {4, 4, 8, 8};
3843 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3844}
3845
3846static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3847 SkPath p, q;
3848 if (explicitMoveTo) {
3849 p.moveTo(1, 1);
3850 }
3851 p.lineTo(1, 2);
3852 if (explicitMoveTo) {
3853 q.moveTo(2, 1);
3854 }
3855 q.lineTo(2, 2);
3856 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3857 uint8_t verbs[4];
3858 int verbcount = p.getVerbs(verbs, 4);
3859 REPORTER_ASSERT(reporter, verbcount == 4);
3860 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3861 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3862 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3863 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3864}
3865
3866static void test_extendClosedPath(skiatest::Reporter* reporter) {
3867 SkPath p, q;
3868 p.moveTo(1, 1);
3869 p.lineTo(1, 2);
3870 p.lineTo(2, 2);
3871 p.close();
3872 q.moveTo(2, 1);
3873 q.lineTo(2, 3);
3874 p.addPath(q, SkPath::kExtend_AddPathMode);
3875 uint8_t verbs[7];
3876 int verbcount = p.getVerbs(verbs, 7);
3877 REPORTER_ASSERT(reporter, verbcount == 7);
3878 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3879 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3880 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3881 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3882 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3883 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3884 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3885
3886 SkPoint pt;
3887 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3888 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3889 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3890}
3891
3892static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3893 SkPath p, q, r;
3894 // case 1: dst is empty
3895 p.moveTo(2, 1);
3896 p.lineTo(2, 3);
3897 q.addPath(p, mode);
3898 REPORTER_ASSERT(reporter, q == p);
3899 // case 2: src is empty
3900 p.addPath(r, mode);
3901 REPORTER_ASSERT(reporter, q == p);
3902 // case 3: src and dst are empty
3903 q.reset();
3904 q.addPath(r, mode);
3905 REPORTER_ASSERT(reporter, q.isEmpty());
3906}
3907
3908static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3909 SkPath p;
3910 p.conicTo(1, 2, 3, 4, -1);
3911 check_path_is_line_and_reset(reporter, &p, 3, 4);
3912 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3913 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3914 p.conicTo(1, 2, 3, 4, 1);
3915 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3916}
3917
3918static void test_get_point(skiatest::Reporter* reporter) {
3919 SkPath p;
3920 SkPoint pt = p.getPoint(0);
3921 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3922 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
3923 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3924 p.setLastPt(10, 10);
3925 pt = p.getPoint(0);
3926 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3927 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
3928 p.rMoveTo(10, 10);
3929 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3930}
3931
3932static void test_contains(skiatest::Reporter* reporter) {
3933 SkPath p;
3934 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3935 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
3936 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
3937 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3938 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
3939 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
3940 // this may return true or false, depending on the platform's numerics, but it should not crash
3941 (void) p.contains(-77.2027664f, 15.3066053f);
3942
3943 p.reset();
3944 p.setFillType(SkPath::kInverseWinding_FillType);
3945 REPORTER_ASSERT(reporter, p.contains(0, 0));
3946 p.setFillType(SkPath::kWinding_FillType);
3947 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3948 p.moveTo(4, 4);
3949 p.lineTo(6, 8);
3950 p.lineTo(8, 4);
3951 // test on edge
3952 REPORTER_ASSERT(reporter, p.contains(6, 4));
3953 REPORTER_ASSERT(reporter, p.contains(5, 6));
3954 REPORTER_ASSERT(reporter, p.contains(7, 6));
3955 // test quick reject
3956 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3957 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3958 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3959 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3960 // test various crossings in x
3961 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3962 REPORTER_ASSERT(reporter, p.contains(6, 7));
3963 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3964 p.reset();
3965 p.moveTo(4, 4);
3966 p.lineTo(8, 6);
3967 p.lineTo(4, 8);
3968 // test on edge
3969 REPORTER_ASSERT(reporter, p.contains(4, 6));
3970 REPORTER_ASSERT(reporter, p.contains(6, 5));
3971 REPORTER_ASSERT(reporter, p.contains(6, 7));
3972 // test various crossings in y
3973 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3974 REPORTER_ASSERT(reporter, p.contains(7, 6));
3975 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3976 p.reset();
3977 p.moveTo(4, 4);
3978 p.lineTo(8, 4);
3979 p.lineTo(8, 8);
3980 p.lineTo(4, 8);
3981 // test on vertices
3982 REPORTER_ASSERT(reporter, p.contains(4, 4));
3983 REPORTER_ASSERT(reporter, p.contains(8, 4));
3984 REPORTER_ASSERT(reporter, p.contains(8, 8));
3985 REPORTER_ASSERT(reporter, p.contains(4, 8));
3986 p.reset();
3987 p.moveTo(4, 4);
3988 p.lineTo(6, 8);
3989 p.lineTo(2, 8);
3990 // test on edge
3991 REPORTER_ASSERT(reporter, p.contains(5, 6));
3992 REPORTER_ASSERT(reporter, p.contains(4, 8));
3993 REPORTER_ASSERT(reporter, p.contains(3, 6));
3994 p.reset();
3995 p.moveTo(4, 4);
3996 p.lineTo(0, 6);
3997 p.lineTo(4, 8);
3998 // test on edge
3999 REPORTER_ASSERT(reporter, p.contains(2, 5));
4000 REPORTER_ASSERT(reporter, p.contains(2, 7));
4001 REPORTER_ASSERT(reporter, p.contains(4, 6));
4002 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4003 p.reset();
4004 p.moveTo(4, 0);
4005 p.lineTo(6, 4);
4006 p.lineTo(2, 4);
4007 p.moveTo(4, 0);
4008 p.lineTo(0, 8);
4009 p.lineTo(8, 8);
4010 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4011 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4012 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4013 REPORTER_ASSERT(reporter, p.contains(4, 4));
4014
4015 // test quads
4016 p.reset();
4017 p.moveTo(4, 4);
4018 p.quadTo(6, 6, 8, 8);
4019 p.quadTo(6, 8, 4, 8);
4020 p.quadTo(4, 6, 4, 4);
4021 REPORTER_ASSERT(reporter, p.contains(5, 6));
4022 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4023 // test quad edge
4024 REPORTER_ASSERT(reporter, p.contains(5, 5));
4025 REPORTER_ASSERT(reporter, p.contains(5, 8));
4026 REPORTER_ASSERT(reporter, p.contains(4, 5));
4027 // test quad endpoints
4028 REPORTER_ASSERT(reporter, p.contains(4, 4));
4029 REPORTER_ASSERT(reporter, p.contains(8, 8));
4030 REPORTER_ASSERT(reporter, p.contains(4, 8));
4031
4032 p.reset();
4033 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4034 p.moveTo(qPts[0]);
4035 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4036 p.quadTo(qPts[index], qPts[index + 1]);
4037 }
4038 REPORTER_ASSERT(reporter, p.contains(5, 6));
4039 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4040 // test quad edge
4041 SkPoint halfway;
4042 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4043 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4044 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4045 }
4046
4047 // test conics
4048 p.reset();
4049 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4050 p.moveTo(kPts[0]);
4051 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4052 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4053 }
4054 REPORTER_ASSERT(reporter, p.contains(5, 6));
4055 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4056 // test conic edge
4057 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4058 SkConic conic(&kPts[index], 0.5f);
4059 halfway = conic.evalAt(0.5f);
4060 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4061 }
4062 // test conic end points
4063 REPORTER_ASSERT(reporter, p.contains(4, 4));
4064 REPORTER_ASSERT(reporter, p.contains(8, 8));
4065 REPORTER_ASSERT(reporter, p.contains(4, 8));
4066
4067 // test cubics
4068 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4069 for (int i = 0; i < 3; ++i) {
4070 p.reset();
4071 p.setFillType(SkPath::kEvenOdd_FillType);
4072 p.moveTo(pts[i].fX, pts[i].fY);
4073 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);
4074 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);
4075 p.close();
4076 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4077 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4078 // test cubic edge
4079 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4080 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4081 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4082 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4083 // test cubic end points
4084 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4085 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4086 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4087 }
4088}
4089
4090class PathRefTest_Private {
4091public:
4092 static void TestPathRef(skiatest::Reporter* reporter) {
4093 static const int kRepeatCnt = 10;
4094
4095 sk_sp<SkPathRef> pathRef(new SkPathRef);
4096
4097 SkPathRef::Editor ed(&pathRef);
4098
4099 {
4100 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4101 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4102 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4103 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4104 for (int i = 0; i < kRepeatCnt; ++i) {
4105 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4106 }
4107 ed.resetToSize(0, 0, 0);
4108 }
4109
4110 {
4111 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4112 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4113 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4114 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4115 for (int i = 0; i < kRepeatCnt; ++i) {
4116 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4117 }
4118 ed.resetToSize(0, 0, 0);
4119 }
4120
4121 {
4122 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4123 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4124 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4125 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4126 for (int i = 0; i < kRepeatCnt; ++i) {
4127 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4128 }
4129 ed.resetToSize(0, 0, 0);
4130 }
4131
4132 {
4133 SkScalar* weights = nullptr;
4134 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4135 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4136 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4137 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4138 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4139 REPORTER_ASSERT(reporter, weights);
4140 for (int i = 0; i < kRepeatCnt; ++i) {
4141 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4142 }
4143 ed.resetToSize(0, 0, 0);
4144 }
4145
4146 {
4147 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4148 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4149 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4150 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4151 for (int i = 0; i < kRepeatCnt; ++i) {
4152 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4153 }
4154 ed.resetToSize(0, 0, 0);
4155 }
4156 }
4157};
4158
4159static void test_operatorEqual(skiatest::Reporter* reporter) {
4160 SkPath a;
4161 SkPath b;
4162 REPORTER_ASSERT(reporter, a == a);
4163 REPORTER_ASSERT(reporter, a == b);
4164 a.setFillType(SkPath::kInverseWinding_FillType);
4165 REPORTER_ASSERT(reporter, a != b);
4166 a.reset();
4167 REPORTER_ASSERT(reporter, a == b);
4168 a.lineTo(1, 1);
4169 REPORTER_ASSERT(reporter, a != b);
4170 a.reset();
4171 REPORTER_ASSERT(reporter, a == b);
4172 a.lineTo(1, 1);
4173 b.lineTo(1, 2);
4174 REPORTER_ASSERT(reporter, a != b);
4175 a.reset();
4176 a.lineTo(1, 2);
4177 REPORTER_ASSERT(reporter, a == b);
4178}
4179
4180static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4181 bool dumpAsHex, const char* str) {
4182 SkDynamicMemoryWStream wStream;
4183 path.dump(&wStream, force, dumpAsHex);
4184 sk_sp<SkData> data = wStream.detachAsData();
4185 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4186 if (strlen(str) > 0) {
4187 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4188 } else {
4189 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4190 }
4191}
4192
4193static void test_dump(skiatest::Reporter* reporter) {
4194 SkPath p;
4195 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4196 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4197 p.moveTo(1, 2);
4198 p.lineTo(3, 4);
4199 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4200 "path.moveTo(1, 2);\n"
4201 "path.lineTo(3, 4);\n");
4202 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4203 "path.moveTo(1, 2);\n"
4204 "path.lineTo(3, 4);\n"
4205 "path.lineTo(1, 2);\n"
4206 "path.close();\n");
4207 p.reset();
4208 p.setFillType(SkPath::kEvenOdd_FillType);
4209 p.moveTo(1, 2);
4210 p.quadTo(3, 4, 5, 6);
4211 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4212 "path.moveTo(1, 2);\n"
4213 "path.quadTo(3, 4, 5, 6);\n");
4214 p.reset();
4215 p.setFillType(SkPath::kInverseWinding_FillType);
4216 p.moveTo(1, 2);
4217 p.conicTo(3, 4, 5, 6, 0.5f);
4218 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4219 "path.moveTo(1, 2);\n"
4220 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4221 p.reset();
4222 p.setFillType(SkPath::kInverseEvenOdd_FillType);
4223 p.moveTo(1, 2);
4224 p.cubicTo(3, 4, 5, 6, 7, 8);
4225 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4226 "path.moveTo(1, 2);\n"
4227 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4228 p.reset();
4229 p.setFillType(SkPath::kWinding_FillType);
4230 p.moveTo(1, 2);
4231 p.lineTo(3, 4);
4232 compare_dump(reporter, p, false, true,
4233 "path.setFillType(SkPath::kWinding_FillType);\n"
4234 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4235 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4236 p.reset();
4237 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4238 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4239 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4240 "path.moveTo(1, 2);\n"
4241 "path.lineTo(3, 4);\n");
4242}
4243
4244namespace {
4245
4246class ChangeListener : public SkPathRef::GenIDChangeListener {
4247public:
4248 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4249 ~ChangeListener() override {}
4250 void onChange() override {
4251 *fChanged = true;
4252 }
4253private:
4254 bool* fChanged;
4255};
4256
4257}
4258
4259class PathTest_Private {
4260public:
4261 static void TestPathTo(skiatest::Reporter* reporter) {
4262 SkPath p, q;
4263 p.lineTo(4, 4);
4264 p.reversePathTo(q);
4265 check_path_is_line(reporter, &p, 4, 4);
4266 q.moveTo(-4, -4);
4267 p.reversePathTo(q);
4268 check_path_is_line(reporter, &p, 4, 4);
4269 q.lineTo(7, 8);
4270 q.conicTo(8, 7, 6, 5, 0.5f);
4271 q.quadTo(6, 7, 8, 6);
4272 q.cubicTo(5, 6, 7, 8, 7, 5);
4273 q.close();
4274 p.reversePathTo(q);
4275 SkRect reverseExpected = {-4, -4, 8, 8};
4276 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4277 }
4278
4279 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4280 SkPath p;
4281
4282 bool changed = false;
4283 p.moveTo(0, 0);
4284
4285 // Check that listener is notified on moveTo().
4286
Chris Daltonafa11582018-06-08 12:00:44 -06004287 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004288 REPORTER_ASSERT(reporter, !changed);
4289 p.moveTo(10, 0);
4290 REPORTER_ASSERT(reporter, changed);
4291
4292 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004293 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004294 REPORTER_ASSERT(reporter, !changed);
4295 p.lineTo(20, 0);
4296 REPORTER_ASSERT(reporter, changed);
4297
4298 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004299 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004300 REPORTER_ASSERT(reporter, !changed);
4301 p.reset();
4302 REPORTER_ASSERT(reporter, changed);
4303
4304 p.moveTo(0, 0);
4305
4306 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004307 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004308 REPORTER_ASSERT(reporter, !changed);
4309 p.rewind();
4310 REPORTER_ASSERT(reporter, changed);
4311
4312 // Check that listener is notified when pathref is deleted.
4313 {
4314 SkPath q;
4315 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004316 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004317 REPORTER_ASSERT(reporter, !changed);
4318 }
4319 // q went out of scope.
4320 REPORTER_ASSERT(reporter, changed);
4321 }
4322};
4323
4324static void test_crbug_629455(skiatest::Reporter* reporter) {
4325 SkPath path;
4326 path.moveTo(0, 0);
4327 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4328 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4329 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4330// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4331 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004332 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004333}
4334
4335static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4336 SkPath path;
4337 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4338 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4339 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4340 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4341 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4342 path.close();
4343
4344 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4345 SkPaint paint;
4346 paint.setAntiAlias(true);
4347 surface->getCanvas()->clipPath(path, true);
4348 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4349}
4350
4351static void test_path_crbugskia6003() {
4352 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4353 SkCanvas* canvas = surface->getCanvas();
4354 SkPaint paint;
4355 paint.setAntiAlias(true);
4356 SkPath path;
4357 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4358 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4359 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4360 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4361 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4362 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4363 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4364 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4365 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4366 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4367 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4368 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4369 path.close();
4370 canvas->clipPath(path, true);
4371 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4372}
4373
4374static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4375 SkPath path;
4376 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4377 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4378 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4379 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4380 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004381 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004382}
4383
Yuqian Li3154a532017-09-06 13:33:30 -04004384static void test_skbug_6947() {
4385 SkPath path;
4386 SkPoint points[] =
4387 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4388 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4389 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4390 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4391 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4392 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4393 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4394 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4395 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4396 {125.119476f, 1.50011659f}};
4397 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4398 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4399 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4400 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4401 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4402 int pointIndex = 0;
4403 for(auto verb : verbs) {
4404 switch (verb) {
4405 case kMove:
4406 path.moveTo(points[pointIndex++]);
4407 break;
4408 case kLine:
4409 path.lineTo(points[pointIndex++]);
4410 break;
4411 case kClose:
4412 default:
4413 path.close();
4414 break;
4415 }
4416 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004417 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004418}
Yuqian Lia81b6262017-09-06 17:10:05 -04004419
4420static void test_skbug_7015() {
4421 SkPath path;
4422 path.setFillType(SkPath::kWinding_FillType);
4423 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4424 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4425 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4426 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4427 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4428 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4429 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004430 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004431}
4432
Yuqian Lic5e4e742017-09-18 14:38:43 -04004433static void test_skbug_7051() {
4434 SkPath path;
4435 path.moveTo(10, 10);
4436 path.cubicTo(10, 20, 10, 30, 30, 30);
4437 path.lineTo(50, 20);
4438 path.lineTo(50, 10);
4439 path.close();
4440 test_draw_AA_path(100, 100, path);
4441}
4442
Yuqian Li3154a532017-09-06 13:33:30 -04004443static void test_interp(skiatest::Reporter* reporter) {
4444 SkPath p1, p2, out;
4445 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4446 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4447 REPORTER_ASSERT(reporter, p1 == out);
4448 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4449 REPORTER_ASSERT(reporter, p1 == out);
4450 p1.moveTo(0, 2);
4451 p1.lineTo(0, 4);
4452 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4453 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4454 p2.moveTo(6, 0);
4455 p2.lineTo(8, 0);
4456 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4457 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4458 REPORTER_ASSERT(reporter, p2 == out);
4459 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4460 REPORTER_ASSERT(reporter, p1 == out);
4461 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4462 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4463 p1.reset();
4464 p1.moveTo(4, 4);
4465 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4466 p2.reset();
4467 p2.moveTo(4, 2);
4468 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4469 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4470 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4471 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4472 p2.reset();
4473 p2.moveTo(4, 2);
4474 p2.conicTo(6, 3, 6, 5, 1);
4475 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4476 p2.reset();
4477 p2.moveTo(4, 4);
4478 p2.conicTo(5, 4, 5, 5, 0.5f);
4479 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4480}
4481
4482DEF_TEST(PathInterp, reporter) {
4483 test_interp(reporter);
4484}
4485
4486#include "SkSurface.h"
4487DEF_TEST(PathBigCubic, reporter) {
4488 SkPath path;
4489 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4490 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4491 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
4492 path.moveTo(0, 512);
4493
4494 // this call should not assert
4495 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4496}
4497
4498DEF_TEST(PathContains, reporter) {
4499 test_contains(reporter);
4500}
4501
4502DEF_TEST(Paths, reporter) {
4503 test_fuzz_crbug_647922();
4504 test_fuzz_crbug_643933();
4505 test_sect_with_horizontal_needs_pinning();
4506 test_crbug_629455(reporter);
4507 test_fuzz_crbug_627414(reporter);
4508 test_path_crbug364224();
4509 test_fuzz_crbug_662952(reporter);
4510 test_fuzz_crbug_662730(reporter);
4511 test_fuzz_crbug_662780();
4512 test_mask_overflow();
4513 test_path_crbugskia6003();
4514 test_fuzz_crbug_668907();
4515#if !defined(SK_SUPPORT_LEGACY_DELTA_AA)
4516 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004517 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004518 test_skbug_7051();
Yuqian Li3154a532017-09-06 13:33:30 -04004519#endif
4520
4521 SkSize::Make(3, 4);
4522
4523 SkPath p, empty;
4524 SkRect bounds, bounds2;
4525 test_empty(reporter, p);
4526
4527 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4528
4529 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4530 SkPath& self = p;
4531 p = self;
4532
4533 // this triggers a code path in SkPath::swap which is otherwise unexercised
4534 p.swap(self);
4535
4536 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
4537
4538 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4539 check_convex_bounds(reporter, p, bounds);
4540 // we have quads or cubics
4541 REPORTER_ASSERT(reporter,
4542 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4543 REPORTER_ASSERT(reporter, !p.isEmpty());
4544
4545 p.reset();
4546 test_empty(reporter, p);
4547
4548 p.addOval(bounds);
4549 check_convex_bounds(reporter, p, bounds);
4550 REPORTER_ASSERT(reporter, !p.isEmpty());
4551
4552 p.rewind();
4553 test_empty(reporter, p);
4554
4555 p.addRect(bounds);
4556 check_convex_bounds(reporter, p, bounds);
4557 // we have only lines
4558 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4559 REPORTER_ASSERT(reporter, !p.isEmpty());
4560
4561 REPORTER_ASSERT(reporter, p != empty);
4562 REPORTER_ASSERT(reporter, !(p == empty));
4563
4564 // do getPoints and getVerbs return the right result
4565 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4566 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4567 SkPoint pts[4];
4568 int count = p.getPoints(pts, 4);
4569 REPORTER_ASSERT(reporter, count == 4);
4570 uint8_t verbs[6];
4571 verbs[5] = 0xff;
4572 p.getVerbs(verbs, 5);
4573 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4574 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4575 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4576 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4577 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4578 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
4579 bounds2.set(pts, 4);
4580 REPORTER_ASSERT(reporter, bounds == bounds2);
4581
4582 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4583 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4584 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4585
4586 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4587 bounds2.setEmpty();
4588 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4589 REPORTER_ASSERT(reporter, bounds == bounds2);
4590
4591 // now force p to not be a rect
4592 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4593 p.addRect(bounds);
4594 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4595
4596 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4597 // path has a trailing moveTo. Please see crbug.com\445368)
4598 {
4599 SkRect r;
4600 p.reset();
4601 p.addRect(bounds);
4602 REPORTER_ASSERT(reporter, p.isRect(&r));
4603 REPORTER_ASSERT(reporter, r == bounds);
4604 // add a moveTo outside of our bounds
4605 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4606 REPORTER_ASSERT(reporter, p.isRect(&r));
4607 REPORTER_ASSERT(reporter, r == bounds);
4608 }
4609
4610 test_operatorEqual(reporter);
4611 test_isLine(reporter);
4612 test_isRect(reporter);
4613 test_is_simple_closed_rect(reporter);
4614 test_isNestedFillRects(reporter);
4615 test_zero_length_paths(reporter);
4616 test_direction(reporter);
4617 test_convexity(reporter);
4618 test_convexity2(reporter);
4619 test_conservativelyContains(reporter);
4620 test_close(reporter);
4621 test_segment_masks(reporter);
4622 test_flattening(reporter);
4623 test_transform(reporter);
4624 test_bounds(reporter);
4625 test_iter(reporter);
4626 test_raw_iter(reporter);
4627 test_circle(reporter);
4628 test_oval(reporter);
4629 test_strokerec(reporter);
4630 test_addPoly(reporter);
4631 test_isfinite(reporter);
4632 test_isfinite_after_transform(reporter);
4633 test_islastcontourclosed(reporter);
4634 test_arb_round_rect_is_convex(reporter);
4635 test_arb_zero_rad_round_rect_is_rect(reporter);
4636 test_addrect(reporter);
4637 test_addrect_isfinite(reporter);
4638 test_tricky_cubic();
4639 test_clipped_cubic();
4640 test_crbug_170666();
4641 test_crbug_493450(reporter);
4642 test_crbug_495894(reporter);
4643 test_crbug_613918();
4644 test_bad_cubic_crbug229478();
4645 test_bad_cubic_crbug234190();
4646 test_gen_id(reporter);
4647 test_path_close_issue1474(reporter);
4648 test_path_to_region(reporter);
4649 test_rrect(reporter);
4650 test_arc(reporter);
4651 test_arc_ovals(reporter);
4652 test_arcTo(reporter);
4653 test_addPath(reporter);
4654 test_addPathMode(reporter, false, false);
4655 test_addPathMode(reporter, true, false);
4656 test_addPathMode(reporter, false, true);
4657 test_addPathMode(reporter, true, true);
4658 test_extendClosedPath(reporter);
4659 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4660 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4661 test_conicTo_special_case(reporter);
4662 test_get_point(reporter);
4663 test_contains(reporter);
4664 PathTest_Private::TestPathTo(reporter);
4665 PathRefTest_Private::TestPathRef(reporter);
4666 PathTest_Private::TestPathrefListeners(reporter);
4667 test_dump(reporter);
4668 test_path_crbug389050(reporter);
4669 test_path_crbugskia2820(reporter);
4670 test_path_crbugskia5995();
4671 test_skbug_3469(reporter);
4672 test_skbug_3239(reporter);
4673 test_bounds_crbug_513799(reporter);
4674 test_fuzz_crbug_638223();
4675}
4676
4677DEF_TEST(conservatively_contains_rect, reporter) {
4678 SkPath path;
4679
4680 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4681 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4682 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4683 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4684 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4685 path.moveTo(0, 0);
4686
4687 // this guy should not assert
4688 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4689}
4690
4691///////////////////////////////////////////////////////////////////////////////////////////////////
4692
4693static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4694 for (int i = 0; i < n; ++i) {
4695 switch (verb) {
4696 case SkPath::kLine_Verb:
4697 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4698 break;
4699 case SkPath::kQuad_Verb:
4700 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4701 rand.nextF()*100, rand.nextF()*100);
4702 break;
4703 case SkPath::kConic_Verb:
4704 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4705 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4706 break;
4707 case SkPath::kCubic_Verb:
4708 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4709 rand.nextF()*100, rand.nextF()*100,
4710 rand.nextF()*100, rand.nextF()*100);
4711 break;
4712 default:
4713 SkASSERT(false);
4714 }
4715 }
4716}
4717
4718#include "SkPathOps.h"
4719DEF_TEST(path_tight_bounds, reporter) {
4720 SkRandom rand;
4721
4722 const SkPath::Verb verbs[] = {
4723 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4724 };
4725 for (int i = 0; i < 1000; ++i) {
4726 for (int n = 1; n <= 10; n += 9) {
4727 for (SkPath::Verb verb : verbs) {
4728 SkPath path;
4729 rand_path(&path, rand, verb, n);
4730 SkRect bounds = path.getBounds();
4731 SkRect tight = path.computeTightBounds();
4732 REPORTER_ASSERT(reporter, bounds.contains(tight));
4733
4734 SkRect tight2;
4735 TightBounds(path, &tight2);
4736 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
4737 }
4738 }
4739 }
4740}
4741
4742DEF_TEST(skbug_6450, r) {
4743 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
4744 SkVector rdi[4] = {
4745 { 1.81159976e-09f, 7.58768801e-05f },
4746 { 0.000118725002f, 0.000118725002f },
4747 { 0.000118725002f, 0.000118725002f },
4748 { 0.000118725002f, 0.486297607f }
4749 };
4750 SkRRect irr;
4751 irr.setRectRadii(ri, rdi);
4752 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
4753 SkVector rdo[4] = {
4754 { 0, 0 },
4755 { 0.0103298295f, 0.185887396f },
4756 { 2.52999727e-29f, 169.001938f },
4757 { 195.262741f, 195.161255f }
4758 };
4759 SkRRect orr;
4760 orr.setRectRadii(ro, rdo);
4761 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
4762}
4763
4764DEF_TEST(PathRefSerialization, reporter) {
4765 SkPath path;
4766 const size_t numMoves = 5;
4767 const size_t numConics = 7;
4768 const size_t numPoints = numMoves + 2 * numConics;
4769 const size_t numVerbs = numMoves + numConics;
4770 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
4771 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
4772 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
4773 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
4774
4775 // Verify that path serializes/deserializes properly.
4776 sk_sp<SkData> data = path.serialize();
4777 size_t bytesWritten = data->size();
4778
4779 {
4780 SkPath readBack;
4781 REPORTER_ASSERT(reporter, readBack != path);
4782 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
4783 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
4784 REPORTER_ASSERT(reporter, readBack == path);
4785 }
4786
Yuqian Li3154a532017-09-06 13:33:30 -04004787 // One less byte (rounded down to alignment) than was written will also
4788 // fail to be deserialized.
4789 {
4790 SkPath readBack;
4791 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
4792 REPORTER_ASSERT(reporter, !bytesRead);
4793 }
4794}
Mike Klein1170a552017-09-08 15:00:25 -04004795
4796DEF_TEST(NonFinitePathIteration, reporter) {
4797 SkPath path;
4798 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
4799
4800 int verbs = 0;
4801
4802 SkPath::RawIter iter(path);
4803 SkPoint pts[4];
4804 while (iter.next(pts) != SkPath::kDone_Verb) {
4805 verbs++;
4806 }
4807
4808 REPORTER_ASSERT(reporter, verbs == 0);
4809}
Cary Clark1acd3c72017-12-08 11:37:01 -05004810
4811
4812#ifndef SK_SUPPORT_LEGACY_SVG_ARC_TO
4813DEF_TEST(AndroidArc, reporter) {
4814 const char* tests[] = {
4815 "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",
4816 "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
4817 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
4818 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
4819 };
4820 for (auto test : tests) {
4821 SkPath aPath;
4822 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
4823 SkASSERT(aPath.isConvex());
4824 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
4825 SkPath scalePath = aPath;
4826 SkMatrix matrix;
4827 matrix.setScale(scale, scale);
4828 scalePath.transform(matrix);
4829 SkASSERT(scalePath.isConvex());
4830 }
4831 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
4832 SkPath scalePath = aPath;
4833 SkMatrix matrix;
4834 matrix.setScale(scale, scale);
4835 scalePath.transform(matrix);
4836 SkASSERT(scalePath.isConvex());
4837 }
4838 }
4839}
4840#endif
Mike Reed1fe0bcc2018-01-22 16:49:49 -05004841
4842/*
4843 * Try a range of crazy values, just to ensure that we don't assert/crash.
4844 */
4845DEF_TEST(HugeGeometry, reporter) {
4846 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
4847 auto canvas = surf->getCanvas();
4848
4849 const bool aas[] = { false, true };
4850 const SkPaint::Style styles[] = {
4851 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
4852 };
4853 const SkScalar values[] = {
4854 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
4855 SK_ScalarInfinity
4856 };
4857
4858 SkPaint paint;
4859 for (auto x : values) {
4860 SkRect r = { -x, -x, x, x };
4861 for (auto width : values) {
4862 paint.setStrokeWidth(width);
4863 for (auto aa : aas) {
4864 paint.setAntiAlias(aa);
4865 for (auto style : styles) {
4866 paint.setStyle(style);
4867 canvas->drawRect(r, paint);
4868 canvas->drawOval(r, paint);
4869 }
4870 }
4871 }
4872 }
4873
4874}
4875
Mike Reed1c6e7832018-01-30 11:29:36 -05004876// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
4877DEF_TEST(ClipPath_nonfinite, reporter) {
4878 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
4879 SkCanvas* canvas = surf->getCanvas();
4880
4881 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
4882 for (bool aa : {false, true}) {
4883 for (SkPath::FillType ft : {SkPath::kWinding_FillType, SkPath::kInverseWinding_FillType}) {
4884 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
4885 for (int bits = 1; bits <= 15; ++bits) {
4886 SkPoint p0 = { 0, 0 };
4887 SkPoint p1 = { 0, 0 };
4888 if (bits & 1) p0.fX = -bad;
4889 if (bits & 2) p0.fY = -bad;
4890 if (bits & 4) p1.fX = bad;
4891 if (bits & 8) p1.fY = bad;
4892
4893 SkPath path;
4894 path.moveTo(p0);
4895 path.lineTo(p1);
4896 path.setFillType(ft);
4897 canvas->save();
4898 canvas->clipPath(path, aa);
4899 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
4900 canvas->restore();
4901 }
4902 }
4903 }
4904 }
4905 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
4906}
4907
Cary Clark5c715182018-04-09 16:07:11 -04004908// skbug.com/7792
4909DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04004910 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
4911 SkPath path;
4912 for (size_t index = 0; index < count; ++index) {
4913 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
4914 }
Cary Clark31608c02018-04-12 10:29:46 -04004915 if (close) {
4916 path.close();
4917 }
Cary Clark8540e112018-04-11 14:30:27 -04004918 return path;
4919 };
4920 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
4921 SkPath path;
4922 for (size_t index = 0; index < count; ++index) {
4923 switch (verbs[index]) {
4924 case SkPath::kMove_Verb:
4925 path.moveTo(*points++);
4926 break;
4927 case SkPath::kLine_Verb:
4928 path.lineTo(*points++);
4929 break;
4930 case SkPath::kClose_Verb:
4931 path.close();
4932 break;
4933 default:
4934 SkASSERT(0);
4935 }
4936 }
4937 return path;
4938 };
Cary Clarkd4228472018-04-13 07:07:04 -04004939 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04004940 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04004941 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
4942 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04004943 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04004944 SkRect compare;
4945 compare.set(&points[1], SK_ARRAY_COUNT(points) - 1);
4946 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004947 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04004948 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
4949 path = makePath(points3, SK_ARRAY_COUNT(points3), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004950 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004951 // isolated from skbug.com/7792#c9
Cary Clark8540e112018-04-11 14:30:27 -04004952 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
4953 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004954 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004955 compare.set(&points9[1], SK_ARRAY_COUNT(points9) - 1);
4956 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004957 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04004958 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4959 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
4960 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
4961 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04004962 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004963 compare.set(&points11[0], SK_ARRAY_COUNT(points11));
4964 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004965 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04004966 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
4967 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4968 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
4969 SkPath::kLine_Verb, SkPath::kClose_Verb };
4970 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
4971 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
4972 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04004973 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004974 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04004975 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4976 SkPath::kLine_Verb, SkPath::kMove_Verb };
4977 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
4978 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04004979 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004980 compare.set(&points15[0], SK_ARRAY_COUNT(points15) - 1);
4981 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004982 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04004983 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
4984 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004985 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004986 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04004987 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4988 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4989 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
4990 SkPath::kLine_Verb, SkPath::kLine_Verb };
4991 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
4992 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
4993 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04004994 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004995 // isolated from skbug.com/7792#c23
4996 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
4997 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4998 SkPath::kLine_Verb, SkPath::kClose_Verb };
4999 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
5000 {75, 150} };
5001 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04005002 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005003 compare.set(&points23[0], SK_ARRAY_COUNT(points23));
5004 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005005 // isolated from skbug.com/7792#c29
5006 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5007 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5008 SkPath::kClose_Verb };
5009 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5010 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005011 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005012 // isolated from skbug.com/7792#c31
5013 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5014 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5015 SkPath::kClose_Verb };
5016 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5017 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005018 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005019 compare.set(&points31[0], 4);
5020 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005021 // isolated from skbug.com/7792#c36
5022 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5023 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5024 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5025 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005026 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005027 // isolated from skbug.com/7792#c39
5028 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005029 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005030 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5031 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005032 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005033 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005034 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5035 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5036 SkPath::kLine_Verb, SkPath::kClose_Verb };
5037 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5038 {32, 2} };
5039 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5040 REPORTER_ASSERT(reporter, path.isRect(&rect));
5041 compare.set(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
5042 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005043 // isolated from skbug.com/7792#c41
5044 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5045 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5046 SkPath::kClose_Verb };
5047 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5048 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5049 REPORTER_ASSERT(reporter, path.isRect(&rect));
5050 compare.set(&points41[1], 4);
5051 REPORTER_ASSERT(reporter, rect == compare);
5052 // isolated from skbug.com/7792#c53
5053 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5054 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5055 SkPath::kClose_Verb };
5056 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5057 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5058 REPORTER_ASSERT(reporter, path.isRect(&rect));
5059 compare.set(&points53[1], 4);
5060 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005061}
Mike Reedee430912018-05-23 12:12:21 -04005062
5063#include "SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005064static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5065 // draw in different ways, looking for an assert
5066
5067 {
5068 SkPath path;
5069 path.addPoly(pts, 3, false);
5070 canvas->drawPath(path, SkPaint());
5071 }
5072
5073 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5074 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5075 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5076}
5077
Mike Reedee430912018-05-23 12:12:21 -04005078DEF_TEST(triangle_onehalf, reporter) {
5079 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5080
5081 const SkPoint pts[] = {
5082 { 0.499069244f, 9.63295173f },
5083 { 0.499402374f, 7.88207579f },
5084 { 10.2363272f, 0.49999997f }
5085 };
Mike Reed50595412018-05-30 16:32:33 -04005086 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005087}
Mike Reed50595412018-05-30 16:32:33 -04005088
5089DEF_TEST(triangle_big, reporter) {
5090 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5091
5092 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5093 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5094 // be conservative, so we invoke clipping if we get in this situation.
5095 // This test was added to demonstrate the need for this conservative bounds calc.
5096 // (found by a fuzzer)
5097 const SkPoint pts[] = {
5098 { 0.327190518f, -114.945152f },
5099 { -0.5f, 1.00003874f },
5100 { 0.666425824f, 4304.26172f },
5101 };
5102 draw_triangle(surface->getCanvas(), pts);
5103}
5104