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