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