blob: a742bcbbeeea7cc80ee6ebcbb3f5a7006ae07bec [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);
2072 bool isClosed = (bool)-1;
2073 SkPath::Direction direction = (SkPath::Direction) - 1;
2074 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
2075 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2076 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
2077 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2078 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
2079 }
2080 }
2081
2082 // fail, close then line
2083 SkPath path1;
2084 path1.moveTo(r1[0].fX, r1[0].fY);
2085 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2086 path1.lineTo(r1[index].fX, r1[index].fY);
2087 }
2088 path1.close();
2089 path1.lineTo(1, 0);
2090 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2091
2092 // fail, move in the middle
2093 path1.reset();
2094 path1.moveTo(r1[0].fX, r1[0].fY);
2095 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2096 if (index == 2) {
2097 path1.moveTo(1, .5f);
2098 }
2099 path1.lineTo(r1[index].fX, r1[index].fY);
2100 }
2101 path1.close();
2102 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2103
2104 // fail, move on the edge
2105 path1.reset();
2106 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2107 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2108 path1.lineTo(r1[index].fX, r1[index].fY);
2109 }
2110 path1.close();
2111 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2112
2113 // fail, quad
2114 path1.reset();
2115 path1.moveTo(r1[0].fX, r1[0].fY);
2116 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2117 if (index == 2) {
2118 path1.quadTo(1, .5f, 1, .5f);
2119 }
2120 path1.lineTo(r1[index].fX, r1[index].fY);
2121 }
2122 path1.close();
2123 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2124
2125 // fail, cubic
2126 path1.reset();
2127 path1.moveTo(r1[0].fX, r1[0].fY);
2128 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2129 if (index == 2) {
2130 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2131 }
2132 path1.lineTo(r1[index].fX, r1[index].fY);
2133 }
2134 path1.close();
2135 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
2136}
2137
2138static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2139 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2140 SkRect r = SkRect::MakeEmpty();
2141 SkPath::Direction d = SkPath::kCCW_Direction;
2142 unsigned s = ~0U;
2143
2144 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2145 REPORTER_ASSERT(reporter, r == rect);
2146 REPORTER_ASSERT(reporter, d == dir);
2147 REPORTER_ASSERT(reporter, s == start);
2148}
2149
2150static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002151 using std::swap;
Yuqian Li3154a532017-09-06 13:33:30 -04002152 SkRect r = SkRect::MakeEmpty();
2153 SkPath::Direction d = SkPath::kCCW_Direction;
2154 unsigned s = ~0U;
2155
2156 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2157 const SkRect emptyRect = SkRect::MakeEmpty();
2158 SkPath path;
2159 for (int start = 0; start < 4; ++start) {
2160 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2161 SkPath path;
2162 path.addRect(testRect, dir, start);
2163 check_simple_closed_rect(reporter, path, testRect, dir, start);
2164 path.close();
2165 check_simple_closed_rect(reporter, path, testRect, dir, start);
2166 SkPath path2 = path;
2167 path2.lineTo(10, 10);
2168 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2169 path2 = path;
2170 path2.moveTo(10, 10);
2171 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2172 path2 = path;
2173 path2.addRect(testRect, dir, start);
2174 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2175 // Make the path by hand, manually closing it.
2176 path2.reset();
2177 SkPath::RawIter iter(path);
2178 SkPath::Verb v;
2179 SkPoint verbPts[4];
2180 SkPoint firstPt = {0.f, 0.f};
2181 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2182 switch(v) {
2183 case SkPath::kMove_Verb:
2184 firstPt = verbPts[0];
2185 path2.moveTo(verbPts[0]);
2186 break;
2187 case SkPath::kLine_Verb:
2188 path2.lineTo(verbPts[1]);
2189 break;
2190 default:
2191 break;
2192 }
2193 }
2194 // We haven't closed it yet...
2195 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2196 // ... now we do and test again.
2197 path2.lineTo(firstPt);
2198 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2199 // A redundant close shouldn't cause a failure.
2200 path2.close();
2201 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2202 // Degenerate point and line rects are not allowed
2203 path2.reset();
2204 path2.addRect(emptyRect, dir, start);
2205 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2206 SkRect degenRect = testRect;
2207 degenRect.fLeft = degenRect.fRight;
2208 path2.reset();
2209 path2.addRect(degenRect, dir, start);
2210 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2211 degenRect = testRect;
2212 degenRect.fTop = degenRect.fBottom;
2213 path2.reset();
2214 path2.addRect(degenRect, dir, start);
2215 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2216 // An inverted rect makes a rect path, but changes the winding dir and start point.
2217 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2218 ? SkPath::kCCW_Direction
2219 : SkPath::kCW_Direction;
2220 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2221 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2222 SkRect swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002223 swap(swapRect.fLeft, swapRect.fRight);
Yuqian Li3154a532017-09-06 13:33:30 -04002224 path2.reset();
2225 path2.addRect(swapRect, dir, start);
2226 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2227 swapRect = testRect;
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002228 swap(swapRect.fTop, swapRect.fBottom);
Yuqian Li3154a532017-09-06 13:33:30 -04002229 path2.reset();
2230 path2.addRect(swapRect, dir, start);
2231 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2232 }
2233 }
2234 // down, up, left, close
2235 path.reset();
2236 path.moveTo(1, 1);
2237 path.lineTo(1, 2);
2238 path.lineTo(1, 1);
2239 path.lineTo(0, 1);
2240 SkRect rect;
2241 SkPath::Direction dir;
2242 unsigned start;
2243 path.close();
2244 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2245 // right, left, up, close
2246 path.reset();
2247 path.moveTo(1, 1);
2248 path.lineTo(2, 1);
2249 path.lineTo(1, 1);
2250 path.lineTo(1, 0);
2251 path.close();
2252 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2253 // parallelogram with horizontal edges
2254 path.reset();
2255 path.moveTo(1, 0);
2256 path.lineTo(3, 0);
2257 path.lineTo(2, 1);
2258 path.lineTo(0, 1);
2259 path.close();
2260 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2261 // parallelogram with vertical edges
2262 path.reset();
2263 path.moveTo(0, 1);
2264 path.lineTo(0, 3);
2265 path.lineTo(1, 2);
2266 path.lineTo(1, 0);
2267 path.close();
2268 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2269
2270}
2271
2272static void test_isNestedFillRects(skiatest::Reporter* reporter) {
2273 // passing tests (all moveTo / lineTo...
2274 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2275 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2276 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2277 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
2278 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
2279 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2280 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2281 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2282 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2283 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2284 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
2285 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2286 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2287 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
2288
2289 // failing tests
2290 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2291 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2292 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2293 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2294 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2295 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2296 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2297 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2298
2299 // success, no close is OK
2300 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2301 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2302
2303 struct IsNestedRectTest {
2304 SkPoint *fPoints;
2305 int fPointCount;
2306 SkPathPriv::FirstDirection fDirection;
2307 bool fClose;
2308 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2309 } tests[] = {
2310 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2311 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2312 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2313 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2314 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2315 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2316 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2317 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2318 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2319 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2320 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2321 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2322 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2323 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
2324
2325 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2326 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2327 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2328 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2329 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2330 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2331 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2332 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
2333
2334 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2335 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
2336 };
2337
2338 const size_t testCount = SK_ARRAY_COUNT(tests);
2339 int index;
2340 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
2341 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2342 SkPath path;
2343 if (rectFirst) {
2344 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2345 }
2346 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2347 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2348 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
2349 }
2350 if (tests[testIndex].fClose) {
2351 path.close();
2352 }
2353 if (!rectFirst) {
2354 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2355 }
2356 REPORTER_ASSERT(reporter,
2357 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
2358 if (tests[testIndex].fIsNestedRect) {
2359 SkRect expected[2], computed[2];
2360 SkPathPriv::FirstDirection expectedDirs[2];
2361 SkPath::Direction computedDirs[2];
2362 SkRect testBounds;
2363 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
2364 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2365 expected[1] = testBounds;
2366 if (rectFirst) {
2367 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
2368 } else {
2369 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
2370 }
2371 expectedDirs[1] = tests[testIndex].fDirection;
2372 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
2373 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2374 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
2375 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2376 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
2377 }
2378 }
2379
2380 // fail, close then line
2381 SkPath path1;
2382 if (rectFirst) {
2383 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2384 }
2385 path1.moveTo(r1[0].fX, r1[0].fY);
2386 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2387 path1.lineTo(r1[index].fX, r1[index].fY);
2388 }
2389 path1.close();
2390 path1.lineTo(1, 0);
2391 if (!rectFirst) {
2392 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2393 }
2394 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2395
2396 // fail, move in the middle
2397 path1.reset();
2398 if (rectFirst) {
2399 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2400 }
2401 path1.moveTo(r1[0].fX, r1[0].fY);
2402 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2403 if (index == 2) {
2404 path1.moveTo(1, .5f);
2405 }
2406 path1.lineTo(r1[index].fX, r1[index].fY);
2407 }
2408 path1.close();
2409 if (!rectFirst) {
2410 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2411 }
2412 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2413
2414 // fail, move on the edge
2415 path1.reset();
2416 if (rectFirst) {
2417 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2418 }
2419 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2420 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2421 path1.lineTo(r1[index].fX, r1[index].fY);
2422 }
2423 path1.close();
2424 if (!rectFirst) {
2425 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2426 }
2427 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2428
2429 // fail, quad
2430 path1.reset();
2431 if (rectFirst) {
2432 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2433 }
2434 path1.moveTo(r1[0].fX, r1[0].fY);
2435 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2436 if (index == 2) {
2437 path1.quadTo(1, .5f, 1, .5f);
2438 }
2439 path1.lineTo(r1[index].fX, r1[index].fY);
2440 }
2441 path1.close();
2442 if (!rectFirst) {
2443 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2444 }
2445 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2446
2447 // fail, cubic
2448 path1.reset();
2449 if (rectFirst) {
2450 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2451 }
2452 path1.moveTo(r1[0].fX, r1[0].fY);
2453 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
2454 if (index == 2) {
2455 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2456 }
2457 path1.lineTo(r1[index].fX, r1[index].fY);
2458 }
2459 path1.close();
2460 if (!rectFirst) {
2461 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2462 }
2463 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2464
2465 // fail, not nested
2466 path1.reset();
2467 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2468 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
2469 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
2470 }
2471
2472 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2473 SkPath path;
2474 path.moveTo(0, 0);
2475 path.lineTo(10, 0);
2476 path.lineTo(10, 10);
2477 path.lineTo(0, 10);
2478 path.lineTo(0, 0);
2479 path.moveTo(1, 1);
2480 path.lineTo(9, 1);
2481 path.lineTo(9, 9);
2482 path.lineTo(1, 9);
2483 path.lineTo(1, 1);
2484 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
2485
2486 // pass, stroke rect
2487 SkPath src, dst;
2488 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2489 SkPaint strokePaint;
2490 strokePaint.setStyle(SkPaint::kStroke_Style);
2491 strokePaint.setStrokeWidth(2);
2492 strokePaint.getFillPath(src, &dst);
2493 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
2494}
2495
2496static void write_and_read_back(skiatest::Reporter* reporter,
2497 const SkPath& p) {
2498 SkWriter32 writer;
2499 writer.writePath(p);
2500 size_t size = writer.bytesWritten();
2501 SkAutoMalloc storage(size);
2502 writer.flatten(storage.get());
2503 SkReader32 reader(storage.get(), size);
2504
2505 SkPath readBack;
2506 REPORTER_ASSERT(reporter, readBack != p);
2507 reader.readPath(&readBack);
2508 REPORTER_ASSERT(reporter, readBack == p);
2509
2510 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
2511 p.getConvexityOrUnknown());
2512
2513 SkRect oval0, oval1;
2514 SkPath::Direction dir0, dir1;
2515 unsigned start0, start1;
2516 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
Mike Reed0c3137c2018-02-20 13:57:05 -05002517 if (SkPathPriv::IsOval(p, &oval0, &dir0, &start0) &&
2518 SkPathPriv::IsOval(readBack, &oval1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002519 REPORTER_ASSERT(reporter, oval0 == oval1);
2520 REPORTER_ASSERT(reporter, dir0 == dir1);
2521 REPORTER_ASSERT(reporter, start0 == start1);
2522 }
2523 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2524 SkRRect rrect0, rrect1;
Mike Reed0c3137c2018-02-20 13:57:05 -05002525 if (SkPathPriv::IsRRect(p, &rrect0, &dir0, &start0) &&
2526 SkPathPriv::IsRRect(readBack, &rrect1, &dir1, &start1)) {
Yuqian Li3154a532017-09-06 13:33:30 -04002527 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2528 REPORTER_ASSERT(reporter, dir0 == dir1);
2529 REPORTER_ASSERT(reporter, start0 == start1);
2530 }
2531 const SkRect& origBounds = p.getBounds();
2532 const SkRect& readBackBounds = readBack.getBounds();
2533
2534 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2535}
2536
Yuqian Li3154a532017-09-06 13:33:30 -04002537static void test_flattening(skiatest::Reporter* reporter) {
2538 SkPath p;
2539
2540 static const SkPoint pts[] = {
2541 { 0, 0 },
2542 { SkIntToScalar(10), SkIntToScalar(10) },
2543 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2544 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2545 };
2546 p.moveTo(pts[0]);
2547 p.lineTo(pts[1]);
2548 p.quadTo(pts[2], pts[3]);
2549 p.cubicTo(pts[4], pts[5], pts[6]);
2550
2551 write_and_read_back(reporter, p);
2552
2553 // create a buffer that should be much larger than the path so we don't
2554 // kill our stack if writer goes too far.
2555 char buffer[1024];
2556 size_t size1 = p.writeToMemory(nullptr);
2557 size_t size2 = p.writeToMemory(buffer);
2558 REPORTER_ASSERT(reporter, size1 == size2);
2559
2560 SkPath p2;
2561 size_t size3 = p2.readFromMemory(buffer, 1024);
2562 REPORTER_ASSERT(reporter, size1 == size3);
2563 REPORTER_ASSERT(reporter, p == p2);
2564
2565 size3 = p2.readFromMemory(buffer, 0);
2566 REPORTER_ASSERT(reporter, !size3);
2567
2568 SkPath tooShort;
2569 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2570 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2571
2572 char buffer2[1024];
2573 size3 = p2.writeToMemory(buffer2);
2574 REPORTER_ASSERT(reporter, size1 == size3);
2575 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
2576
2577 // test persistence of the oval flag & convexity
2578 {
2579 SkPath oval;
2580 SkRect rect = SkRect::MakeWH(10, 10);
2581 oval.addOval(rect);
2582
2583 write_and_read_back(reporter, oval);
2584 }
Yuqian Li3154a532017-09-06 13:33:30 -04002585}
2586
2587static void test_transform(skiatest::Reporter* reporter) {
2588 SkPath p;
2589
2590#define CONIC_PERSPECTIVE_BUG_FIXED 0
2591 static const SkPoint pts[] = {
2592 { 0, 0 }, // move
2593 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2594 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2595 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2596#if CONIC_PERSPECTIVE_BUG_FIXED
2597 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2598#endif
2599 };
2600 const int kPtCount = SK_ARRAY_COUNT(pts);
2601
2602 p.moveTo(pts[0]);
2603 p.lineTo(pts[1]);
2604 p.quadTo(pts[2], pts[3]);
2605 p.cubicTo(pts[4], pts[5], pts[6]);
2606#if CONIC_PERSPECTIVE_BUG_FIXED
2607 p.conicTo(pts[4], pts[5], 0.5f);
2608#endif
2609 p.close();
2610
2611 {
2612 SkMatrix matrix;
2613 matrix.reset();
2614 SkPath p1;
2615 p.transform(matrix, &p1);
2616 REPORTER_ASSERT(reporter, p == p1);
2617 }
2618
2619
2620 {
2621 SkMatrix matrix;
2622 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2623
2624 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2625
2626 p.transform(matrix, &p1);
2627 SkPoint pts1[kPtCount];
2628 int count = p1.getPoints(pts1, kPtCount);
2629 REPORTER_ASSERT(reporter, kPtCount == count);
2630 for (int i = 0; i < count; ++i) {
2631 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2632 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2633 }
2634 }
2635
2636 {
2637 SkMatrix matrix;
2638 matrix.reset();
2639 matrix.setPerspX(4);
2640
2641 SkPath p1;
2642 p1.moveTo(SkPoint::Make(0, 0));
2643
2644 p.transform(matrix, &p1);
2645 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
2646 p1.transform(matrix, nullptr);
2647 SkRect pBounds = p.getBounds();
2648 SkRect p1Bounds = p1.getBounds();
2649 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2650 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2651 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2652 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2653 }
2654
2655 p.reset();
2656 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
2657
2658 {
2659 SkMatrix matrix;
2660 matrix.reset();
2661 SkPath p1;
2662 p1.moveTo(SkPoint::Make(0, 0));
2663
2664 p.transform(matrix, &p1);
2665 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
2666 }
2667
2668
2669 {
2670 SkMatrix matrix;
2671 matrix.reset();
2672 matrix.setScaleX(-1);
2673 SkPath p1;
2674 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2675
2676 p.transform(matrix, &p1);
2677 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
2678 }
2679
2680 {
2681 SkMatrix matrix;
2682 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2683 SkPath p1;
2684 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2685
2686 p.transform(matrix, &p1);
2687 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
2688 }
2689}
2690
2691static void test_zero_length_paths(skiatest::Reporter* reporter) {
2692 SkPath p;
2693 uint8_t verbs[32];
2694
2695 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
2696 const char* testPath;
2697 const size_t numResultPts;
2698 const SkRect resultBound;
2699 const SkPath::Verb* resultVerbs;
2700 const size_t numResultVerbs;
2701 };
2702
2703 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2704 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2705 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2706 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2707 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2708 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2709 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2710 static const SkPath::Verb resultVerbs8[] = {
2711 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2712 };
2713 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2714 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2715 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2716 static const SkPath::Verb resultVerbs12[] = {
2717 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2718 };
2719 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2720 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2721 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2722 static const SkPath::Verb resultVerbs16[] = {
2723 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2724 };
2725 static const struct zeroPathTestData gZeroLengthTests[] = {
2726 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2727 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2728 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2729 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2730 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2731 { "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) },
2732 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2733 { "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) },
2734 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2735 { "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) },
2736 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2737 { "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) },
2738 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2739 { "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,
2740 SK_ARRAY_COUNT(resultVerbs14)
2741 },
2742 { "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) },
2743 { "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,
2744 SK_ARRAY_COUNT(resultVerbs16)
2745 }
2746 };
2747
2748 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2749 p.reset();
2750 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2751 REPORTER_ASSERT(reporter, valid);
2752 REPORTER_ASSERT(reporter, !p.isEmpty());
2753 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2754 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2755 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2756 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2757 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2758 }
2759 }
2760}
2761
2762struct SegmentInfo {
2763 SkPath fPath;
2764 int fPointCount;
2765};
2766
2767#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2768
2769static void test_segment_masks(skiatest::Reporter* reporter) {
2770 SkPath p, p2;
2771
2772 p.moveTo(0, 0);
2773 p.quadTo(100, 100, 200, 200);
2774 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2775 REPORTER_ASSERT(reporter, !p.isEmpty());
2776 p2 = p;
2777 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2778 p.cubicTo(100, 100, 200, 200, 300, 300);
2779 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2780 REPORTER_ASSERT(reporter, !p.isEmpty());
2781 p2 = p;
2782 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2783
2784 p.reset();
2785 p.moveTo(0, 0);
2786 p.cubicTo(100, 100, 200, 200, 300, 300);
2787 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
2788 p2 = p;
2789 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2790
2791 REPORTER_ASSERT(reporter, !p.isEmpty());
2792}
2793
2794static void test_iter(skiatest::Reporter* reporter) {
2795 SkPath p;
2796 SkPoint pts[4];
2797
2798 // Test an iterator with no path
2799 SkPath::Iter noPathIter;
2800 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2801
2802 // Test that setting an empty path works
2803 noPathIter.setPath(p, false);
2804 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2805
2806 // Test that close path makes no difference for an empty path
2807 noPathIter.setPath(p, true);
2808 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2809
2810 // Test an iterator with an initial empty path
2811 SkPath::Iter iter(p, false);
2812 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2813
2814 // Test that close path makes no difference
2815 iter.setPath(p, true);
2816 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2817
2818
2819 struct iterTestData {
2820 const char* testPath;
2821 const bool forceClose;
2822 const bool consumeDegenerates;
2823 const size_t* numResultPtsPerVerb;
2824 const SkPoint* resultPts;
2825 const SkPath::Verb* resultVerbs;
2826 const size_t numResultVerbs;
2827 };
2828
2829 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2830 static const SkPath::Verb resultVerbs2[] = {
2831 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2832 };
2833 static const SkPath::Verb resultVerbs3[] = {
2834 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2835 };
2836 static const SkPath::Verb resultVerbs4[] = {
2837 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2838 };
2839 static const SkPath::Verb resultVerbs5[] = {
2840 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2841 };
2842 static const size_t resultPtsSizes1[] = { 0 };
2843 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2844 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2845 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2846 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
2847 static const SkPoint* resultPts1 = nullptr;
2848 static const SkPoint resultPts2[] = {
2849 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2850 };
2851 static const SkPoint resultPts3[] = {
2852 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2853 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2854 };
2855 static const SkPoint resultPts4[] = {
2856 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2857 };
2858 static const SkPoint resultPts5[] = {
2859 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2860 };
2861 static const struct iterTestData gIterTests[] = {
2862 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2863 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2864 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2865 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2866 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2867 { "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) },
2868 { "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) },
2869 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2870 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2871 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2872 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2873 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2874 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
2875 };
2876
2877 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2878 p.reset();
2879 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2880 REPORTER_ASSERT(reporter, valid);
2881 iter.setPath(p, gIterTests[i].forceClose);
2882 int j = 0, l = 0;
2883 do {
2884 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2885 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2886 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2887 }
2888 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2889 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2890 }
2891
2892 p.reset();
2893 iter.setPath(p, false);
2894 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2895 p.lineTo(1, 1);
2896 p.close();
2897 iter.setPath(p, false);
2898 REPORTER_ASSERT(reporter, iter.isClosedContour());
2899 p.reset();
2900 iter.setPath(p, true);
2901 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2902 p.lineTo(1, 1);
2903 iter.setPath(p, true);
2904 REPORTER_ASSERT(reporter, iter.isClosedContour());
2905 p.moveTo(0, 0);
2906 p.lineTo(2, 2);
2907 iter.setPath(p, false);
2908 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2909
2910 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2911 // check to see if the result is correct.
2912 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2913 p.reset();
2914 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2915 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2916 iter.setPath(p, true);
2917 iter.next(pts, false);
2918 iter.next(pts, false);
2919 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2920 }
2921
2922 p.reset();
2923 p.quadTo(0, 0, 0, 0);
2924 iter.setPath(p, false);
2925 iter.next(pts, false);
2926 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2927 iter.setPath(p, false);
2928 iter.next(pts, false);
2929 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2930
2931 p.reset();
2932 p.conicTo(0, 0, 0, 0, 0.5f);
2933 iter.setPath(p, false);
2934 iter.next(pts, false);
2935 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2936 iter.setPath(p, false);
2937 iter.next(pts, false);
2938 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2939
2940 p.reset();
2941 p.cubicTo(0, 0, 0, 0, 0, 0);
2942 iter.setPath(p, false);
2943 iter.next(pts, false);
2944 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2945 iter.setPath(p, false);
2946 iter.next(pts, false);
2947 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2948
2949 p.moveTo(1, 1); // add a trailing moveto
2950 iter.setPath(p, false);
2951 iter.next(pts, false);
2952 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2953 iter.setPath(p, false);
2954 iter.next(pts, false);
2955 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2956
2957 // The GM degeneratesegments.cpp test is more extensive
2958
2959 // Test out mixed degenerate and non-degenerate geometry with Conics
2960 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2961 SkRect r = SkRect::MakeWH(100, 100);
2962 SkRRect rr;
2963 rr.setRectRadii(r, radii);
2964 p.reset();
2965 p.addRRect(rr);
2966 iter.setPath(p, false);
2967 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2968 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2969 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2970 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2971 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
2972}
2973
2974static void test_raw_iter(skiatest::Reporter* reporter) {
2975 SkPath p;
2976 SkPoint pts[4];
2977
2978 // Test an iterator with no path
2979 SkPath::RawIter noPathIter;
2980 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2981 // Test that setting an empty path works
2982 noPathIter.setPath(p);
2983 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2984
2985 // Test an iterator with an initial empty path
2986 SkPath::RawIter iter(p);
2987 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2988
2989 // Test that a move-only path returns the move.
2990 p.moveTo(SK_Scalar1, 0);
2991 iter.setPath(p);
2992 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2993 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2994 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2995 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2996
2997 // No matter how many moves we add, we should get them all back
2998 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2999 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3000 iter.setPath(p);
3001 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3002 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3003 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3004 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3005 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3006 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3007 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3008 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3009 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3010 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3011
3012 // Initial close is never ever stored
3013 p.reset();
3014 p.close();
3015 iter.setPath(p);
3016 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3017
3018 // Move/close sequences
3019 p.reset();
3020 p.close(); // Not stored, no purpose
3021 p.moveTo(SK_Scalar1, 0);
3022 p.close();
3023 p.close(); // Not stored, no purpose
3024 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3025 p.close();
3026 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3027 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3028 p.close();
3029 iter.setPath(p);
3030 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3031 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3032 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3033 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3034 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3035 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3036 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3037 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3038 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3039 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3040 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3041 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3042 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3043 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3044 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
3045 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3046
3047 // Generate random paths and verify
3048 SkPoint randomPts[25];
3049 for (int i = 0; i < 5; ++i) {
3050 for (int j = 0; j < 5; ++j) {
3051 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3052 }
3053 }
3054
3055 // Max of 10 segments, max 3 points per segment
3056 SkRandom rand(9876543);
3057 SkPoint expectedPts[31]; // May have leading moveTo
3058 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
3059 SkPath::Verb nextVerb;
3060
3061 for (int i = 0; i < 500; ++i) {
3062 p.reset();
3063 bool lastWasClose = true;
3064 bool haveMoveTo = false;
3065 SkPoint lastMoveToPt = { 0, 0 };
3066 int numPoints = 0;
3067 int numVerbs = (rand.nextU() >> 16) % 10;
3068 int numIterVerbs = 0;
3069 for (int j = 0; j < numVerbs; ++j) {
3070 do {
3071 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3072 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
3073 switch (nextVerb) {
3074 case SkPath::kMove_Verb:
3075 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3076 p.moveTo(expectedPts[numPoints]);
3077 lastMoveToPt = expectedPts[numPoints];
3078 numPoints += 1;
3079 lastWasClose = false;
3080 haveMoveTo = true;
3081 break;
3082 case SkPath::kLine_Verb:
3083 if (!haveMoveTo) {
3084 expectedPts[numPoints++] = lastMoveToPt;
3085 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3086 haveMoveTo = true;
3087 }
3088 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3089 p.lineTo(expectedPts[numPoints]);
3090 numPoints += 1;
3091 lastWasClose = false;
3092 break;
3093 case SkPath::kQuad_Verb:
3094 if (!haveMoveTo) {
3095 expectedPts[numPoints++] = lastMoveToPt;
3096 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3097 haveMoveTo = true;
3098 }
3099 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3100 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3101 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3102 numPoints += 2;
3103 lastWasClose = false;
3104 break;
3105 case SkPath::kConic_Verb:
3106 if (!haveMoveTo) {
3107 expectedPts[numPoints++] = lastMoveToPt;
3108 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3109 haveMoveTo = true;
3110 }
3111 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3112 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3113 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3114 rand.nextUScalar1() * 4);
3115 numPoints += 2;
3116 lastWasClose = false;
3117 break;
3118 case SkPath::kCubic_Verb:
3119 if (!haveMoveTo) {
3120 expectedPts[numPoints++] = lastMoveToPt;
3121 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3122 haveMoveTo = true;
3123 }
3124 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3125 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3126 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3127 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3128 expectedPts[numPoints + 2]);
3129 numPoints += 3;
3130 lastWasClose = false;
3131 break;
3132 case SkPath::kClose_Verb:
3133 p.close();
3134 haveMoveTo = false;
3135 lastWasClose = true;
3136 break;
3137 default:
3138 SkDEBUGFAIL("unexpected verb");
3139 }
3140 expectedVerbs[numIterVerbs++] = nextVerb;
3141 }
3142
3143 iter.setPath(p);
3144 numVerbs = numIterVerbs;
3145 numIterVerbs = 0;
3146 int numIterPts = 0;
3147 SkPoint lastMoveTo;
3148 SkPoint lastPt;
3149 lastMoveTo.set(0, 0);
3150 lastPt.set(0, 0);
3151 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3152 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3153 numIterVerbs++;
3154 switch (nextVerb) {
3155 case SkPath::kMove_Verb:
3156 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3157 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3158 lastPt = lastMoveTo = pts[0];
3159 numIterPts += 1;
3160 break;
3161 case SkPath::kLine_Verb:
3162 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3163 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3164 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3165 lastPt = pts[1];
3166 numIterPts += 1;
3167 break;
3168 case SkPath::kQuad_Verb:
3169 case SkPath::kConic_Verb:
3170 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3171 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3172 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3173 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3174 lastPt = pts[2];
3175 numIterPts += 2;
3176 break;
3177 case SkPath::kCubic_Verb:
3178 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3179 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3180 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3181 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3182 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3183 lastPt = pts[3];
3184 numIterPts += 3;
3185 break;
3186 case SkPath::kClose_Verb:
3187 lastPt = lastMoveTo;
3188 break;
3189 default:
3190 SkDEBUGFAIL("unexpected verb");
3191 }
3192 }
3193 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3194 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3195 }
3196}
3197
3198static void check_for_circle(skiatest::Reporter* reporter,
3199 const SkPath& path,
3200 bool expectedCircle,
3201 SkPathPriv::FirstDirection expectedDir) {
3202 SkRect rect = SkRect::MakeEmpty();
3203 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
3204 SkPath::Direction isOvalDir;
3205 unsigned isOvalStart;
Mike Reed0c3137c2018-02-20 13:57:05 -05003206 if (SkPathPriv::IsOval(path, &rect, &isOvalDir, &isOvalStart)) {
Yuqian Li3154a532017-09-06 13:33:30 -04003207 REPORTER_ASSERT(reporter, rect.height() == rect.width());
3208 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3209 SkPath tmpPath;
3210 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3211 REPORTER_ASSERT(reporter, path == tmpPath);
3212 }
3213 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
3214}
3215
3216static void test_circle_skew(skiatest::Reporter* reporter,
3217 const SkPath& path,
3218 SkPathPriv::FirstDirection dir) {
3219 SkPath tmp;
3220
3221 SkMatrix m;
3222 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3223 path.transform(m, &tmp);
3224 // this matrix reverses the direction.
3225 if (SkPathPriv::kCCW_FirstDirection == dir) {
3226 dir = SkPathPriv::kCW_FirstDirection;
3227 } else {
3228 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3229 dir = SkPathPriv::kCCW_FirstDirection;
3230 }
3231 check_for_circle(reporter, tmp, false, dir);
3232}
3233
3234static void test_circle_translate(skiatest::Reporter* reporter,
3235 const SkPath& path,
3236 SkPathPriv::FirstDirection dir) {
3237 SkPath tmp;
3238
3239 // translate at small offset
3240 SkMatrix m;
3241 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3242 path.transform(m, &tmp);
3243 check_for_circle(reporter, tmp, true, dir);
3244
3245 tmp.reset();
3246 m.reset();
3247
3248 // translate at a relatively big offset
3249 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3250 path.transform(m, &tmp);
3251 check_for_circle(reporter, tmp, true, dir);
3252}
3253
3254static void test_circle_rotate(skiatest::Reporter* reporter,
3255 const SkPath& path,
3256 SkPathPriv::FirstDirection dir) {
3257 for (int angle = 0; angle < 360; ++angle) {
3258 SkPath tmp;
3259 SkMatrix m;
3260 m.setRotate(SkIntToScalar(angle));
3261 path.transform(m, &tmp);
3262
3263 // TODO: a rotated circle whose rotated angle is not a multiple of 90
3264 // degrees is not an oval anymore, this can be improved. we made this
3265 // for the simplicity of our implementation.
3266 if (angle % 90 == 0) {
3267 check_for_circle(reporter, tmp, true, dir);
3268 } else {
3269 check_for_circle(reporter, tmp, false, dir);
3270 }
3271 }
3272}
3273
3274static void test_circle_mirror_x(skiatest::Reporter* reporter,
3275 const SkPath& path,
3276 SkPathPriv::FirstDirection dir) {
3277 SkPath tmp;
3278 SkMatrix m;
3279 m.reset();
3280 m.setScaleX(-SK_Scalar1);
3281 path.transform(m, &tmp);
3282 if (SkPathPriv::kCW_FirstDirection == dir) {
3283 dir = SkPathPriv::kCCW_FirstDirection;
3284 } else {
3285 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3286 dir = SkPathPriv::kCW_FirstDirection;
3287 }
3288 check_for_circle(reporter, tmp, true, dir);
3289}
3290
3291static void test_circle_mirror_y(skiatest::Reporter* reporter,
3292 const SkPath& path,
3293 SkPathPriv::FirstDirection dir) {
3294 SkPath tmp;
3295 SkMatrix m;
3296 m.reset();
3297 m.setScaleY(-SK_Scalar1);
3298 path.transform(m, &tmp);
3299
3300 if (SkPathPriv::kCW_FirstDirection == dir) {
3301 dir = SkPathPriv::kCCW_FirstDirection;
3302 } else {
3303 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3304 dir = SkPathPriv::kCW_FirstDirection;
3305 }
3306
3307 check_for_circle(reporter, tmp, true, dir);
3308}
3309
3310static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3311 const SkPath& path,
3312 SkPathPriv::FirstDirection dir) {
3313 SkPath tmp;
3314 SkMatrix m;
3315 m.reset();
3316 m.setScaleX(-SK_Scalar1);
3317 m.setScaleY(-SK_Scalar1);
3318 path.transform(m, &tmp);
3319
3320 check_for_circle(reporter, tmp, true, dir);
3321}
3322
3323static void test_circle_with_direction(skiatest::Reporter* reporter,
3324 SkPath::Direction inDir) {
3325 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
3326 SkPath path;
3327
3328 // circle at origin
3329 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3330
3331 check_for_circle(reporter, path, true, dir);
3332 test_circle_rotate(reporter, path, dir);
3333 test_circle_translate(reporter, path, dir);
3334 test_circle_skew(reporter, path, dir);
3335 test_circle_mirror_x(reporter, path, dir);
3336 test_circle_mirror_y(reporter, path, dir);
3337 test_circle_mirror_xy(reporter, path, dir);
3338
3339 // circle at an offset at (10, 10)
3340 path.reset();
3341 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
3342 SkIntToScalar(20), inDir);
3343
3344 check_for_circle(reporter, path, true, dir);
3345 test_circle_rotate(reporter, path, dir);
3346 test_circle_translate(reporter, path, dir);
3347 test_circle_skew(reporter, path, dir);
3348 test_circle_mirror_x(reporter, path, dir);
3349 test_circle_mirror_y(reporter, path, dir);
3350 test_circle_mirror_xy(reporter, path, dir);
3351
3352 // Try different starting points for the contour.
3353 for (unsigned start = 0; start < 4; ++start) {
3354 path.reset();
3355 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3356 test_circle_rotate(reporter, path, dir);
3357 test_circle_translate(reporter, path, dir);
3358 test_circle_skew(reporter, path, dir);
3359 test_circle_mirror_x(reporter, path, dir);
3360 test_circle_mirror_y(reporter, path, dir);
3361 test_circle_mirror_xy(reporter, path, dir);
3362 }
3363}
3364
3365static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3366 SkPath path;
3367 SkPath circle;
3368 SkPath rect;
3369 SkPath empty;
3370
3371 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3372 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
3373
3374 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
3375 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3376 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3377
3378 SkMatrix translate;
3379 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3380
3381 // Although all the path concatenation related operations leave
3382 // the path a circle, most mark it as a non-circle for simplicity
3383
3384 // empty + circle (translate)
3385 path = empty;
3386 path.addPath(circle, translate);
3387 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
3388
3389 // circle + empty (translate)
3390 path = circle;
3391 path.addPath(empty, translate);
3392
3393 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
3394
3395 // test reverseAddPath
3396 path = circle;
3397 path.reverseAddPath(rect);
3398 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
3399}
3400
3401static void test_circle(skiatest::Reporter* reporter) {
3402 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3403 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3404
3405 // multiple addCircle()
3406 SkPath path;
3407 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3408 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
3409 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3410
3411 // some extra lineTo() would make isOval() fail
3412 path.reset();
3413 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3414 path.lineTo(0, 0);
3415 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3416
3417 // not back to the original point
3418 path.reset();
3419 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3420 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
3421 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
3422
3423 test_circle_with_add_paths(reporter);
3424
3425 // test negative radius
3426 path.reset();
3427 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3428 REPORTER_ASSERT(reporter, path.isEmpty());
3429}
3430
3431static void test_oval(skiatest::Reporter* reporter) {
3432 SkRect rect;
3433 SkMatrix m;
3434 SkPath path;
3435 unsigned start = 0;
3436 SkPath::Direction dir = SkPath::kCCW_Direction;
3437
3438 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3439 path.addOval(rect);
3440
3441 // Defaults to dir = CW and start = 1
3442 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3443
3444 m.setRotate(SkIntToScalar(90));
3445 SkPath tmp;
3446 path.transform(m, &tmp);
3447 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3448 // is unchanged.
Mike Reed0c3137c2018-02-20 13:57:05 -05003449 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(tmp, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003450 REPORTER_ASSERT(reporter, 2 == start);
3451 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3452
3453 m.reset();
3454 m.setRotate(SkIntToScalar(30));
3455 tmp.reset();
3456 path.transform(m, &tmp);
3457 // an oval rotated 30 degrees is not an oval anymore.
3458 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3459
3460 // since empty path being transformed.
3461 path.reset();
3462 tmp.reset();
3463 m.reset();
3464 path.transform(m, &tmp);
3465 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3466
3467 // empty path is not an oval
3468 tmp.reset();
3469 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3470
3471 // only has moveTo()s
3472 tmp.reset();
3473 tmp.moveTo(0, 0);
3474 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
3475 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
3476
3477 // mimic WebKit's calling convention,
3478 // call moveTo() first and then call addOval()
3479 path.reset();
3480 path.moveTo(0, 0);
3481 path.addOval(rect);
3482 REPORTER_ASSERT(reporter, path.isOval(nullptr));
3483
3484 // copy path
3485 path.reset();
3486 tmp.reset();
3487 tmp.addOval(rect);
3488 path = tmp;
Mike Reed0c3137c2018-02-20 13:57:05 -05003489 REPORTER_ASSERT(reporter, SkPathPriv::IsOval(path, nullptr, &dir, &start));
Yuqian Li3154a532017-09-06 13:33:30 -04003490 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3491 REPORTER_ASSERT(reporter, 1 == start);
3492}
3493
3494static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3495 SkPath empty;
3496
3497 REPORTER_ASSERT(reporter, p.isEmpty());
3498 REPORTER_ASSERT(reporter, 0 == p.countPoints());
3499 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
3500 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
3501 REPORTER_ASSERT(reporter, p.isConvex());
3502 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3503 REPORTER_ASSERT(reporter, !p.isInverseFillType());
3504 REPORTER_ASSERT(reporter, p == empty);
3505 REPORTER_ASSERT(reporter, !(p != empty));
3506}
3507
3508static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3509 SkPath::Direction dir) {
3510 REPORTER_ASSERT(reporter, path->isConvex());
3511 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3512 path->setConvexity(SkPath::kUnknown_Convexity);
3513 REPORTER_ASSERT(reporter, path->isConvex());
3514 path->reset();
3515}
3516
3517static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3518 SkPath::Direction dir) {
3519 REPORTER_ASSERT(reporter, path->isConvex());
3520 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
3521 path->setConvexity(SkPath::kUnknown_Convexity);
3522 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3523 path->reset();
3524}
3525
3526static void test_rrect(skiatest::Reporter* reporter) {
3527 SkPath p;
3528 SkRRect rr;
3529 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3530 SkRect r = {10, 20, 30, 40};
3531 rr.setRectRadii(r, radii);
3532 p.addRRect(rr);
3533 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3534 p.addRRect(rr, SkPath::kCCW_Direction);
3535 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3536 p.addRoundRect(r, &radii[0].fX);
3537 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3538 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
3539 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3540 p.addRoundRect(r, radii[1].fX, radii[1].fY);
3541 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3542 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
3543 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3544 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3545 SkVector save = radii[i];
3546 radii[i].set(0, 0);
3547 rr.setRectRadii(r, radii);
3548 p.addRRect(rr);
3549 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3550 radii[i] = save;
3551 }
3552 p.addRoundRect(r, 0, 0);
3553 SkRect returnedRect;
3554 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3555 REPORTER_ASSERT(reporter, returnedRect == r);
3556 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3557 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3558 rr.setRectRadii(r, zeroRadii);
3559 p.addRRect(rr);
3560 bool closed;
3561 SkPath::Direction dir;
3562 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
3563 REPORTER_ASSERT(reporter, closed);
3564 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3565 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3566 p.addRRect(rr, SkPath::kCW_Direction);
3567 p.addRRect(rr, SkPath::kCW_Direction);
3568 REPORTER_ASSERT(reporter, !p.isConvex());
3569 p.reset();
3570 p.addRRect(rr, SkPath::kCCW_Direction);
3571 p.addRRect(rr, SkPath::kCCW_Direction);
3572 REPORTER_ASSERT(reporter, !p.isConvex());
3573 p.reset();
3574 SkRect emptyR = {10, 20, 10, 30};
3575 rr.setRectRadii(emptyR, radii);
3576 p.addRRect(rr);
Brian Salomon0a241ce2017-12-15 11:31:05 -05003577 // The round rect is "empty" in that it has no fill area. However,
3578 // the path isn't "empty" in that it should have verbs and points.
3579 REPORTER_ASSERT(reporter, !p.isEmpty());
3580 p.reset();
Yuqian Li3154a532017-09-06 13:33:30 -04003581 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3582 rr.setRectRadii(largeR, radii);
3583 p.addRRect(rr);
3584 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
3585
3586 // we check for non-finites
3587 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3588 rr.setRectRadii(infR, radii);
3589 REPORTER_ASSERT(reporter, rr.isEmpty());
3590
3591 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3592 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3593 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3594}
3595
3596static void test_arc(skiatest::Reporter* reporter) {
3597 SkPath p;
3598 SkRect emptyOval = {10, 20, 30, 20};
3599 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3600 p.addArc(emptyOval, 1, 2);
3601 REPORTER_ASSERT(reporter, p.isEmpty());
3602 p.reset();
3603 SkRect oval = {10, 20, 30, 40};
3604 p.addArc(oval, 1, 0);
3605 REPORTER_ASSERT(reporter, p.isEmpty());
3606 p.reset();
3607 SkPath cwOval;
3608 cwOval.addOval(oval);
3609 p.addArc(oval, 0, 360);
3610 REPORTER_ASSERT(reporter, p == cwOval);
3611 p.reset();
3612 SkPath ccwOval;
3613 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3614 p.addArc(oval, 0, -360);
3615 REPORTER_ASSERT(reporter, p == ccwOval);
3616 p.reset();
3617 p.addArc(oval, 1, 180);
3618 REPORTER_ASSERT(reporter, p.isConvex());
3619 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
3620 p.setConvexity(SkPath::kUnknown_Convexity);
3621 REPORTER_ASSERT(reporter, p.isConvex());
3622}
3623
3624static inline SkScalar oval_start_index_to_angle(unsigned start) {
3625 switch (start) {
3626 case 0:
3627 return 270.f;
3628 case 1:
3629 return 0.f;
3630 case 2:
3631 return 90.f;
3632 case 3:
3633 return 180.f;
3634 default:
3635 return -1.f;
3636 }
3637}
3638
3639static inline SkScalar canonical_start_angle(float angle) {
3640 while (angle < 0.f) {
3641 angle += 360.f;
3642 }
3643 while (angle >= 360.f) {
3644 angle -= 360.f;
3645 }
3646 return angle;
3647}
3648
3649static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3650 const SkPath& path) {
3651 SkRect r = SkRect::MakeEmpty();
3652 SkPath::Direction d = SkPath::kCCW_Direction;
3653 unsigned s = ~0U;
Mike Reed0c3137c2018-02-20 13:57:05 -05003654 bool isOval = SkPathPriv::IsOval(path, &r, &d, &s);
Yuqian Li3154a532017-09-06 13:33:30 -04003655 REPORTER_ASSERT(reporter, isOval);
3656 SkPath recreatedPath;
3657 recreatedPath.addOval(r, d, s);
3658 REPORTER_ASSERT(reporter, path == recreatedPath);
3659 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3660 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3661}
3662
3663static void test_arc_ovals(skiatest::Reporter* reporter) {
3664 SkRect oval = SkRect::MakeWH(10, 20);
3665 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3666 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3667 SkPath path;
3668 path.addArc(oval, start, sweep);
3669 // SkPath's interfaces for inserting and extracting ovals only allow contours
3670 // to start at multiples of 90 degrees.
3671 if (std::fmod(start, 90.f) == 0) {
3672 check_oval_arc(reporter, start, sweep, path);
3673 } else {
3674 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3675 }
3676 }
3677 // Test start angles that are nearly at valid oval start angles.
3678 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3679 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3680 SkPath path;
3681 path.addArc(oval, start + delta, sweep);
3682 check_oval_arc(reporter, start, sweep, path);
3683 }
3684 }
3685 }
3686}
3687
3688static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3689 SkScalar x0, SkScalar y0) {
3690 SkPoint pts[4];
3691 SkPath::Verb v = iter->next(pts);
3692 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3693 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3694 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3695}
3696
3697static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3698 SkScalar x1, SkScalar y1) {
3699 SkPoint pts[4];
3700 SkPath::Verb v = iter->next(pts);
3701 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3702 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3703 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3704}
3705
3706static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3707 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3708 SkPoint pts[4];
3709 SkPath::Verb v = iter->next(pts);
3710 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3711 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3712 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3713 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3714 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3715}
3716
3717static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3718 SkPoint pts[4];
3719 SkPath::Verb v = iter->next(pts);
3720 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3721}
3722
3723static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3724 check_done(reporter, p, iter);
3725 p->reset();
3726}
3727
3728static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3729 SkScalar x0, SkScalar y0) {
3730 SkPath::RawIter iter(*p);
3731 check_move(reporter, &iter, x0, y0);
3732 check_done_and_reset(reporter, p, &iter);
3733}
3734
3735static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3736 SkScalar x1, SkScalar y1) {
3737 SkPath::RawIter iter(*p);
3738 check_move(reporter, &iter, 0, 0);
3739 check_line(reporter, &iter, x1, y1);
3740 check_done_and_reset(reporter, p, &iter);
3741}
3742
3743static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3744 SkScalar x1, SkScalar y1) {
3745 SkPath::RawIter iter(*p);
3746 check_move(reporter, &iter, 0, 0);
3747 check_line(reporter, &iter, x1, y1);
3748 check_done(reporter, p, &iter);
3749}
3750
3751static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3752 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3753 SkPath::RawIter iter(*p);
3754 check_move(reporter, &iter, 0, 0);
3755 check_line(reporter, &iter, x1, y1);
3756 check_line(reporter, &iter, x2, y2);
3757 check_done_and_reset(reporter, p, &iter);
3758}
3759
3760static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3761 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3762 SkPath::RawIter iter(*p);
3763 check_move(reporter, &iter, 0, 0);
3764 check_quad(reporter, &iter, x1, y1, x2, y2);
3765 check_done_and_reset(reporter, p, &iter);
3766}
3767
3768static bool nearly_equal(const SkRect& a, const SkRect& b) {
3769 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3770 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3771 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3772 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3773}
3774
3775static void test_arcTo(skiatest::Reporter* reporter) {
3776 SkPath p;
3777 p.arcTo(0, 0, 1, 2, 1);
3778 check_path_is_line_and_reset(reporter, &p, 0, 0);
3779 p.arcTo(1, 2, 1, 2, 1);
3780 check_path_is_line_and_reset(reporter, &p, 1, 2);
3781 p.arcTo(1, 2, 3, 4, 0);
3782 check_path_is_line_and_reset(reporter, &p, 1, 2);
3783 p.arcTo(1, 2, 0, 0, 1);
3784 check_path_is_line_and_reset(reporter, &p, 1, 2);
3785 p.arcTo(1, 0, 1, 1, 1);
3786 SkPoint pt;
3787 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3788 p.reset();
3789 p.arcTo(1, 0, 1, -1, 1);
3790 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3791 p.reset();
3792 SkRect oval = {1, 2, 3, 4};
3793 p.arcTo(oval, 0, 0, true);
3794 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3795 p.arcTo(oval, 0, 0, false);
3796 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3797 p.arcTo(oval, 360, 0, true);
3798 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3799 p.arcTo(oval, 360, 0, false);
3800 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3801
3802 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
3803 p.arcTo(oval, 0, sweep, false);
3804 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3805 sweep += delta;
3806 delta /= 2;
3807 }
3808 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
3809 p.arcTo(oval, 0, sweep, false);
3810 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
3811 sweep -= delta;
3812 delta /= 2;
3813 }
3814 SkRect noOvalWidth = {1, 2, 0, 3};
3815 p.reset();
3816 p.arcTo(noOvalWidth, 0, 360, false);
3817 REPORTER_ASSERT(reporter, p.isEmpty());
3818
3819 SkRect noOvalHeight = {1, 2, 3, 1};
3820 p.reset();
3821 p.arcTo(noOvalHeight, 0, 360, false);
3822 REPORTER_ASSERT(reporter, p.isEmpty());
3823}
3824
3825static void test_addPath(skiatest::Reporter* reporter) {
3826 SkPath p, q;
3827 p.lineTo(1, 2);
3828 q.moveTo(4, 4);
3829 q.lineTo(7, 8);
3830 q.conicTo(8, 7, 6, 5, 0.5f);
3831 q.quadTo(6, 7, 8, 6);
3832 q.cubicTo(5, 6, 7, 8, 7, 5);
3833 q.close();
3834 p.addPath(q, -4, -4);
3835 SkRect expected = {0, 0, 4, 4};
3836 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3837 p.reset();
3838 p.reverseAddPath(q);
3839 SkRect reverseExpected = {4, 4, 8, 8};
3840 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3841}
3842
3843static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3844 SkPath p, q;
3845 if (explicitMoveTo) {
3846 p.moveTo(1, 1);
3847 }
3848 p.lineTo(1, 2);
3849 if (explicitMoveTo) {
3850 q.moveTo(2, 1);
3851 }
3852 q.lineTo(2, 2);
3853 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3854 uint8_t verbs[4];
3855 int verbcount = p.getVerbs(verbs, 4);
3856 REPORTER_ASSERT(reporter, verbcount == 4);
3857 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3858 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3859 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3860 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3861}
3862
3863static void test_extendClosedPath(skiatest::Reporter* reporter) {
3864 SkPath p, q;
3865 p.moveTo(1, 1);
3866 p.lineTo(1, 2);
3867 p.lineTo(2, 2);
3868 p.close();
3869 q.moveTo(2, 1);
3870 q.lineTo(2, 3);
3871 p.addPath(q, SkPath::kExtend_AddPathMode);
3872 uint8_t verbs[7];
3873 int verbcount = p.getVerbs(verbs, 7);
3874 REPORTER_ASSERT(reporter, verbcount == 7);
3875 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3876 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3877 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3878 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3879 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3880 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3881 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3882
3883 SkPoint pt;
3884 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3885 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3886 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3887}
3888
3889static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3890 SkPath p, q, r;
3891 // case 1: dst is empty
3892 p.moveTo(2, 1);
3893 p.lineTo(2, 3);
3894 q.addPath(p, mode);
3895 REPORTER_ASSERT(reporter, q == p);
3896 // case 2: src is empty
3897 p.addPath(r, mode);
3898 REPORTER_ASSERT(reporter, q == p);
3899 // case 3: src and dst are empty
3900 q.reset();
3901 q.addPath(r, mode);
3902 REPORTER_ASSERT(reporter, q.isEmpty());
3903}
3904
3905static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3906 SkPath p;
3907 p.conicTo(1, 2, 3, 4, -1);
3908 check_path_is_line_and_reset(reporter, &p, 3, 4);
3909 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3910 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3911 p.conicTo(1, 2, 3, 4, 1);
3912 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3913}
3914
3915static void test_get_point(skiatest::Reporter* reporter) {
3916 SkPath p;
3917 SkPoint pt = p.getPoint(0);
3918 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
3919 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
3920 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3921 p.setLastPt(10, 10);
3922 pt = p.getPoint(0);
3923 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
3924 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
3925 p.rMoveTo(10, 10);
3926 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3927}
3928
3929static void test_contains(skiatest::Reporter* reporter) {
3930 SkPath p;
3931 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3932 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
3933 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
3934 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3935 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
3936 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
3937 // this may return true or false, depending on the platform's numerics, but it should not crash
3938 (void) p.contains(-77.2027664f, 15.3066053f);
3939
3940 p.reset();
3941 p.setFillType(SkPath::kInverseWinding_FillType);
3942 REPORTER_ASSERT(reporter, p.contains(0, 0));
3943 p.setFillType(SkPath::kWinding_FillType);
3944 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3945 p.moveTo(4, 4);
3946 p.lineTo(6, 8);
3947 p.lineTo(8, 4);
3948 // test on edge
3949 REPORTER_ASSERT(reporter, p.contains(6, 4));
3950 REPORTER_ASSERT(reporter, p.contains(5, 6));
3951 REPORTER_ASSERT(reporter, p.contains(7, 6));
3952 // test quick reject
3953 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3954 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3955 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3956 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3957 // test various crossings in x
3958 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3959 REPORTER_ASSERT(reporter, p.contains(6, 7));
3960 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3961 p.reset();
3962 p.moveTo(4, 4);
3963 p.lineTo(8, 6);
3964 p.lineTo(4, 8);
3965 // test on edge
3966 REPORTER_ASSERT(reporter, p.contains(4, 6));
3967 REPORTER_ASSERT(reporter, p.contains(6, 5));
3968 REPORTER_ASSERT(reporter, p.contains(6, 7));
3969 // test various crossings in y
3970 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3971 REPORTER_ASSERT(reporter, p.contains(7, 6));
3972 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3973 p.reset();
3974 p.moveTo(4, 4);
3975 p.lineTo(8, 4);
3976 p.lineTo(8, 8);
3977 p.lineTo(4, 8);
3978 // test on vertices
3979 REPORTER_ASSERT(reporter, p.contains(4, 4));
3980 REPORTER_ASSERT(reporter, p.contains(8, 4));
3981 REPORTER_ASSERT(reporter, p.contains(8, 8));
3982 REPORTER_ASSERT(reporter, p.contains(4, 8));
3983 p.reset();
3984 p.moveTo(4, 4);
3985 p.lineTo(6, 8);
3986 p.lineTo(2, 8);
3987 // test on edge
3988 REPORTER_ASSERT(reporter, p.contains(5, 6));
3989 REPORTER_ASSERT(reporter, p.contains(4, 8));
3990 REPORTER_ASSERT(reporter, p.contains(3, 6));
3991 p.reset();
3992 p.moveTo(4, 4);
3993 p.lineTo(0, 6);
3994 p.lineTo(4, 8);
3995 // test on edge
3996 REPORTER_ASSERT(reporter, p.contains(2, 5));
3997 REPORTER_ASSERT(reporter, p.contains(2, 7));
3998 REPORTER_ASSERT(reporter, p.contains(4, 6));
3999 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4000 p.reset();
4001 p.moveTo(4, 0);
4002 p.lineTo(6, 4);
4003 p.lineTo(2, 4);
4004 p.moveTo(4, 0);
4005 p.lineTo(0, 8);
4006 p.lineTo(8, 8);
4007 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4008 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4009 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4010 REPORTER_ASSERT(reporter, p.contains(4, 4));
4011
4012 // test quads
4013 p.reset();
4014 p.moveTo(4, 4);
4015 p.quadTo(6, 6, 8, 8);
4016 p.quadTo(6, 8, 4, 8);
4017 p.quadTo(4, 6, 4, 4);
4018 REPORTER_ASSERT(reporter, p.contains(5, 6));
4019 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4020 // test quad edge
4021 REPORTER_ASSERT(reporter, p.contains(5, 5));
4022 REPORTER_ASSERT(reporter, p.contains(5, 8));
4023 REPORTER_ASSERT(reporter, p.contains(4, 5));
4024 // test quad endpoints
4025 REPORTER_ASSERT(reporter, p.contains(4, 4));
4026 REPORTER_ASSERT(reporter, p.contains(8, 8));
4027 REPORTER_ASSERT(reporter, p.contains(4, 8));
4028
4029 p.reset();
4030 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4031 p.moveTo(qPts[0]);
4032 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4033 p.quadTo(qPts[index], qPts[index + 1]);
4034 }
4035 REPORTER_ASSERT(reporter, p.contains(5, 6));
4036 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4037 // test quad edge
4038 SkPoint halfway;
4039 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4040 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4041 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4042 }
4043
4044 // test conics
4045 p.reset();
4046 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4047 p.moveTo(kPts[0]);
4048 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4049 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4050 }
4051 REPORTER_ASSERT(reporter, p.contains(5, 6));
4052 REPORTER_ASSERT(reporter, !p.contains(6, 5));
4053 // test conic edge
4054 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4055 SkConic conic(&kPts[index], 0.5f);
4056 halfway = conic.evalAt(0.5f);
4057 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4058 }
4059 // test conic end points
4060 REPORTER_ASSERT(reporter, p.contains(4, 4));
4061 REPORTER_ASSERT(reporter, p.contains(8, 8));
4062 REPORTER_ASSERT(reporter, p.contains(4, 8));
4063
4064 // test cubics
4065 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4066 for (int i = 0; i < 3; ++i) {
4067 p.reset();
4068 p.setFillType(SkPath::kEvenOdd_FillType);
4069 p.moveTo(pts[i].fX, pts[i].fY);
4070 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);
4071 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);
4072 p.close();
4073 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4074 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
4075 // test cubic edge
4076 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4077 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4078 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4079 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4080 // test cubic end points
4081 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4082 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4083 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
4084 }
4085}
4086
4087class PathRefTest_Private {
4088public:
4089 static void TestPathRef(skiatest::Reporter* reporter) {
4090 static const int kRepeatCnt = 10;
4091
4092 sk_sp<SkPathRef> pathRef(new SkPathRef);
4093
4094 SkPathRef::Editor ed(&pathRef);
4095
4096 {
4097 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4098 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4099 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4100 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4101 for (int i = 0; i < kRepeatCnt; ++i) {
4102 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4103 }
4104 ed.resetToSize(0, 0, 0);
4105 }
4106
4107 {
4108 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4109 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4110 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4111 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4112 for (int i = 0; i < kRepeatCnt; ++i) {
4113 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4114 }
4115 ed.resetToSize(0, 0, 0);
4116 }
4117
4118 {
4119 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4120 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4121 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4122 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4123 for (int i = 0; i < kRepeatCnt; ++i) {
4124 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4125 }
4126 ed.resetToSize(0, 0, 0);
4127 }
4128
4129 {
4130 SkScalar* weights = nullptr;
4131 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4132 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4133 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4134 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4135 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
4136 REPORTER_ASSERT(reporter, weights);
4137 for (int i = 0; i < kRepeatCnt; ++i) {
4138 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4139 }
4140 ed.resetToSize(0, 0, 0);
4141 }
4142
4143 {
4144 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4145 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4146 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4147 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4148 for (int i = 0; i < kRepeatCnt; ++i) {
4149 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4150 }
4151 ed.resetToSize(0, 0, 0);
4152 }
4153 }
4154};
4155
4156static void test_operatorEqual(skiatest::Reporter* reporter) {
4157 SkPath a;
4158 SkPath b;
4159 REPORTER_ASSERT(reporter, a == a);
4160 REPORTER_ASSERT(reporter, a == b);
4161 a.setFillType(SkPath::kInverseWinding_FillType);
4162 REPORTER_ASSERT(reporter, a != b);
4163 a.reset();
4164 REPORTER_ASSERT(reporter, a == b);
4165 a.lineTo(1, 1);
4166 REPORTER_ASSERT(reporter, a != b);
4167 a.reset();
4168 REPORTER_ASSERT(reporter, a == b);
4169 a.lineTo(1, 1);
4170 b.lineTo(1, 2);
4171 REPORTER_ASSERT(reporter, a != b);
4172 a.reset();
4173 a.lineTo(1, 2);
4174 REPORTER_ASSERT(reporter, a == b);
4175}
4176
4177static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
4178 bool dumpAsHex, const char* str) {
4179 SkDynamicMemoryWStream wStream;
4180 path.dump(&wStream, force, dumpAsHex);
4181 sk_sp<SkData> data = wStream.detachAsData();
4182 REPORTER_ASSERT(reporter, data->size() == strlen(str));
4183 if (strlen(str) > 0) {
4184 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4185 } else {
4186 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
4187 }
4188}
4189
4190static void test_dump(skiatest::Reporter* reporter) {
4191 SkPath p;
4192 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4193 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4194 p.moveTo(1, 2);
4195 p.lineTo(3, 4);
4196 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4197 "path.moveTo(1, 2);\n"
4198 "path.lineTo(3, 4);\n");
4199 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4200 "path.moveTo(1, 2);\n"
4201 "path.lineTo(3, 4);\n"
4202 "path.lineTo(1, 2);\n"
4203 "path.close();\n");
4204 p.reset();
4205 p.setFillType(SkPath::kEvenOdd_FillType);
4206 p.moveTo(1, 2);
4207 p.quadTo(3, 4, 5, 6);
4208 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4209 "path.moveTo(1, 2);\n"
4210 "path.quadTo(3, 4, 5, 6);\n");
4211 p.reset();
4212 p.setFillType(SkPath::kInverseWinding_FillType);
4213 p.moveTo(1, 2);
4214 p.conicTo(3, 4, 5, 6, 0.5f);
4215 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4216 "path.moveTo(1, 2);\n"
4217 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
4218 p.reset();
4219 p.setFillType(SkPath::kInverseEvenOdd_FillType);
4220 p.moveTo(1, 2);
4221 p.cubicTo(3, 4, 5, 6, 7, 8);
4222 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4223 "path.moveTo(1, 2);\n"
4224 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4225 p.reset();
4226 p.setFillType(SkPath::kWinding_FillType);
4227 p.moveTo(1, 2);
4228 p.lineTo(3, 4);
4229 compare_dump(reporter, p, false, true,
4230 "path.setFillType(SkPath::kWinding_FillType);\n"
4231 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4232 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
4233 p.reset();
4234 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4235 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4236 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4237 "path.moveTo(1, 2);\n"
4238 "path.lineTo(3, 4);\n");
4239}
4240
4241namespace {
4242
4243class ChangeListener : public SkPathRef::GenIDChangeListener {
4244public:
4245 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4246 ~ChangeListener() override {}
4247 void onChange() override {
4248 *fChanged = true;
4249 }
4250private:
4251 bool* fChanged;
4252};
4253
4254}
4255
4256class PathTest_Private {
4257public:
4258 static void TestPathTo(skiatest::Reporter* reporter) {
4259 SkPath p, q;
4260 p.lineTo(4, 4);
4261 p.reversePathTo(q);
4262 check_path_is_line(reporter, &p, 4, 4);
4263 q.moveTo(-4, -4);
4264 p.reversePathTo(q);
4265 check_path_is_line(reporter, &p, 4, 4);
4266 q.lineTo(7, 8);
4267 q.conicTo(8, 7, 6, 5, 0.5f);
4268 q.quadTo(6, 7, 8, 6);
4269 q.cubicTo(5, 6, 7, 8, 7, 5);
4270 q.close();
4271 p.reversePathTo(q);
4272 SkRect reverseExpected = {-4, -4, 8, 8};
4273 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4274 }
4275
4276 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4277 SkPath p;
4278
4279 bool changed = false;
4280 p.moveTo(0, 0);
4281
4282 // Check that listener is notified on moveTo().
4283
Chris Daltonafa11582018-06-08 12:00:44 -06004284 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004285 REPORTER_ASSERT(reporter, !changed);
4286 p.moveTo(10, 0);
4287 REPORTER_ASSERT(reporter, changed);
4288
4289 // Check that listener is notified on lineTo().
Chris Daltonafa11582018-06-08 12:00:44 -06004290 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004291 REPORTER_ASSERT(reporter, !changed);
4292 p.lineTo(20, 0);
4293 REPORTER_ASSERT(reporter, changed);
4294
4295 // Check that listener is notified on reset().
Chris Daltonafa11582018-06-08 12:00:44 -06004296 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004297 REPORTER_ASSERT(reporter, !changed);
4298 p.reset();
4299 REPORTER_ASSERT(reporter, changed);
4300
4301 p.moveTo(0, 0);
4302
4303 // Check that listener is notified on rewind().
Chris Daltonafa11582018-06-08 12:00:44 -06004304 SkPathPriv::AddGenIDChangeListener(p, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004305 REPORTER_ASSERT(reporter, !changed);
4306 p.rewind();
4307 REPORTER_ASSERT(reporter, changed);
4308
4309 // Check that listener is notified when pathref is deleted.
4310 {
4311 SkPath q;
4312 q.moveTo(10, 10);
Chris Daltonafa11582018-06-08 12:00:44 -06004313 SkPathPriv::AddGenIDChangeListener(q, sk_make_sp<ChangeListener>(&changed));
Yuqian Li3154a532017-09-06 13:33:30 -04004314 REPORTER_ASSERT(reporter, !changed);
4315 }
4316 // q went out of scope.
4317 REPORTER_ASSERT(reporter, changed);
4318 }
4319};
4320
4321static void test_crbug_629455(skiatest::Reporter* reporter) {
4322 SkPath path;
4323 path.moveTo(0, 0);
4324 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4325 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4326 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4327// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4328 path.lineTo(0, 0);
Yuqian Lif13beef2017-09-14 17:15:04 -04004329 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004330}
4331
4332static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4333 SkPath path;
4334 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4335 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4336 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4337 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4338 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4339 path.close();
4340
4341 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4342 SkPaint paint;
4343 paint.setAntiAlias(true);
4344 surface->getCanvas()->clipPath(path, true);
4345 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
4346}
4347
4348static void test_path_crbugskia6003() {
4349 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4350 SkCanvas* canvas = surface->getCanvas();
4351 SkPaint paint;
4352 paint.setAntiAlias(true);
4353 SkPath path;
4354 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4355 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4356 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4357 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4358 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4359 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4360 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4361 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4362 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4363 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4364 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4365 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4366 path.close();
4367 canvas->clipPath(path, true);
4368 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
4369}
4370
4371static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4372 SkPath path;
4373 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4374 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4375 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4376 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4377 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004378 test_draw_AA_path(100, 100, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004379}
4380
Yuqian Li3154a532017-09-06 13:33:30 -04004381static void test_skbug_6947() {
4382 SkPath path;
4383 SkPoint points[] =
4384 {{125.126022f, -0.499872506f}, {125.288895f, -0.499338806f},
4385 {125.299316f, -0.499290764f}, {126.294594f, 0.505449712f},
4386 {125.999992f, 62.5047531f}, {124.0f, 62.4980202f},
4387 {124.122749f, 0.498142242f}, {125.126022f, -0.499872506f},
4388 {125.119476f, 1.50011659f}, {125.122749f, 0.50012207f},
4389 {126.122749f, 0.502101898f}, {126.0f, 62.5019798f},
4390 {125.0f, 62.5f}, {124.000008f, 62.4952469f},
4391 {124.294609f, 0.495946467f}, {125.294601f, 0.50069809f},
4392 {125.289886f, 1.50068688f}, {125.282349f, 1.50065041f},
4393 {125.119476f, 1.50011659f}};
4394 constexpr SkPath::Verb kMove = SkPath::kMove_Verb;
4395 constexpr SkPath::Verb kLine = SkPath::kLine_Verb;
4396 constexpr SkPath::Verb kClose = SkPath::kClose_Verb;
4397 SkPath::Verb verbs[] = {kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose,
4398 kMove, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kLine, kClose};
4399 int pointIndex = 0;
4400 for(auto verb : verbs) {
4401 switch (verb) {
4402 case kMove:
4403 path.moveTo(points[pointIndex++]);
4404 break;
4405 case kLine:
4406 path.lineTo(points[pointIndex++]);
4407 break;
4408 case kClose:
4409 default:
4410 path.close();
4411 break;
4412 }
4413 }
Yuqian Lif13beef2017-09-14 17:15:04 -04004414 test_draw_AA_path(250, 125, path);
Yuqian Li3154a532017-09-06 13:33:30 -04004415}
Yuqian Lia81b6262017-09-06 17:10:05 -04004416
4417static void test_skbug_7015() {
4418 SkPath path;
4419 path.setFillType(SkPath::kWinding_FillType);
4420 path.moveTo(SkBits2Float(0x4388c000), SkBits2Float(0x43947c08)); // 273.5f, 296.969f
4421 path.lineTo(SkBits2Float(0x4386c000), SkBits2Float(0x43947c08)); // 269.5f, 296.969f
4422 // 269.297f, 292.172f, 273.695f, 292.172f, 273.5f, 296.969f
4423 path.cubicTo(SkBits2Float(0x4386a604), SkBits2Float(0x43921604),
4424 SkBits2Float(0x4388d8f6), SkBits2Float(0x43921604),
4425 SkBits2Float(0x4388c000), SkBits2Float(0x43947c08));
4426 path.close();
Yuqian Lif13beef2017-09-14 17:15:04 -04004427 test_draw_AA_path(500, 500, path);
Yuqian Lia81b6262017-09-06 17:10:05 -04004428}
4429
Yuqian Lic5e4e742017-09-18 14:38:43 -04004430static void test_skbug_7051() {
4431 SkPath path;
4432 path.moveTo(10, 10);
4433 path.cubicTo(10, 20, 10, 30, 30, 30);
4434 path.lineTo(50, 20);
4435 path.lineTo(50, 10);
4436 path.close();
4437 test_draw_AA_path(100, 100, path);
4438}
4439
Yuqian Li3154a532017-09-06 13:33:30 -04004440static void test_interp(skiatest::Reporter* reporter) {
4441 SkPath p1, p2, out;
4442 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4443 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4444 REPORTER_ASSERT(reporter, p1 == out);
4445 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4446 REPORTER_ASSERT(reporter, p1 == out);
4447 p1.moveTo(0, 2);
4448 p1.lineTo(0, 4);
4449 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4450 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4451 p2.moveTo(6, 0);
4452 p2.lineTo(8, 0);
4453 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4454 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4455 REPORTER_ASSERT(reporter, p2 == out);
4456 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4457 REPORTER_ASSERT(reporter, p1 == out);
4458 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4459 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4460 p1.reset();
4461 p1.moveTo(4, 4);
4462 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4463 p2.reset();
4464 p2.moveTo(4, 2);
4465 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4466 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4467 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4468 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4469 p2.reset();
4470 p2.moveTo(4, 2);
4471 p2.conicTo(6, 3, 6, 5, 1);
4472 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4473 p2.reset();
4474 p2.moveTo(4, 4);
4475 p2.conicTo(5, 4, 5, 5, 0.5f);
4476 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4477}
4478
4479DEF_TEST(PathInterp, reporter) {
4480 test_interp(reporter);
4481}
4482
4483#include "SkSurface.h"
4484DEF_TEST(PathBigCubic, reporter) {
4485 SkPath path;
4486 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4487 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4488 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
4489 path.moveTo(0, 512);
4490
4491 // this call should not assert
4492 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
4493}
4494
4495DEF_TEST(PathContains, reporter) {
4496 test_contains(reporter);
4497}
4498
4499DEF_TEST(Paths, reporter) {
4500 test_fuzz_crbug_647922();
4501 test_fuzz_crbug_643933();
4502 test_sect_with_horizontal_needs_pinning();
4503 test_crbug_629455(reporter);
4504 test_fuzz_crbug_627414(reporter);
4505 test_path_crbug364224();
4506 test_fuzz_crbug_662952(reporter);
4507 test_fuzz_crbug_662730(reporter);
4508 test_fuzz_crbug_662780();
4509 test_mask_overflow();
4510 test_path_crbugskia6003();
4511 test_fuzz_crbug_668907();
4512#if !defined(SK_SUPPORT_LEGACY_DELTA_AA)
4513 test_skbug_6947();
Yuqian Lia81b6262017-09-06 17:10:05 -04004514 test_skbug_7015();
Yuqian Lic5e4e742017-09-18 14:38:43 -04004515 test_skbug_7051();
Yuqian Li3154a532017-09-06 13:33:30 -04004516#endif
4517
4518 SkSize::Make(3, 4);
4519
4520 SkPath p, empty;
4521 SkRect bounds, bounds2;
4522 test_empty(reporter, p);
4523
4524 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
4525
4526 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4527 SkPath& self = p;
4528 p = self;
4529
4530 // this triggers a code path in SkPath::swap which is otherwise unexercised
4531 p.swap(self);
4532
4533 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
4534
4535 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4536 check_convex_bounds(reporter, p, bounds);
4537 // we have quads or cubics
4538 REPORTER_ASSERT(reporter,
4539 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
4540 REPORTER_ASSERT(reporter, !p.isEmpty());
4541
4542 p.reset();
4543 test_empty(reporter, p);
4544
4545 p.addOval(bounds);
4546 check_convex_bounds(reporter, p, bounds);
4547 REPORTER_ASSERT(reporter, !p.isEmpty());
4548
4549 p.rewind();
4550 test_empty(reporter, p);
4551
4552 p.addRect(bounds);
4553 check_convex_bounds(reporter, p, bounds);
4554 // we have only lines
4555 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
4556 REPORTER_ASSERT(reporter, !p.isEmpty());
4557
4558 REPORTER_ASSERT(reporter, p != empty);
4559 REPORTER_ASSERT(reporter, !(p == empty));
4560
4561 // do getPoints and getVerbs return the right result
4562 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4563 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
4564 SkPoint pts[4];
4565 int count = p.getPoints(pts, 4);
4566 REPORTER_ASSERT(reporter, count == 4);
4567 uint8_t verbs[6];
4568 verbs[5] = 0xff;
4569 p.getVerbs(verbs, 5);
4570 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4571 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4572 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4573 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4574 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4575 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
4576 bounds2.set(pts, 4);
4577 REPORTER_ASSERT(reporter, bounds == bounds2);
4578
4579 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4580 p.offset(SK_Scalar1*3, SK_Scalar1*4);
4581 REPORTER_ASSERT(reporter, bounds == p.getBounds());
4582
4583 REPORTER_ASSERT(reporter, p.isRect(nullptr));
4584 bounds2.setEmpty();
4585 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4586 REPORTER_ASSERT(reporter, bounds == bounds2);
4587
4588 // now force p to not be a rect
4589 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4590 p.addRect(bounds);
4591 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
4592
4593 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4594 // path has a trailing moveTo. Please see crbug.com\445368)
4595 {
4596 SkRect r;
4597 p.reset();
4598 p.addRect(bounds);
4599 REPORTER_ASSERT(reporter, p.isRect(&r));
4600 REPORTER_ASSERT(reporter, r == bounds);
4601 // add a moveTo outside of our bounds
4602 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4603 REPORTER_ASSERT(reporter, p.isRect(&r));
4604 REPORTER_ASSERT(reporter, r == bounds);
4605 }
4606
4607 test_operatorEqual(reporter);
4608 test_isLine(reporter);
4609 test_isRect(reporter);
4610 test_is_simple_closed_rect(reporter);
4611 test_isNestedFillRects(reporter);
4612 test_zero_length_paths(reporter);
4613 test_direction(reporter);
4614 test_convexity(reporter);
4615 test_convexity2(reporter);
4616 test_conservativelyContains(reporter);
4617 test_close(reporter);
4618 test_segment_masks(reporter);
4619 test_flattening(reporter);
4620 test_transform(reporter);
4621 test_bounds(reporter);
4622 test_iter(reporter);
4623 test_raw_iter(reporter);
4624 test_circle(reporter);
4625 test_oval(reporter);
4626 test_strokerec(reporter);
4627 test_addPoly(reporter);
4628 test_isfinite(reporter);
4629 test_isfinite_after_transform(reporter);
4630 test_islastcontourclosed(reporter);
4631 test_arb_round_rect_is_convex(reporter);
4632 test_arb_zero_rad_round_rect_is_rect(reporter);
4633 test_addrect(reporter);
4634 test_addrect_isfinite(reporter);
4635 test_tricky_cubic();
4636 test_clipped_cubic();
4637 test_crbug_170666();
4638 test_crbug_493450(reporter);
4639 test_crbug_495894(reporter);
4640 test_crbug_613918();
4641 test_bad_cubic_crbug229478();
4642 test_bad_cubic_crbug234190();
4643 test_gen_id(reporter);
4644 test_path_close_issue1474(reporter);
4645 test_path_to_region(reporter);
4646 test_rrect(reporter);
4647 test_arc(reporter);
4648 test_arc_ovals(reporter);
4649 test_arcTo(reporter);
4650 test_addPath(reporter);
4651 test_addPathMode(reporter, false, false);
4652 test_addPathMode(reporter, true, false);
4653 test_addPathMode(reporter, false, true);
4654 test_addPathMode(reporter, true, true);
4655 test_extendClosedPath(reporter);
4656 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4657 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
4658 test_conicTo_special_case(reporter);
4659 test_get_point(reporter);
4660 test_contains(reporter);
4661 PathTest_Private::TestPathTo(reporter);
4662 PathRefTest_Private::TestPathRef(reporter);
4663 PathTest_Private::TestPathrefListeners(reporter);
4664 test_dump(reporter);
4665 test_path_crbug389050(reporter);
4666 test_path_crbugskia2820(reporter);
4667 test_path_crbugskia5995();
4668 test_skbug_3469(reporter);
4669 test_skbug_3239(reporter);
4670 test_bounds_crbug_513799(reporter);
4671 test_fuzz_crbug_638223();
4672}
4673
4674DEF_TEST(conservatively_contains_rect, reporter) {
4675 SkPath path;
4676
4677 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4678 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4679 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4680 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4681 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4682 path.moveTo(0, 0);
4683
4684 // this guy should not assert
4685 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4686}
4687
4688///////////////////////////////////////////////////////////////////////////////////////////////////
4689
4690static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4691 for (int i = 0; i < n; ++i) {
4692 switch (verb) {
4693 case SkPath::kLine_Verb:
4694 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4695 break;
4696 case SkPath::kQuad_Verb:
4697 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4698 rand.nextF()*100, rand.nextF()*100);
4699 break;
4700 case SkPath::kConic_Verb:
4701 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4702 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4703 break;
4704 case SkPath::kCubic_Verb:
4705 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4706 rand.nextF()*100, rand.nextF()*100,
4707 rand.nextF()*100, rand.nextF()*100);
4708 break;
4709 default:
4710 SkASSERT(false);
4711 }
4712 }
4713}
4714
4715#include "SkPathOps.h"
4716DEF_TEST(path_tight_bounds, reporter) {
4717 SkRandom rand;
4718
4719 const SkPath::Verb verbs[] = {
4720 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4721 };
4722 for (int i = 0; i < 1000; ++i) {
4723 for (int n = 1; n <= 10; n += 9) {
4724 for (SkPath::Verb verb : verbs) {
4725 SkPath path;
4726 rand_path(&path, rand, verb, n);
4727 SkRect bounds = path.getBounds();
4728 SkRect tight = path.computeTightBounds();
4729 REPORTER_ASSERT(reporter, bounds.contains(tight));
4730
4731 SkRect tight2;
4732 TightBounds(path, &tight2);
4733 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
4734 }
4735 }
4736 }
4737}
4738
4739DEF_TEST(skbug_6450, r) {
4740 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
4741 SkVector rdi[4] = {
4742 { 1.81159976e-09f, 7.58768801e-05f },
4743 { 0.000118725002f, 0.000118725002f },
4744 { 0.000118725002f, 0.000118725002f },
4745 { 0.000118725002f, 0.486297607f }
4746 };
4747 SkRRect irr;
4748 irr.setRectRadii(ri, rdi);
4749 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
4750 SkVector rdo[4] = {
4751 { 0, 0 },
4752 { 0.0103298295f, 0.185887396f },
4753 { 2.52999727e-29f, 169.001938f },
4754 { 195.262741f, 195.161255f }
4755 };
4756 SkRRect orr;
4757 orr.setRectRadii(ro, rdo);
4758 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
4759}
4760
4761DEF_TEST(PathRefSerialization, reporter) {
4762 SkPath path;
4763 const size_t numMoves = 5;
4764 const size_t numConics = 7;
4765 const size_t numPoints = numMoves + 2 * numConics;
4766 const size_t numVerbs = numMoves + numConics;
4767 for (size_t i = 0; i < numMoves; ++i) path.moveTo(1, 2);
4768 for (size_t i = 0; i < numConics; ++i) path.conicTo(1, 2, 3, 4, 5);
4769 REPORTER_ASSERT(reporter, path.countPoints() == numPoints);
4770 REPORTER_ASSERT(reporter, path.countVerbs() == numVerbs);
4771
4772 // Verify that path serializes/deserializes properly.
4773 sk_sp<SkData> data = path.serialize();
4774 size_t bytesWritten = data->size();
4775
4776 {
4777 SkPath readBack;
4778 REPORTER_ASSERT(reporter, readBack != path);
4779 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten);
4780 REPORTER_ASSERT(reporter, bytesRead == bytesWritten);
4781 REPORTER_ASSERT(reporter, readBack == path);
4782 }
4783
Yuqian Li3154a532017-09-06 13:33:30 -04004784 // One less byte (rounded down to alignment) than was written will also
4785 // fail to be deserialized.
4786 {
4787 SkPath readBack;
4788 size_t bytesRead = readBack.readFromMemory(data->data(), bytesWritten - 4);
4789 REPORTER_ASSERT(reporter, !bytesRead);
4790 }
4791}
Mike Klein1170a552017-09-08 15:00:25 -04004792
4793DEF_TEST(NonFinitePathIteration, reporter) {
4794 SkPath path;
4795 path.moveTo(SK_ScalarInfinity, SK_ScalarInfinity);
4796
4797 int verbs = 0;
4798
4799 SkPath::RawIter iter(path);
4800 SkPoint pts[4];
4801 while (iter.next(pts) != SkPath::kDone_Verb) {
4802 verbs++;
4803 }
4804
4805 REPORTER_ASSERT(reporter, verbs == 0);
4806}
Cary Clark1acd3c72017-12-08 11:37:01 -05004807
4808
4809#ifndef SK_SUPPORT_LEGACY_SVG_ARC_TO
4810DEF_TEST(AndroidArc, reporter) {
4811 const char* tests[] = {
4812 "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",
4813 "M50,0L92,0 A8,8,0,0 1 100,8 L100,92 A8,8,0,0 1 92,100 L8,100"
4814 " A8,8,0,0 1 0,92 L 0,8 A8,8,0,0 1 8,0z",
4815 "M50 0A50 50,0,1,1,50 100A50 50,0,1,1,50 0"
4816 };
4817 for (auto test : tests) {
4818 SkPath aPath;
4819 SkAssertResult(SkParsePath::FromSVGString(test, &aPath));
4820 SkASSERT(aPath.isConvex());
4821 for (SkScalar scale = 1; scale < 1000; scale *= 1.1f) {
4822 SkPath scalePath = aPath;
4823 SkMatrix matrix;
4824 matrix.setScale(scale, scale);
4825 scalePath.transform(matrix);
4826 SkASSERT(scalePath.isConvex());
4827 }
4828 for (SkScalar scale = 1; scale < .001; scale /= 1.1f) {
4829 SkPath scalePath = aPath;
4830 SkMatrix matrix;
4831 matrix.setScale(scale, scale);
4832 scalePath.transform(matrix);
4833 SkASSERT(scalePath.isConvex());
4834 }
4835 }
4836}
4837#endif
Mike Reed1fe0bcc2018-01-22 16:49:49 -05004838
4839/*
4840 * Try a range of crazy values, just to ensure that we don't assert/crash.
4841 */
4842DEF_TEST(HugeGeometry, reporter) {
4843 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
4844 auto canvas = surf->getCanvas();
4845
4846 const bool aas[] = { false, true };
4847 const SkPaint::Style styles[] = {
4848 SkPaint::kFill_Style, SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
4849 };
4850 const SkScalar values[] = {
4851 0, 1, 1000, 1000 * 1000, 1000.f * 1000 * 10000, SK_ScalarMax / 2, SK_ScalarMax,
4852 SK_ScalarInfinity
4853 };
4854
4855 SkPaint paint;
4856 for (auto x : values) {
4857 SkRect r = { -x, -x, x, x };
4858 for (auto width : values) {
4859 paint.setStrokeWidth(width);
4860 for (auto aa : aas) {
4861 paint.setAntiAlias(aa);
4862 for (auto style : styles) {
4863 paint.setStyle(style);
4864 canvas->drawRect(r, paint);
4865 canvas->drawOval(r, paint);
4866 }
4867 }
4868 }
4869 }
4870
4871}
4872
Mike Reed1c6e7832018-01-30 11:29:36 -05004873// Treat nonfinite paths as "empty" or "full", depending on inverse-filltype
4874DEF_TEST(ClipPath_nonfinite, reporter) {
4875 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
4876 SkCanvas* canvas = surf->getCanvas();
4877
4878 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
4879 for (bool aa : {false, true}) {
4880 for (SkPath::FillType ft : {SkPath::kWinding_FillType, SkPath::kInverseWinding_FillType}) {
4881 for (SkScalar bad : {SK_ScalarInfinity, SK_ScalarNaN}) {
4882 for (int bits = 1; bits <= 15; ++bits) {
4883 SkPoint p0 = { 0, 0 };
4884 SkPoint p1 = { 0, 0 };
4885 if (bits & 1) p0.fX = -bad;
4886 if (bits & 2) p0.fY = -bad;
4887 if (bits & 4) p1.fX = bad;
4888 if (bits & 8) p1.fY = bad;
4889
4890 SkPath path;
4891 path.moveTo(p0);
4892 path.lineTo(p1);
4893 path.setFillType(ft);
4894 canvas->save();
4895 canvas->clipPath(path, aa);
4896 REPORTER_ASSERT(reporter, canvas->isClipEmpty() == !path.isInverseFillType());
4897 canvas->restore();
4898 }
4899 }
4900 }
4901 }
4902 REPORTER_ASSERT(reporter, !canvas->isClipEmpty());
4903}
4904
Cary Clark5c715182018-04-09 16:07:11 -04004905// skbug.com/7792
4906DEF_TEST(Path_isRect, reporter) {
Cary Clark8540e112018-04-11 14:30:27 -04004907 auto makePath = [](const SkPoint* points, size_t count, bool close) -> SkPath {
4908 SkPath path;
4909 for (size_t index = 0; index < count; ++index) {
4910 index < 2 ? path.moveTo(points[index]) : path.lineTo(points[index]);
4911 }
Cary Clark31608c02018-04-12 10:29:46 -04004912 if (close) {
4913 path.close();
4914 }
Cary Clark8540e112018-04-11 14:30:27 -04004915 return path;
4916 };
4917 auto makePath2 = [](const SkPoint* points, const SkPath::Verb* verbs, size_t count) -> SkPath {
4918 SkPath path;
4919 for (size_t index = 0; index < count; ++index) {
4920 switch (verbs[index]) {
4921 case SkPath::kMove_Verb:
4922 path.moveTo(*points++);
4923 break;
4924 case SkPath::kLine_Verb:
4925 path.lineTo(*points++);
4926 break;
4927 case SkPath::kClose_Verb:
4928 path.close();
4929 break;
4930 default:
4931 SkASSERT(0);
4932 }
4933 }
4934 return path;
4935 };
Cary Clarkd4228472018-04-13 07:07:04 -04004936 // isolated from skbug.com/7792 (bug description)
Cary Clark5c715182018-04-09 16:07:11 -04004937 SkRect rect;
Cary Clark8540e112018-04-11 14:30:27 -04004938 SkPoint points[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
4939 SkPath path = makePath(points, SK_ARRAY_COUNT(points), false);
Cary Clarkb120e922018-04-18 12:25:08 -04004940 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark5c715182018-04-09 16:07:11 -04004941 SkRect compare;
4942 compare.set(&points[1], SK_ARRAY_COUNT(points) - 1);
4943 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004944 // isolated from skbug.com/7792#c3
Cary Clark8540e112018-04-11 14:30:27 -04004945 SkPoint points3[] = { {75, 50}, {100, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 50} };
4946 path = makePath(points3, SK_ARRAY_COUNT(points3), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004947 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004948 // isolated from skbug.com/7792#c9
Cary Clark8540e112018-04-11 14:30:27 -04004949 SkPoint points9[] = { {10, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150} };
4950 path = makePath(points9, SK_ARRAY_COUNT(points9), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004951 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004952 compare.set(&points9[1], SK_ARRAY_COUNT(points9) - 1);
4953 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004954 // isolated from skbug.com/7792#c11
Cary Clark8540e112018-04-11 14:30:27 -04004955 SkPath::Verb verbs11[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4956 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb };
4957 SkPoint points11[] = { {75, 150}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 150} };
4958 path = makePath2(points11, verbs11, SK_ARRAY_COUNT(verbs11));
Cary Clarkb120e922018-04-18 12:25:08 -04004959 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004960 compare.set(&points11[0], SK_ARRAY_COUNT(points11));
4961 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004962 // isolated from skbug.com/7792#c14
Cary Clark8540e112018-04-11 14:30:27 -04004963 SkPath::Verb verbs14[] = { SkPath::kMove_Verb, SkPath::kMove_Verb, SkPath::kMove_Verb,
4964 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4965 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb,
4966 SkPath::kLine_Verb, SkPath::kClose_Verb };
4967 SkPoint points14[] = { {250, 75}, {250, 75}, {250, 75}, {100, 75},
4968 {150, 75}, {150, 150}, {75, 150}, {75, 75}, {0, 0} };
4969 path = makePath2(points14, verbs14, SK_ARRAY_COUNT(verbs14));
Cary Clarkb120e922018-04-18 12:25:08 -04004970 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004971 // isolated from skbug.com/7792#c15
Cary Clark8540e112018-04-11 14:30:27 -04004972 SkPath::Verb verbs15[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4973 SkPath::kLine_Verb, SkPath::kMove_Verb };
4974 SkPoint points15[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {250, 75} };
4975 path = makePath2(points15, verbs15, SK_ARRAY_COUNT(verbs15));
Cary Clarkb120e922018-04-18 12:25:08 -04004976 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clark8540e112018-04-11 14:30:27 -04004977 compare.set(&points15[0], SK_ARRAY_COUNT(points15) - 1);
4978 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkd4228472018-04-13 07:07:04 -04004979 // isolated from skbug.com/7792#c17
Cary Clark31608c02018-04-12 10:29:46 -04004980 SkPoint points17[] = { {75, 10}, {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10} };
4981 path = makePath(points17, SK_ARRAY_COUNT(points17), true);
Cary Clarkb120e922018-04-18 12:25:08 -04004982 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004983 // isolated from skbug.com/7792#c19
Cary Clark88ba9712018-04-12 14:00:24 -04004984 SkPath::Verb verbs19[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4985 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4986 SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb,
4987 SkPath::kLine_Verb, SkPath::kLine_Verb };
4988 SkPoint points19[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
4989 {75, 150}, {10, 10}, {30, 10}, {10, 30} };
4990 path = makePath2(points19, verbs19, SK_ARRAY_COUNT(verbs19));
Cary Clarkb120e922018-04-18 12:25:08 -04004991 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04004992 // isolated from skbug.com/7792#c23
4993 SkPath::Verb verbs23[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
4994 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
4995 SkPath::kLine_Verb, SkPath::kClose_Verb };
4996 SkPoint points23[] = { {75, 75}, {75, 75}, {75, 75}, {75, 75}, {150, 75}, {150, 150},
4997 {75, 150} };
4998 path = makePath2(points23, verbs23, SK_ARRAY_COUNT(verbs23));
Cary Clarkb120e922018-04-18 12:25:08 -04004999 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarkd4228472018-04-13 07:07:04 -04005000 compare.set(&points23[0], SK_ARRAY_COUNT(points23));
5001 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark48c464a2018-04-16 12:06:07 -04005002 // isolated from skbug.com/7792#c29
5003 SkPath::Verb verbs29[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5004 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5005 SkPath::kClose_Verb };
5006 SkPoint points29[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 250}, {75, 75} };
5007 path = makePath2(points29, verbs29, SK_ARRAY_COUNT(verbs29));
Cary Clarkb120e922018-04-18 12:25:08 -04005008 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005009 // isolated from skbug.com/7792#c31
5010 SkPath::Verb verbs31[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5011 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5012 SkPath::kClose_Verb };
5013 SkPoint points31[] = { {75, 75}, {150, 75}, {150, 150}, {75, 150}, {75, 10}, {75, 75} };
5014 path = makePath2(points31, verbs31, SK_ARRAY_COUNT(verbs31));
Cary Clarkb120e922018-04-18 12:25:08 -04005015 REPORTER_ASSERT(reporter, path.isRect(&rect));
Cary Clarka7651562018-04-17 09:30:14 -04005016 compare.set(&points31[0], 4);
5017 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark1cd60982018-04-17 11:53:34 -04005018 // isolated from skbug.com/7792#c36
5019 SkPath::Verb verbs36[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5020 SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
5021 SkPoint points36[] = { {75, 75}, {150, 75}, {150, 150}, {10, 150}, {75, 75}, {75, 75} };
5022 path = makePath2(points36, verbs36, SK_ARRAY_COUNT(verbs36));
Cary Clarkb120e922018-04-18 12:25:08 -04005023 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005024 // isolated from skbug.com/7792#c39
5025 SkPath::Verb verbs39[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005026 SkPath::kLine_Verb };
Cary Clark4eb6f7a2018-04-17 13:34:37 -04005027 SkPoint points39[] = { {150, 75}, {150, 150}, {75, 150}, {75, 100} };
5028 path = makePath2(points39, verbs39, SK_ARRAY_COUNT(verbs39));
Cary Clarkb120e922018-04-18 12:25:08 -04005029 REPORTER_ASSERT(reporter, !path.isRect(&rect));
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005030 // isolated from zero_length_paths_aa
Cary Clarkb120e922018-04-18 12:25:08 -04005031 SkPath::Verb verbsAA[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5032 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5033 SkPath::kLine_Verb, SkPath::kClose_Verb };
5034 SkPoint pointsAA[] = { {32, 9.5f}, {32, 9.5f}, {32, 17}, {17, 17}, {17, 9.5f}, {17, 2},
5035 {32, 2} };
5036 path = makePath2(pointsAA, verbsAA, SK_ARRAY_COUNT(verbsAA));
5037 REPORTER_ASSERT(reporter, path.isRect(&rect));
5038 compare.set(&pointsAA[0], SK_ARRAY_COUNT(pointsAA));
5039 REPORTER_ASSERT(reporter, rect == compare);
Cary Clarkdbc59ba2018-04-19 07:37:29 -04005040 // isolated from skbug.com/7792#c41
5041 SkPath::Verb verbs41[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5042 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5043 SkPath::kClose_Verb };
5044 SkPoint points41[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5045 path = makePath2(points41, verbs41, SK_ARRAY_COUNT(verbs41));
5046 REPORTER_ASSERT(reporter, path.isRect(&rect));
5047 compare.set(&points41[1], 4);
5048 REPORTER_ASSERT(reporter, rect == compare);
5049 // isolated from skbug.com/7792#c53
5050 SkPath::Verb verbs53[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb,
5051 SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb,
5052 SkPath::kClose_Verb };
5053 SkPoint points53[] = { {75, 75}, {150, 75}, {150, 150}, {140, 150}, {140, 75}, {75, 75} };
5054 path = makePath2(points53, verbs53, SK_ARRAY_COUNT(verbs53));
5055 REPORTER_ASSERT(reporter, path.isRect(&rect));
5056 compare.set(&points53[1], 4);
5057 REPORTER_ASSERT(reporter, rect == compare);
Cary Clark5c715182018-04-09 16:07:11 -04005058}
Mike Reedee430912018-05-23 12:12:21 -04005059
5060#include "SkVertices.h"
Mike Reed50595412018-05-30 16:32:33 -04005061static void draw_triangle(SkCanvas* canvas, const SkPoint pts[]) {
5062 // draw in different ways, looking for an assert
5063
5064 {
5065 SkPath path;
5066 path.addPoly(pts, 3, false);
5067 canvas->drawPath(path, SkPaint());
5068 }
5069
5070 const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, SK_ColorBLACK };
5071 auto v = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
5072 canvas->drawVertices(v, SkBlendMode::kSrcOver, SkPaint());
5073}
5074
Mike Reedee430912018-05-23 12:12:21 -04005075DEF_TEST(triangle_onehalf, reporter) {
5076 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
5077
5078 const SkPoint pts[] = {
5079 { 0.499069244f, 9.63295173f },
5080 { 0.499402374f, 7.88207579f },
5081 { 10.2363272f, 0.49999997f }
5082 };
Mike Reed50595412018-05-30 16:32:33 -04005083 draw_triangle(surface->getCanvas(), pts);
Mike Reedee430912018-05-23 12:12:21 -04005084}
Mike Reed50595412018-05-30 16:32:33 -04005085
5086DEF_TEST(triangle_big, reporter) {
5087 auto surface(SkSurface::MakeRasterN32Premul(4, 4304));
5088
5089 // The first two points, when sent through our fixed-point SkEdge, can walk negative beyond
5090 // -0.5 due to accumulated += error of the slope. We have since make the bounds calculation
5091 // be conservative, so we invoke clipping if we get in this situation.
5092 // This test was added to demonstrate the need for this conservative bounds calc.
5093 // (found by a fuzzer)
5094 const SkPoint pts[] = {
5095 { 0.327190518f, -114.945152f },
5096 { -0.5f, 1.00003874f },
5097 { 0.666425824f, 4304.26172f },
5098 };
5099 draw_triangle(surface->getCanvas(), pts);
5100}
5101