blob: 0949a5d8a82301e4cdc2082ea020fc8f38fb1a41 [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);
Mike Reed3661bc92017-02-22 13:21:42 -0500198 canvas->drawRect(SkRect::MakeWH(250, 250), paint);
liyuqian041da382016-11-11 09:59:51 -0800199}
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());
Mike Reeddf85c382017-02-14 10:59:19 -05001742 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
Lee Salzmana19f0242017-01-12 13:06:21 -05001926 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
1927 // following the non-empty sub-path. Verifies that this does not trigger assertions.
1928 path.reset();
1929 path.moveTo(0, 0);
1930 path.lineTo(SkIntToScalar(100), 0);
1931 path.lineTo(0, SkIntToScalar(100));
1932 path.moveTo(100, 100);
1933
1934 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1935 SkIntToScalar(10),
1936 SkIntToScalar(10))));
1937
bsalomonb17c1292014-08-28 14:04:55 -07001938 // Test that multiple move commands do not cause asserts and that the function
1939 // is not confused by the multiple moves.
1940 path.reset();
1941 path.moveTo(0, 0);
1942 path.lineTo(SkIntToScalar(100), 0);
1943 path.lineTo(0, SkIntToScalar(100));
1944 path.moveTo(0, SkIntToScalar(200));
1945 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1946 path.lineTo(0, SkIntToScalar(300));
1947
1948 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1949 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1950 SkIntToScalar(10),
1951 SkIntToScalar(10))));
1952
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001953 path.reset();
1954 path.lineTo(100, 100);
1955 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001956}
1957
reed@google.comf32322b2013-10-16 15:14:04 +00001958static void test_isRect_open_close(skiatest::Reporter* reporter) {
1959 SkPath path;
1960 bool isClosed;
1961
1962 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001963 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001964
halcanary96fcdcc2015-08-27 07:41:13 -07001965 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001966 REPORTER_ASSERT(reporter, isClosed);
1967}
1968
caryclark@google.comf1316942011-07-26 19:54:45 +00001969// Simple isRect test is inline TestPath, below.
1970// test_isRect provides more extensive testing.
1971static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001972 test_isRect_open_close(reporter);
1973
caryclark@google.comf1316942011-07-26 19:54:45 +00001974 // passing tests (all moveTo / lineTo...
1975 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1976 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1977 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1978 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1979 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1980 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1981 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1982 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1983 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001984 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1985 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 +00001986 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1987 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1988 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001989 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001990
caryclark@google.comf1316942011-07-26 19:54:45 +00001991 // failing tests
1992 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1993 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1994 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1995 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1996 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1997 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1998 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1999 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00002000 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2001 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2002 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00002003
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002004 // no close, but we should detect them as fillably the same as a rect
2005 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2006 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2007 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2008
2009 // like c2, but we double-back on ourselves
2010 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2011 // like c2, but we overshoot the start point
2012 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2013 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00002014
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002015 struct IsRectTest {
2016 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002017 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002018 bool fClose;
2019 bool fIsRect;
2020 } tests[] = {
2021 { r1, SK_ARRAY_COUNT(r1), true, true },
2022 { r2, SK_ARRAY_COUNT(r2), true, true },
2023 { r3, SK_ARRAY_COUNT(r3), true, true },
2024 { r4, SK_ARRAY_COUNT(r4), true, true },
2025 { r5, SK_ARRAY_COUNT(r5), true, true },
2026 { r6, SK_ARRAY_COUNT(r6), true, true },
2027 { r7, SK_ARRAY_COUNT(r7), true, true },
2028 { r8, SK_ARRAY_COUNT(r8), true, true },
2029 { r9, SK_ARRAY_COUNT(r9), true, true },
2030 { ra, SK_ARRAY_COUNT(ra), true, true },
2031 { rb, SK_ARRAY_COUNT(rb), true, true },
2032 { rc, SK_ARRAY_COUNT(rc), true, true },
2033 { rd, SK_ARRAY_COUNT(rd), true, true },
2034 { re, SK_ARRAY_COUNT(re), true, true },
2035 { rf, SK_ARRAY_COUNT(rf), true, true },
2036
2037 { f1, SK_ARRAY_COUNT(f1), true, false },
2038 { f2, SK_ARRAY_COUNT(f2), true, false },
2039 { f3, SK_ARRAY_COUNT(f3), true, false },
2040 { f4, SK_ARRAY_COUNT(f4), true, false },
2041 { f5, SK_ARRAY_COUNT(f5), true, false },
2042 { f6, SK_ARRAY_COUNT(f6), true, false },
2043 { f7, SK_ARRAY_COUNT(f7), true, false },
2044 { f8, SK_ARRAY_COUNT(f8), true, false },
2045 { f9, SK_ARRAY_COUNT(f9), true, false },
2046 { fa, SK_ARRAY_COUNT(fa), true, false },
2047 { fb, SK_ARRAY_COUNT(fb), true, false },
2048
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002049 { c1, SK_ARRAY_COUNT(c1), false, true },
2050 { c2, SK_ARRAY_COUNT(c2), false, true },
2051 { c3, SK_ARRAY_COUNT(c3), false, true },
2052
2053 { d1, SK_ARRAY_COUNT(d1), false, false },
2054 { d2, SK_ARRAY_COUNT(d2), false, false },
2055 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00002056 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002057
2058 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002059 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002060 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2061 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002062 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2063 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2064 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002065 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002066 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002067 path.close();
2068 }
halcanary96fcdcc2015-08-27 07:41:13 -07002069 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002070
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002071 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002072 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002073 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002074 SkPath::Direction direction;
2075 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002076 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002077 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002078 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2079 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002080 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002081 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002082 } else {
2083 SkRect computed;
2084 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002085 bool isClosed = (bool)-1;
2086 SkPath::Direction direction = (SkPath::Direction) - 1;
2087 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002088 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2089 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002090 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2091 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002092 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002093 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002094
caryclark@google.comf1316942011-07-26 19:54:45 +00002095 // fail, close then line
2096 SkPath path1;
2097 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002098 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002099 path1.lineTo(r1[index].fX, r1[index].fY);
2100 }
2101 path1.close();
2102 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002103 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002104
caryclark@google.comf1316942011-07-26 19:54:45 +00002105 // fail, move in the middle
2106 path1.reset();
2107 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002108 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002109 if (index == 2) {
2110 path1.moveTo(1, .5f);
2111 }
2112 path1.lineTo(r1[index].fX, r1[index].fY);
2113 }
2114 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002115 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002116
2117 // fail, move on the edge
2118 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002119 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002120 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
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, quad
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.quadTo(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));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002137
caryclark@google.comf1316942011-07-26 19:54:45 +00002138 // fail, cubic
2139 path1.reset();
2140 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002141 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002142 if (index == 2) {
2143 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2144 }
2145 path1.lineTo(r1[index].fX, r1[index].fY);
2146 }
2147 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002148 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002149}
2150
bsalomonedc743a2016-06-01 09:42:31 -07002151static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2152 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2153 SkRect r = SkRect::MakeEmpty();
2154 SkPath::Direction d = SkPath::kCCW_Direction;
2155 unsigned s = ~0U;
2156
2157 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2158 REPORTER_ASSERT(reporter, r == rect);
2159 REPORTER_ASSERT(reporter, d == dir);
2160 REPORTER_ASSERT(reporter, s == start);
2161}
2162
2163static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2164 SkRect r = SkRect::MakeEmpty();
2165 SkPath::Direction d = SkPath::kCCW_Direction;
2166 unsigned s = ~0U;
2167
2168 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2169 const SkRect emptyRect = SkRect::MakeEmpty();
2170 SkPath path;
2171 for (int start = 0; start < 4; ++start) {
2172 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2173 SkPath path;
2174 path.addRect(testRect, dir, start);
2175 check_simple_closed_rect(reporter, path, testRect, dir, start);
2176 path.close();
2177 check_simple_closed_rect(reporter, path, testRect, dir, start);
2178 SkPath path2 = path;
2179 path2.lineTo(10, 10);
2180 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2181 path2 = path;
2182 path2.moveTo(10, 10);
2183 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2184 path2 = path;
2185 path2.addRect(testRect, dir, start);
2186 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2187 // Make the path by hand, manually closing it.
2188 path2.reset();
2189 SkPath::RawIter iter(path);
2190 SkPath::Verb v;
2191 SkPoint verbPts[4];
2192 SkPoint firstPt = {0.f, 0.f};
2193 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2194 switch(v) {
2195 case SkPath::kMove_Verb:
2196 firstPt = verbPts[0];
2197 path2.moveTo(verbPts[0]);
2198 break;
2199 case SkPath::kLine_Verb:
2200 path2.lineTo(verbPts[1]);
2201 break;
2202 default:
2203 break;
2204 }
2205 }
2206 // We haven't closed it yet...
2207 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2208 // ... now we do and test again.
2209 path2.lineTo(firstPt);
2210 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2211 // A redundant close shouldn't cause a failure.
2212 path2.close();
2213 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2214 // Degenerate point and line rects are not allowed
2215 path2.reset();
2216 path2.addRect(emptyRect, dir, start);
2217 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2218 SkRect degenRect = testRect;
2219 degenRect.fLeft = degenRect.fRight;
2220 path2.reset();
2221 path2.addRect(degenRect, dir, start);
2222 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2223 degenRect = testRect;
2224 degenRect.fTop = degenRect.fBottom;
2225 path2.reset();
2226 path2.addRect(degenRect, dir, start);
2227 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2228 // An inverted rect makes a rect path, but changes the winding dir and start point.
2229 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2230 ? SkPath::kCCW_Direction
2231 : SkPath::kCW_Direction;
2232 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2233 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2234 SkRect swapRect = testRect;
2235 SkTSwap(swapRect.fLeft, swapRect.fRight);
2236 path2.reset();
2237 path2.addRect(swapRect, dir, start);
2238 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2239 swapRect = testRect;
2240 SkTSwap(swapRect.fTop, swapRect.fBottom);
2241 path2.reset();
2242 path2.addRect(swapRect, dir, start);
2243 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2244 }
2245 }
bsalomon057ae8a2016-07-24 05:37:26 -07002246 // down, up, left, close
2247 path.reset();
2248 path.moveTo(1, 1);
2249 path.lineTo(1, 2);
2250 path.lineTo(1, 1);
2251 path.lineTo(0, 1);
2252 SkRect rect;
2253 SkPath::Direction dir;
2254 unsigned start;
2255 path.close();
2256 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2257 // right, left, up, close
2258 path.reset();
2259 path.moveTo(1, 1);
2260 path.lineTo(2, 1);
2261 path.lineTo(1, 1);
2262 path.lineTo(1, 0);
2263 path.close();
2264 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2265 // parallelogram with horizontal edges
2266 path.reset();
2267 path.moveTo(1, 0);
2268 path.lineTo(3, 0);
2269 path.lineTo(2, 1);
2270 path.lineTo(0, 1);
2271 path.close();
2272 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2273 // parallelogram with vertical edges
2274 path.reset();
2275 path.moveTo(0, 1);
2276 path.lineTo(0, 3);
2277 path.lineTo(1, 2);
2278 path.lineTo(1, 0);
2279 path.close();
2280 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2281
bsalomonedc743a2016-06-01 09:42:31 -07002282}
2283
caryclark95bc5f32015-04-08 08:34:15 -07002284static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002285 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002286 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002287 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2288 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2289 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002290 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002291 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2292 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2293 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2294 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002295 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2296 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 +00002297 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2298 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2299 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002300
2301 // failing tests
2302 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2303 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2304 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2305 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2306 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2307 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2308 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2309 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2310
caryclark95bc5f32015-04-08 08:34:15 -07002311 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002312 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2313 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2314
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002315 struct IsNestedRectTest {
2316 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002317 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002318 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002319 bool fClose;
2320 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2321 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002322 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2323 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2324 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2325 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2326 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2327 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2328 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2329 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2330 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2331 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2332 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2333 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2334 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2335 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002336
reed026beb52015-06-10 14:23:15 -07002337 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2338 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2339 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2340 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2341 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2342 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2343 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2344 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002345
reed026beb52015-06-10 14:23:15 -07002346 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2347 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002348 };
2349
2350 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002351 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002352 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002353 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2354 SkPath path;
2355 if (rectFirst) {
2356 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2357 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002358 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2359 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2360 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002361 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002362 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002363 path.close();
2364 }
2365 if (!rectFirst) {
2366 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2367 }
caryclark95bc5f32015-04-08 08:34:15 -07002368 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002369 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002370 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002371 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002372 SkPathPriv::FirstDirection expectedDirs[2];
2373 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002374 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002375 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002376 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2377 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002378 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002379 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002380 } else {
reed026beb52015-06-10 14:23:15 -07002381 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002382 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002383 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002384 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002385 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2386 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002387 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2388 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002389 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002390 }
2391
2392 // fail, close then line
2393 SkPath path1;
2394 if (rectFirst) {
2395 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2396 }
2397 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002398 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002399 path1.lineTo(r1[index].fX, r1[index].fY);
2400 }
2401 path1.close();
2402 path1.lineTo(1, 0);
2403 if (!rectFirst) {
2404 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2405 }
halcanary96fcdcc2015-08-27 07:41:13 -07002406 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002407
2408 // fail, move in the middle
2409 path1.reset();
2410 if (rectFirst) {
2411 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2412 }
2413 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002414 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002415 if (index == 2) {
2416 path1.moveTo(1, .5f);
2417 }
2418 path1.lineTo(r1[index].fX, r1[index].fY);
2419 }
2420 path1.close();
2421 if (!rectFirst) {
2422 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2423 }
halcanary96fcdcc2015-08-27 07:41:13 -07002424 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002425
2426 // fail, move on the edge
2427 path1.reset();
2428 if (rectFirst) {
2429 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2430 }
bsalomon98806072014-12-12 15:11:17 -08002431 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002432 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2433 path1.lineTo(r1[index].fX, r1[index].fY);
2434 }
2435 path1.close();
2436 if (!rectFirst) {
2437 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2438 }
halcanary96fcdcc2015-08-27 07:41:13 -07002439 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002440
2441 // fail, quad
2442 path1.reset();
2443 if (rectFirst) {
2444 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2445 }
2446 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002447 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002448 if (index == 2) {
2449 path1.quadTo(1, .5f, 1, .5f);
2450 }
2451 path1.lineTo(r1[index].fX, r1[index].fY);
2452 }
2453 path1.close();
2454 if (!rectFirst) {
2455 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2456 }
halcanary96fcdcc2015-08-27 07:41:13 -07002457 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002458
2459 // fail, cubic
2460 path1.reset();
2461 if (rectFirst) {
2462 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2463 }
2464 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002465 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002466 if (index == 2) {
2467 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2468 }
2469 path1.lineTo(r1[index].fX, r1[index].fY);
2470 }
2471 path1.close();
2472 if (!rectFirst) {
2473 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2474 }
halcanary96fcdcc2015-08-27 07:41:13 -07002475 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002476
caryclark@google.com56f233a2012-11-19 13:06:06 +00002477 // fail, not nested
2478 path1.reset();
2479 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2480 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002481 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002482 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002483
caryclark95bc5f32015-04-08 08:34:15 -07002484 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2485 SkPath path;
2486 path.moveTo(0, 0);
2487 path.lineTo(10, 0);
2488 path.lineTo(10, 10);
2489 path.lineTo(0, 10);
2490 path.lineTo(0, 0);
2491 path.moveTo(1, 1);
2492 path.lineTo(9, 1);
2493 path.lineTo(9, 9);
2494 path.lineTo(1, 9);
2495 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002496 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002497
caryclark@google.combfe90372012-11-21 13:56:20 +00002498 // pass, stroke rect
2499 SkPath src, dst;
2500 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2501 SkPaint strokePaint;
2502 strokePaint.setStyle(SkPaint::kStroke_Style);
2503 strokePaint.setStrokeWidth(2);
2504 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002505 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002506}
2507
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002508static void write_and_read_back(skiatest::Reporter* reporter,
2509 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002510 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002511 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002512 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002513 SkAutoMalloc storage(size);
2514 writer.flatten(storage.get());
2515 SkReader32 reader(storage.get(), size);
2516
2517 SkPath readBack;
2518 REPORTER_ASSERT(reporter, readBack != p);
2519 reader.readPath(&readBack);
2520 REPORTER_ASSERT(reporter, readBack == p);
2521
rmistry@google.comd6176b02012-08-23 18:14:13 +00002522 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002523 p.getConvexityOrUnknown());
2524
bsalomon78d58d12016-05-27 09:17:04 -07002525 SkRect oval0, oval1;
2526 SkPath::Direction dir0, dir1;
2527 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002528 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002529 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2530 REPORTER_ASSERT(reporter, oval0 == oval1);
2531 REPORTER_ASSERT(reporter, dir0 == dir1);
2532 REPORTER_ASSERT(reporter, start0 == start1);
2533 }
2534 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2535 SkRRect rrect0, rrect1;
2536 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2537 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2538 REPORTER_ASSERT(reporter, dir0 == dir1);
2539 REPORTER_ASSERT(reporter, start0 == start1);
2540 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002541 const SkRect& origBounds = p.getBounds();
2542 const SkRect& readBackBounds = readBack.getBounds();
2543
2544 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2545}
2546
Mike Reede3374d62017-01-03 13:58:21 -05002547static void test_corrupt_flattening(skiatest::Reporter* reporter) {
2548 SkPath path;
2549 path.moveTo(1, 2);
2550 path.lineTo(1, 2);
2551 path.quadTo(1, 2, 3, 4);
2552 path.conicTo(1, 2, 3, 4, 0.5f);
2553 path.cubicTo(1, 2, 3, 4, 5, 6);
2554 uint8_t buffer[1024];
2555 SkDEBUGCODE(size_t size =) path.writeToMemory(buffer);
2556 SkASSERT(size <= sizeof(buffer));
2557
2558 // find where the counts and verbs are stored : from the impl in SkPathRef.cpp
2559 int32_t* vCount = (int32_t*)&buffer[16];
2560 SkASSERT(*vCount == 5);
2561 int32_t* pCount = (int32_t*)&buffer[20];
2562 SkASSERT(*pCount == 9);
2563 int32_t* cCount = (int32_t*)&buffer[24];
2564 SkASSERT(*cCount == 1);
2565 uint8_t* verbs = &buffer[28];
2566
2567 REPORTER_ASSERT(reporter, path.readFromMemory(buffer, sizeof(buffer)));
2568
2569 // check that we detect under/over-flow of counts
2570
2571 *vCount += 1;
2572 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2573 *vCount -= 1; // restore
2574
2575 *pCount += 1;
2576 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2577 *pCount -= 2;
2578 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2579 *pCount += 1; // restore
2580
2581 *cCount += 1;
2582 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2583 *cCount -= 2;
2584 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2585 *cCount += 1; // restore
2586
2587 // Check that we detect when the verbs indicate more or fewer pts/conics
2588
2589 uint8_t save = verbs[0];
2590 SkASSERT(save == SkPath::kCubic_Verb);
2591 verbs[0] = SkPath::kQuad_Verb;
2592 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2593 verbs[0] = save;
2594
2595 save = verbs[1];
2596 SkASSERT(save == SkPath::kConic_Verb);
2597 verbs[1] = SkPath::kQuad_Verb;
2598 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2599 verbs[1] = SkPath::kCubic_Verb;
2600 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2601 verbs[1] = save;
2602
2603 // Check that we detect invalid verbs
2604 save = verbs[1];
2605 verbs[1] = 17;
2606 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2607 verbs[1] = save;
2608}
2609
reed@google.com53effc52011-09-21 19:05:12 +00002610static void test_flattening(skiatest::Reporter* reporter) {
2611 SkPath p;
2612
2613 static const SkPoint pts[] = {
2614 { 0, 0 },
2615 { SkIntToScalar(10), SkIntToScalar(10) },
2616 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2617 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2618 };
2619 p.moveTo(pts[0]);
2620 p.lineTo(pts[1]);
2621 p.quadTo(pts[2], pts[3]);
2622 p.cubicTo(pts[4], pts[5], pts[6]);
2623
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002624 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002625
2626 // create a buffer that should be much larger than the path so we don't
2627 // kill our stack if writer goes too far.
2628 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002629 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002630 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002631 REPORTER_ASSERT(reporter, size1 == size2);
2632
2633 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002634 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002635 REPORTER_ASSERT(reporter, size1 == size3);
2636 REPORTER_ASSERT(reporter, p == p2);
2637
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002638 size3 = p2.readFromMemory(buffer, 0);
2639 REPORTER_ASSERT(reporter, !size3);
2640
2641 SkPath tooShort;
2642 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2643 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2644
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002645 char buffer2[1024];
2646 size3 = p2.writeToMemory(buffer2);
2647 REPORTER_ASSERT(reporter, size1 == size3);
2648 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002649
2650 // test persistence of the oval flag & convexity
2651 {
2652 SkPath oval;
2653 SkRect rect = SkRect::MakeWH(10, 10);
2654 oval.addOval(rect);
2655
2656 write_and_read_back(reporter, oval);
2657 }
Mike Reede3374d62017-01-03 13:58:21 -05002658
2659 test_corrupt_flattening(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002660}
2661
2662static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002663 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002664
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002665#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002666 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002667 { 0, 0 }, // move
2668 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2669 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2670 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2671#if CONIC_PERSPECTIVE_BUG_FIXED
2672 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2673#endif
reed@google.com53effc52011-09-21 19:05:12 +00002674 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002675 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002676
reed@google.com53effc52011-09-21 19:05:12 +00002677 p.moveTo(pts[0]);
2678 p.lineTo(pts[1]);
2679 p.quadTo(pts[2], pts[3]);
2680 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002681#if CONIC_PERSPECTIVE_BUG_FIXED
2682 p.conicTo(pts[4], pts[5], 0.5f);
2683#endif
2684 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002685
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002686 {
2687 SkMatrix matrix;
2688 matrix.reset();
2689 SkPath p1;
2690 p.transform(matrix, &p1);
2691 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002692 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002693
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002694
2695 {
2696 SkMatrix matrix;
2697 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2698
2699 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2700
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002701 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002702 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002703 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002704 REPORTER_ASSERT(reporter, kPtCount == count);
2705 for (int i = 0; i < count; ++i) {
2706 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2707 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2708 }
2709 }
2710
2711 {
2712 SkMatrix matrix;
2713 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002714 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002715
2716 SkPath p1;
2717 p1.moveTo(SkPoint::Make(0, 0));
2718
2719 p.transform(matrix, &p1);
2720 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002721 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002722 SkRect pBounds = p.getBounds();
2723 SkRect p1Bounds = p1.getBounds();
2724 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2725 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2726 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2727 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2728 }
2729
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002730 p.reset();
2731 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002732
2733 {
2734 SkMatrix matrix;
2735 matrix.reset();
2736 SkPath p1;
2737 p1.moveTo(SkPoint::Make(0, 0));
2738
2739 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002740 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002741 }
2742
2743
2744 {
2745 SkMatrix matrix;
2746 matrix.reset();
2747 matrix.setScaleX(-1);
2748 SkPath p1;
2749 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2750
2751 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002752 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002753 }
2754
2755 {
2756 SkMatrix matrix;
2757 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2758 SkPath p1;
2759 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2760
2761 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002762 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002763 }
reed@google.com53effc52011-09-21 19:05:12 +00002764}
2765
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002766static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002767 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002768 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002769
caryclark@google.com56f233a2012-11-19 13:06:06 +00002770 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002771 const char* testPath;
2772 const size_t numResultPts;
2773 const SkRect resultBound;
2774 const SkPath::Verb* resultVerbs;
2775 const size_t numResultVerbs;
2776 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002777
schenney@chromium.org7e963602012-06-13 17:05:43 +00002778 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2779 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2780 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2781 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2782 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2783 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2784 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2785 static const SkPath::Verb resultVerbs8[] = {
2786 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2787 };
2788 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2789 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2790 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2791 static const SkPath::Verb resultVerbs12[] = {
2792 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2793 };
2794 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2795 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2796 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2797 static const SkPath::Verb resultVerbs16[] = {
2798 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2799 };
2800 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002801 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002802 { "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 -07002803 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002804 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2805 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2806 { "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) },
2807 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2808 { "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) },
2809 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2810 { "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) },
2811 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2812 { "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) },
2813 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2814 { "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 +00002815 SK_ARRAY_COUNT(resultVerbs14)
2816 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002817 { "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) },
2818 { "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 +00002819 SK_ARRAY_COUNT(resultVerbs16)
2820 }
2821 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002822
schenney@chromium.org7e963602012-06-13 17:05:43 +00002823 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2824 p.reset();
2825 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2826 REPORTER_ASSERT(reporter, valid);
2827 REPORTER_ASSERT(reporter, !p.isEmpty());
2828 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2829 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2830 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2831 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2832 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2833 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002834 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002835}
2836
2837struct SegmentInfo {
2838 SkPath fPath;
2839 int fPointCount;
2840};
2841
reed@google.com10296cc2011-09-21 12:29:05 +00002842#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2843
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002844static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002845 SkPath p, p2;
2846
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002847 p.moveTo(0, 0);
2848 p.quadTo(100, 100, 200, 200);
2849 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2850 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002851 p2 = p;
2852 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002853 p.cubicTo(100, 100, 200, 200, 300, 300);
2854 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2855 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002856 p2 = p;
2857 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2858
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002859 p.reset();
2860 p.moveTo(0, 0);
2861 p.cubicTo(100, 100, 200, 200, 300, 300);
2862 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002863 p2 = p;
2864 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002865
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002866 REPORTER_ASSERT(reporter, !p.isEmpty());
2867}
2868
2869static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002870 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002871 SkPoint pts[4];
2872
2873 // Test an iterator with no path
2874 SkPath::Iter noPathIter;
2875 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002876
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002877 // Test that setting an empty path works
2878 noPathIter.setPath(p, false);
2879 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002880
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002881 // Test that close path makes no difference for an empty path
2882 noPathIter.setPath(p, true);
2883 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002884
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002885 // Test an iterator with an initial empty path
2886 SkPath::Iter iter(p, false);
2887 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2888
2889 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002890 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002891 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2892
rmistry@google.comd6176b02012-08-23 18:14:13 +00002893
schenney@chromium.org7e963602012-06-13 17:05:43 +00002894 struct iterTestData {
2895 const char* testPath;
2896 const bool forceClose;
2897 const bool consumeDegenerates;
2898 const size_t* numResultPtsPerVerb;
2899 const SkPoint* resultPts;
2900 const SkPath::Verb* resultVerbs;
2901 const size_t numResultVerbs;
2902 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002903
schenney@chromium.org7e963602012-06-13 17:05:43 +00002904 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2905 static const SkPath::Verb resultVerbs2[] = {
2906 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2907 };
2908 static const SkPath::Verb resultVerbs3[] = {
2909 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2910 };
2911 static const SkPath::Verb resultVerbs4[] = {
2912 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2913 };
2914 static const SkPath::Verb resultVerbs5[] = {
2915 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2916 };
2917 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002918 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2919 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2920 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2921 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002922 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002923 static const SkPoint resultPts2[] = {
2924 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2925 };
2926 static const SkPoint resultPts3[] = {
2927 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2928 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2929 };
2930 static const SkPoint resultPts4[] = {
2931 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2932 };
2933 static const SkPoint resultPts5[] = {
2934 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2935 };
2936 static const struct iterTestData gIterTests[] = {
2937 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002938 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2939 { "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 +00002940 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2941 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2942 { "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) },
2943 { "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 +00002944 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2945 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2946 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2947 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2948 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2949 { "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 +00002950 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002951
schenney@chromium.org7e963602012-06-13 17:05:43 +00002952 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2953 p.reset();
2954 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2955 REPORTER_ASSERT(reporter, valid);
2956 iter.setPath(p, gIterTests[i].forceClose);
2957 int j = 0, l = 0;
2958 do {
2959 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2960 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2961 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2962 }
2963 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2964 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2965 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002966
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002967 p.reset();
2968 iter.setPath(p, false);
2969 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2970 p.lineTo(1, 1);
2971 p.close();
2972 iter.setPath(p, false);
2973 REPORTER_ASSERT(reporter, iter.isClosedContour());
2974 p.reset();
2975 iter.setPath(p, true);
2976 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2977 p.lineTo(1, 1);
2978 iter.setPath(p, true);
2979 REPORTER_ASSERT(reporter, iter.isClosedContour());
2980 p.moveTo(0, 0);
2981 p.lineTo(2, 2);
2982 iter.setPath(p, false);
2983 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2984
2985 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2986 // check to see if the result is correct.
2987 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2988 p.reset();
2989 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2990 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2991 iter.setPath(p, true);
2992 iter.next(pts, false);
2993 iter.next(pts, false);
2994 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2995 }
2996
2997 p.reset();
2998 p.quadTo(0, 0, 0, 0);
2999 iter.setPath(p, false);
3000 iter.next(pts, false);
3001 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
3002 iter.setPath(p, false);
3003 iter.next(pts, false);
3004 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3005
3006 p.reset();
3007 p.conicTo(0, 0, 0, 0, 0.5f);
3008 iter.setPath(p, false);
3009 iter.next(pts, false);
3010 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
3011 iter.setPath(p, false);
3012 iter.next(pts, false);
3013 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3014
3015 p.reset();
3016 p.cubicTo(0, 0, 0, 0, 0, 0);
3017 iter.setPath(p, false);
3018 iter.next(pts, false);
3019 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3020 iter.setPath(p, false);
3021 iter.next(pts, false);
3022 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3023
3024 p.moveTo(1, 1); // add a trailing moveto
3025 iter.setPath(p, false);
3026 iter.next(pts, false);
3027 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3028 iter.setPath(p, false);
3029 iter.next(pts, false);
3030 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3031
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003032 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08003033
3034 // Test out mixed degenerate and non-degenerate geometry with Conics
3035 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3036 SkRect r = SkRect::MakeWH(100, 100);
3037 SkRRect rr;
3038 rr.setRectRadii(r, radii);
3039 p.reset();
3040 p.addRRect(rr);
3041 iter.setPath(p, false);
3042 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3043 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3044 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3045 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3046 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003047}
3048
3049static void test_raw_iter(skiatest::Reporter* reporter) {
3050 SkPath p;
3051 SkPoint pts[4];
3052
3053 // Test an iterator with no path
3054 SkPath::RawIter noPathIter;
3055 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3056 // Test that setting an empty path works
3057 noPathIter.setPath(p);
3058 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00003059
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003060 // Test an iterator with an initial empty path
3061 SkPath::RawIter iter(p);
3062 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3063
3064 // Test that a move-only path returns the move.
3065 p.moveTo(SK_Scalar1, 0);
3066 iter.setPath(p);
3067 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3068 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3069 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3070 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3071
3072 // No matter how many moves we add, we should get them all back
3073 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3074 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3075 iter.setPath(p);
3076 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3077 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3078 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3079 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3080 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3081 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3082 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3083 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3084 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3085 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3086
3087 // Initial close is never ever stored
3088 p.reset();
3089 p.close();
3090 iter.setPath(p);
3091 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3092
3093 // Move/close sequences
3094 p.reset();
3095 p.close(); // Not stored, no purpose
3096 p.moveTo(SK_Scalar1, 0);
3097 p.close();
3098 p.close(); // Not stored, no purpose
3099 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3100 p.close();
3101 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3102 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3103 p.close();
3104 iter.setPath(p);
3105 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3106 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3107 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3108 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003109 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3110 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3111 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3112 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003113 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3114 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3115 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3116 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3117 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3118 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3119 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003120 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3121
3122 // Generate random paths and verify
3123 SkPoint randomPts[25];
3124 for (int i = 0; i < 5; ++i) {
3125 for (int j = 0; j < 5; ++j) {
3126 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3127 }
3128 }
3129
3130 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003131 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003132 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003133 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003134 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003135
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003136 for (int i = 0; i < 500; ++i) {
3137 p.reset();
3138 bool lastWasClose = true;
3139 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003140 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003141 int numPoints = 0;
3142 int numVerbs = (rand.nextU() >> 16) % 10;
3143 int numIterVerbs = 0;
3144 for (int j = 0; j < numVerbs; ++j) {
3145 do {
3146 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3147 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003148 switch (nextVerb) {
3149 case SkPath::kMove_Verb:
3150 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3151 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003152 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003153 numPoints += 1;
3154 lastWasClose = false;
3155 haveMoveTo = true;
3156 break;
3157 case SkPath::kLine_Verb:
3158 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003159 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003160 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3161 haveMoveTo = true;
3162 }
3163 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3164 p.lineTo(expectedPts[numPoints]);
3165 numPoints += 1;
3166 lastWasClose = false;
3167 break;
3168 case SkPath::kQuad_Verb:
3169 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003170 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003171 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.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3177 numPoints += 2;
3178 lastWasClose = false;
3179 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003180 case SkPath::kConic_Verb:
3181 if (!haveMoveTo) {
3182 expectedPts[numPoints++] = lastMoveToPt;
3183 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3184 haveMoveTo = true;
3185 }
3186 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3187 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3188 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3189 rand.nextUScalar1() * 4);
3190 numPoints += 2;
3191 lastWasClose = false;
3192 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003193 case SkPath::kCubic_Verb:
3194 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003195 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003196 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3197 haveMoveTo = true;
3198 }
3199 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3200 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3201 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3202 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3203 expectedPts[numPoints + 2]);
3204 numPoints += 3;
3205 lastWasClose = false;
3206 break;
3207 case SkPath::kClose_Verb:
3208 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003209 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003210 lastWasClose = true;
3211 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003212 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003213 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003214 }
3215 expectedVerbs[numIterVerbs++] = nextVerb;
3216 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003217
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003218 iter.setPath(p);
3219 numVerbs = numIterVerbs;
3220 numIterVerbs = 0;
3221 int numIterPts = 0;
3222 SkPoint lastMoveTo;
3223 SkPoint lastPt;
3224 lastMoveTo.set(0, 0);
3225 lastPt.set(0, 0);
3226 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3227 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3228 numIterVerbs++;
3229 switch (nextVerb) {
3230 case SkPath::kMove_Verb:
3231 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3232 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3233 lastPt = lastMoveTo = pts[0];
3234 numIterPts += 1;
3235 break;
3236 case SkPath::kLine_Verb:
3237 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3238 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3239 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3240 lastPt = pts[1];
3241 numIterPts += 1;
3242 break;
3243 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003244 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003245 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3246 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3247 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3248 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3249 lastPt = pts[2];
3250 numIterPts += 2;
3251 break;
3252 case SkPath::kCubic_Verb:
3253 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3254 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3255 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3256 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3257 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3258 lastPt = pts[3];
3259 numIterPts += 3;
3260 break;
3261 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003262 lastPt = lastMoveTo;
3263 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003264 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003265 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003266 }
3267 }
3268 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3269 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3270 }
3271}
3272
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003273static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003274 const SkPath& path,
3275 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003276 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003277 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003278 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003279 SkPath::Direction isOvalDir;
3280 unsigned isOvalStart;
3281 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003282 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003283 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3284 SkPath tmpPath;
3285 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3286 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003287 }
bsalomon78d58d12016-05-27 09:17:04 -07003288 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003289}
3290
3291static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003292 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003293 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003294 SkPath tmp;
3295
3296 SkMatrix m;
3297 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3298 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003299 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003300 if (SkPathPriv::kCCW_FirstDirection == dir) {
3301 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003302 } else {
reed026beb52015-06-10 14:23:15 -07003303 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3304 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003305 }
3306 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003307}
3308
3309static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003310 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003311 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003312 SkPath tmp;
3313
3314 // translate at small offset
3315 SkMatrix m;
3316 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3317 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003318 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003319
3320 tmp.reset();
3321 m.reset();
3322
3323 // translate at a relatively big offset
3324 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3325 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003326 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003327}
3328
3329static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003330 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003331 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003332 for (int angle = 0; angle < 360; ++angle) {
3333 SkPath tmp;
3334 SkMatrix m;
3335 m.setRotate(SkIntToScalar(angle));
3336 path.transform(m, &tmp);
3337
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003338 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003339 // degrees is not an oval anymore, this can be improved. we made this
3340 // for the simplicity of our implementation.
3341 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003342 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003343 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003344 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003345 }
3346 }
3347}
3348
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003349static void test_circle_mirror_x(skiatest::Reporter* reporter,
3350 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003351 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003352 SkPath tmp;
3353 SkMatrix m;
3354 m.reset();
3355 m.setScaleX(-SK_Scalar1);
3356 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003357 if (SkPathPriv::kCW_FirstDirection == dir) {
3358 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003359 } else {
reed026beb52015-06-10 14:23:15 -07003360 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3361 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003362 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003363 check_for_circle(reporter, tmp, true, dir);
3364}
3365
3366static void test_circle_mirror_y(skiatest::Reporter* reporter,
3367 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003368 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003369 SkPath tmp;
3370 SkMatrix m;
3371 m.reset();
3372 m.setScaleY(-SK_Scalar1);
3373 path.transform(m, &tmp);
3374
reed026beb52015-06-10 14:23:15 -07003375 if (SkPathPriv::kCW_FirstDirection == dir) {
3376 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003377 } else {
reed026beb52015-06-10 14:23:15 -07003378 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3379 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003380 }
3381
3382 check_for_circle(reporter, tmp, true, dir);
3383}
3384
3385static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3386 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003387 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003388 SkPath tmp;
3389 SkMatrix m;
3390 m.reset();
3391 m.setScaleX(-SK_Scalar1);
3392 m.setScaleY(-SK_Scalar1);
3393 path.transform(m, &tmp);
3394
3395 check_for_circle(reporter, tmp, true, dir);
3396}
3397
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003398static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003399 SkPath::Direction inDir) {
3400 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003401 SkPath path;
3402
3403 // circle at origin
reed026beb52015-06-10 14:23:15 -07003404 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3405
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003406 check_for_circle(reporter, path, true, dir);
3407 test_circle_rotate(reporter, path, dir);
3408 test_circle_translate(reporter, path, dir);
3409 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003410 test_circle_mirror_x(reporter, path, dir);
3411 test_circle_mirror_y(reporter, path, dir);
3412 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003413
3414 // circle at an offset at (10, 10)
3415 path.reset();
3416 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003417 SkIntToScalar(20), inDir);
3418
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003419 check_for_circle(reporter, path, true, dir);
3420 test_circle_rotate(reporter, path, dir);
3421 test_circle_translate(reporter, path, dir);
3422 test_circle_skew(reporter, path, dir);
3423 test_circle_mirror_x(reporter, path, dir);
3424 test_circle_mirror_y(reporter, path, dir);
3425 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003426
3427 // Try different starting points for the contour.
3428 for (unsigned start = 0; start < 4; ++start) {
3429 path.reset();
3430 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3431 test_circle_rotate(reporter, path, dir);
3432 test_circle_translate(reporter, path, dir);
3433 test_circle_skew(reporter, path, dir);
3434 test_circle_mirror_x(reporter, path, dir);
3435 test_circle_mirror_y(reporter, path, dir);
3436 test_circle_mirror_xy(reporter, path, dir);
3437 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003438}
3439
3440static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3441 SkPath path;
3442 SkPath circle;
3443 SkPath rect;
3444 SkPath empty;
3445
reed026beb52015-06-10 14:23:15 -07003446 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3447 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003448
3449 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003450 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3451 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3452
3453 SkMatrix translate;
3454 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3455
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003456 // Although all the path concatenation related operations leave
3457 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003458
3459 // empty + circle (translate)
3460 path = empty;
3461 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003462 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003463
3464 // circle + empty (translate)
3465 path = circle;
3466 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003467
reed026beb52015-06-10 14:23:15 -07003468 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003469
3470 // test reverseAddPath
3471 path = circle;
3472 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003473 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003474}
3475
3476static void test_circle(skiatest::Reporter* reporter) {
3477 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3478 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3479
3480 // multiple addCircle()
3481 SkPath path;
3482 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3483 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
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 // some extra lineTo() would make isOval() fail
3487 path.reset();
3488 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3489 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003490 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003491
3492 // not back to the original point
3493 path.reset();
3494 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3495 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003496 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003497
3498 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003499
3500 // test negative radius
3501 path.reset();
3502 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3503 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003504}
3505
3506static void test_oval(skiatest::Reporter* reporter) {
3507 SkRect rect;
3508 SkMatrix m;
3509 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003510 unsigned start = 0;
3511 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003512
3513 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3514 path.addOval(rect);
3515
bsalomon78d58d12016-05-27 09:17:04 -07003516 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003517 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003518
3519 m.setRotate(SkIntToScalar(90));
3520 SkPath tmp;
3521 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003522 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3523 // is unchanged.
3524 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3525 REPORTER_ASSERT(reporter, 2 == start);
3526 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003527
3528 m.reset();
3529 m.setRotate(SkIntToScalar(30));
3530 tmp.reset();
3531 path.transform(m, &tmp);
3532 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003533 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003534
3535 // since empty path being transformed.
3536 path.reset();
3537 tmp.reset();
3538 m.reset();
3539 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003540 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003541
3542 // empty path is not an oval
3543 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003544 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003545
3546 // only has moveTo()s
3547 tmp.reset();
3548 tmp.moveTo(0, 0);
3549 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003550 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003551
3552 // mimic WebKit's calling convention,
3553 // call moveTo() first and then call addOval()
3554 path.reset();
3555 path.moveTo(0, 0);
3556 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003557 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003558
3559 // copy path
3560 path.reset();
3561 tmp.reset();
3562 tmp.addOval(rect);
3563 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003564 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3565 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3566 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003567}
3568
bungeman@google.coma5809a32013-06-21 15:13:34 +00003569static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3570 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003571
reed@android.com3abec1d2009-03-02 05:36:20 +00003572 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003573 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003574 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003575 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003576 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003577 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3578 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003579 REPORTER_ASSERT(reporter, p == empty);
3580 REPORTER_ASSERT(reporter, !(p != empty));
3581}
3582
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003583static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3584 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003585 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003586 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003587 path->setConvexity(SkPath::kUnknown_Convexity);
3588 REPORTER_ASSERT(reporter, path->isConvex());
3589 path->reset();
3590}
3591
caryclarkd3d1a982014-12-08 04:57:38 -08003592static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3593 SkPath::Direction dir) {
3594 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003595 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003596 path->setConvexity(SkPath::kUnknown_Convexity);
3597 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3598 path->reset();
3599}
3600
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003601static void test_rrect(skiatest::Reporter* reporter) {
3602 SkPath p;
3603 SkRRect rr;
3604 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3605 SkRect r = {10, 20, 30, 40};
3606 rr.setRectRadii(r, radii);
3607 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003608 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003609 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003610 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003611 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003612 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003613 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003614 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003615 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003616 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003617 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003618 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3619 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3620 SkVector save = radii[i];
3621 radii[i].set(0, 0);
3622 rr.setRectRadii(r, radii);
3623 p.addRRect(rr);
3624 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3625 radii[i] = save;
3626 }
3627 p.addRoundRect(r, 0, 0);
3628 SkRect returnedRect;
3629 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3630 REPORTER_ASSERT(reporter, returnedRect == r);
3631 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3632 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3633 rr.setRectRadii(r, zeroRadii);
3634 p.addRRect(rr);
3635 bool closed;
3636 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003637 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003638 REPORTER_ASSERT(reporter, closed);
3639 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3640 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3641 p.addRRect(rr, SkPath::kCW_Direction);
3642 p.addRRect(rr, SkPath::kCW_Direction);
3643 REPORTER_ASSERT(reporter, !p.isConvex());
3644 p.reset();
3645 p.addRRect(rr, SkPath::kCCW_Direction);
3646 p.addRRect(rr, SkPath::kCCW_Direction);
3647 REPORTER_ASSERT(reporter, !p.isConvex());
3648 p.reset();
3649 SkRect emptyR = {10, 20, 10, 30};
3650 rr.setRectRadii(emptyR, radii);
3651 p.addRRect(rr);
3652 REPORTER_ASSERT(reporter, p.isEmpty());
3653 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3654 rr.setRectRadii(largeR, radii);
3655 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003656 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003657
3658 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003659 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3660 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003661 REPORTER_ASSERT(reporter, rr.isEmpty());
3662
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003663 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3664 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3665 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003666}
3667
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003668static void test_arc(skiatest::Reporter* reporter) {
3669 SkPath p;
3670 SkRect emptyOval = {10, 20, 30, 20};
3671 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3672 p.addArc(emptyOval, 1, 2);
3673 REPORTER_ASSERT(reporter, p.isEmpty());
3674 p.reset();
3675 SkRect oval = {10, 20, 30, 40};
3676 p.addArc(oval, 1, 0);
3677 REPORTER_ASSERT(reporter, p.isEmpty());
3678 p.reset();
3679 SkPath cwOval;
3680 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003681 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003682 REPORTER_ASSERT(reporter, p == cwOval);
3683 p.reset();
3684 SkPath ccwOval;
3685 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003686 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003687 REPORTER_ASSERT(reporter, p == ccwOval);
3688 p.reset();
3689 p.addArc(oval, 1, 180);
3690 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003691 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003692 p.setConvexity(SkPath::kUnknown_Convexity);
3693 REPORTER_ASSERT(reporter, p.isConvex());
3694}
3695
bsalomon1978ce22016-05-31 10:42:16 -07003696static inline SkScalar oval_start_index_to_angle(unsigned start) {
3697 switch (start) {
3698 case 0:
3699 return 270.f;
3700 case 1:
3701 return 0.f;
3702 case 2:
3703 return 90.f;
3704 case 3:
3705 return 180.f;
3706 default:
3707 return -1.f;
3708 }
3709}
3710
3711static inline SkScalar canonical_start_angle(float angle) {
3712 while (angle < 0.f) {
3713 angle += 360.f;
3714 }
3715 while (angle >= 360.f) {
3716 angle -= 360.f;
3717 }
3718 return angle;
3719}
3720
3721static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3722 const SkPath& path) {
3723 SkRect r = SkRect::MakeEmpty();
3724 SkPath::Direction d = SkPath::kCCW_Direction;
3725 unsigned s = ~0U;
3726 bool isOval = path.isOval(&r, &d, &s);
3727 REPORTER_ASSERT(reporter, isOval);
3728 SkPath recreatedPath;
3729 recreatedPath.addOval(r, d, s);
3730 REPORTER_ASSERT(reporter, path == recreatedPath);
3731 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3732 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3733}
3734
3735static void test_arc_ovals(skiatest::Reporter* reporter) {
3736 SkRect oval = SkRect::MakeWH(10, 20);
3737 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3738 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3739 SkPath path;
3740 path.addArc(oval, start, sweep);
3741 // SkPath's interfaces for inserting and extracting ovals only allow contours
3742 // to start at multiples of 90 degrees.
3743 if (std::fmod(start, 90.f) == 0) {
3744 check_oval_arc(reporter, start, sweep, path);
3745 } else {
3746 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3747 }
3748 }
3749 // Test start angles that are nearly at valid oval start angles.
3750 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3751 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3752 SkPath path;
3753 path.addArc(oval, start + delta, sweep);
3754 check_oval_arc(reporter, start, sweep, path);
3755 }
3756 }
3757 }
3758}
3759
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003760static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3761 SkScalar x0, SkScalar y0) {
3762 SkPoint pts[4];
3763 SkPath::Verb v = iter->next(pts);
3764 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3765 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3766 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3767}
3768
3769static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3770 SkScalar x1, SkScalar y1) {
3771 SkPoint pts[4];
3772 SkPath::Verb v = iter->next(pts);
3773 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3774 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3775 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3776}
3777
3778static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3779 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3780 SkPoint pts[4];
3781 SkPath::Verb v = iter->next(pts);
3782 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3783 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3784 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3785 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3786 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3787}
3788
3789static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3790 SkPoint pts[4];
3791 SkPath::Verb v = iter->next(pts);
3792 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3793}
3794
3795static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3796 check_done(reporter, p, iter);
3797 p->reset();
3798}
3799
3800static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3801 SkScalar x0, SkScalar y0) {
3802 SkPath::RawIter iter(*p);
3803 check_move(reporter, &iter, x0, y0);
3804 check_done_and_reset(reporter, p, &iter);
3805}
3806
3807static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3808 SkScalar x1, SkScalar y1) {
3809 SkPath::RawIter iter(*p);
3810 check_move(reporter, &iter, 0, 0);
3811 check_line(reporter, &iter, x1, y1);
3812 check_done_and_reset(reporter, p, &iter);
3813}
3814
3815static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3816 SkScalar x1, SkScalar y1) {
3817 SkPath::RawIter iter(*p);
3818 check_move(reporter, &iter, 0, 0);
3819 check_line(reporter, &iter, x1, y1);
3820 check_done(reporter, p, &iter);
3821}
3822
3823static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3824 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3825 SkPath::RawIter iter(*p);
3826 check_move(reporter, &iter, 0, 0);
3827 check_line(reporter, &iter, x1, y1);
3828 check_line(reporter, &iter, x2, y2);
3829 check_done_and_reset(reporter, p, &iter);
3830}
3831
3832static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3833 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3834 SkPath::RawIter iter(*p);
3835 check_move(reporter, &iter, 0, 0);
3836 check_quad(reporter, &iter, x1, y1, x2, y2);
3837 check_done_and_reset(reporter, p, &iter);
3838}
3839
reedd5d27d92015-02-09 13:54:43 -08003840static bool nearly_equal(const SkRect& a, const SkRect& b) {
3841 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3842 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3843 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3844 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3845}
3846
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003847static void test_arcTo(skiatest::Reporter* reporter) {
3848 SkPath p;
3849 p.arcTo(0, 0, 1, 2, 1);
3850 check_path_is_line_and_reset(reporter, &p, 0, 0);
3851 p.arcTo(1, 2, 1, 2, 1);
3852 check_path_is_line_and_reset(reporter, &p, 1, 2);
3853 p.arcTo(1, 2, 3, 4, 0);
3854 check_path_is_line_and_reset(reporter, &p, 1, 2);
3855 p.arcTo(1, 2, 0, 0, 1);
3856 check_path_is_line_and_reset(reporter, &p, 1, 2);
3857 p.arcTo(1, 0, 1, 1, 1);
3858 SkPoint pt;
3859 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3860 p.reset();
3861 p.arcTo(1, 0, 1, -1, 1);
3862 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3863 p.reset();
3864 SkRect oval = {1, 2, 3, 4};
3865 p.arcTo(oval, 0, 0, true);
3866 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3867 p.arcTo(oval, 0, 0, false);
3868 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3869 p.arcTo(oval, 360, 0, true);
3870 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3871 p.arcTo(oval, 360, 0, false);
3872 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003873
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003874 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003875 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003876 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003877 sweep += delta;
3878 delta /= 2;
3879 }
3880 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003881 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003882 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003883 sweep -= delta;
3884 delta /= 2;
3885 }
3886 SkRect noOvalWidth = {1, 2, 0, 3};
3887 p.reset();
3888 p.arcTo(noOvalWidth, 0, 360, false);
3889 REPORTER_ASSERT(reporter, p.isEmpty());
3890
3891 SkRect noOvalHeight = {1, 2, 3, 1};
3892 p.reset();
3893 p.arcTo(noOvalHeight, 0, 360, false);
3894 REPORTER_ASSERT(reporter, p.isEmpty());
3895}
3896
3897static void test_addPath(skiatest::Reporter* reporter) {
3898 SkPath p, q;
3899 p.lineTo(1, 2);
3900 q.moveTo(4, 4);
3901 q.lineTo(7, 8);
3902 q.conicTo(8, 7, 6, 5, 0.5f);
3903 q.quadTo(6, 7, 8, 6);
3904 q.cubicTo(5, 6, 7, 8, 7, 5);
3905 q.close();
3906 p.addPath(q, -4, -4);
3907 SkRect expected = {0, 0, 4, 4};
3908 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3909 p.reset();
3910 p.reverseAddPath(q);
3911 SkRect reverseExpected = {4, 4, 8, 8};
3912 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3913}
3914
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003915static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3916 SkPath p, q;
3917 if (explicitMoveTo) {
3918 p.moveTo(1, 1);
3919 }
3920 p.lineTo(1, 2);
3921 if (explicitMoveTo) {
3922 q.moveTo(2, 1);
3923 }
3924 q.lineTo(2, 2);
3925 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3926 uint8_t verbs[4];
3927 int verbcount = p.getVerbs(verbs, 4);
3928 REPORTER_ASSERT(reporter, verbcount == 4);
3929 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3930 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3931 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3932 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3933}
3934
3935static void test_extendClosedPath(skiatest::Reporter* reporter) {
3936 SkPath p, q;
3937 p.moveTo(1, 1);
3938 p.lineTo(1, 2);
3939 p.lineTo(2, 2);
3940 p.close();
3941 q.moveTo(2, 1);
3942 q.lineTo(2, 3);
3943 p.addPath(q, SkPath::kExtend_AddPathMode);
3944 uint8_t verbs[7];
3945 int verbcount = p.getVerbs(verbs, 7);
3946 REPORTER_ASSERT(reporter, verbcount == 7);
3947 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3948 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3949 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3950 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3951 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3952 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3953 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3954
3955 SkPoint pt;
3956 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3957 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3958 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3959}
3960
3961static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3962 SkPath p, q, r;
3963 // case 1: dst is empty
3964 p.moveTo(2, 1);
3965 p.lineTo(2, 3);
3966 q.addPath(p, mode);
3967 REPORTER_ASSERT(reporter, q == p);
3968 // case 2: src is empty
3969 p.addPath(r, mode);
3970 REPORTER_ASSERT(reporter, q == p);
3971 // case 3: src and dst are empty
3972 q.reset();
3973 q.addPath(r, mode);
3974 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003975}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003976
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003977static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3978 SkPath p;
3979 p.conicTo(1, 2, 3, 4, -1);
3980 check_path_is_line_and_reset(reporter, &p, 3, 4);
3981 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3982 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3983 p.conicTo(1, 2, 3, 4, 1);
3984 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3985}
3986
3987static void test_get_point(skiatest::Reporter* reporter) {
3988 SkPath p;
3989 SkPoint pt = p.getPoint(0);
3990 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003991 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003992 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3993 p.setLastPt(10, 10);
3994 pt = p.getPoint(0);
3995 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003996 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003997 p.rMoveTo(10, 10);
3998 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3999}
4000
4001static void test_contains(skiatest::Reporter* reporter) {
4002 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08004003 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4004 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
4005 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
4006 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4007 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
4008 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 -08004009 // this may return true or false, depending on the platform's numerics, but it should not crash
4010 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08004011
4012 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004013 p.setFillType(SkPath::kInverseWinding_FillType);
4014 REPORTER_ASSERT(reporter, p.contains(0, 0));
4015 p.setFillType(SkPath::kWinding_FillType);
4016 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4017 p.moveTo(4, 4);
4018 p.lineTo(6, 8);
4019 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08004020 // test on edge
4021 REPORTER_ASSERT(reporter, p.contains(6, 4));
4022 REPORTER_ASSERT(reporter, p.contains(5, 6));
4023 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004024 // test quick reject
4025 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4026 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4027 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4028 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4029 // test various crossings in x
4030 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4031 REPORTER_ASSERT(reporter, p.contains(6, 7));
4032 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4033 p.reset();
4034 p.moveTo(4, 4);
4035 p.lineTo(8, 6);
4036 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08004037 // test on edge
4038 REPORTER_ASSERT(reporter, p.contains(4, 6));
4039 REPORTER_ASSERT(reporter, p.contains(6, 5));
4040 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004041 // test various crossings in y
4042 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4043 REPORTER_ASSERT(reporter, p.contains(7, 6));
4044 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08004045 p.reset();
4046 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08004047 p.lineTo(8, 4);
4048 p.lineTo(8, 8);
4049 p.lineTo(4, 8);
4050 // test on vertices
4051 REPORTER_ASSERT(reporter, p.contains(4, 4));
4052 REPORTER_ASSERT(reporter, p.contains(8, 4));
4053 REPORTER_ASSERT(reporter, p.contains(8, 8));
4054 REPORTER_ASSERT(reporter, p.contains(4, 8));
4055 p.reset();
4056 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08004057 p.lineTo(6, 8);
4058 p.lineTo(2, 8);
4059 // test on edge
4060 REPORTER_ASSERT(reporter, p.contains(5, 6));
4061 REPORTER_ASSERT(reporter, p.contains(4, 8));
4062 REPORTER_ASSERT(reporter, p.contains(3, 6));
4063 p.reset();
4064 p.moveTo(4, 4);
4065 p.lineTo(0, 6);
4066 p.lineTo(4, 8);
4067 // test on edge
4068 REPORTER_ASSERT(reporter, p.contains(2, 5));
4069 REPORTER_ASSERT(reporter, p.contains(2, 7));
4070 REPORTER_ASSERT(reporter, p.contains(4, 6));
4071 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4072 p.reset();
4073 p.moveTo(4, 0);
4074 p.lineTo(6, 4);
4075 p.lineTo(2, 4);
4076 p.moveTo(4, 0);
4077 p.lineTo(0, 8);
4078 p.lineTo(8, 8);
4079 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4080 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4081 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4082 REPORTER_ASSERT(reporter, p.contains(4, 4));
4083
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004084 // test quads
4085 p.reset();
4086 p.moveTo(4, 4);
4087 p.quadTo(6, 6, 8, 8);
4088 p.quadTo(6, 8, 4, 8);
4089 p.quadTo(4, 6, 4, 4);
4090 REPORTER_ASSERT(reporter, p.contains(5, 6));
4091 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004092 // test quad edge
4093 REPORTER_ASSERT(reporter, p.contains(5, 5));
4094 REPORTER_ASSERT(reporter, p.contains(5, 8));
4095 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08004096 // test quad endpoints
4097 REPORTER_ASSERT(reporter, p.contains(4, 4));
4098 REPORTER_ASSERT(reporter, p.contains(8, 8));
4099 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004100
4101 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004102 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4103 p.moveTo(qPts[0]);
4104 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4105 p.quadTo(qPts[index], qPts[index + 1]);
4106 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004107 REPORTER_ASSERT(reporter, p.contains(5, 6));
4108 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004109 // test quad edge
4110 SkPoint halfway;
4111 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4112 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4113 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4114 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004115
caryclark9aacd902015-12-14 08:38:09 -08004116 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004117 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004118 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4119 p.moveTo(kPts[0]);
4120 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4121 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4122 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004123 REPORTER_ASSERT(reporter, p.contains(5, 6));
4124 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004125 // test conic edge
4126 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4127 SkConic conic(&kPts[index], 0.5f);
4128 halfway = conic.evalAt(0.5f);
4129 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4130 }
caryclark9cb5d752015-12-18 04:35:24 -08004131 // test conic end points
4132 REPORTER_ASSERT(reporter, p.contains(4, 4));
4133 REPORTER_ASSERT(reporter, p.contains(8, 8));
4134 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004135
4136 // test cubics
4137 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4138 for (int i = 0; i < 3; ++i) {
4139 p.reset();
4140 p.setFillType(SkPath::kEvenOdd_FillType);
4141 p.moveTo(pts[i].fX, pts[i].fY);
4142 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);
4143 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);
4144 p.close();
4145 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4146 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004147 // test cubic edge
4148 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4149 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4150 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4151 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004152 // test cubic end points
4153 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4154 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4155 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004156 }
4157}
4158
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004159class PathRefTest_Private {
4160public:
4161 static void TestPathRef(skiatest::Reporter* reporter) {
4162 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004163
bungeman6bd52842016-10-27 09:30:08 -07004164 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004165
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004166 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004167
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004168 {
4169 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4170 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4171 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4172 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4173 for (int i = 0; i < kRepeatCnt; ++i) {
4174 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4175 }
4176 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004177 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004178
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004179 {
4180 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4181 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4182 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4183 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4184 for (int i = 0; i < kRepeatCnt; ++i) {
4185 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4186 }
4187 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004188 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004189
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004190 {
4191 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4192 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4193 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4194 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4195 for (int i = 0; i < kRepeatCnt; ++i) {
4196 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4197 }
4198 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004199 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004200
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004201 {
halcanary96fcdcc2015-08-27 07:41:13 -07004202 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004203 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4204 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4205 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4206 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4207 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004208 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004209 for (int i = 0; i < kRepeatCnt; ++i) {
4210 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4211 }
4212 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004213 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004214
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004215 {
4216 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4217 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4218 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4219 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4220 for (int i = 0; i < kRepeatCnt; ++i) {
4221 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4222 }
4223 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004224 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004225 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004226};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004227
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004228static void test_operatorEqual(skiatest::Reporter* reporter) {
4229 SkPath a;
4230 SkPath b;
4231 REPORTER_ASSERT(reporter, a == a);
4232 REPORTER_ASSERT(reporter, a == b);
4233 a.setFillType(SkPath::kInverseWinding_FillType);
4234 REPORTER_ASSERT(reporter, a != b);
4235 a.reset();
4236 REPORTER_ASSERT(reporter, a == b);
4237 a.lineTo(1, 1);
4238 REPORTER_ASSERT(reporter, a != b);
4239 a.reset();
4240 REPORTER_ASSERT(reporter, a == b);
4241 a.lineTo(1, 1);
4242 b.lineTo(1, 2);
4243 REPORTER_ASSERT(reporter, a != b);
4244 a.reset();
4245 a.lineTo(1, 2);
4246 REPORTER_ASSERT(reporter, a == b);
4247}
4248
caryclark66a5d8b2014-06-24 08:30:15 -07004249static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004250 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004251 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004252 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004253 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004254 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004255 if (strlen(str) > 0) {
4256 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4257 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004258 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004259 }
caryclark66a5d8b2014-06-24 08:30:15 -07004260}
4261
4262static void test_dump(skiatest::Reporter* reporter) {
4263 SkPath p;
Cary Clark9f67f042016-12-16 11:32:54 -05004264 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4265 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004266 p.moveTo(1, 2);
4267 p.lineTo(3, 4);
Cary Clark9f67f042016-12-16 11:32:54 -05004268 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4269 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004270 "path.lineTo(3, 4);\n");
Cary Clark9f67f042016-12-16 11:32:54 -05004271 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4272 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004273 "path.lineTo(3, 4);\n"
4274 "path.lineTo(1, 2);\n"
4275 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004276 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004277 p.setFillType(SkPath::kEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004278 p.moveTo(1, 2);
4279 p.quadTo(3, 4, 5, 6);
Cary Clark9f67f042016-12-16 11:32:54 -05004280 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4281 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004282 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004283 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004284 p.setFillType(SkPath::kInverseWinding_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004285 p.moveTo(1, 2);
4286 p.conicTo(3, 4, 5, 6, 0.5f);
Cary Clark9f67f042016-12-16 11:32:54 -05004287 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4288 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004289 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004290 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004291 p.setFillType(SkPath::kInverseEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004292 p.moveTo(1, 2);
4293 p.cubicTo(3, 4, 5, 6, 7, 8);
Cary Clark9f67f042016-12-16 11:32:54 -05004294 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4295 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004296 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4297 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004298 p.setFillType(SkPath::kWinding_FillType);
caryclarke9562592014-09-15 09:26:09 -07004299 p.moveTo(1, 2);
4300 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004301 compare_dump(reporter, p, false, true,
Cary Clark9f67f042016-12-16 11:32:54 -05004302 "path.setFillType(SkPath::kWinding_FillType);\n"
caryclark08fa28c2014-10-23 13:08:56 -07004303 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4304 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004305 p.reset();
4306 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4307 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Cary Clark9f67f042016-12-16 11:32:54 -05004308 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4309 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004310 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004311}
4312
senorblanco84cd6212015-08-04 10:01:58 -07004313namespace {
4314
4315class ChangeListener : public SkPathRef::GenIDChangeListener {
4316public:
4317 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4318 virtual ~ChangeListener() {}
4319 void onChange() override {
4320 *fChanged = true;
4321 }
4322private:
4323 bool* fChanged;
4324};
4325
4326}
4327
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004328class PathTest_Private {
4329public:
4330 static void TestPathTo(skiatest::Reporter* reporter) {
4331 SkPath p, q;
4332 p.lineTo(4, 4);
4333 p.reversePathTo(q);
4334 check_path_is_line(reporter, &p, 4, 4);
4335 q.moveTo(-4, -4);
4336 p.reversePathTo(q);
4337 check_path_is_line(reporter, &p, 4, 4);
4338 q.lineTo(7, 8);
4339 q.conicTo(8, 7, 6, 5, 0.5f);
4340 q.quadTo(6, 7, 8, 6);
4341 q.cubicTo(5, 6, 7, 8, 7, 5);
4342 q.close();
4343 p.reversePathTo(q);
4344 SkRect reverseExpected = {-4, -4, 8, 8};
4345 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4346 }
senorblanco84cd6212015-08-04 10:01:58 -07004347
4348 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4349 SkPath p;
4350
4351 bool changed = false;
4352 p.moveTo(0, 0);
4353
4354 // Check that listener is notified on moveTo().
4355
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.moveTo(10, 0);
4359 REPORTER_ASSERT(reporter, changed);
4360
4361 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004362 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004363 REPORTER_ASSERT(reporter, !changed);
4364 p.lineTo(20, 0);
4365 REPORTER_ASSERT(reporter, changed);
4366
4367 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004368 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004369 REPORTER_ASSERT(reporter, !changed);
4370 p.reset();
4371 REPORTER_ASSERT(reporter, changed);
4372
4373 p.moveTo(0, 0);
4374
4375 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004376 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004377 REPORTER_ASSERT(reporter, !changed);
4378 p.rewind();
4379 REPORTER_ASSERT(reporter, changed);
4380
4381 // Check that listener is notified when pathref is deleted.
4382 {
4383 SkPath q;
4384 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004385 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004386 REPORTER_ASSERT(reporter, !changed);
4387 }
4388 // q went out of scope.
4389 REPORTER_ASSERT(reporter, changed);
4390 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004391};
4392
reed158fabb2016-07-20 10:06:59 -07004393static void test_crbug_629455(skiatest::Reporter* reporter) {
4394 SkPath path;
4395 path.moveTo(0, 0);
4396 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4397 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4398 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4399// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4400 path.lineTo(0, 0);
4401
4402 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4403 SkPaint paint;
4404 paint.setAntiAlias(true);
4405 surface->getCanvas()->drawPath(path, paint);
4406}
4407
liyuqianc78eff92016-11-09 11:18:30 -08004408static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4409 SkPath path;
4410 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4411 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4412 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4413 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4414 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4415 path.close();
4416
4417 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4418 SkPaint paint;
4419 paint.setAntiAlias(true);
4420 surface->getCanvas()->clipPath(path, true);
Mike Reed3661bc92017-02-22 13:21:42 -05004421 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
liyuqianc78eff92016-11-09 11:18:30 -08004422}
4423
Yuqian Lidb13a092016-11-29 10:29:22 -05004424static void test_path_crbugskia6003() {
4425 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4426 SkCanvas* canvas = surface->getCanvas();
4427 SkPaint paint;
4428 paint.setAntiAlias(true);
4429 SkPath path;
4430 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4431 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4432 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4433 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4434 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4435 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4436 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4437 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4438 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4439 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4440 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4441 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4442 path.close();
4443 canvas->clipPath(path, true);
Mike Reed3661bc92017-02-22 13:21:42 -05004444 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
Yuqian Lidb13a092016-11-29 10:29:22 -05004445}
4446
Yuqian Lic4f66af2016-11-11 09:36:53 -05004447static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4448 SkPath path;
4449 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4450 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4451 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4452 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4453 path.close();
4454
4455 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4456 SkPaint paint;
4457 paint.setAntiAlias(true);
4458 surface->getCanvas()->drawPath(path, paint);
4459}
4460
caryclark8e7b19d2016-02-18 04:11:48 -08004461static void test_interp(skiatest::Reporter* reporter) {
4462 SkPath p1, p2, out;
4463 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4464 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4465 REPORTER_ASSERT(reporter, p1 == out);
4466 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4467 REPORTER_ASSERT(reporter, p1 == out);
4468 p1.moveTo(0, 2);
4469 p1.lineTo(0, 4);
4470 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4471 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4472 p2.moveTo(6, 0);
4473 p2.lineTo(8, 0);
4474 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4475 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4476 REPORTER_ASSERT(reporter, p2 == out);
4477 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4478 REPORTER_ASSERT(reporter, p1 == out);
4479 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4480 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4481 p1.reset();
4482 p1.moveTo(4, 4);
4483 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4484 p2.reset();
4485 p2.moveTo(4, 2);
4486 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4487 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4488 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4489 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4490 p2.reset();
4491 p2.moveTo(4, 2);
4492 p2.conicTo(6, 3, 6, 5, 1);
4493 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4494 p2.reset();
4495 p2.moveTo(4, 4);
4496 p2.conicTo(5, 4, 5, 5, 0.5f);
4497 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4498}
4499
4500DEF_TEST(PathInterp, reporter) {
4501 test_interp(reporter);
4502}
4503
Mike Reed0c0da2b2017-01-26 11:03:50 -05004504#include "SkSurface.h"
4505DEF_TEST(PathBigCubic, reporter) {
4506 SkPath path;
4507 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4508 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4509 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), SkBits2Float(0x00000100), SkBits2Float(0x00000000), SkBits2Float(0x00000100), SkBits2Float(0x00000000)); // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4510 path.moveTo(0, 512);
4511
4512 // this call should not assert
Mike Reedc121a882017-01-27 11:59:07 -05004513 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
Mike Reed0c0da2b2017-01-26 11:03:50 -05004514}
4515
caryclark9aacd902015-12-14 08:38:09 -08004516DEF_TEST(PathContains, reporter) {
4517 test_contains(reporter);
4518}
4519
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004520DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004521 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004522 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004523 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004524 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004525 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004526 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004527 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004528 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004529 test_fuzz_crbug_662780();
Yuqian Li20079a92016-11-16 13:07:57 -05004530 test_mask_overflow();
Yuqian Lidb13a092016-11-29 10:29:22 -05004531 test_path_crbugskia6003();
Yuqian Li79252f72016-11-29 15:02:49 -05004532 test_fuzz_crbug_668907();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004533
bungeman@google.coma5809a32013-06-21 15:13:34 +00004534 SkTSize<SkScalar>::Make(3,4);
4535
4536 SkPath p, empty;
4537 SkRect bounds, bounds2;
4538 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004539
reed@android.comd252db02009-04-01 18:31:44 +00004540 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004541
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004542 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4543 SkPath& self = p;
4544 p = self;
4545
4546 // this triggers a code path in SkPath::swap which is otherwise unexercised
4547 p.swap(self);
4548
reed@android.com3abec1d2009-03-02 05:36:20 +00004549 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004550
reed@android.com6b82d1a2009-06-03 02:35:01 +00004551 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4552 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004553 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004554 REPORTER_ASSERT(reporter,
4555 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004556 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004557
reed@android.com6b82d1a2009-06-03 02:35:01 +00004558 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004559 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004560
reed@android.com6b82d1a2009-06-03 02:35:01 +00004561 p.addOval(bounds);
4562 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004563 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004564
bungeman@google.coma5809a32013-06-21 15:13:34 +00004565 p.rewind();
4566 test_empty(reporter, p);
4567
reed@android.com3abec1d2009-03-02 05:36:20 +00004568 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004569 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004570 // we have only lines
4571 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004572 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004573
bungeman@google.coma5809a32013-06-21 15:13:34 +00004574 REPORTER_ASSERT(reporter, p != empty);
4575 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004576
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004577 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004578 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4579 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004580 SkPoint pts[4];
4581 int count = p.getPoints(pts, 4);
4582 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004583 uint8_t verbs[6];
4584 verbs[5] = 0xff;
4585 p.getVerbs(verbs, 5);
4586 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4587 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4588 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4589 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4590 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4591 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004592 bounds2.set(pts, 4);
4593 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004594
reed@android.com3abec1d2009-03-02 05:36:20 +00004595 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4596 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004597 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004598
halcanary96fcdcc2015-08-27 07:41:13 -07004599 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004600 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004601 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4602 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004603
reed@android.com3abec1d2009-03-02 05:36:20 +00004604 // now force p to not be a rect
4605 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4606 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004607 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004608
robertphillipsfe7c4272014-12-29 11:36:39 -08004609 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4610 // path has a trailing moveTo. Please see crbug.com\445368)
4611 {
4612 SkRect r;
4613 p.reset();
4614 p.addRect(bounds);
4615 REPORTER_ASSERT(reporter, p.isRect(&r));
4616 REPORTER_ASSERT(reporter, r == bounds);
4617 // add a moveTo outside of our bounds
4618 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4619 REPORTER_ASSERT(reporter, p.isRect(&r));
4620 REPORTER_ASSERT(reporter, r == bounds);
4621 }
4622
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004623 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004624 test_isLine(reporter);
4625 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004626 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004627 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004628 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004629 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004630 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004631 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004632 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004633 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004634 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004635 test_flattening(reporter);
4636 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004637 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004638 test_iter(reporter);
4639 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004640 test_circle(reporter);
4641 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004642 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004643 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004644 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004645 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004646 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004647 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004648 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004649 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004650 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004651 test_tricky_cubic();
4652 test_clipped_cubic();
4653 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004654 test_crbug_493450(reporter);
4655 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004656 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004657 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004658 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004659 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004660 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004661 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004662 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004663 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004664 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004665 test_arcTo(reporter);
4666 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004667 test_addPathMode(reporter, false, false);
4668 test_addPathMode(reporter, true, false);
4669 test_addPathMode(reporter, false, true);
4670 test_addPathMode(reporter, true, true);
4671 test_extendClosedPath(reporter);
4672 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4673 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004674 test_conicTo_special_case(reporter);
4675 test_get_point(reporter);
4676 test_contains(reporter);
4677 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004678 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004679 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004680 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004681 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004682 test_path_crbugskia2820(reporter);
Yuqian Li9939bd62016-11-29 10:27:16 -05004683 test_path_crbugskia5995();
caryclark5ccef572015-03-02 10:07:56 -08004684 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004685 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004686 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004687 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004688}
Mike Reed7717d4a2017-01-24 10:12:53 -05004689
4690DEF_TEST(conservatively_contains_rect, reporter) {
4691 SkPath path;
4692
4693 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4694 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4695 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4696 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4697 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4698 path.moveTo(0, 0);
4699
4700 // this guy should not assert
4701 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4702}
Mike Reed0d7dac82017-02-02 17:45:56 -08004703
4704///////////////////////////////////////////////////////////////////////////////////////////////////
4705
4706static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4707 for (int i = 0; i < n; ++i) {
4708 switch (verb) {
4709 case SkPath::kLine_Verb:
4710 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4711 break;
4712 case SkPath::kQuad_Verb:
4713 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4714 rand.nextF()*100, rand.nextF()*100);
4715 break;
4716 case SkPath::kConic_Verb:
4717 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4718 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4719 break;
4720 case SkPath::kCubic_Verb:
4721 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4722 rand.nextF()*100, rand.nextF()*100,
4723 rand.nextF()*100, rand.nextF()*100);
4724 break;
4725 default:
4726 SkASSERT(false);
4727 }
4728 }
4729}
4730
4731#include "SkPathOps.h"
4732DEF_TEST(path_tight_bounds, reporter) {
4733 SkRandom rand;
4734
4735 const SkPath::Verb verbs[] = {
4736 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4737 };
4738 for (int i = 0; i < 1000; ++i) {
4739 for (int n = 1; n <= 10; n += 9) {
4740 for (SkPath::Verb verb : verbs) {
4741 SkPath path;
4742 rand_path(&path, rand, verb, n);
4743 SkRect bounds = path.getBounds();
Mike Reed8d3196b2017-02-03 11:34:13 -05004744 SkRect tight = path.computeTightBounds();
Mike Reed0d7dac82017-02-02 17:45:56 -08004745 REPORTER_ASSERT(reporter, bounds.contains(tight));
4746
4747 SkRect tight2;
4748 TightBounds(path, &tight2);
4749 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
4750 }
4751 }
4752 }
4753}