blob: 0ace812ef1010dfaf12cf2a94130d4a2909a78b5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -080010#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000011#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000014#include "SkPathEffect.h"
Hal Canary95e3c052017-01-11 12:44:43 -050015#include "SkPathPriv.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000017#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000018#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000019#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070020#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070021#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000022#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000023#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "Test.h"
Hal Canary95e3c052017-01-11 12:44:43 -050025#include <cmath>
reed@google.com8cae8352012-09-14 15:18:41 +000026
reed5bcbe912014-12-15 12:28:33 -080027static void set_radii(SkVector radii[4], int index, float rad) {
28 sk_bzero(radii, sizeof(SkVector) * 4);
29 radii[index].set(rad, rad);
30}
31
32static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33 const SkVector radii[4]) {
34 SkRRect rrect;
35 rrect.setRectRadii(bounds, radii);
36 REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38 SkPath path;
39 // this line should not assert in the debug build (from validate)
40 path.addRRect(rrect);
41 REPORTER_ASSERT(reporter, bounds == path.getBounds());
42}
43
caryclark5ccef572015-03-02 10:07:56 -080044static void test_skbug_3469(skiatest::Reporter* reporter) {
45 SkPath path;
46 path.moveTo(20, 20);
47 path.quadTo(20, 50, 80, 50);
48 path.quadTo(20, 50, 20, 80);
49 REPORTER_ASSERT(reporter, !path.isConvex());
50}
51
reed5bcbe912014-12-15 12:28:33 -080052static void test_skbug_3239(skiatest::Reporter* reporter) {
53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
56
57 const float rad = 33436320;
58
59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62 SkVector radii[4];
63 for (int i = 0; i < 4; ++i) {
64 set_radii(radii, i, rad);
65 test_add_rrect(reporter, rectx, radii);
66 test_add_rrect(reporter, recty, radii);
67 }
68}
69
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000070static void make_path_crbug364224(SkPath* path) {
71 path->reset();
72 path->moveTo(3.747501373f, 2.724499941f);
73 path->lineTo(3.747501373f, 3.75f);
74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75 path->lineTo(0.7475013733f, 4.0f);
76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77 path->lineTo(0.4975013733f, 1.0f);
78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79 path->lineTo(3.497501373f, 0.75f);
80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81 path->lineTo(3.715001345f, 0.5512499809f);
82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83 path->lineTo(0.7475013733f, 0.4999999702f);
84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85 path->lineTo(0.2475013733f, 3.75f);
86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87 path->lineTo(3.497501373f, 4.25f);
88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89 path->lineTo(3.997501373f, 2.474750042f);
90 path->lineTo(3.747501373f, 2.724499941f);
91 path->close();
92}
93
94static void make_path_crbug364224_simplified(SkPath* path) {
95 path->moveTo(3.747501373f, 2.724499941f);
96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97 path->close();
98}
99
reedff863bc2016-08-17 07:37:57 -0700100static void test_sect_with_horizontal_needs_pinning() {
101 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
102 // intersection.
103 SkPath path;
104 path.reset();
105 path.moveTo(-540000, -720000);
106 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
107 path.lineTo(1, 1);
108
109 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
110 SkPaint paint;
111 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
112}
113
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000114static void test_path_crbug364224() {
115 SkPath path;
116 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700117 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000118 SkCanvas* canvas = surface->getCanvas();
119
120 make_path_crbug364224_simplified(&path);
121 canvas->drawPath(path, paint);
122
123 make_path_crbug364224(&path);
124 canvas->drawPath(path, paint);
125}
126
caryclark3cebe242016-08-23 09:41:00 -0700127// this is a unit test instead of a GM because it doesn't draw anything
128static void test_fuzz_crbug_638223() {
129 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
130 SkCanvas* canvas = surface->getCanvas();
131 SkPath path;
132 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
133 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
134 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
135 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
caryclarke97fe832016-09-06 08:54:10 -0700141static void test_fuzz_crbug_643933() {
142 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
143 SkCanvas* canvas = surface->getCanvas();
144 SkPaint paint;
145 paint.setAntiAlias(true);
146 SkPath path;
147 path.moveTo(0, 0);
148 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
149 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
150 SkBits2Float(0x721aee0c)); // 3.0687e+30f
151 canvas->drawPath(path, paint);
152 path.reset();
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
155 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
156 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
157 canvas->drawPath(path, paint);
158}
159
caryclarkbac10462016-09-22 10:24:59 -0700160static void test_fuzz_crbug_647922() {
161 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
162 SkCanvas* canvas = surface->getCanvas();
163 SkPaint paint;
164 paint.setAntiAlias(true);
165 SkPath path;
166 path.moveTo(0, 0);
167 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
168 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
169 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
170 canvas->drawPath(path, paint);
171}
172
liyuqian041da382016-11-11 09:59:51 -0800173static void test_fuzz_crbug_662780() {
174 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
175 SkCanvas* canvas = surface->getCanvas();
176 SkPaint paint;
177 paint.setAntiAlias(true);
178 SkPath path;
179 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
180 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
181 // 8, 8, 8.00002f, 8, 0.707107f
182 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
183 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
184 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
185 // 308, 8, 308, 8, 0.707107f
186 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
187 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
188 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
189 // 308, 158, 308, 158, 0.707107f
190 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
191 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
192 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
193 // 8, 158, 8, 158, 0.707107f
194 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
195 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
196 path.close();
197 canvas->clipPath(path, true);
198 canvas->drawRectCoords(0, 0, 250, 250, paint);
199}
200
Yuqian Li20079a92016-11-16 13:07:57 -0500201static void test_mask_overflow() {
202 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
203 SkCanvas* canvas = surface->getCanvas();
204 SkPaint paint;
205 paint.setAntiAlias(true);
206 SkPath path;
207 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
208 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
209 // 440.47f, 341, 437, 344.47f, 437, 348.75f
210 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
211 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
212 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
213 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
214 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
215 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
216 canvas->drawPath(path, paint);
217}
218
Yuqian Li79252f72016-11-29 15:02:49 -0500219static void test_fuzz_crbug_668907() {
220 auto surface(SkSurface::MakeRasterN32Premul(400, 500));
221 SkCanvas* canvas = surface->getCanvas();
222 SkPaint paint;
223 paint.setAntiAlias(true);
224 SkPath path;
225 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
226 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
227 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
228 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
229 path.close();
230 canvas->drawPath(path, paint);
231}
232
piotaixrfac4e0e2014-08-26 11:59:04 -0700233/**
234 * In debug mode, this path was causing an assertion to fail in
235 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
236 */
237static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
238 SkPoint orig, p1, p2, p3;
239 orig = SkPoint::Make(1.f, 1.f);
240 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
241 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
242 p3 = SkPoint::Make(2.f, 2.f);
243
244 path->reset();
245 path->moveTo(orig);
246 path->cubicTo(p1, p2, p3);
247 path->close();
248}
249
250static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
251 SkPath path;
252 make_path_crbugskia2820(&path, reporter);
253
254 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
255 stroke.setStrokeStyle(2 * SK_Scalar1);
256 stroke.applyToPath(&path, path);
257}
258
Yuqian Li9939bd62016-11-29 10:27:16 -0500259static void test_path_crbugskia5995() {
260 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
261 SkCanvas* canvas = surface->getCanvas();
262 SkPaint paint;
263 paint.setAntiAlias(true);
264 SkPath path;
265 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
266 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
267 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
268 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
269 canvas->drawPath(path, paint);
270}
271
reed@google.comcc8be772013-10-15 15:35:29 +0000272static void make_path0(SkPath* path) {
273 // from * https://code.google.com/p/skia/issues/detail?id=1706
274
275 path->moveTo(146.939f, 1012.84f);
276 path->lineTo(181.747f, 1009.18f);
277 path->lineTo(182.165f, 1013.16f);
278 path->lineTo(147.357f, 1016.82f);
279 path->lineTo(146.939f, 1012.84f);
280 path->close();
281}
282
283static void make_path1(SkPath* path) {
284 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
285}
286
287typedef void (*PathProc)(SkPath*);
288
289/*
290 * Regression test: we used to crash (overwrite internal storage) during
291 * construction of the region when the path was INVERSE. That is now fixed,
292 * so test these regions (which used to assert/crash).
293 *
294 * https://code.google.com/p/skia/issues/detail?id=1706
295 */
296static void test_path_to_region(skiatest::Reporter* reporter) {
297 PathProc procs[] = {
298 make_path0,
299 make_path1,
300 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000301
reed@google.comcc8be772013-10-15 15:35:29 +0000302 SkRegion clip;
303 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000304
reed@google.comcc8be772013-10-15 15:35:29 +0000305 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
306 SkPath path;
307 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000308
reed@google.comcc8be772013-10-15 15:35:29 +0000309 SkRegion rgn;
310 rgn.setPath(path, clip);
311 path.toggleInverseFillType();
312 rgn.setPath(path, clip);
313 }
314}
315
halcanary4dbbd042016-06-07 17:21:10 -0700316#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000317 #define SUPPRESS_VISIBILITY_WARNING
318#else
319 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
320#endif
321
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000322static void test_path_close_issue1474(skiatest::Reporter* reporter) {
323 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
324 // are relative to the point we close to, not relative to the point we close from.
325 SkPath path;
326 SkPoint last;
327
328 // Test rLineTo().
329 path.rLineTo(0, 100);
330 path.rLineTo(100, 0);
331 path.close(); // Returns us back to 0,0.
332 path.rLineTo(50, 50); // This should go to 50,50.
333
334 path.getLastPt(&last);
335 REPORTER_ASSERT(reporter, 50 == last.fX);
336 REPORTER_ASSERT(reporter, 50 == last.fY);
337
338 // Test rQuadTo().
339 path.rewind();
340 path.rLineTo(0, 100);
341 path.rLineTo(100, 0);
342 path.close();
343 path.rQuadTo(50, 50, 75, 75);
344
345 path.getLastPt(&last);
346 REPORTER_ASSERT(reporter, 75 == last.fX);
347 REPORTER_ASSERT(reporter, 75 == last.fY);
348
349 // Test rConicTo().
350 path.rewind();
351 path.rLineTo(0, 100);
352 path.rLineTo(100, 0);
353 path.close();
354 path.rConicTo(50, 50, 85, 85, 2);
355
356 path.getLastPt(&last);
357 REPORTER_ASSERT(reporter, 85 == last.fX);
358 REPORTER_ASSERT(reporter, 85 == last.fY);
359
360 // Test rCubicTo().
361 path.rewind();
362 path.rLineTo(0, 100);
363 path.rLineTo(100, 0);
364 path.close();
365 path.rCubicTo(50, 50, 85, 85, 95, 95);
366
367 path.getLastPt(&last);
368 REPORTER_ASSERT(reporter, 95 == last.fX);
369 REPORTER_ASSERT(reporter, 95 == last.fY);
370}
371
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000372static void test_gen_id(skiatest::Reporter* reporter) {
373 SkPath a, b;
374 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
375
376 a.moveTo(0, 0);
377 const uint32_t z = a.getGenerationID();
378 REPORTER_ASSERT(reporter, z != b.getGenerationID());
379
380 a.reset();
381 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
382
383 a.moveTo(1, 1);
384 const uint32_t y = a.getGenerationID();
385 REPORTER_ASSERT(reporter, z != y);
386
387 b.moveTo(2, 2);
388 const uint32_t x = b.getGenerationID();
389 REPORTER_ASSERT(reporter, x != y && x != z);
390
391 a.swap(b);
392 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
393
394 b = a;
395 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
396
397 SkPath c(a);
398 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
399
400 c.lineTo(3, 3);
401 const uint32_t w = c.getGenerationID();
402 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
403 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
404 REPORTER_ASSERT(reporter, w != x);
405
djsollen50da1d82015-02-17 09:09:53 -0800406#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000407 static bool kExpectGenIDToIgnoreFill = false;
408#else
409 static bool kExpectGenIDToIgnoreFill = true;
410#endif
411
412 c.toggleInverseFillType();
413 const uint32_t v = c.getGenerationID();
414 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
415
416 c.rewind();
417 REPORTER_ASSERT(reporter, v != c.getGenerationID());
418}
419
reed@google.com3eff3592013-05-08 21:08:21 +0000420// This used to assert in the debug build, as the edges did not all line-up.
421static void test_bad_cubic_crbug234190() {
422 SkPath path;
423 path.moveTo(13.8509f, 3.16858f);
424 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
425 -2.38991e+08f, -4.26573e+08f,
426 -2.41016e+08f, -4.30188e+08f);
427
428 SkPaint paint;
429 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700430 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000431 surface->getCanvas()->drawPath(path, paint);
432}
433
reed@google.com7a90daf2013-04-10 18:44:00 +0000434static void test_bad_cubic_crbug229478() {
435 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000436 { 4595.91064f, -11596.9873f },
437 { 4597.2168f, -11595.9414f },
438 { 4598.52344f, -11594.8955f },
439 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000440 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000441
reed@google.com7a90daf2013-04-10 18:44:00 +0000442 SkPath path;
443 path.moveTo(pts[0]);
444 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000445
reed@google.com7a90daf2013-04-10 18:44:00 +0000446 SkPaint paint;
447 paint.setStyle(SkPaint::kStroke_Style);
448 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000449
reed@google.com7a90daf2013-04-10 18:44:00 +0000450 SkPath dst;
451 // Before the fix, this would infinite-recurse, and run out of stack
452 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700453 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000454}
455
reed@google.com64d62952013-01-18 17:49:28 +0000456static void build_path_170666(SkPath& path) {
457 path.moveTo(17.9459f, 21.6344f);
458 path.lineTo(139.545f, -47.8105f);
459 path.lineTo(139.545f, -47.8105f);
460 path.lineTo(131.07f, -47.3888f);
461 path.lineTo(131.07f, -47.3888f);
462 path.lineTo(122.586f, -46.9532f);
463 path.lineTo(122.586f, -46.9532f);
464 path.lineTo(18076.6f, 31390.9f);
465 path.lineTo(18076.6f, 31390.9f);
466 path.lineTo(18085.1f, 31390.5f);
467 path.lineTo(18085.1f, 31390.5f);
468 path.lineTo(18076.6f, 31390.9f);
469 path.lineTo(18076.6f, 31390.9f);
470 path.lineTo(17955, 31460.3f);
471 path.lineTo(17955, 31460.3f);
472 path.lineTo(17963.5f, 31459.9f);
473 path.lineTo(17963.5f, 31459.9f);
474 path.lineTo(17971.9f, 31459.5f);
475 path.lineTo(17971.9f, 31459.5f);
476 path.lineTo(17.9551f, 21.6205f);
477 path.lineTo(17.9551f, 21.6205f);
478 path.lineTo(9.47091f, 22.0561f);
479 path.lineTo(9.47091f, 22.0561f);
480 path.lineTo(17.9459f, 21.6344f);
481 path.lineTo(17.9459f, 21.6344f);
482 path.close();path.moveTo(0.995934f, 22.4779f);
483 path.lineTo(0.986725f, 22.4918f);
484 path.lineTo(0.986725f, 22.4918f);
485 path.lineTo(17955, 31460.4f);
486 path.lineTo(17955, 31460.4f);
487 path.lineTo(17971.9f, 31459.5f);
488 path.lineTo(17971.9f, 31459.5f);
489 path.lineTo(18093.6f, 31390.1f);
490 path.lineTo(18093.6f, 31390.1f);
491 path.lineTo(18093.6f, 31390);
492 path.lineTo(18093.6f, 31390);
493 path.lineTo(139.555f, -47.8244f);
494 path.lineTo(139.555f, -47.8244f);
495 path.lineTo(122.595f, -46.9671f);
496 path.lineTo(122.595f, -46.9671f);
497 path.lineTo(0.995934f, 22.4779f);
498 path.lineTo(0.995934f, 22.4779f);
499 path.close();
500 path.moveTo(5.43941f, 25.5223f);
501 path.lineTo(798267, -28871.1f);
502 path.lineTo(798267, -28871.1f);
503 path.lineTo(3.12512e+06f, -113102);
504 path.lineTo(3.12512e+06f, -113102);
505 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
506 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
507 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
508 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
509 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
510 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
511 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
512 path.lineTo(2.78271e+08f, -1.00733e+07f);
513 path.lineTo(2.78271e+08f, -1.00733e+07f);
514 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
515 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
516 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
517 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
518 path.lineTo(2.77473e+08f, -1.00444e+07f);
519 path.lineTo(2.77473e+08f, -1.00444e+07f);
520 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
521 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
522 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
523 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
524 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
525 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
526 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
527 path.lineTo(798284, -28872);
528 path.lineTo(798284, -28872);
529 path.lineTo(22.4044f, 24.6677f);
530 path.lineTo(22.4044f, 24.6677f);
531 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
532 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
533 path.close();
534}
535
536static void build_path_simple_170666(SkPath& path) {
537 path.moveTo(126.677f, 24.1591f);
538 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
539}
540
541// This used to assert in the SK_DEBUG build, as the clip step would fail with
542// too-few interations in our cubic-line intersection code. That code now runs
543// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000544static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000545 SkPath path;
546 SkPaint paint;
547 paint.setAntiAlias(true);
548
reede8f30622016-03-23 18:59:25 -0700549 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000550
reed@google.com64d62952013-01-18 17:49:28 +0000551 build_path_simple_170666(path);
552 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000553
reed@google.com64d62952013-01-18 17:49:28 +0000554 build_path_170666(path);
555 surface->getCanvas()->drawPath(path, paint);
556}
557
caryclarke8c56662015-07-14 11:19:26 -0700558
559static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
560 SkScalar tx, SkScalar ty, SkScalar scale) {
561 SkPath smallPath;
562 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
563 bool smallConvex = smallPath.isConvex();
564 SkPath largePath;
565 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
566 SkMatrix matrix;
567 matrix.reset();
568 matrix.preTranslate(100, 100);
569 matrix.preScale(scale, scale);
570 largePath.transform(matrix);
571 bool largeConvex = largePath.isConvex();
572 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
573}
574
575static void test_crbug_493450(skiatest::Reporter* reporter) {
576 const char reducedCase[] =
577 "M0,0"
578 "L0.0002, 0"
579 "L0.0002, 0.0002"
580 "L0.0001, 0.0001"
581 "L0,0.0002"
582 "Z";
583 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
584 const char originalFiddleData[] =
585 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
586 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
587 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
588 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
589 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
590 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
591 826357.3384828606f);
592}
593
594static void test_crbug_495894(skiatest::Reporter* reporter) {
595 const char originalFiddleData[] =
596 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
597 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
598 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
599 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
600 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
601 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
602 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
603 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
604 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
605 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
606 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
607 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
608 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
609 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
610 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
611 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
612 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
613 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
614 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
615 "L-0.33993994441916414,-0.11288906492739594Z";
616 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
617 65536);
618}
619
mbarbella99600d02016-06-01 15:39:47 -0700620static void test_crbug_613918() {
621 SkPath path;
622 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
623 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
624 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
625
626 // This call could lead to an assert or uninitialized read due to a failure
627 // to check the return value from SkCubicClipper::ChopMonoAtY.
628 path.contains(-1.84817e-08f, 1.15465e-08f);
629}
630
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000631static void test_addrect(skiatest::Reporter* reporter) {
632 SkPath path;
633 path.lineTo(0, 0);
634 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700635 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000636
637 path.reset();
638 path.lineTo(FLT_EPSILON, FLT_EPSILON);
639 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000641
642 path.reset();
643 path.quadTo(0, 0, 0, 0);
644 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700645 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000646
647 path.reset();
648 path.conicTo(0, 0, 0, 0, 0.5f);
649 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700650 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000651
652 path.reset();
653 path.cubicTo(0, 0, 0, 0, 0, 0);
654 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700655 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000656}
657
reed@google.coma8790de2012-10-24 21:04:04 +0000658// Make sure we stay non-finite once we get there (unless we reset or rewind).
659static void test_addrect_isfinite(skiatest::Reporter* reporter) {
660 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000661
reed@google.coma8790de2012-10-24 21:04:04 +0000662 path.addRect(SkRect::MakeWH(50, 100));
663 REPORTER_ASSERT(reporter, path.isFinite());
664
665 path.moveTo(0, 0);
666 path.lineTo(SK_ScalarInfinity, 42);
667 REPORTER_ASSERT(reporter, !path.isFinite());
668
669 path.addRect(SkRect::MakeWH(50, 100));
670 REPORTER_ASSERT(reporter, !path.isFinite());
671
672 path.reset();
673 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000674
reed@google.coma8790de2012-10-24 21:04:04 +0000675 path.addRect(SkRect::MakeWH(50, 100));
676 REPORTER_ASSERT(reporter, path.isFinite());
677}
678
reed@google.com848148e2013-01-15 15:51:59 +0000679static void build_big_path(SkPath* path, bool reducedCase) {
680 if (reducedCase) {
681 path->moveTo(577330, 1971.72f);
682 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
683 } else {
684 path->moveTo(60.1631f, 7.70567f);
685 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
686 path->lineTo(577379, 1977.77f);
687 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
688 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
689 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
690 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
691 path->quadTo(577198, 1972, 577238, 1971.31f);
692 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
693 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
694 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
695 path->moveTo(306.718f, -32.912f);
696 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
697 }
698}
699
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000700static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700701 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000702
703 // This path used to assert, because our cubic-chopping code incorrectly
704 // moved control points after the chop. This test should be run in SK_DEBUG
705 // mode to ensure that we no long assert.
706 SkPath path;
707 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
708 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000709
reed@google.com848148e2013-01-15 15:51:59 +0000710 SkPaint paint;
711 for (int doAA = 0; doAA <= 1; ++doAA) {
712 paint.setAntiAlias(SkToBool(doAA));
713 surface->getCanvas()->drawPath(path, paint);
714 }
715 }
716}
717
reed3b2fb982015-07-29 08:37:13 -0700718static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
719 if (expected != bounds) {
720 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
721 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
722 expected.left(), expected.top(), expected.right(), expected.bottom());
723 }
724}
725
reed91f283b2015-07-28 06:00:50 -0700726static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
727 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700728#if 0
729 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
730 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700731 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
732
733 path.moveTo(-5, -8);
734 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
735
736 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
737 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
738
739 path.moveTo(1, 2);
740 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700741#else
742 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
743
744 path.moveTo(-5, -8); // should set the bounds
745 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
746
747 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
748 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
749
750 path.moveTo(1, 2); // don't expect this to have changed the bounds
751 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
752#endif
reed91f283b2015-07-28 06:00:50 -0700753}
754
reedb1b12f82016-07-13 10:56:53 -0700755#include "SkSurface.h"
756static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
757 SkPath path;
758 path.moveTo(0, 0);
759 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
760
761 SkPaint paint;
762 paint.setAntiAlias(true);
763
764 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
765 surf->getCanvas()->drawPath(path, paint);
766}
767
reed@google.com8cae8352012-09-14 15:18:41 +0000768// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
769// which triggered an assert, from a tricky cubic. This test replicates that
770// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
771// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000772static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000773 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000774 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
775 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
776 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
777 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000778 };
779
780 SkPath path;
781 path.moveTo(pts[0]);
782 path.cubicTo(pts[1], pts[2], pts[3]);
783
784 SkPaint paint;
785 paint.setAntiAlias(true);
786
reede8f30622016-03-23 18:59:25 -0700787 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000788}
reed@android.com3abec1d2009-03-02 05:36:20 +0000789
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000790// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
791//
792static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
793 SkPath path;
794 path.quadTo(157, 366, 286, 208);
795 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000797 SkMatrix matrix;
798 matrix.setScale(1000*1000, 1000*1000);
799
800 // Be sure that path::transform correctly updates isFinite and the bounds
801 // if the transformation overflows. The previous bug was that isFinite was
802 // set to true in this case, but the bounds were not set to empty (which
803 // they should be).
804 while (path.isFinite()) {
805 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
806 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
807 path.transform(matrix);
808 }
809 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
810
811 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
812 path.transform(matrix);
813 // we need to still be non-finite
814 REPORTER_ASSERT(reporter, !path.isFinite());
815 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
816}
817
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000818static void add_corner_arc(SkPath* path, const SkRect& rect,
819 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000820 int startAngle)
821{
822
823 SkScalar rx = SkMinScalar(rect.width(), xIn);
824 SkScalar ry = SkMinScalar(rect.height(), yIn);
825
826 SkRect arcRect;
827 arcRect.set(-rx, -ry, rx, ry);
828 switch (startAngle) {
829 case 0:
830 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
831 break;
832 case 90:
833 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
834 break;
835 case 180:
836 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
837 break;
838 case 270:
839 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
840 break;
841 default:
842 break;
843 }
844
845 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
846}
847
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000848static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000849 SkScalar xCorner, SkScalar yCorner) {
850 // we are lazy here and use the same x & y for each corner
851 add_corner_arc(path, r, xCorner, yCorner, 270);
852 add_corner_arc(path, r, xCorner, yCorner, 0);
853 add_corner_arc(path, r, xCorner, yCorner, 90);
854 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000855 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000856}
857
858// Chrome creates its own round rects with each corner possibly being different.
859// Performance will suffer if they are not convex.
860// Note: PathBench::ArbRoundRectBench performs almost exactly
861// the same test (but with drawing)
862static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000863 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000864 SkRect r;
865
866 for (int i = 0; i < 5000; ++i) {
867
robertphillips@google.com158618e2012-10-23 16:56:56 +0000868 SkScalar size = rand.nextUScalar1() * 30;
869 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000870 continue;
871 }
872 r.fLeft = rand.nextUScalar1() * 300;
873 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000874 r.fRight = r.fLeft + 2 * size;
875 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000876
877 SkPath temp;
878
879 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
880
881 REPORTER_ASSERT(reporter, temp.isConvex());
882 }
883}
884
robertphillips@google.com158618e2012-10-23 16:56:56 +0000885// Chrome will sometimes create a 0 radius round rect. The degenerate
886// quads prevent the path from being converted to a rect
887// Note: PathBench::ArbRoundRectBench performs almost exactly
888// the same test (but with drawing)
889static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000890 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000891 SkRect r;
892
893 for (int i = 0; i < 5000; ++i) {
894
895 SkScalar size = rand.nextUScalar1() * 30;
896 if (size < SK_Scalar1) {
897 continue;
898 }
899 r.fLeft = rand.nextUScalar1() * 300;
900 r.fTop = rand.nextUScalar1() * 300;
901 r.fRight = r.fLeft + 2 * size;
902 r.fBottom = r.fTop + 2 * size;
903
904 SkPath temp;
905
906 make_arb_round_rect(&temp, r, 0, 0);
907
robertphillips@google.com158618e2012-10-23 16:56:56 +0000908 SkRect result;
909 REPORTER_ASSERT(reporter, temp.isRect(&result));
910 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000911 }
912}
913
reed@google.com0bb18bb2012-07-26 15:20:36 +0000914static void test_rect_isfinite(skiatest::Reporter* reporter) {
915 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000916 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000917 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918
reed@google.com0bb18bb2012-07-26 15:20:36 +0000919 SkRect r;
920 r.setEmpty();
921 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000922 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000923 REPORTER_ASSERT(reporter, !r.isFinite());
924 r.set(0, 0, nan, 0);
925 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000926
reed@google.com0bb18bb2012-07-26 15:20:36 +0000927 SkPoint pts[] = {
928 { 0, 0 },
929 { SK_Scalar1, 0 },
930 { 0, SK_Scalar1 },
931 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000932
reed@google.com0bb18bb2012-07-26 15:20:36 +0000933 bool isFine = r.setBoundsCheck(pts, 3);
934 REPORTER_ASSERT(reporter, isFine);
935 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000936
reed@google.com0bb18bb2012-07-26 15:20:36 +0000937 pts[1].set(inf, 0);
938 isFine = r.setBoundsCheck(pts, 3);
939 REPORTER_ASSERT(reporter, !isFine);
940 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000941
reed@google.com0bb18bb2012-07-26 15:20:36 +0000942 pts[1].set(nan, 0);
943 isFine = r.setBoundsCheck(pts, 3);
944 REPORTER_ASSERT(reporter, !isFine);
945 REPORTER_ASSERT(reporter, r.isEmpty());
946}
947
948static void test_path_isfinite(skiatest::Reporter* reporter) {
949 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000950 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000951 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000952
reed@google.com0bb18bb2012-07-26 15:20:36 +0000953 SkPath path;
954 REPORTER_ASSERT(reporter, path.isFinite());
955
956 path.reset();
957 REPORTER_ASSERT(reporter, path.isFinite());
958
959 path.reset();
960 path.moveTo(SK_Scalar1, 0);
961 REPORTER_ASSERT(reporter, path.isFinite());
962
963 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000964 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000965 REPORTER_ASSERT(reporter, !path.isFinite());
966
967 path.reset();
968 path.moveTo(nan, 0);
969 REPORTER_ASSERT(reporter, !path.isFinite());
970}
971
972static void test_isfinite(skiatest::Reporter* reporter) {
973 test_rect_isfinite(reporter);
974 test_path_isfinite(reporter);
975}
976
fsb1475b02016-01-20 09:51:07 -0800977static void test_islastcontourclosed(skiatest::Reporter* reporter) {
978 SkPath path;
979 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
980 path.moveTo(0, 0);
981 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
982 path.close();
983 REPORTER_ASSERT(reporter, path.isLastContourClosed());
984 path.lineTo(100, 100);
985 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
986 path.moveTo(200, 200);
987 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
988 path.close();
989 REPORTER_ASSERT(reporter, path.isLastContourClosed());
990 path.moveTo(0, 0);
991 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
992}
993
reed@google.com744faba2012-05-29 19:54:52 +0000994// assert that we always
995// start with a moveTo
996// only have 1 moveTo
997// only have Lines after that
998// end with a single close
999// only have (at most) 1 close
1000//
1001static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001002 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +00001003 SkPath::RawIter iter(path);
1004 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +00001005
1006 bool firstTime = true;
1007 bool foundClose = false;
1008 for (;;) {
1009 switch (iter.next(pts)) {
1010 case SkPath::kMove_Verb:
1011 REPORTER_ASSERT(reporter, firstTime);
1012 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
1013 srcPts++;
1014 firstTime = false;
1015 break;
1016 case SkPath::kLine_Verb:
1017 REPORTER_ASSERT(reporter, !firstTime);
1018 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
1019 srcPts++;
1020 break;
1021 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001022 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +00001023 break;
reed@google.com277c3f82013-05-31 15:17:50 +00001024 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001025 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +00001026 break;
reed@google.com744faba2012-05-29 19:54:52 +00001027 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001028 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +00001029 break;
1030 case SkPath::kClose_Verb:
1031 REPORTER_ASSERT(reporter, !firstTime);
1032 REPORTER_ASSERT(reporter, !foundClose);
1033 REPORTER_ASSERT(reporter, expectClose);
1034 foundClose = true;
1035 break;
1036 case SkPath::kDone_Verb:
1037 goto DONE;
1038 }
1039 }
1040DONE:
1041 REPORTER_ASSERT(reporter, foundClose == expectClose);
1042}
1043
1044static void test_addPoly(skiatest::Reporter* reporter) {
1045 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001046 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047
reed@google.com744faba2012-05-29 19:54:52 +00001048 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1049 pts[i].fX = rand.nextSScalar1();
1050 pts[i].fY = rand.nextSScalar1();
1051 }
1052
1053 for (int doClose = 0; doClose <= 1; ++doClose) {
1054 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1055 SkPath path;
bsalomon98806072014-12-12 15:11:17 -08001056 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001057 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +00001058 }
1059 }
1060}
1061
reed@google.com8b06f1a2012-05-29 12:03:46 +00001062static void test_strokerec(skiatest::Reporter* reporter) {
1063 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1064 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001065
reed@google.com8b06f1a2012-05-29 12:03:46 +00001066 rec.setHairlineStyle();
1067 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001068
reed@google.com8b06f1a2012-05-29 12:03:46 +00001069 rec.setStrokeStyle(SK_Scalar1, false);
1070 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001071
reed@google.com8b06f1a2012-05-29 12:03:46 +00001072 rec.setStrokeStyle(SK_Scalar1, true);
1073 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001074
reed@google.com8b06f1a2012-05-29 12:03:46 +00001075 rec.setStrokeStyle(0, false);
1076 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001077
reed@google.com8b06f1a2012-05-29 12:03:46 +00001078 rec.setStrokeStyle(0, true);
1079 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1080}
1081
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001082// Set this for paths that don't have a consistent direction such as a bowtie.
1083// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001084const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001085
1086static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001087 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001088 if (expected == kDontCheckDir) {
1089 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001090 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001091 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1092
reed026beb52015-06-10 14:23:15 -07001093 SkPathPriv::FirstDirection dir;
1094 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001095 REPORTER_ASSERT(reporter, dir == expected);
1096 } else {
reed026beb52015-06-10 14:23:15 -07001097 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001098 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001099}
1100
reed@google.com3e71a882012-01-10 18:44:37 +00001101static void test_direction(skiatest::Reporter* reporter) {
1102 size_t i;
1103 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001104 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001105 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1106 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1107 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001108
1109 static const char* gDegen[] = {
1110 "M 10 10",
1111 "M 10 10 M 20 20",
1112 "M 10 10 L 20 20",
1113 "M 10 10 L 10 10 L 10 10",
1114 "M 10 10 Q 10 10 10 10",
1115 "M 10 10 C 10 10 10 10 10 10",
1116 };
1117 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1118 path.reset();
1119 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1120 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001121 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001122 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001123
reed@google.com3e71a882012-01-10 18:44:37 +00001124 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001125 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001126 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001127 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +00001128 // rect with top two corners replaced by cubics with identical middle
1129 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001130 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1131 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001132 };
1133 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1134 path.reset();
1135 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1136 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001137 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001138 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001139
reed@google.com3e71a882012-01-10 18:44:37 +00001140 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001141 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001142 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001143 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +00001144 // rect with top two corners replaced by cubics with identical middle
1145 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001146 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1147 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001148 };
1149 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1150 path.reset();
1151 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1152 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001153 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001154 }
reed@google.comac8543f2012-01-30 20:51:25 +00001155
1156 // Test two donuts, each wound a different direction. Only the outer contour
1157 // determines the cheap direction
1158 path.reset();
1159 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1160 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001161 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001162
reed@google.comac8543f2012-01-30 20:51:25 +00001163 path.reset();
1164 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1165 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001166 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001167
1168 // triangle with one point really far from the origin.
1169 path.reset();
1170 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001171 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001172 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1173 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001174 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001175
1176 path.reset();
1177 path.conicTo(20, 0, 20, 20, 0.5f);
1178 path.close();
reed026beb52015-06-10 14:23:15 -07001179 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001180
1181 path.reset();
1182 path.lineTo(1, 1e7f);
1183 path.lineTo(1e7f, 2e7f);
1184 path.close();
1185 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001186 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001187}
1188
reed@google.comffdb0182011-11-14 19:29:14 +00001189static void add_rect(SkPath* path, const SkRect& r) {
1190 path->moveTo(r.fLeft, r.fTop);
1191 path->lineTo(r.fRight, r.fTop);
1192 path->lineTo(r.fRight, r.fBottom);
1193 path->lineTo(r.fLeft, r.fBottom);
1194 path->close();
1195}
1196
1197static void test_bounds(skiatest::Reporter* reporter) {
1198 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001199 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1200 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1201 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1202 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001203 };
1204
1205 SkPath path0, path1;
1206 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1207 path0.addRect(rects[i]);
1208 add_rect(&path1, rects[i]);
1209 }
1210
1211 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1212}
1213
reed@google.com55b5f4b2011-09-07 12:23:41 +00001214static void stroke_cubic(const SkPoint pts[4]) {
1215 SkPath path;
1216 path.moveTo(pts[0]);
1217 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001218
reed@google.com55b5f4b2011-09-07 12:23:41 +00001219 SkPaint paint;
1220 paint.setStyle(SkPaint::kStroke_Style);
1221 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001222
reed@google.com55b5f4b2011-09-07 12:23:41 +00001223 SkPath fill;
1224 paint.getFillPath(path, &fill);
1225}
1226
1227// just ensure this can run w/o any SkASSERTS firing in the debug build
1228// we used to assert due to differences in how we determine a degenerate vector
1229// but that was fixed with the introduction of SkPoint::CanNormalize
1230static void stroke_tiny_cubic() {
1231 SkPoint p0[] = {
1232 { 372.0f, 92.0f },
1233 { 372.0f, 92.0f },
1234 { 372.0f, 92.0f },
1235 { 372.0f, 92.0f },
1236 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001237
reed@google.com55b5f4b2011-09-07 12:23:41 +00001238 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001239
reed@google.com55b5f4b2011-09-07 12:23:41 +00001240 SkPoint p1[] = {
1241 { 372.0f, 92.0f },
1242 { 372.0007f, 92.000755f },
1243 { 371.99927f, 92.003922f },
1244 { 371.99826f, 92.003899f },
1245 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001246
reed@google.com55b5f4b2011-09-07 12:23:41 +00001247 stroke_cubic(p1);
1248}
1249
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001250static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1251 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001252 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001253 SkPoint mv;
1254 SkPoint pts[4];
1255 SkPath::Verb v;
1256 int nMT = 0;
1257 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001258 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001259 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1260 switch (v) {
1261 case SkPath::kMove_Verb:
1262 mv = pts[0];
1263 ++nMT;
1264 break;
1265 case SkPath::kClose_Verb:
1266 REPORTER_ASSERT(reporter, mv == pts[0]);
1267 ++nCL;
1268 break;
1269 default:
1270 break;
1271 }
1272 }
1273 // if we force a close on the interator we should have a close
1274 // for every moveTo
1275 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1276 }
1277}
1278
1279static void test_close(skiatest::Reporter* reporter) {
1280 SkPath closePt;
1281 closePt.moveTo(0, 0);
1282 closePt.close();
1283 check_close(reporter, closePt);
1284
1285 SkPath openPt;
1286 openPt.moveTo(0, 0);
1287 check_close(reporter, openPt);
1288
1289 SkPath empty;
1290 check_close(reporter, empty);
1291 empty.close();
1292 check_close(reporter, empty);
1293
1294 SkPath rect;
1295 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1296 check_close(reporter, rect);
1297 rect.close();
1298 check_close(reporter, rect);
1299
1300 SkPath quad;
1301 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1302 check_close(reporter, quad);
1303 quad.close();
1304 check_close(reporter, quad);
1305
1306 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001307 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001308 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1309 check_close(reporter, cubic);
1310 cubic.close();
1311 check_close(reporter, cubic);
1312
1313 SkPath line;
1314 line.moveTo(SK_Scalar1, SK_Scalar1);
1315 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1316 check_close(reporter, line);
1317 line.close();
1318 check_close(reporter, line);
1319
1320 SkPath rect2;
1321 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1322 rect2.close();
1323 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1324 check_close(reporter, rect2);
1325 rect2.close();
1326 check_close(reporter, rect2);
1327
1328 SkPath oval3;
1329 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1330 oval3.close();
1331 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1332 check_close(reporter, oval3);
1333 oval3.close();
1334 check_close(reporter, oval3);
1335
1336 SkPath moves;
1337 moves.moveTo(SK_Scalar1, SK_Scalar1);
1338 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1339 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1340 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1341 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001342
1343 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001344}
1345
reed@google.com7c424812011-05-15 04:38:34 +00001346static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1347 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001348 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1349 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001350 REPORTER_ASSERT(reporter, c == expected);
1351}
1352
caryclarkb4216502015-03-02 13:02:34 -08001353static void test_path_crbug389050(skiatest::Reporter* reporter) {
1354 SkPath tinyConvexPolygon;
1355 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1356 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1357 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1358 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1359 tinyConvexPolygon.close();
1360 tinyConvexPolygon.getConvexity();
1361 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001362 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001363
1364 SkPath platTriangle;
1365 platTriangle.moveTo(0, 0);
1366 platTriangle.lineTo(200, 0);
1367 platTriangle.lineTo(100, 0.04f);
1368 platTriangle.close();
1369 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001370 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001371
1372 platTriangle.reset();
1373 platTriangle.moveTo(0, 0);
1374 platTriangle.lineTo(200, 0);
1375 platTriangle.lineTo(100, 0.03f);
1376 platTriangle.close();
1377 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001378 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001379}
1380
reed@google.com7c424812011-05-15 04:38:34 +00001381static void test_convexity2(skiatest::Reporter* reporter) {
1382 SkPath pt;
1383 pt.moveTo(0, 0);
1384 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001385 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001386 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001387
reed@google.com7c424812011-05-15 04:38:34 +00001388 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001389 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1390 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001391 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001392 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001393 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001394
reed@google.com7c424812011-05-15 04:38:34 +00001395 SkPath triLeft;
1396 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001397 triLeft.lineTo(SK_Scalar1, 0);
1398 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001399 triLeft.close();
1400 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001401 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001402
reed@google.com7c424812011-05-15 04:38:34 +00001403 SkPath triRight;
1404 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001405 triRight.lineTo(-SK_Scalar1, 0);
1406 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001407 triRight.close();
1408 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001409 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001410
reed@google.com7c424812011-05-15 04:38:34 +00001411 SkPath square;
1412 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001413 square.lineTo(SK_Scalar1, 0);
1414 square.lineTo(SK_Scalar1, SK_Scalar1);
1415 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001416 square.close();
1417 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001418 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001419
reed@google.com7c424812011-05-15 04:38:34 +00001420 SkPath redundantSquare;
1421 redundantSquare.moveTo(0, 0);
1422 redundantSquare.lineTo(0, 0);
1423 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001424 redundantSquare.lineTo(SK_Scalar1, 0);
1425 redundantSquare.lineTo(SK_Scalar1, 0);
1426 redundantSquare.lineTo(SK_Scalar1, 0);
1427 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1428 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1429 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1430 redundantSquare.lineTo(0, SK_Scalar1);
1431 redundantSquare.lineTo(0, SK_Scalar1);
1432 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001433 redundantSquare.close();
1434 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001435 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001436
reed@google.com7c424812011-05-15 04:38:34 +00001437 SkPath bowTie;
1438 bowTie.moveTo(0, 0);
1439 bowTie.lineTo(0, 0);
1440 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001441 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1442 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1443 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1444 bowTie.lineTo(SK_Scalar1, 0);
1445 bowTie.lineTo(SK_Scalar1, 0);
1446 bowTie.lineTo(SK_Scalar1, 0);
1447 bowTie.lineTo(0, SK_Scalar1);
1448 bowTie.lineTo(0, SK_Scalar1);
1449 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001450 bowTie.close();
1451 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001452 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001453
reed@google.com7c424812011-05-15 04:38:34 +00001454 SkPath spiral;
1455 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001456 spiral.lineTo(100*SK_Scalar1, 0);
1457 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1458 spiral.lineTo(0, 100*SK_Scalar1);
1459 spiral.lineTo(0, 50*SK_Scalar1);
1460 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1461 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001462 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001463 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001464 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001465
reed@google.com7c424812011-05-15 04:38:34 +00001466 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001467 dent.moveTo(0, 0);
1468 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1469 dent.lineTo(0, 100*SK_Scalar1);
1470 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1471 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001472 dent.close();
1473 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001474 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001475
halcanary6950de62015-11-07 05:29:00 -08001476 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001477 SkPath strokedSin;
1478 for (int i = 0; i < 2000; i++) {
1479 SkScalar x = SkIntToScalar(i) / 2;
1480 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1481 if (0 == i) {
1482 strokedSin.moveTo(x, y);
1483 } else {
1484 strokedSin.lineTo(x, y);
1485 }
1486 }
1487 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1488 stroke.setStrokeStyle(2 * SK_Scalar1);
1489 stroke.applyToPath(&strokedSin, strokedSin);
1490 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1491 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001492
1493 // http://crbug.com/412640
1494 SkPath degenerateConcave;
1495 degenerateConcave.moveTo(148.67912f, 191.875f);
1496 degenerateConcave.lineTo(470.37695f, 7.5f);
1497 degenerateConcave.lineTo(148.67912f, 191.875f);
1498 degenerateConcave.lineTo(41.446522f, 376.25f);
1499 degenerateConcave.lineTo(-55.971577f, 460.0f);
1500 degenerateConcave.lineTo(41.446522f, 376.25f);
1501 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001502 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001503
1504 // http://crbug.com/433683
1505 SkPath badFirstVector;
1506 badFirstVector.moveTo(501.087708f, 319.610352f);
1507 badFirstVector.lineTo(501.087708f, 319.610352f);
1508 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1509 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1510 badFirstVector.lineTo(301.557678f, 98.044601f);
1511 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1512 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1513 badFirstVector.lineTo(504.912292f, 316.389648f);
1514 badFirstVector.lineTo(501.087708f, 319.610352f);
1515 badFirstVector.close();
1516 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001517}
1518
reed@android.com6b82d1a2009-06-03 02:35:01 +00001519static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1520 const SkRect& bounds) {
1521 REPORTER_ASSERT(reporter, p.isConvex());
1522 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001523
reed@android.com6b82d1a2009-06-03 02:35:01 +00001524 SkPath p2(p);
1525 REPORTER_ASSERT(reporter, p2.isConvex());
1526 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1527
1528 SkPath other;
1529 other.swap(p2);
1530 REPORTER_ASSERT(reporter, other.isConvex());
1531 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1532}
1533
reed@google.com04863fa2011-05-15 04:08:24 +00001534static void setFromString(SkPath* path, const char str[]) {
1535 bool first = true;
1536 while (str) {
1537 SkScalar x, y;
1538 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001539 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001540 break;
1541 }
1542 str = SkParse::FindScalar(str, &y);
1543 SkASSERT(str);
1544 if (first) {
1545 path->moveTo(x, y);
1546 first = false;
1547 } else {
1548 path->lineTo(x, y);
1549 }
1550 }
1551}
1552
1553static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001554 SkPath path;
1555
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001556 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001557 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001558 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001559 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001560 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1561
reed@google.com04863fa2011-05-15 04:08:24 +00001562 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001563 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001564 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001565 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001566
reed@google.com04863fa2011-05-15 04:08:24 +00001567 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001568 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001569 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001570 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001571
liyuqianbdabcc42016-11-03 11:40:19 -07001572 path.reset();
1573 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1574 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1575
reed@google.com04863fa2011-05-15 04:08:24 +00001576 static const struct {
reed026beb52015-06-10 14:23:15 -07001577 const char* fPathStr;
1578 SkPath::Convexity fExpectedConvexity;
1579 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001580 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001581 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1582 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1583 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1584 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1585 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1586 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001587 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001588 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001589 };
1590
1591 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1592 SkPath path;
1593 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001594 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1595 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001596 // check after setting the initial convex and direction
1597 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1598 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001599 SkPathPriv::FirstDirection dir;
1600 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1601 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001602 ^ foundDir);
1603 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1604 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1605 }
1606 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1607 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001608 }
caryclarkd3d1a982014-12-08 04:57:38 -08001609
1610 static const SkPoint nonFinitePts[] = {
1611 { SK_ScalarInfinity, 0 },
1612 { 0, SK_ScalarInfinity },
1613 { SK_ScalarInfinity, SK_ScalarInfinity },
1614 { SK_ScalarNegativeInfinity, 0},
1615 { 0, SK_ScalarNegativeInfinity },
1616 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1617 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1618 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1619 { SK_ScalarNaN, 0 },
1620 { 0, SK_ScalarNaN },
1621 { SK_ScalarNaN, SK_ScalarNaN },
1622 };
1623
1624 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1625
1626 static const SkPoint finitePts[] = {
1627 { SK_ScalarMax, 0 },
1628 { 0, SK_ScalarMax },
1629 { SK_ScalarMax, SK_ScalarMax },
1630 { SK_ScalarMin, 0 },
1631 { 0, SK_ScalarMin },
1632 { SK_ScalarMin, SK_ScalarMin },
1633 };
1634
1635 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1636
1637 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1638 int i = (int) (index % nonFinitePtsCount);
1639 int f = (int) (index % finitePtsCount);
1640 int g = (int) ((f + 1) % finitePtsCount);
1641 path.reset();
1642 switch (index % 13) {
1643 case 0: path.lineTo(nonFinitePts[i]); break;
1644 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1645 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1646 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1647 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1648 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1649 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1650 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1651 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1652 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1653 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1654 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1655 case 12: path.moveTo(nonFinitePts[i]); break;
1656 }
1657 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1658 }
1659
1660 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1661 int f = (int) (index % finitePtsCount);
1662 int g = (int) ((f + 1) % finitePtsCount);
1663 path.reset();
1664 int curveSelect = index % 11;
1665 switch (curveSelect) {
1666 case 0: path.moveTo(finitePts[f]); break;
1667 case 1: path.lineTo(finitePts[f]); break;
1668 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1669 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1670 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1671 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1672 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1673 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1674 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1675 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1676 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1677 }
1678 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1679 : SkPath::kUnknown_Convexity);
1680 }
1681
reed@google.com04863fa2011-05-15 04:08:24 +00001682}
1683
reed@google.com7e6c4d12012-05-10 14:05:43 +00001684static void test_isLine(skiatest::Reporter* reporter) {
1685 SkPath path;
1686 SkPoint pts[2];
1687 const SkScalar value = SkIntToScalar(5);
1688
halcanary96fcdcc2015-08-27 07:41:13 -07001689 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001690
reed@google.com7e6c4d12012-05-10 14:05:43 +00001691 // set some non-zero values
1692 pts[0].set(value, value);
1693 pts[1].set(value, value);
1694 REPORTER_ASSERT(reporter, !path.isLine(pts));
1695 // check that pts was untouched
1696 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1697 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1698
1699 const SkScalar moveX = SkIntToScalar(1);
1700 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001701 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001702
1703 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001704 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001705 REPORTER_ASSERT(reporter, !path.isLine(pts));
1706 // check that pts was untouched
1707 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1708 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1709
1710 const SkScalar lineX = SkIntToScalar(2);
1711 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001712 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001713
1714 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001715 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001716
1717 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1718 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1719 REPORTER_ASSERT(reporter, path.isLine(pts));
1720 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1721 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1722
1723 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001724 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001725 REPORTER_ASSERT(reporter, !path.isLine(pts));
1726 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1727 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001728
1729 path.reset();
1730 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001731 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001732}
1733
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001734static void test_conservativelyContains(skiatest::Reporter* reporter) {
1735 SkPath path;
1736
1737 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1738 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1739
1740 // A circle that bounds kBaseRect (with a significant amount of slop)
1741 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001742 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001743 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1744
1745 // round-rect radii
1746 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001747
caryclark@google.com56f233a2012-11-19 13:06:06 +00001748 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001749 SkRect fQueryRect;
1750 bool fInRect;
1751 bool fInCircle;
1752 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001753 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001754 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001755 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001756
1757 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001758 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1759 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1760 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1761 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001762 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001763
1764 // rects with edges off by one from kBaseRect's edges
1765 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1766 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001767 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001768 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1769 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001770 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001771 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1772 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001773 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001774 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1775 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001776 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001777 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1778 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001779 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001780 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1781 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001782 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001783 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1784 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001785 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001786
1787 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001788 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1789 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1790 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1791 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001792
1793 // far away rect
1794 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1795 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001796 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001797
1798 // very large rect containing kBaseRect
1799 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1800 kBaseRect.fTop - 5 * kBaseRect.height(),
1801 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001802 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001803
1804 // skinny rect that spans same y-range as kBaseRect
1805 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1806 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001807 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001808
1809 // short rect that spans same x-range as kBaseRect
1810 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001811 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001812
1813 // skinny rect that spans slightly larger y-range than kBaseRect
1814 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1815 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001816 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001817
1818 // short rect that spans slightly larger x-range than kBaseRect
1819 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1820 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001821 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001822 };
1823
1824 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001825 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001826 SkRect qRect = kQueries[q].fQueryRect;
1827 if (inv & 0x1) {
1828 SkTSwap(qRect.fLeft, qRect.fRight);
1829 }
1830 if (inv & 0x2) {
1831 SkTSwap(qRect.fTop, qRect.fBottom);
1832 }
1833 for (int d = 0; d < 2; ++d) {
1834 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1835 path.reset();
1836 path.addRect(kBaseRect, dir);
1837 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1838 path.conservativelyContainsRect(qRect));
1839
1840 path.reset();
1841 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1842 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1843 path.conservativelyContainsRect(qRect));
1844
1845 path.reset();
1846 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1847 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1848 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001849
1850 path.reset();
1851 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1852 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1853 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1854 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1855 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1856 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1857 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1858 path.close();
1859 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1860 path.conservativelyContainsRect(qRect));
1861
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001862 }
1863 // Slightly non-convex shape, shouldn't contain any rects.
1864 path.reset();
1865 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001866 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001867 path.lineTo(SkIntToScalar(100), 0);
1868 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1869 path.lineTo(0, SkIntToScalar(100));
1870 path.close();
1871 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1872 }
1873 }
1874
1875 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1876 path.reset();
1877 path.moveTo(0, 0);
1878 path.lineTo(SkIntToScalar(100), 0);
1879 path.lineTo(0, SkIntToScalar(100));
1880
1881 // inside, on along top edge
1882 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1883 SkIntToScalar(10),
1884 SkIntToScalar(10))));
1885 // above
1886 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1887 SkRect::MakeXYWH(SkIntToScalar(50),
1888 SkIntToScalar(-10),
1889 SkIntToScalar(10),
1890 SkIntToScalar(10))));
1891 // to the left
1892 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1893 SkIntToScalar(5),
1894 SkIntToScalar(5),
1895 SkIntToScalar(5))));
1896
1897 // outside the diagonal edge
1898 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1899 SkIntToScalar(200),
1900 SkIntToScalar(20),
1901 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001902
bsalomonb17c1292014-08-28 14:04:55 -07001903
1904 // Test that multiple move commands do not cause asserts.
1905
1906 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001907 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001908 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1909#if 0
1910 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1911 SkIntToScalar(10),
1912 SkIntToScalar(10))));
1913#endif
1914
1915 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001916 path.reset();
1917 path.moveTo(100, 100);
1918 path.moveTo(0, 0);
1919 path.lineTo(SkIntToScalar(100), 0);
1920 path.lineTo(0, SkIntToScalar(100));
1921
1922 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1923 SkIntToScalar(10),
1924 SkIntToScalar(10))));
1925
bsalomonb17c1292014-08-28 14:04:55 -07001926 // Test that multiple move commands do not cause asserts and that the function
1927 // is not confused by the multiple moves.
1928 path.reset();
1929 path.moveTo(0, 0);
1930 path.lineTo(SkIntToScalar(100), 0);
1931 path.lineTo(0, SkIntToScalar(100));
1932 path.moveTo(0, SkIntToScalar(200));
1933 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1934 path.lineTo(0, SkIntToScalar(300));
1935
1936 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1937 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1938 SkIntToScalar(10),
1939 SkIntToScalar(10))));
1940
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001941 path.reset();
1942 path.lineTo(100, 100);
1943 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001944}
1945
reed@google.comf32322b2013-10-16 15:14:04 +00001946static void test_isRect_open_close(skiatest::Reporter* reporter) {
1947 SkPath path;
1948 bool isClosed;
1949
1950 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001951 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001952
halcanary96fcdcc2015-08-27 07:41:13 -07001953 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001954 REPORTER_ASSERT(reporter, isClosed);
1955}
1956
caryclark@google.comf1316942011-07-26 19:54:45 +00001957// Simple isRect test is inline TestPath, below.
1958// test_isRect provides more extensive testing.
1959static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001960 test_isRect_open_close(reporter);
1961
caryclark@google.comf1316942011-07-26 19:54:45 +00001962 // passing tests (all moveTo / lineTo...
1963 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1964 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1965 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1966 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1967 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1968 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1969 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1970 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1971 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001972 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1973 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001974 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1975 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1976 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001977 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001978
caryclark@google.comf1316942011-07-26 19:54:45 +00001979 // failing tests
1980 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1981 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1982 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1983 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1984 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1985 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1986 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1987 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001988 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1989 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1990 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001991
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001992 // no close, but we should detect them as fillably the same as a rect
1993 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1994 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1995 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1996
1997 // like c2, but we double-back on ourselves
1998 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1999 // like c2, but we overshoot the start point
2000 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2001 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00002002
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002003 struct IsRectTest {
2004 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002005 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002006 bool fClose;
2007 bool fIsRect;
2008 } tests[] = {
2009 { r1, SK_ARRAY_COUNT(r1), true, true },
2010 { r2, SK_ARRAY_COUNT(r2), true, true },
2011 { r3, SK_ARRAY_COUNT(r3), true, true },
2012 { r4, SK_ARRAY_COUNT(r4), true, true },
2013 { r5, SK_ARRAY_COUNT(r5), true, true },
2014 { r6, SK_ARRAY_COUNT(r6), true, true },
2015 { r7, SK_ARRAY_COUNT(r7), true, true },
2016 { r8, SK_ARRAY_COUNT(r8), true, true },
2017 { r9, SK_ARRAY_COUNT(r9), true, true },
2018 { ra, SK_ARRAY_COUNT(ra), true, true },
2019 { rb, SK_ARRAY_COUNT(rb), true, true },
2020 { rc, SK_ARRAY_COUNT(rc), true, true },
2021 { rd, SK_ARRAY_COUNT(rd), true, true },
2022 { re, SK_ARRAY_COUNT(re), true, true },
2023 { rf, SK_ARRAY_COUNT(rf), true, true },
2024
2025 { f1, SK_ARRAY_COUNT(f1), true, false },
2026 { f2, SK_ARRAY_COUNT(f2), true, false },
2027 { f3, SK_ARRAY_COUNT(f3), true, false },
2028 { f4, SK_ARRAY_COUNT(f4), true, false },
2029 { f5, SK_ARRAY_COUNT(f5), true, false },
2030 { f6, SK_ARRAY_COUNT(f6), true, false },
2031 { f7, SK_ARRAY_COUNT(f7), true, false },
2032 { f8, SK_ARRAY_COUNT(f8), true, false },
2033 { f9, SK_ARRAY_COUNT(f9), true, false },
2034 { fa, SK_ARRAY_COUNT(fa), true, false },
2035 { fb, SK_ARRAY_COUNT(fb), true, false },
2036
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002037 { c1, SK_ARRAY_COUNT(c1), false, true },
2038 { c2, SK_ARRAY_COUNT(c2), false, true },
2039 { c3, SK_ARRAY_COUNT(c3), false, true },
2040
2041 { d1, SK_ARRAY_COUNT(d1), false, false },
2042 { d2, SK_ARRAY_COUNT(d2), false, false },
2043 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00002044 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002045
2046 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002047 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002048 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2049 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002050 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2051 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2052 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002053 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002054 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002055 path.close();
2056 }
halcanary96fcdcc2015-08-27 07:41:13 -07002057 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002058
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002059 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002060 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002061 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002062 SkPath::Direction direction;
2063 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002064 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002065 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002066 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2067 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002068 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002069 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002070 } else {
2071 SkRect computed;
2072 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002073 bool isClosed = (bool)-1;
2074 SkPath::Direction direction = (SkPath::Direction) - 1;
2075 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002076 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2077 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002078 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2079 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002080 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002081 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002082
caryclark@google.comf1316942011-07-26 19:54:45 +00002083 // fail, close then line
2084 SkPath path1;
2085 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002086 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002087 path1.lineTo(r1[index].fX, r1[index].fY);
2088 }
2089 path1.close();
2090 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002091 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002092
caryclark@google.comf1316942011-07-26 19:54:45 +00002093 // fail, move in the middle
2094 path1.reset();
2095 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002096 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002097 if (index == 2) {
2098 path1.moveTo(1, .5f);
2099 }
2100 path1.lineTo(r1[index].fX, r1[index].fY);
2101 }
2102 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002103 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002104
2105 // fail, move on the edge
2106 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002107 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002108 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2109 path1.lineTo(r1[index].fX, r1[index].fY);
2110 }
2111 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002112 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002113
caryclark@google.comf1316942011-07-26 19:54:45 +00002114 // fail, quad
2115 path1.reset();
2116 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002117 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002118 if (index == 2) {
2119 path1.quadTo(1, .5f, 1, .5f);
2120 }
2121 path1.lineTo(r1[index].fX, r1[index].fY);
2122 }
2123 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002124 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002125
caryclark@google.comf1316942011-07-26 19:54:45 +00002126 // fail, cubic
2127 path1.reset();
2128 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002129 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002130 if (index == 2) {
2131 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2132 }
2133 path1.lineTo(r1[index].fX, r1[index].fY);
2134 }
2135 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002136 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002137}
2138
bsalomonedc743a2016-06-01 09:42:31 -07002139static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2140 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2141 SkRect r = SkRect::MakeEmpty();
2142 SkPath::Direction d = SkPath::kCCW_Direction;
2143 unsigned s = ~0U;
2144
2145 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2146 REPORTER_ASSERT(reporter, r == rect);
2147 REPORTER_ASSERT(reporter, d == dir);
2148 REPORTER_ASSERT(reporter, s == start);
2149}
2150
2151static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2152 SkRect r = SkRect::MakeEmpty();
2153 SkPath::Direction d = SkPath::kCCW_Direction;
2154 unsigned s = ~0U;
2155
2156 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2157 const SkRect emptyRect = SkRect::MakeEmpty();
2158 SkPath path;
2159 for (int start = 0; start < 4; ++start) {
2160 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2161 SkPath path;
2162 path.addRect(testRect, dir, start);
2163 check_simple_closed_rect(reporter, path, testRect, dir, start);
2164 path.close();
2165 check_simple_closed_rect(reporter, path, testRect, dir, start);
2166 SkPath path2 = path;
2167 path2.lineTo(10, 10);
2168 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2169 path2 = path;
2170 path2.moveTo(10, 10);
2171 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2172 path2 = path;
2173 path2.addRect(testRect, dir, start);
2174 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2175 // Make the path by hand, manually closing it.
2176 path2.reset();
2177 SkPath::RawIter iter(path);
2178 SkPath::Verb v;
2179 SkPoint verbPts[4];
2180 SkPoint firstPt = {0.f, 0.f};
2181 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2182 switch(v) {
2183 case SkPath::kMove_Verb:
2184 firstPt = verbPts[0];
2185 path2.moveTo(verbPts[0]);
2186 break;
2187 case SkPath::kLine_Verb:
2188 path2.lineTo(verbPts[1]);
2189 break;
2190 default:
2191 break;
2192 }
2193 }
2194 // We haven't closed it yet...
2195 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2196 // ... now we do and test again.
2197 path2.lineTo(firstPt);
2198 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2199 // A redundant close shouldn't cause a failure.
2200 path2.close();
2201 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2202 // Degenerate point and line rects are not allowed
2203 path2.reset();
2204 path2.addRect(emptyRect, dir, start);
2205 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2206 SkRect degenRect = testRect;
2207 degenRect.fLeft = degenRect.fRight;
2208 path2.reset();
2209 path2.addRect(degenRect, dir, start);
2210 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2211 degenRect = testRect;
2212 degenRect.fTop = degenRect.fBottom;
2213 path2.reset();
2214 path2.addRect(degenRect, dir, start);
2215 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2216 // An inverted rect makes a rect path, but changes the winding dir and start point.
2217 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2218 ? SkPath::kCCW_Direction
2219 : SkPath::kCW_Direction;
2220 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2221 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2222 SkRect swapRect = testRect;
2223 SkTSwap(swapRect.fLeft, swapRect.fRight);
2224 path2.reset();
2225 path2.addRect(swapRect, dir, start);
2226 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2227 swapRect = testRect;
2228 SkTSwap(swapRect.fTop, swapRect.fBottom);
2229 path2.reset();
2230 path2.addRect(swapRect, dir, start);
2231 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2232 }
2233 }
bsalomon057ae8a2016-07-24 05:37:26 -07002234 // down, up, left, close
2235 path.reset();
2236 path.moveTo(1, 1);
2237 path.lineTo(1, 2);
2238 path.lineTo(1, 1);
2239 path.lineTo(0, 1);
2240 SkRect rect;
2241 SkPath::Direction dir;
2242 unsigned start;
2243 path.close();
2244 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2245 // right, left, up, close
2246 path.reset();
2247 path.moveTo(1, 1);
2248 path.lineTo(2, 1);
2249 path.lineTo(1, 1);
2250 path.lineTo(1, 0);
2251 path.close();
2252 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2253 // parallelogram with horizontal edges
2254 path.reset();
2255 path.moveTo(1, 0);
2256 path.lineTo(3, 0);
2257 path.lineTo(2, 1);
2258 path.lineTo(0, 1);
2259 path.close();
2260 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2261 // parallelogram with vertical edges
2262 path.reset();
2263 path.moveTo(0, 1);
2264 path.lineTo(0, 3);
2265 path.lineTo(1, 2);
2266 path.lineTo(1, 0);
2267 path.close();
2268 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2269
bsalomonedc743a2016-06-01 09:42:31 -07002270}
2271
caryclark95bc5f32015-04-08 08:34:15 -07002272static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002273 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002274 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002275 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2276 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2277 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002278 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002279 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2280 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2281 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2282 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002283 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2284 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002285 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2286 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2287 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002288
2289 // failing tests
2290 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2291 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2292 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2293 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2294 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2295 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2296 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2297 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2298
caryclark95bc5f32015-04-08 08:34:15 -07002299 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002300 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2301 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2302
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002303 struct IsNestedRectTest {
2304 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002305 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002306 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002307 bool fClose;
2308 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2309 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002310 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2311 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2312 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2313 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2314 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2315 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2316 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2317 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2318 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2319 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2320 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2321 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2322 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2323 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002324
reed026beb52015-06-10 14:23:15 -07002325 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2326 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2327 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2328 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2329 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2330 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2331 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2332 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002333
reed026beb52015-06-10 14:23:15 -07002334 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2335 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002336 };
2337
2338 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002339 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002340 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002341 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2342 SkPath path;
2343 if (rectFirst) {
2344 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2345 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002346 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2347 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2348 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002349 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002350 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002351 path.close();
2352 }
2353 if (!rectFirst) {
2354 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2355 }
caryclark95bc5f32015-04-08 08:34:15 -07002356 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002357 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002358 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002359 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002360 SkPathPriv::FirstDirection expectedDirs[2];
2361 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002362 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002363 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002364 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2365 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002366 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002367 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002368 } else {
reed026beb52015-06-10 14:23:15 -07002369 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002370 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002371 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002372 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002373 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2374 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002375 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2376 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002377 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002378 }
2379
2380 // fail, close then line
2381 SkPath path1;
2382 if (rectFirst) {
2383 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2384 }
2385 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002386 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002387 path1.lineTo(r1[index].fX, r1[index].fY);
2388 }
2389 path1.close();
2390 path1.lineTo(1, 0);
2391 if (!rectFirst) {
2392 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2393 }
halcanary96fcdcc2015-08-27 07:41:13 -07002394 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002395
2396 // fail, move in the middle
2397 path1.reset();
2398 if (rectFirst) {
2399 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2400 }
2401 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002402 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002403 if (index == 2) {
2404 path1.moveTo(1, .5f);
2405 }
2406 path1.lineTo(r1[index].fX, r1[index].fY);
2407 }
2408 path1.close();
2409 if (!rectFirst) {
2410 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2411 }
halcanary96fcdcc2015-08-27 07:41:13 -07002412 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002413
2414 // fail, move on the edge
2415 path1.reset();
2416 if (rectFirst) {
2417 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2418 }
bsalomon98806072014-12-12 15:11:17 -08002419 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002420 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2421 path1.lineTo(r1[index].fX, r1[index].fY);
2422 }
2423 path1.close();
2424 if (!rectFirst) {
2425 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2426 }
halcanary96fcdcc2015-08-27 07:41:13 -07002427 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002428
2429 // fail, quad
2430 path1.reset();
2431 if (rectFirst) {
2432 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2433 }
2434 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002435 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002436 if (index == 2) {
2437 path1.quadTo(1, .5f, 1, .5f);
2438 }
2439 path1.lineTo(r1[index].fX, r1[index].fY);
2440 }
2441 path1.close();
2442 if (!rectFirst) {
2443 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2444 }
halcanary96fcdcc2015-08-27 07:41:13 -07002445 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002446
2447 // fail, cubic
2448 path1.reset();
2449 if (rectFirst) {
2450 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2451 }
2452 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002453 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002454 if (index == 2) {
2455 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2456 }
2457 path1.lineTo(r1[index].fX, r1[index].fY);
2458 }
2459 path1.close();
2460 if (!rectFirst) {
2461 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2462 }
halcanary96fcdcc2015-08-27 07:41:13 -07002463 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002464
caryclark@google.com56f233a2012-11-19 13:06:06 +00002465 // fail, not nested
2466 path1.reset();
2467 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2468 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002469 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002470 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002471
caryclark95bc5f32015-04-08 08:34:15 -07002472 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2473 SkPath path;
2474 path.moveTo(0, 0);
2475 path.lineTo(10, 0);
2476 path.lineTo(10, 10);
2477 path.lineTo(0, 10);
2478 path.lineTo(0, 0);
2479 path.moveTo(1, 1);
2480 path.lineTo(9, 1);
2481 path.lineTo(9, 9);
2482 path.lineTo(1, 9);
2483 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002484 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002485
caryclark@google.combfe90372012-11-21 13:56:20 +00002486 // pass, stroke rect
2487 SkPath src, dst;
2488 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2489 SkPaint strokePaint;
2490 strokePaint.setStyle(SkPaint::kStroke_Style);
2491 strokePaint.setStrokeWidth(2);
2492 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002493 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002494}
2495
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002496static void write_and_read_back(skiatest::Reporter* reporter,
2497 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002498 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002499 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002500 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002501 SkAutoMalloc storage(size);
2502 writer.flatten(storage.get());
2503 SkReader32 reader(storage.get(), size);
2504
2505 SkPath readBack;
2506 REPORTER_ASSERT(reporter, readBack != p);
2507 reader.readPath(&readBack);
2508 REPORTER_ASSERT(reporter, readBack == p);
2509
rmistry@google.comd6176b02012-08-23 18:14:13 +00002510 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002511 p.getConvexityOrUnknown());
2512
bsalomon78d58d12016-05-27 09:17:04 -07002513 SkRect oval0, oval1;
2514 SkPath::Direction dir0, dir1;
2515 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002516 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002517 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2518 REPORTER_ASSERT(reporter, oval0 == oval1);
2519 REPORTER_ASSERT(reporter, dir0 == dir1);
2520 REPORTER_ASSERT(reporter, start0 == start1);
2521 }
2522 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2523 SkRRect rrect0, rrect1;
2524 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2525 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2526 REPORTER_ASSERT(reporter, dir0 == dir1);
2527 REPORTER_ASSERT(reporter, start0 == start1);
2528 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002529 const SkRect& origBounds = p.getBounds();
2530 const SkRect& readBackBounds = readBack.getBounds();
2531
2532 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2533}
2534
Mike Reede3374d62017-01-03 13:58:21 -05002535static void test_corrupt_flattening(skiatest::Reporter* reporter) {
2536 SkPath path;
2537 path.moveTo(1, 2);
2538 path.lineTo(1, 2);
2539 path.quadTo(1, 2, 3, 4);
2540 path.conicTo(1, 2, 3, 4, 0.5f);
2541 path.cubicTo(1, 2, 3, 4, 5, 6);
2542 uint8_t buffer[1024];
2543 SkDEBUGCODE(size_t size =) path.writeToMemory(buffer);
2544 SkASSERT(size <= sizeof(buffer));
2545
2546 // find where the counts and verbs are stored : from the impl in SkPathRef.cpp
2547 int32_t* vCount = (int32_t*)&buffer[16];
2548 SkASSERT(*vCount == 5);
2549 int32_t* pCount = (int32_t*)&buffer[20];
2550 SkASSERT(*pCount == 9);
2551 int32_t* cCount = (int32_t*)&buffer[24];
2552 SkASSERT(*cCount == 1);
2553 uint8_t* verbs = &buffer[28];
2554
2555 REPORTER_ASSERT(reporter, path.readFromMemory(buffer, sizeof(buffer)));
2556
2557 // check that we detect under/over-flow of counts
2558
2559 *vCount += 1;
2560 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2561 *vCount -= 1; // restore
2562
2563 *pCount += 1;
2564 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2565 *pCount -= 2;
2566 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2567 *pCount += 1; // restore
2568
2569 *cCount += 1;
2570 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2571 *cCount -= 2;
2572 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2573 *cCount += 1; // restore
2574
2575 // Check that we detect when the verbs indicate more or fewer pts/conics
2576
2577 uint8_t save = verbs[0];
2578 SkASSERT(save == SkPath::kCubic_Verb);
2579 verbs[0] = SkPath::kQuad_Verb;
2580 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2581 verbs[0] = save;
2582
2583 save = verbs[1];
2584 SkASSERT(save == SkPath::kConic_Verb);
2585 verbs[1] = SkPath::kQuad_Verb;
2586 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2587 verbs[1] = SkPath::kCubic_Verb;
2588 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2589 verbs[1] = save;
2590
2591 // Check that we detect invalid verbs
2592 save = verbs[1];
2593 verbs[1] = 17;
2594 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2595 verbs[1] = save;
2596}
2597
reed@google.com53effc52011-09-21 19:05:12 +00002598static void test_flattening(skiatest::Reporter* reporter) {
2599 SkPath p;
2600
2601 static const SkPoint pts[] = {
2602 { 0, 0 },
2603 { SkIntToScalar(10), SkIntToScalar(10) },
2604 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2605 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2606 };
2607 p.moveTo(pts[0]);
2608 p.lineTo(pts[1]);
2609 p.quadTo(pts[2], pts[3]);
2610 p.cubicTo(pts[4], pts[5], pts[6]);
2611
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002612 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002613
2614 // create a buffer that should be much larger than the path so we don't
2615 // kill our stack if writer goes too far.
2616 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002617 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002618 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002619 REPORTER_ASSERT(reporter, size1 == size2);
2620
2621 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002622 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002623 REPORTER_ASSERT(reporter, size1 == size3);
2624 REPORTER_ASSERT(reporter, p == p2);
2625
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002626 size3 = p2.readFromMemory(buffer, 0);
2627 REPORTER_ASSERT(reporter, !size3);
2628
2629 SkPath tooShort;
2630 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2631 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2632
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002633 char buffer2[1024];
2634 size3 = p2.writeToMemory(buffer2);
2635 REPORTER_ASSERT(reporter, size1 == size3);
2636 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002637
2638 // test persistence of the oval flag & convexity
2639 {
2640 SkPath oval;
2641 SkRect rect = SkRect::MakeWH(10, 10);
2642 oval.addOval(rect);
2643
2644 write_and_read_back(reporter, oval);
2645 }
Mike Reede3374d62017-01-03 13:58:21 -05002646
2647 test_corrupt_flattening(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002648}
2649
2650static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002651 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002652
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002653#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002654 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002655 { 0, 0 }, // move
2656 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2657 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2658 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2659#if CONIC_PERSPECTIVE_BUG_FIXED
2660 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2661#endif
reed@google.com53effc52011-09-21 19:05:12 +00002662 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002663 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002664
reed@google.com53effc52011-09-21 19:05:12 +00002665 p.moveTo(pts[0]);
2666 p.lineTo(pts[1]);
2667 p.quadTo(pts[2], pts[3]);
2668 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002669#if CONIC_PERSPECTIVE_BUG_FIXED
2670 p.conicTo(pts[4], pts[5], 0.5f);
2671#endif
2672 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002673
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002674 {
2675 SkMatrix matrix;
2676 matrix.reset();
2677 SkPath p1;
2678 p.transform(matrix, &p1);
2679 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002680 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002681
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002682
2683 {
2684 SkMatrix matrix;
2685 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2686
2687 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2688
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002689 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002690 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002691 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002692 REPORTER_ASSERT(reporter, kPtCount == count);
2693 for (int i = 0; i < count; ++i) {
2694 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2695 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2696 }
2697 }
2698
2699 {
2700 SkMatrix matrix;
2701 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002702 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002703
2704 SkPath p1;
2705 p1.moveTo(SkPoint::Make(0, 0));
2706
2707 p.transform(matrix, &p1);
2708 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002709 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002710 SkRect pBounds = p.getBounds();
2711 SkRect p1Bounds = p1.getBounds();
2712 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2713 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2714 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2715 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2716 }
2717
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002718 p.reset();
2719 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002720
2721 {
2722 SkMatrix matrix;
2723 matrix.reset();
2724 SkPath p1;
2725 p1.moveTo(SkPoint::Make(0, 0));
2726
2727 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002728 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002729 }
2730
2731
2732 {
2733 SkMatrix matrix;
2734 matrix.reset();
2735 matrix.setScaleX(-1);
2736 SkPath p1;
2737 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2738
2739 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002740 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002741 }
2742
2743 {
2744 SkMatrix matrix;
2745 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2746 SkPath p1;
2747 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2748
2749 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002750 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002751 }
reed@google.com53effc52011-09-21 19:05:12 +00002752}
2753
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002754static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002755 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002756 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002757
caryclark@google.com56f233a2012-11-19 13:06:06 +00002758 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002759 const char* testPath;
2760 const size_t numResultPts;
2761 const SkRect resultBound;
2762 const SkPath::Verb* resultVerbs;
2763 const size_t numResultVerbs;
2764 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002765
schenney@chromium.org7e963602012-06-13 17:05:43 +00002766 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2767 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2768 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2769 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2770 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2771 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2772 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2773 static const SkPath::Verb resultVerbs8[] = {
2774 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2775 };
2776 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2777 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2778 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2779 static const SkPath::Verb resultVerbs12[] = {
2780 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2781 };
2782 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2783 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2784 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2785 static const SkPath::Verb resultVerbs16[] = {
2786 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2787 };
2788 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002789 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002790 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
reed91f283b2015-07-28 06:00:50 -07002791 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002792 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2793 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2794 { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) },
2795 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2796 { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) },
2797 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2798 { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) },
2799 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2800 { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) },
2801 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2802 { "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,
schenney@chromium.org7e963602012-06-13 17:05:43 +00002803 SK_ARRAY_COUNT(resultVerbs14)
2804 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002805 { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) },
2806 { "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,
schenney@chromium.org7e963602012-06-13 17:05:43 +00002807 SK_ARRAY_COUNT(resultVerbs16)
2808 }
2809 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002810
schenney@chromium.org7e963602012-06-13 17:05:43 +00002811 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2812 p.reset();
2813 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2814 REPORTER_ASSERT(reporter, valid);
2815 REPORTER_ASSERT(reporter, !p.isEmpty());
2816 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2817 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2818 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2819 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2820 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2821 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002822 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002823}
2824
2825struct SegmentInfo {
2826 SkPath fPath;
2827 int fPointCount;
2828};
2829
reed@google.com10296cc2011-09-21 12:29:05 +00002830#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2831
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002832static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002833 SkPath p, p2;
2834
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002835 p.moveTo(0, 0);
2836 p.quadTo(100, 100, 200, 200);
2837 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2838 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002839 p2 = p;
2840 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002841 p.cubicTo(100, 100, 200, 200, 300, 300);
2842 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2843 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002844 p2 = p;
2845 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2846
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002847 p.reset();
2848 p.moveTo(0, 0);
2849 p.cubicTo(100, 100, 200, 200, 300, 300);
2850 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002851 p2 = p;
2852 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002853
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002854 REPORTER_ASSERT(reporter, !p.isEmpty());
2855}
2856
2857static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002858 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002859 SkPoint pts[4];
2860
2861 // Test an iterator with no path
2862 SkPath::Iter noPathIter;
2863 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002864
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002865 // Test that setting an empty path works
2866 noPathIter.setPath(p, false);
2867 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002868
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002869 // Test that close path makes no difference for an empty path
2870 noPathIter.setPath(p, true);
2871 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002872
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002873 // Test an iterator with an initial empty path
2874 SkPath::Iter iter(p, false);
2875 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2876
2877 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002878 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002879 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2880
rmistry@google.comd6176b02012-08-23 18:14:13 +00002881
schenney@chromium.org7e963602012-06-13 17:05:43 +00002882 struct iterTestData {
2883 const char* testPath;
2884 const bool forceClose;
2885 const bool consumeDegenerates;
2886 const size_t* numResultPtsPerVerb;
2887 const SkPoint* resultPts;
2888 const SkPath::Verb* resultVerbs;
2889 const size_t numResultVerbs;
2890 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002891
schenney@chromium.org7e963602012-06-13 17:05:43 +00002892 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2893 static const SkPath::Verb resultVerbs2[] = {
2894 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2895 };
2896 static const SkPath::Verb resultVerbs3[] = {
2897 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2898 };
2899 static const SkPath::Verb resultVerbs4[] = {
2900 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2901 };
2902 static const SkPath::Verb resultVerbs5[] = {
2903 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2904 };
2905 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002906 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2907 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2908 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2909 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002910 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002911 static const SkPoint resultPts2[] = {
2912 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2913 };
2914 static const SkPoint resultPts3[] = {
2915 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2916 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2917 };
2918 static const SkPoint resultPts4[] = {
2919 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2920 };
2921 static const SkPoint resultPts5[] = {
2922 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2923 };
2924 static const struct iterTestData gIterTests[] = {
2925 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002926 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2927 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.org7e963602012-06-13 17:05:43 +00002928 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2929 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2930 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2931 { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002932 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2933 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2934 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2935 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2936 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2937 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }
schenney@chromium.org7e963602012-06-13 17:05:43 +00002938 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002939
schenney@chromium.org7e963602012-06-13 17:05:43 +00002940 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2941 p.reset();
2942 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2943 REPORTER_ASSERT(reporter, valid);
2944 iter.setPath(p, gIterTests[i].forceClose);
2945 int j = 0, l = 0;
2946 do {
2947 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2948 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2949 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2950 }
2951 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2952 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2953 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002954
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002955 p.reset();
2956 iter.setPath(p, false);
2957 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2958 p.lineTo(1, 1);
2959 p.close();
2960 iter.setPath(p, false);
2961 REPORTER_ASSERT(reporter, iter.isClosedContour());
2962 p.reset();
2963 iter.setPath(p, true);
2964 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2965 p.lineTo(1, 1);
2966 iter.setPath(p, true);
2967 REPORTER_ASSERT(reporter, iter.isClosedContour());
2968 p.moveTo(0, 0);
2969 p.lineTo(2, 2);
2970 iter.setPath(p, false);
2971 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2972
2973 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2974 // check to see if the result is correct.
2975 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2976 p.reset();
2977 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2978 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2979 iter.setPath(p, true);
2980 iter.next(pts, false);
2981 iter.next(pts, false);
2982 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2983 }
2984
2985 p.reset();
2986 p.quadTo(0, 0, 0, 0);
2987 iter.setPath(p, false);
2988 iter.next(pts, false);
2989 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2990 iter.setPath(p, false);
2991 iter.next(pts, false);
2992 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2993
2994 p.reset();
2995 p.conicTo(0, 0, 0, 0, 0.5f);
2996 iter.setPath(p, false);
2997 iter.next(pts, false);
2998 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2999 iter.setPath(p, false);
3000 iter.next(pts, false);
3001 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3002
3003 p.reset();
3004 p.cubicTo(0, 0, 0, 0, 0, 0);
3005 iter.setPath(p, false);
3006 iter.next(pts, false);
3007 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3008 iter.setPath(p, false);
3009 iter.next(pts, false);
3010 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3011
3012 p.moveTo(1, 1); // add a trailing moveto
3013 iter.setPath(p, false);
3014 iter.next(pts, false);
3015 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3016 iter.setPath(p, false);
3017 iter.next(pts, false);
3018 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3019
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003020 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08003021
3022 // Test out mixed degenerate and non-degenerate geometry with Conics
3023 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3024 SkRect r = SkRect::MakeWH(100, 100);
3025 SkRRect rr;
3026 rr.setRectRadii(r, radii);
3027 p.reset();
3028 p.addRRect(rr);
3029 iter.setPath(p, false);
3030 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3031 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3032 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3033 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3034 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003035}
3036
3037static void test_raw_iter(skiatest::Reporter* reporter) {
3038 SkPath p;
3039 SkPoint pts[4];
3040
3041 // Test an iterator with no path
3042 SkPath::RawIter noPathIter;
3043 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3044 // Test that setting an empty path works
3045 noPathIter.setPath(p);
3046 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00003047
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003048 // Test an iterator with an initial empty path
3049 SkPath::RawIter iter(p);
3050 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3051
3052 // Test that a move-only path returns the move.
3053 p.moveTo(SK_Scalar1, 0);
3054 iter.setPath(p);
3055 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3056 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3057 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3058 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3059
3060 // No matter how many moves we add, we should get them all back
3061 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3062 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3063 iter.setPath(p);
3064 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3065 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3066 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3067 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3068 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3069 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3070 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3071 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3072 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3073 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3074
3075 // Initial close is never ever stored
3076 p.reset();
3077 p.close();
3078 iter.setPath(p);
3079 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3080
3081 // Move/close sequences
3082 p.reset();
3083 p.close(); // Not stored, no purpose
3084 p.moveTo(SK_Scalar1, 0);
3085 p.close();
3086 p.close(); // Not stored, no purpose
3087 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3088 p.close();
3089 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3090 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3091 p.close();
3092 iter.setPath(p);
3093 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3094 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3095 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3096 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003097 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3098 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3099 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3100 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003101 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3102 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3103 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3104 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3105 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3106 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3107 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003108 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3109
3110 // Generate random paths and verify
3111 SkPoint randomPts[25];
3112 for (int i = 0; i < 5; ++i) {
3113 for (int j = 0; j < 5; ++j) {
3114 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3115 }
3116 }
3117
3118 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003119 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003120 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003121 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003122 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003123
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003124 for (int i = 0; i < 500; ++i) {
3125 p.reset();
3126 bool lastWasClose = true;
3127 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003128 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003129 int numPoints = 0;
3130 int numVerbs = (rand.nextU() >> 16) % 10;
3131 int numIterVerbs = 0;
3132 for (int j = 0; j < numVerbs; ++j) {
3133 do {
3134 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3135 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003136 switch (nextVerb) {
3137 case SkPath::kMove_Verb:
3138 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3139 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003140 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003141 numPoints += 1;
3142 lastWasClose = false;
3143 haveMoveTo = true;
3144 break;
3145 case SkPath::kLine_Verb:
3146 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003147 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003148 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3149 haveMoveTo = true;
3150 }
3151 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3152 p.lineTo(expectedPts[numPoints]);
3153 numPoints += 1;
3154 lastWasClose = false;
3155 break;
3156 case SkPath::kQuad_Verb:
3157 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003158 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003159 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3160 haveMoveTo = true;
3161 }
3162 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3163 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3164 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3165 numPoints += 2;
3166 lastWasClose = false;
3167 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003168 case SkPath::kConic_Verb:
3169 if (!haveMoveTo) {
3170 expectedPts[numPoints++] = lastMoveToPt;
3171 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3172 haveMoveTo = true;
3173 }
3174 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3175 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3176 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3177 rand.nextUScalar1() * 4);
3178 numPoints += 2;
3179 lastWasClose = false;
3180 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003181 case SkPath::kCubic_Verb:
3182 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003183 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003184 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3185 haveMoveTo = true;
3186 }
3187 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3188 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3189 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3190 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3191 expectedPts[numPoints + 2]);
3192 numPoints += 3;
3193 lastWasClose = false;
3194 break;
3195 case SkPath::kClose_Verb:
3196 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003197 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003198 lastWasClose = true;
3199 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003200 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003201 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003202 }
3203 expectedVerbs[numIterVerbs++] = nextVerb;
3204 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003205
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003206 iter.setPath(p);
3207 numVerbs = numIterVerbs;
3208 numIterVerbs = 0;
3209 int numIterPts = 0;
3210 SkPoint lastMoveTo;
3211 SkPoint lastPt;
3212 lastMoveTo.set(0, 0);
3213 lastPt.set(0, 0);
3214 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3215 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3216 numIterVerbs++;
3217 switch (nextVerb) {
3218 case SkPath::kMove_Verb:
3219 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3220 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3221 lastPt = lastMoveTo = pts[0];
3222 numIterPts += 1;
3223 break;
3224 case SkPath::kLine_Verb:
3225 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3226 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3227 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3228 lastPt = pts[1];
3229 numIterPts += 1;
3230 break;
3231 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003232 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003233 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3234 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3235 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3236 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3237 lastPt = pts[2];
3238 numIterPts += 2;
3239 break;
3240 case SkPath::kCubic_Verb:
3241 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3242 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3243 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3244 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3245 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3246 lastPt = pts[3];
3247 numIterPts += 3;
3248 break;
3249 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003250 lastPt = lastMoveTo;
3251 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003252 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003253 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003254 }
3255 }
3256 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3257 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3258 }
3259}
3260
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003261static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003262 const SkPath& path,
3263 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003264 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003265 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003266 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003267 SkPath::Direction isOvalDir;
3268 unsigned isOvalStart;
3269 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003270 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003271 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3272 SkPath tmpPath;
3273 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3274 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003275 }
bsalomon78d58d12016-05-27 09:17:04 -07003276 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003277}
3278
3279static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003280 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003281 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003282 SkPath tmp;
3283
3284 SkMatrix m;
3285 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3286 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003287 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003288 if (SkPathPriv::kCCW_FirstDirection == dir) {
3289 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003290 } else {
reed026beb52015-06-10 14:23:15 -07003291 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3292 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003293 }
3294 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003295}
3296
3297static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003298 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003299 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003300 SkPath tmp;
3301
3302 // translate at small offset
3303 SkMatrix m;
3304 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3305 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003306 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003307
3308 tmp.reset();
3309 m.reset();
3310
3311 // translate at a relatively big offset
3312 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3313 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003314 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003315}
3316
3317static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003318 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003319 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003320 for (int angle = 0; angle < 360; ++angle) {
3321 SkPath tmp;
3322 SkMatrix m;
3323 m.setRotate(SkIntToScalar(angle));
3324 path.transform(m, &tmp);
3325
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003326 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003327 // degrees is not an oval anymore, this can be improved. we made this
3328 // for the simplicity of our implementation.
3329 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003330 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003331 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003332 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003333 }
3334 }
3335}
3336
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003337static void test_circle_mirror_x(skiatest::Reporter* reporter,
3338 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003339 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003340 SkPath tmp;
3341 SkMatrix m;
3342 m.reset();
3343 m.setScaleX(-SK_Scalar1);
3344 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003345 if (SkPathPriv::kCW_FirstDirection == dir) {
3346 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003347 } else {
reed026beb52015-06-10 14:23:15 -07003348 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3349 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003350 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003351 check_for_circle(reporter, tmp, true, dir);
3352}
3353
3354static void test_circle_mirror_y(skiatest::Reporter* reporter,
3355 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003356 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003357 SkPath tmp;
3358 SkMatrix m;
3359 m.reset();
3360 m.setScaleY(-SK_Scalar1);
3361 path.transform(m, &tmp);
3362
reed026beb52015-06-10 14:23:15 -07003363 if (SkPathPriv::kCW_FirstDirection == dir) {
3364 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003365 } else {
reed026beb52015-06-10 14:23:15 -07003366 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3367 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003368 }
3369
3370 check_for_circle(reporter, tmp, true, dir);
3371}
3372
3373static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3374 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003375 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003376 SkPath tmp;
3377 SkMatrix m;
3378 m.reset();
3379 m.setScaleX(-SK_Scalar1);
3380 m.setScaleY(-SK_Scalar1);
3381 path.transform(m, &tmp);
3382
3383 check_for_circle(reporter, tmp, true, dir);
3384}
3385
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003386static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003387 SkPath::Direction inDir) {
3388 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003389 SkPath path;
3390
3391 // circle at origin
reed026beb52015-06-10 14:23:15 -07003392 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3393
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003394 check_for_circle(reporter, path, true, dir);
3395 test_circle_rotate(reporter, path, dir);
3396 test_circle_translate(reporter, path, dir);
3397 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003398 test_circle_mirror_x(reporter, path, dir);
3399 test_circle_mirror_y(reporter, path, dir);
3400 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003401
3402 // circle at an offset at (10, 10)
3403 path.reset();
3404 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003405 SkIntToScalar(20), inDir);
3406
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003407 check_for_circle(reporter, path, true, dir);
3408 test_circle_rotate(reporter, path, dir);
3409 test_circle_translate(reporter, path, dir);
3410 test_circle_skew(reporter, path, dir);
3411 test_circle_mirror_x(reporter, path, dir);
3412 test_circle_mirror_y(reporter, path, dir);
3413 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003414
3415 // Try different starting points for the contour.
3416 for (unsigned start = 0; start < 4; ++start) {
3417 path.reset();
3418 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3419 test_circle_rotate(reporter, path, dir);
3420 test_circle_translate(reporter, path, dir);
3421 test_circle_skew(reporter, path, dir);
3422 test_circle_mirror_x(reporter, path, dir);
3423 test_circle_mirror_y(reporter, path, dir);
3424 test_circle_mirror_xy(reporter, path, dir);
3425 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003426}
3427
3428static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3429 SkPath path;
3430 SkPath circle;
3431 SkPath rect;
3432 SkPath empty;
3433
reed026beb52015-06-10 14:23:15 -07003434 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3435 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003436
3437 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003438 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3439 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3440
3441 SkMatrix translate;
3442 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3443
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003444 // Although all the path concatenation related operations leave
3445 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003446
3447 // empty + circle (translate)
3448 path = empty;
3449 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003450 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003451
3452 // circle + empty (translate)
3453 path = circle;
3454 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003455
reed026beb52015-06-10 14:23:15 -07003456 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003457
3458 // test reverseAddPath
3459 path = circle;
3460 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003461 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003462}
3463
3464static void test_circle(skiatest::Reporter* reporter) {
3465 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3466 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3467
3468 // multiple addCircle()
3469 SkPath path;
3470 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3471 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003472 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003473
3474 // some extra lineTo() would make isOval() fail
3475 path.reset();
3476 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3477 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003478 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003479
3480 // not back to the original point
3481 path.reset();
3482 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3483 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003484 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003485
3486 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003487
3488 // test negative radius
3489 path.reset();
3490 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3491 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003492}
3493
3494static void test_oval(skiatest::Reporter* reporter) {
3495 SkRect rect;
3496 SkMatrix m;
3497 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003498 unsigned start = 0;
3499 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003500
3501 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3502 path.addOval(rect);
3503
bsalomon78d58d12016-05-27 09:17:04 -07003504 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003505 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003506
3507 m.setRotate(SkIntToScalar(90));
3508 SkPath tmp;
3509 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003510 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3511 // is unchanged.
3512 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3513 REPORTER_ASSERT(reporter, 2 == start);
3514 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003515
3516 m.reset();
3517 m.setRotate(SkIntToScalar(30));
3518 tmp.reset();
3519 path.transform(m, &tmp);
3520 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003521 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003522
3523 // since empty path being transformed.
3524 path.reset();
3525 tmp.reset();
3526 m.reset();
3527 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003528 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003529
3530 // empty path is not an oval
3531 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003532 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003533
3534 // only has moveTo()s
3535 tmp.reset();
3536 tmp.moveTo(0, 0);
3537 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003538 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003539
3540 // mimic WebKit's calling convention,
3541 // call moveTo() first and then call addOval()
3542 path.reset();
3543 path.moveTo(0, 0);
3544 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003545 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003546
3547 // copy path
3548 path.reset();
3549 tmp.reset();
3550 tmp.addOval(rect);
3551 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003552 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3553 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3554 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003555}
3556
bungeman@google.coma5809a32013-06-21 15:13:34 +00003557static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3558 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003559
reed@android.com3abec1d2009-03-02 05:36:20 +00003560 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003561 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003562 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003563 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003564 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003565 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3566 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003567 REPORTER_ASSERT(reporter, p == empty);
3568 REPORTER_ASSERT(reporter, !(p != empty));
3569}
3570
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003571static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3572 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003573 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003574 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003575 path->setConvexity(SkPath::kUnknown_Convexity);
3576 REPORTER_ASSERT(reporter, path->isConvex());
3577 path->reset();
3578}
3579
caryclarkd3d1a982014-12-08 04:57:38 -08003580static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3581 SkPath::Direction dir) {
3582 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003583 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003584 path->setConvexity(SkPath::kUnknown_Convexity);
3585 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3586 path->reset();
3587}
3588
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003589static void test_rrect(skiatest::Reporter* reporter) {
3590 SkPath p;
3591 SkRRect rr;
3592 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3593 SkRect r = {10, 20, 30, 40};
3594 rr.setRectRadii(r, radii);
3595 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003596 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003597 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003598 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003599 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003600 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003601 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003602 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003603 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003604 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003605 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003606 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3607 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3608 SkVector save = radii[i];
3609 radii[i].set(0, 0);
3610 rr.setRectRadii(r, radii);
3611 p.addRRect(rr);
3612 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3613 radii[i] = save;
3614 }
3615 p.addRoundRect(r, 0, 0);
3616 SkRect returnedRect;
3617 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3618 REPORTER_ASSERT(reporter, returnedRect == r);
3619 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3620 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3621 rr.setRectRadii(r, zeroRadii);
3622 p.addRRect(rr);
3623 bool closed;
3624 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003625 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003626 REPORTER_ASSERT(reporter, closed);
3627 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3628 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3629 p.addRRect(rr, SkPath::kCW_Direction);
3630 p.addRRect(rr, SkPath::kCW_Direction);
3631 REPORTER_ASSERT(reporter, !p.isConvex());
3632 p.reset();
3633 p.addRRect(rr, SkPath::kCCW_Direction);
3634 p.addRRect(rr, SkPath::kCCW_Direction);
3635 REPORTER_ASSERT(reporter, !p.isConvex());
3636 p.reset();
3637 SkRect emptyR = {10, 20, 10, 30};
3638 rr.setRectRadii(emptyR, radii);
3639 p.addRRect(rr);
3640 REPORTER_ASSERT(reporter, p.isEmpty());
3641 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3642 rr.setRectRadii(largeR, radii);
3643 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003644 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003645
3646 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003647 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3648 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003649 REPORTER_ASSERT(reporter, rr.isEmpty());
3650
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003651 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3652 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3653 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003654}
3655
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003656static void test_arc(skiatest::Reporter* reporter) {
3657 SkPath p;
3658 SkRect emptyOval = {10, 20, 30, 20};
3659 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3660 p.addArc(emptyOval, 1, 2);
3661 REPORTER_ASSERT(reporter, p.isEmpty());
3662 p.reset();
3663 SkRect oval = {10, 20, 30, 40};
3664 p.addArc(oval, 1, 0);
3665 REPORTER_ASSERT(reporter, p.isEmpty());
3666 p.reset();
3667 SkPath cwOval;
3668 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003669 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003670 REPORTER_ASSERT(reporter, p == cwOval);
3671 p.reset();
3672 SkPath ccwOval;
3673 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003674 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003675 REPORTER_ASSERT(reporter, p == ccwOval);
3676 p.reset();
3677 p.addArc(oval, 1, 180);
3678 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003679 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003680 p.setConvexity(SkPath::kUnknown_Convexity);
3681 REPORTER_ASSERT(reporter, p.isConvex());
3682}
3683
bsalomon1978ce22016-05-31 10:42:16 -07003684static inline SkScalar oval_start_index_to_angle(unsigned start) {
3685 switch (start) {
3686 case 0:
3687 return 270.f;
3688 case 1:
3689 return 0.f;
3690 case 2:
3691 return 90.f;
3692 case 3:
3693 return 180.f;
3694 default:
3695 return -1.f;
3696 }
3697}
3698
3699static inline SkScalar canonical_start_angle(float angle) {
3700 while (angle < 0.f) {
3701 angle += 360.f;
3702 }
3703 while (angle >= 360.f) {
3704 angle -= 360.f;
3705 }
3706 return angle;
3707}
3708
3709static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3710 const SkPath& path) {
3711 SkRect r = SkRect::MakeEmpty();
3712 SkPath::Direction d = SkPath::kCCW_Direction;
3713 unsigned s = ~0U;
3714 bool isOval = path.isOval(&r, &d, &s);
3715 REPORTER_ASSERT(reporter, isOval);
3716 SkPath recreatedPath;
3717 recreatedPath.addOval(r, d, s);
3718 REPORTER_ASSERT(reporter, path == recreatedPath);
3719 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3720 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3721}
3722
3723static void test_arc_ovals(skiatest::Reporter* reporter) {
3724 SkRect oval = SkRect::MakeWH(10, 20);
3725 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3726 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3727 SkPath path;
3728 path.addArc(oval, start, sweep);
3729 // SkPath's interfaces for inserting and extracting ovals only allow contours
3730 // to start at multiples of 90 degrees.
3731 if (std::fmod(start, 90.f) == 0) {
3732 check_oval_arc(reporter, start, sweep, path);
3733 } else {
3734 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3735 }
3736 }
3737 // Test start angles that are nearly at valid oval start angles.
3738 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3739 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3740 SkPath path;
3741 path.addArc(oval, start + delta, sweep);
3742 check_oval_arc(reporter, start, sweep, path);
3743 }
3744 }
3745 }
3746}
3747
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003748static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3749 SkScalar x0, SkScalar y0) {
3750 SkPoint pts[4];
3751 SkPath::Verb v = iter->next(pts);
3752 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3753 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3754 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3755}
3756
3757static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3758 SkScalar x1, SkScalar y1) {
3759 SkPoint pts[4];
3760 SkPath::Verb v = iter->next(pts);
3761 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3762 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3763 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3764}
3765
3766static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3767 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3768 SkPoint pts[4];
3769 SkPath::Verb v = iter->next(pts);
3770 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3771 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3772 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3773 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3774 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3775}
3776
3777static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3778 SkPoint pts[4];
3779 SkPath::Verb v = iter->next(pts);
3780 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3781}
3782
3783static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3784 check_done(reporter, p, iter);
3785 p->reset();
3786}
3787
3788static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3789 SkScalar x0, SkScalar y0) {
3790 SkPath::RawIter iter(*p);
3791 check_move(reporter, &iter, x0, y0);
3792 check_done_and_reset(reporter, p, &iter);
3793}
3794
3795static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3796 SkScalar x1, SkScalar y1) {
3797 SkPath::RawIter iter(*p);
3798 check_move(reporter, &iter, 0, 0);
3799 check_line(reporter, &iter, x1, y1);
3800 check_done_and_reset(reporter, p, &iter);
3801}
3802
3803static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3804 SkScalar x1, SkScalar y1) {
3805 SkPath::RawIter iter(*p);
3806 check_move(reporter, &iter, 0, 0);
3807 check_line(reporter, &iter, x1, y1);
3808 check_done(reporter, p, &iter);
3809}
3810
3811static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3812 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3813 SkPath::RawIter iter(*p);
3814 check_move(reporter, &iter, 0, 0);
3815 check_line(reporter, &iter, x1, y1);
3816 check_line(reporter, &iter, x2, y2);
3817 check_done_and_reset(reporter, p, &iter);
3818}
3819
3820static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3821 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3822 SkPath::RawIter iter(*p);
3823 check_move(reporter, &iter, 0, 0);
3824 check_quad(reporter, &iter, x1, y1, x2, y2);
3825 check_done_and_reset(reporter, p, &iter);
3826}
3827
reedd5d27d92015-02-09 13:54:43 -08003828static bool nearly_equal(const SkRect& a, const SkRect& b) {
3829 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3830 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3831 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3832 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3833}
3834
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003835static void test_arcTo(skiatest::Reporter* reporter) {
3836 SkPath p;
3837 p.arcTo(0, 0, 1, 2, 1);
3838 check_path_is_line_and_reset(reporter, &p, 0, 0);
3839 p.arcTo(1, 2, 1, 2, 1);
3840 check_path_is_line_and_reset(reporter, &p, 1, 2);
3841 p.arcTo(1, 2, 3, 4, 0);
3842 check_path_is_line_and_reset(reporter, &p, 1, 2);
3843 p.arcTo(1, 2, 0, 0, 1);
3844 check_path_is_line_and_reset(reporter, &p, 1, 2);
3845 p.arcTo(1, 0, 1, 1, 1);
3846 SkPoint pt;
3847 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3848 p.reset();
3849 p.arcTo(1, 0, 1, -1, 1);
3850 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3851 p.reset();
3852 SkRect oval = {1, 2, 3, 4};
3853 p.arcTo(oval, 0, 0, true);
3854 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3855 p.arcTo(oval, 0, 0, false);
3856 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3857 p.arcTo(oval, 360, 0, true);
3858 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3859 p.arcTo(oval, 360, 0, false);
3860 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003861
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003862 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003863 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003864 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003865 sweep += delta;
3866 delta /= 2;
3867 }
3868 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003869 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003870 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003871 sweep -= delta;
3872 delta /= 2;
3873 }
3874 SkRect noOvalWidth = {1, 2, 0, 3};
3875 p.reset();
3876 p.arcTo(noOvalWidth, 0, 360, false);
3877 REPORTER_ASSERT(reporter, p.isEmpty());
3878
3879 SkRect noOvalHeight = {1, 2, 3, 1};
3880 p.reset();
3881 p.arcTo(noOvalHeight, 0, 360, false);
3882 REPORTER_ASSERT(reporter, p.isEmpty());
3883}
3884
3885static void test_addPath(skiatest::Reporter* reporter) {
3886 SkPath p, q;
3887 p.lineTo(1, 2);
3888 q.moveTo(4, 4);
3889 q.lineTo(7, 8);
3890 q.conicTo(8, 7, 6, 5, 0.5f);
3891 q.quadTo(6, 7, 8, 6);
3892 q.cubicTo(5, 6, 7, 8, 7, 5);
3893 q.close();
3894 p.addPath(q, -4, -4);
3895 SkRect expected = {0, 0, 4, 4};
3896 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3897 p.reset();
3898 p.reverseAddPath(q);
3899 SkRect reverseExpected = {4, 4, 8, 8};
3900 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3901}
3902
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003903static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3904 SkPath p, q;
3905 if (explicitMoveTo) {
3906 p.moveTo(1, 1);
3907 }
3908 p.lineTo(1, 2);
3909 if (explicitMoveTo) {
3910 q.moveTo(2, 1);
3911 }
3912 q.lineTo(2, 2);
3913 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3914 uint8_t verbs[4];
3915 int verbcount = p.getVerbs(verbs, 4);
3916 REPORTER_ASSERT(reporter, verbcount == 4);
3917 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3918 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3919 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3920 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3921}
3922
3923static void test_extendClosedPath(skiatest::Reporter* reporter) {
3924 SkPath p, q;
3925 p.moveTo(1, 1);
3926 p.lineTo(1, 2);
3927 p.lineTo(2, 2);
3928 p.close();
3929 q.moveTo(2, 1);
3930 q.lineTo(2, 3);
3931 p.addPath(q, SkPath::kExtend_AddPathMode);
3932 uint8_t verbs[7];
3933 int verbcount = p.getVerbs(verbs, 7);
3934 REPORTER_ASSERT(reporter, verbcount == 7);
3935 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3936 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3937 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3938 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3939 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3940 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3941 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3942
3943 SkPoint pt;
3944 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3945 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3946 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3947}
3948
3949static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3950 SkPath p, q, r;
3951 // case 1: dst is empty
3952 p.moveTo(2, 1);
3953 p.lineTo(2, 3);
3954 q.addPath(p, mode);
3955 REPORTER_ASSERT(reporter, q == p);
3956 // case 2: src is empty
3957 p.addPath(r, mode);
3958 REPORTER_ASSERT(reporter, q == p);
3959 // case 3: src and dst are empty
3960 q.reset();
3961 q.addPath(r, mode);
3962 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003963}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003964
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003965static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3966 SkPath p;
3967 p.conicTo(1, 2, 3, 4, -1);
3968 check_path_is_line_and_reset(reporter, &p, 3, 4);
3969 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3970 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3971 p.conicTo(1, 2, 3, 4, 1);
3972 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3973}
3974
3975static void test_get_point(skiatest::Reporter* reporter) {
3976 SkPath p;
3977 SkPoint pt = p.getPoint(0);
3978 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003979 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003980 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3981 p.setLastPt(10, 10);
3982 pt = p.getPoint(0);
3983 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003984 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003985 p.rMoveTo(10, 10);
3986 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3987}
3988
3989static void test_contains(skiatest::Reporter* reporter) {
3990 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003991 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3992 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
3993 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
3994 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3995 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
3996 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
caryclarkba25ddb2016-01-05 12:36:09 -08003997 // this may return true or false, depending on the platform's numerics, but it should not crash
3998 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003999
4000 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004001 p.setFillType(SkPath::kInverseWinding_FillType);
4002 REPORTER_ASSERT(reporter, p.contains(0, 0));
4003 p.setFillType(SkPath::kWinding_FillType);
4004 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4005 p.moveTo(4, 4);
4006 p.lineTo(6, 8);
4007 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08004008 // test on edge
4009 REPORTER_ASSERT(reporter, p.contains(6, 4));
4010 REPORTER_ASSERT(reporter, p.contains(5, 6));
4011 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004012 // test quick reject
4013 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4014 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4015 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4016 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4017 // test various crossings in x
4018 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4019 REPORTER_ASSERT(reporter, p.contains(6, 7));
4020 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4021 p.reset();
4022 p.moveTo(4, 4);
4023 p.lineTo(8, 6);
4024 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08004025 // test on edge
4026 REPORTER_ASSERT(reporter, p.contains(4, 6));
4027 REPORTER_ASSERT(reporter, p.contains(6, 5));
4028 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004029 // test various crossings in y
4030 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4031 REPORTER_ASSERT(reporter, p.contains(7, 6));
4032 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08004033 p.reset();
4034 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08004035 p.lineTo(8, 4);
4036 p.lineTo(8, 8);
4037 p.lineTo(4, 8);
4038 // test on vertices
4039 REPORTER_ASSERT(reporter, p.contains(4, 4));
4040 REPORTER_ASSERT(reporter, p.contains(8, 4));
4041 REPORTER_ASSERT(reporter, p.contains(8, 8));
4042 REPORTER_ASSERT(reporter, p.contains(4, 8));
4043 p.reset();
4044 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08004045 p.lineTo(6, 8);
4046 p.lineTo(2, 8);
4047 // test on edge
4048 REPORTER_ASSERT(reporter, p.contains(5, 6));
4049 REPORTER_ASSERT(reporter, p.contains(4, 8));
4050 REPORTER_ASSERT(reporter, p.contains(3, 6));
4051 p.reset();
4052 p.moveTo(4, 4);
4053 p.lineTo(0, 6);
4054 p.lineTo(4, 8);
4055 // test on edge
4056 REPORTER_ASSERT(reporter, p.contains(2, 5));
4057 REPORTER_ASSERT(reporter, p.contains(2, 7));
4058 REPORTER_ASSERT(reporter, p.contains(4, 6));
4059 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4060 p.reset();
4061 p.moveTo(4, 0);
4062 p.lineTo(6, 4);
4063 p.lineTo(2, 4);
4064 p.moveTo(4, 0);
4065 p.lineTo(0, 8);
4066 p.lineTo(8, 8);
4067 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4068 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4069 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4070 REPORTER_ASSERT(reporter, p.contains(4, 4));
4071
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004072 // test quads
4073 p.reset();
4074 p.moveTo(4, 4);
4075 p.quadTo(6, 6, 8, 8);
4076 p.quadTo(6, 8, 4, 8);
4077 p.quadTo(4, 6, 4, 4);
4078 REPORTER_ASSERT(reporter, p.contains(5, 6));
4079 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004080 // test quad edge
4081 REPORTER_ASSERT(reporter, p.contains(5, 5));
4082 REPORTER_ASSERT(reporter, p.contains(5, 8));
4083 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08004084 // test quad endpoints
4085 REPORTER_ASSERT(reporter, p.contains(4, 4));
4086 REPORTER_ASSERT(reporter, p.contains(8, 8));
4087 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004088
4089 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004090 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4091 p.moveTo(qPts[0]);
4092 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4093 p.quadTo(qPts[index], qPts[index + 1]);
4094 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004095 REPORTER_ASSERT(reporter, p.contains(5, 6));
4096 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004097 // test quad edge
4098 SkPoint halfway;
4099 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4100 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4101 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4102 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004103
caryclark9aacd902015-12-14 08:38:09 -08004104 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004105 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004106 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4107 p.moveTo(kPts[0]);
4108 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4109 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4110 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004111 REPORTER_ASSERT(reporter, p.contains(5, 6));
4112 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004113 // test conic edge
4114 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4115 SkConic conic(&kPts[index], 0.5f);
4116 halfway = conic.evalAt(0.5f);
4117 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4118 }
caryclark9cb5d752015-12-18 04:35:24 -08004119 // test conic end points
4120 REPORTER_ASSERT(reporter, p.contains(4, 4));
4121 REPORTER_ASSERT(reporter, p.contains(8, 8));
4122 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004123
4124 // test cubics
4125 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4126 for (int i = 0; i < 3; ++i) {
4127 p.reset();
4128 p.setFillType(SkPath::kEvenOdd_FillType);
4129 p.moveTo(pts[i].fX, pts[i].fY);
4130 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);
4131 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);
4132 p.close();
4133 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4134 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004135 // test cubic edge
4136 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4137 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4138 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4139 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004140 // test cubic end points
4141 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4142 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4143 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004144 }
4145}
4146
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004147class PathRefTest_Private {
4148public:
4149 static void TestPathRef(skiatest::Reporter* reporter) {
4150 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004151
bungeman6bd52842016-10-27 09:30:08 -07004152 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004153
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004154 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004155
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004156 {
4157 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4158 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4159 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4160 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4161 for (int i = 0; i < kRepeatCnt; ++i) {
4162 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4163 }
4164 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004165 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004166
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004167 {
4168 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4169 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4170 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4171 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4172 for (int i = 0; i < kRepeatCnt; ++i) {
4173 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4174 }
4175 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004176 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004177
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004178 {
4179 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4180 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4181 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4182 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4183 for (int i = 0; i < kRepeatCnt; ++i) {
4184 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4185 }
4186 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004187 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004188
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004189 {
halcanary96fcdcc2015-08-27 07:41:13 -07004190 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004191 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4192 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4193 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4194 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4195 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004196 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004197 for (int i = 0; i < kRepeatCnt; ++i) {
4198 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4199 }
4200 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004201 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004202
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004203 {
4204 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4205 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4206 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4207 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4208 for (int i = 0; i < kRepeatCnt; ++i) {
4209 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4210 }
4211 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004212 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004213 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004214};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004215
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004216static void test_operatorEqual(skiatest::Reporter* reporter) {
4217 SkPath a;
4218 SkPath b;
4219 REPORTER_ASSERT(reporter, a == a);
4220 REPORTER_ASSERT(reporter, a == b);
4221 a.setFillType(SkPath::kInverseWinding_FillType);
4222 REPORTER_ASSERT(reporter, a != b);
4223 a.reset();
4224 REPORTER_ASSERT(reporter, a == b);
4225 a.lineTo(1, 1);
4226 REPORTER_ASSERT(reporter, a != b);
4227 a.reset();
4228 REPORTER_ASSERT(reporter, a == b);
4229 a.lineTo(1, 1);
4230 b.lineTo(1, 2);
4231 REPORTER_ASSERT(reporter, a != b);
4232 a.reset();
4233 a.lineTo(1, 2);
4234 REPORTER_ASSERT(reporter, a == b);
4235}
4236
caryclark66a5d8b2014-06-24 08:30:15 -07004237static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004238 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004239 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004240 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004241 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004242 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004243 if (strlen(str) > 0) {
4244 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4245 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004246 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004247 }
caryclark66a5d8b2014-06-24 08:30:15 -07004248}
4249
4250static void test_dump(skiatest::Reporter* reporter) {
4251 SkPath p;
Cary Clark9f67f042016-12-16 11:32:54 -05004252 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4253 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004254 p.moveTo(1, 2);
4255 p.lineTo(3, 4);
Cary Clark9f67f042016-12-16 11:32:54 -05004256 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4257 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004258 "path.lineTo(3, 4);\n");
Cary Clark9f67f042016-12-16 11:32:54 -05004259 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4260 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004261 "path.lineTo(3, 4);\n"
4262 "path.lineTo(1, 2);\n"
4263 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004264 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004265 p.setFillType(SkPath::kEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004266 p.moveTo(1, 2);
4267 p.quadTo(3, 4, 5, 6);
Cary Clark9f67f042016-12-16 11:32:54 -05004268 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4269 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004270 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004271 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004272 p.setFillType(SkPath::kInverseWinding_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004273 p.moveTo(1, 2);
4274 p.conicTo(3, 4, 5, 6, 0.5f);
Cary Clark9f67f042016-12-16 11:32:54 -05004275 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4276 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004277 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004278 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004279 p.setFillType(SkPath::kInverseEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004280 p.moveTo(1, 2);
4281 p.cubicTo(3, 4, 5, 6, 7, 8);
Cary Clark9f67f042016-12-16 11:32:54 -05004282 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4283 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004284 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4285 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004286 p.setFillType(SkPath::kWinding_FillType);
caryclarke9562592014-09-15 09:26:09 -07004287 p.moveTo(1, 2);
4288 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004289 compare_dump(reporter, p, false, true,
Cary Clark9f67f042016-12-16 11:32:54 -05004290 "path.setFillType(SkPath::kWinding_FillType);\n"
caryclark08fa28c2014-10-23 13:08:56 -07004291 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4292 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004293 p.reset();
4294 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4295 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Cary Clark9f67f042016-12-16 11:32:54 -05004296 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4297 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004298 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004299}
4300
senorblanco84cd6212015-08-04 10:01:58 -07004301namespace {
4302
4303class ChangeListener : public SkPathRef::GenIDChangeListener {
4304public:
4305 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4306 virtual ~ChangeListener() {}
4307 void onChange() override {
4308 *fChanged = true;
4309 }
4310private:
4311 bool* fChanged;
4312};
4313
4314}
4315
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004316class PathTest_Private {
4317public:
4318 static void TestPathTo(skiatest::Reporter* reporter) {
4319 SkPath p, q;
4320 p.lineTo(4, 4);
4321 p.reversePathTo(q);
4322 check_path_is_line(reporter, &p, 4, 4);
4323 q.moveTo(-4, -4);
4324 p.reversePathTo(q);
4325 check_path_is_line(reporter, &p, 4, 4);
4326 q.lineTo(7, 8);
4327 q.conicTo(8, 7, 6, 5, 0.5f);
4328 q.quadTo(6, 7, 8, 6);
4329 q.cubicTo(5, 6, 7, 8, 7, 5);
4330 q.close();
4331 p.reversePathTo(q);
4332 SkRect reverseExpected = {-4, -4, 8, 8};
4333 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4334 }
senorblanco84cd6212015-08-04 10:01:58 -07004335
4336 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4337 SkPath p;
4338
4339 bool changed = false;
4340 p.moveTo(0, 0);
4341
4342 // Check that listener is notified on moveTo().
4343
halcanary385fe4d2015-08-26 13:07:48 -07004344 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004345 REPORTER_ASSERT(reporter, !changed);
4346 p.moveTo(10, 0);
4347 REPORTER_ASSERT(reporter, changed);
4348
4349 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004350 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004351 REPORTER_ASSERT(reporter, !changed);
4352 p.lineTo(20, 0);
4353 REPORTER_ASSERT(reporter, changed);
4354
4355 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004356 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004357 REPORTER_ASSERT(reporter, !changed);
4358 p.reset();
4359 REPORTER_ASSERT(reporter, changed);
4360
4361 p.moveTo(0, 0);
4362
4363 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004364 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004365 REPORTER_ASSERT(reporter, !changed);
4366 p.rewind();
4367 REPORTER_ASSERT(reporter, changed);
4368
4369 // Check that listener is notified when pathref is deleted.
4370 {
4371 SkPath q;
4372 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004373 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004374 REPORTER_ASSERT(reporter, !changed);
4375 }
4376 // q went out of scope.
4377 REPORTER_ASSERT(reporter, changed);
4378 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004379};
4380
reed158fabb2016-07-20 10:06:59 -07004381static void test_crbug_629455(skiatest::Reporter* reporter) {
4382 SkPath path;
4383 path.moveTo(0, 0);
4384 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4385 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4386 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4387// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4388 path.lineTo(0, 0);
4389
4390 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4391 SkPaint paint;
4392 paint.setAntiAlias(true);
4393 surface->getCanvas()->drawPath(path, paint);
4394}
4395
liyuqianc78eff92016-11-09 11:18:30 -08004396static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4397 SkPath path;
4398 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4399 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4400 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4401 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4402 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4403 path.close();
4404
4405 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4406 SkPaint paint;
4407 paint.setAntiAlias(true);
4408 surface->getCanvas()->clipPath(path, true);
4409 surface->getCanvas()->drawRectCoords(0, 0, 100, 100, paint);
4410}
4411
Yuqian Lidb13a092016-11-29 10:29:22 -05004412static void test_path_crbugskia6003() {
4413 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4414 SkCanvas* canvas = surface->getCanvas();
4415 SkPaint paint;
4416 paint.setAntiAlias(true);
4417 SkPath path;
4418 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4419 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4420 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4421 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4422 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4423 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4424 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4425 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4426 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4427 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4428 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4429 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4430 path.close();
4431 canvas->clipPath(path, true);
4432 canvas->drawRectCoords(0, 0, 500, 500, paint);
4433}
4434
Yuqian Lic4f66af2016-11-11 09:36:53 -05004435static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4436 SkPath path;
4437 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4438 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4439 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4440 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4441 path.close();
4442
4443 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4444 SkPaint paint;
4445 paint.setAntiAlias(true);
4446 surface->getCanvas()->drawPath(path, paint);
4447}
4448
caryclark8e7b19d2016-02-18 04:11:48 -08004449static void test_interp(skiatest::Reporter* reporter) {
4450 SkPath p1, p2, out;
4451 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4452 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4453 REPORTER_ASSERT(reporter, p1 == out);
4454 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4455 REPORTER_ASSERT(reporter, p1 == out);
4456 p1.moveTo(0, 2);
4457 p1.lineTo(0, 4);
4458 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4459 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4460 p2.moveTo(6, 0);
4461 p2.lineTo(8, 0);
4462 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4463 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4464 REPORTER_ASSERT(reporter, p2 == out);
4465 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4466 REPORTER_ASSERT(reporter, p1 == out);
4467 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4468 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4469 p1.reset();
4470 p1.moveTo(4, 4);
4471 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4472 p2.reset();
4473 p2.moveTo(4, 2);
4474 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4475 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4476 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4477 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4478 p2.reset();
4479 p2.moveTo(4, 2);
4480 p2.conicTo(6, 3, 6, 5, 1);
4481 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4482 p2.reset();
4483 p2.moveTo(4, 4);
4484 p2.conicTo(5, 4, 5, 5, 0.5f);
4485 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4486}
4487
4488DEF_TEST(PathInterp, reporter) {
4489 test_interp(reporter);
4490}
4491
caryclark9aacd902015-12-14 08:38:09 -08004492DEF_TEST(PathContains, reporter) {
4493 test_contains(reporter);
4494}
4495
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004496DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004497 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004498 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004499 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004500 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004501 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004502 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004503 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004504 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004505 test_fuzz_crbug_662780();
Yuqian Li20079a92016-11-16 13:07:57 -05004506 test_mask_overflow();
Yuqian Lidb13a092016-11-29 10:29:22 -05004507 test_path_crbugskia6003();
Yuqian Li79252f72016-11-29 15:02:49 -05004508 test_fuzz_crbug_668907();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004509
bungeman@google.coma5809a32013-06-21 15:13:34 +00004510 SkTSize<SkScalar>::Make(3,4);
4511
4512 SkPath p, empty;
4513 SkRect bounds, bounds2;
4514 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004515
reed@android.comd252db02009-04-01 18:31:44 +00004516 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004517
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004518 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4519 SkPath& self = p;
4520 p = self;
4521
4522 // this triggers a code path in SkPath::swap which is otherwise unexercised
4523 p.swap(self);
4524
reed@android.com3abec1d2009-03-02 05:36:20 +00004525 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004526
reed@android.com6b82d1a2009-06-03 02:35:01 +00004527 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4528 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004529 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004530 REPORTER_ASSERT(reporter,
4531 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004532 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004533
reed@android.com6b82d1a2009-06-03 02:35:01 +00004534 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004535 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004536
reed@android.com6b82d1a2009-06-03 02:35:01 +00004537 p.addOval(bounds);
4538 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004539 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004540
bungeman@google.coma5809a32013-06-21 15:13:34 +00004541 p.rewind();
4542 test_empty(reporter, p);
4543
reed@android.com3abec1d2009-03-02 05:36:20 +00004544 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004545 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004546 // we have only lines
4547 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004548 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004549
bungeman@google.coma5809a32013-06-21 15:13:34 +00004550 REPORTER_ASSERT(reporter, p != empty);
4551 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004552
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004553 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004554 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4555 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004556 SkPoint pts[4];
4557 int count = p.getPoints(pts, 4);
4558 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004559 uint8_t verbs[6];
4560 verbs[5] = 0xff;
4561 p.getVerbs(verbs, 5);
4562 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4563 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4564 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4565 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4566 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4567 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004568 bounds2.set(pts, 4);
4569 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004570
reed@android.com3abec1d2009-03-02 05:36:20 +00004571 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4572 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004573 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004574
halcanary96fcdcc2015-08-27 07:41:13 -07004575 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004576 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004577 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4578 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004579
reed@android.com3abec1d2009-03-02 05:36:20 +00004580 // now force p to not be a rect
4581 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4582 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004583 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004584
robertphillipsfe7c4272014-12-29 11:36:39 -08004585 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4586 // path has a trailing moveTo. Please see crbug.com\445368)
4587 {
4588 SkRect r;
4589 p.reset();
4590 p.addRect(bounds);
4591 REPORTER_ASSERT(reporter, p.isRect(&r));
4592 REPORTER_ASSERT(reporter, r == bounds);
4593 // add a moveTo outside of our bounds
4594 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4595 REPORTER_ASSERT(reporter, p.isRect(&r));
4596 REPORTER_ASSERT(reporter, r == bounds);
4597 }
4598
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004599 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004600 test_isLine(reporter);
4601 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004602 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004603 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004604 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004605 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004606 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004607 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004608 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004609 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004610 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004611 test_flattening(reporter);
4612 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004613 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004614 test_iter(reporter);
4615 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004616 test_circle(reporter);
4617 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004618 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004619 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004620 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004621 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004622 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004623 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004624 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004625 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004626 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004627 test_tricky_cubic();
4628 test_clipped_cubic();
4629 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004630 test_crbug_493450(reporter);
4631 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004632 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004633 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004634 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004635 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004636 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004637 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004638 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004639 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004640 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004641 test_arcTo(reporter);
4642 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004643 test_addPathMode(reporter, false, false);
4644 test_addPathMode(reporter, true, false);
4645 test_addPathMode(reporter, false, true);
4646 test_addPathMode(reporter, true, true);
4647 test_extendClosedPath(reporter);
4648 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4649 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004650 test_conicTo_special_case(reporter);
4651 test_get_point(reporter);
4652 test_contains(reporter);
4653 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004654 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004655 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004656 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004657 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004658 test_path_crbugskia2820(reporter);
Yuqian Li9939bd62016-11-29 10:27:16 -05004659 test_path_crbugskia5995();
caryclark5ccef572015-03-02 10:07:56 -08004660 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004661 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004662 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004663 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004664}