blob: 9a44d2932d2d37b752696eb6a106e44d8faa701f [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
bsalomon1978ce22016-05-31 10:42:16 -07008#include <cmath>
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"
reed026beb52015-06-10 14:23:15 -070014#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000015#include "SkPathEffect.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 "SkTypes.h"
24#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000026
reed5bcbe912014-12-15 12:28:33 -080027static void set_radii(SkVector radii[4], int index, float rad) {
28 sk_bzero(radii, sizeof(SkVector) * 4);
29 radii[index].set(rad, rad);
30}
31
32static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33 const SkVector radii[4]) {
34 SkRRect rrect;
35 rrect.setRectRadii(bounds, radii);
36 REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38 SkPath path;
39 // this line should not assert in the debug build (from validate)
40 path.addRRect(rrect);
41 REPORTER_ASSERT(reporter, bounds == path.getBounds());
42}
43
caryclark5ccef572015-03-02 10:07:56 -080044static void test_skbug_3469(skiatest::Reporter* reporter) {
45 SkPath path;
46 path.moveTo(20, 20);
47 path.quadTo(20, 50, 80, 50);
48 path.quadTo(20, 50, 20, 80);
49 REPORTER_ASSERT(reporter, !path.isConvex());
50}
51
reed5bcbe912014-12-15 12:28:33 -080052static void test_skbug_3239(skiatest::Reporter* reporter) {
53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
56
57 const float rad = 33436320;
58
59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62 SkVector radii[4];
63 for (int i = 0; i < 4; ++i) {
64 set_radii(radii, i, rad);
65 test_add_rrect(reporter, rectx, radii);
66 test_add_rrect(reporter, recty, radii);
67 }
68}
69
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000070static void make_path_crbug364224(SkPath* path) {
71 path->reset();
72 path->moveTo(3.747501373f, 2.724499941f);
73 path->lineTo(3.747501373f, 3.75f);
74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75 path->lineTo(0.7475013733f, 4.0f);
76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77 path->lineTo(0.4975013733f, 1.0f);
78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79 path->lineTo(3.497501373f, 0.75f);
80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81 path->lineTo(3.715001345f, 0.5512499809f);
82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83 path->lineTo(0.7475013733f, 0.4999999702f);
84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85 path->lineTo(0.2475013733f, 3.75f);
86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87 path->lineTo(3.497501373f, 4.25f);
88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89 path->lineTo(3.997501373f, 2.474750042f);
90 path->lineTo(3.747501373f, 2.724499941f);
91 path->close();
92}
93
94static void make_path_crbug364224_simplified(SkPath* path) {
95 path->moveTo(3.747501373f, 2.724499941f);
96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97 path->close();
98}
99
reedff863bc2016-08-17 07:37:57 -0700100static void test_sect_with_horizontal_needs_pinning() {
101 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
102 // intersection.
103 SkPath path;
104 path.reset();
105 path.moveTo(-540000, -720000);
106 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
107 path.lineTo(1, 1);
108
109 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
110 SkPaint paint;
111 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
112}
113
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000114static void test_path_crbug364224() {
115 SkPath path;
116 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700117 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000118 SkCanvas* canvas = surface->getCanvas();
119
120 make_path_crbug364224_simplified(&path);
121 canvas->drawPath(path, paint);
122
123 make_path_crbug364224(&path);
124 canvas->drawPath(path, paint);
125}
126
caryclark3cebe242016-08-23 09:41:00 -0700127// this is a unit test instead of a GM because it doesn't draw anything
128static void test_fuzz_crbug_638223() {
129 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
130 SkCanvas* canvas = surface->getCanvas();
131 SkPath path;
132 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
133 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
134 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
135 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
caryclarke97fe832016-09-06 08:54:10 -0700141static void test_fuzz_crbug_643933() {
142 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
143 SkCanvas* canvas = surface->getCanvas();
144 SkPaint paint;
145 paint.setAntiAlias(true);
146 SkPath path;
147 path.moveTo(0, 0);
148 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
149 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
150 SkBits2Float(0x721aee0c)); // 3.0687e+30f
151 canvas->drawPath(path, paint);
152 path.reset();
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
155 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
156 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
157 canvas->drawPath(path, paint);
158}
159
caryclarkbac10462016-09-22 10:24:59 -0700160static void test_fuzz_crbug_647922() {
161 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
162 SkCanvas* canvas = surface->getCanvas();
163 SkPaint paint;
164 paint.setAntiAlias(true);
165 SkPath path;
166 path.moveTo(0, 0);
167 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
168 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
169 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
170 canvas->drawPath(path, paint);
171}
172
liyuqian041da382016-11-11 09:59:51 -0800173static void test_fuzz_crbug_662780() {
174 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
175 SkCanvas* canvas = surface->getCanvas();
176 SkPaint paint;
177 paint.setAntiAlias(true);
178 SkPath path;
179 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
180 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
181 // 8, 8, 8.00002f, 8, 0.707107f
182 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
183 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
184 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
185 // 308, 8, 308, 8, 0.707107f
186 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
187 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
188 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
189 // 308, 158, 308, 158, 0.707107f
190 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
191 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
192 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
193 // 8, 158, 8, 158, 0.707107f
194 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
195 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
196 path.close();
197 canvas->clipPath(path, true);
198 canvas->drawRectCoords(0, 0, 250, 250, paint);
199}
200
Yuqian Li20079a92016-11-16 13:07:57 -0500201static void test_mask_overflow() {
202 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
203 SkCanvas* canvas = surface->getCanvas();
204 SkPaint paint;
205 paint.setAntiAlias(true);
206 SkPath path;
207 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
208 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
209 // 440.47f, 341, 437, 344.47f, 437, 348.75f
210 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
211 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
212 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
213 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
214 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
215 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
216 canvas->drawPath(path, paint);
217}
218
Yuqian Li79252f72016-11-29 15:02:49 -0500219static void test_fuzz_crbug_668907() {
220 auto surface(SkSurface::MakeRasterN32Premul(400, 500));
221 SkCanvas* canvas = surface->getCanvas();
222 SkPaint paint;
223 paint.setAntiAlias(true);
224 SkPath path;
225 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
226 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
227 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
228 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
229 path.close();
230 canvas->drawPath(path, paint);
231}
232
piotaixrfac4e0e2014-08-26 11:59:04 -0700233/**
234 * In debug mode, this path was causing an assertion to fail in
235 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
236 */
237static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
238 SkPoint orig, p1, p2, p3;
239 orig = SkPoint::Make(1.f, 1.f);
240 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
241 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
242 p3 = SkPoint::Make(2.f, 2.f);
243
244 path->reset();
245 path->moveTo(orig);
246 path->cubicTo(p1, p2, p3);
247 path->close();
248}
249
250static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
251 SkPath path;
252 make_path_crbugskia2820(&path, reporter);
253
254 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
255 stroke.setStrokeStyle(2 * SK_Scalar1);
256 stroke.applyToPath(&path, path);
257}
258
Yuqian Li9939bd62016-11-29 10:27:16 -0500259static void test_path_crbugskia5995() {
260 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
261 SkCanvas* canvas = surface->getCanvas();
262 SkPaint paint;
263 paint.setAntiAlias(true);
264 SkPath path;
265 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
266 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
267 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
268 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
269 canvas->drawPath(path, paint);
270}
271
reed@google.comcc8be772013-10-15 15:35:29 +0000272static void make_path0(SkPath* path) {
273 // from * https://code.google.com/p/skia/issues/detail?id=1706
274
275 path->moveTo(146.939f, 1012.84f);
276 path->lineTo(181.747f, 1009.18f);
277 path->lineTo(182.165f, 1013.16f);
278 path->lineTo(147.357f, 1016.82f);
279 path->lineTo(146.939f, 1012.84f);
280 path->close();
281}
282
283static void make_path1(SkPath* path) {
284 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
285}
286
287typedef void (*PathProc)(SkPath*);
288
289/*
290 * Regression test: we used to crash (overwrite internal storage) during
291 * construction of the region when the path was INVERSE. That is now fixed,
292 * so test these regions (which used to assert/crash).
293 *
294 * https://code.google.com/p/skia/issues/detail?id=1706
295 */
296static void test_path_to_region(skiatest::Reporter* reporter) {
297 PathProc procs[] = {
298 make_path0,
299 make_path1,
300 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000301
reed@google.comcc8be772013-10-15 15:35:29 +0000302 SkRegion clip;
303 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000304
reed@google.comcc8be772013-10-15 15:35:29 +0000305 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
306 SkPath path;
307 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000308
reed@google.comcc8be772013-10-15 15:35:29 +0000309 SkRegion rgn;
310 rgn.setPath(path, clip);
311 path.toggleInverseFillType();
312 rgn.setPath(path, clip);
313 }
314}
315
halcanary4dbbd042016-06-07 17:21:10 -0700316#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000317 #define SUPPRESS_VISIBILITY_WARNING
318#else
319 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
320#endif
321
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000322static void test_path_close_issue1474(skiatest::Reporter* reporter) {
323 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
324 // are relative to the point we close to, not relative to the point we close from.
325 SkPath path;
326 SkPoint last;
327
328 // Test rLineTo().
329 path.rLineTo(0, 100);
330 path.rLineTo(100, 0);
331 path.close(); // Returns us back to 0,0.
332 path.rLineTo(50, 50); // This should go to 50,50.
333
334 path.getLastPt(&last);
335 REPORTER_ASSERT(reporter, 50 == last.fX);
336 REPORTER_ASSERT(reporter, 50 == last.fY);
337
338 // Test rQuadTo().
339 path.rewind();
340 path.rLineTo(0, 100);
341 path.rLineTo(100, 0);
342 path.close();
343 path.rQuadTo(50, 50, 75, 75);
344
345 path.getLastPt(&last);
346 REPORTER_ASSERT(reporter, 75 == last.fX);
347 REPORTER_ASSERT(reporter, 75 == last.fY);
348
349 // Test rConicTo().
350 path.rewind();
351 path.rLineTo(0, 100);
352 path.rLineTo(100, 0);
353 path.close();
354 path.rConicTo(50, 50, 85, 85, 2);
355
356 path.getLastPt(&last);
357 REPORTER_ASSERT(reporter, 85 == last.fX);
358 REPORTER_ASSERT(reporter, 85 == last.fY);
359
360 // Test rCubicTo().
361 path.rewind();
362 path.rLineTo(0, 100);
363 path.rLineTo(100, 0);
364 path.close();
365 path.rCubicTo(50, 50, 85, 85, 95, 95);
366
367 path.getLastPt(&last);
368 REPORTER_ASSERT(reporter, 95 == last.fX);
369 REPORTER_ASSERT(reporter, 95 == last.fY);
370}
371
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000372static void test_gen_id(skiatest::Reporter* reporter) {
373 SkPath a, b;
374 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
375
376 a.moveTo(0, 0);
377 const uint32_t z = a.getGenerationID();
378 REPORTER_ASSERT(reporter, z != b.getGenerationID());
379
380 a.reset();
381 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
382
383 a.moveTo(1, 1);
384 const uint32_t y = a.getGenerationID();
385 REPORTER_ASSERT(reporter, z != y);
386
387 b.moveTo(2, 2);
388 const uint32_t x = b.getGenerationID();
389 REPORTER_ASSERT(reporter, x != y && x != z);
390
391 a.swap(b);
392 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
393
394 b = a;
395 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
396
397 SkPath c(a);
398 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
399
400 c.lineTo(3, 3);
401 const uint32_t w = c.getGenerationID();
402 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
403 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
404 REPORTER_ASSERT(reporter, w != x);
405
djsollen50da1d82015-02-17 09:09:53 -0800406#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000407 static bool kExpectGenIDToIgnoreFill = false;
408#else
409 static bool kExpectGenIDToIgnoreFill = true;
410#endif
411
412 c.toggleInverseFillType();
413 const uint32_t v = c.getGenerationID();
414 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
415
416 c.rewind();
417 REPORTER_ASSERT(reporter, v != c.getGenerationID());
418}
419
reed@google.com3eff3592013-05-08 21:08:21 +0000420// This used to assert in the debug build, as the edges did not all line-up.
421static void test_bad_cubic_crbug234190() {
422 SkPath path;
423 path.moveTo(13.8509f, 3.16858f);
424 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
425 -2.38991e+08f, -4.26573e+08f,
426 -2.41016e+08f, -4.30188e+08f);
427
428 SkPaint paint;
429 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700430 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000431 surface->getCanvas()->drawPath(path, paint);
432}
433
reed@google.com7a90daf2013-04-10 18:44:00 +0000434static void test_bad_cubic_crbug229478() {
435 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000436 { 4595.91064f, -11596.9873f },
437 { 4597.2168f, -11595.9414f },
438 { 4598.52344f, -11594.8955f },
439 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000440 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000441
reed@google.com7a90daf2013-04-10 18:44:00 +0000442 SkPath path;
443 path.moveTo(pts[0]);
444 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000445
reed@google.com7a90daf2013-04-10 18:44:00 +0000446 SkPaint paint;
447 paint.setStyle(SkPaint::kStroke_Style);
448 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000449
reed@google.com7a90daf2013-04-10 18:44:00 +0000450 SkPath dst;
451 // Before the fix, this would infinite-recurse, and run out of stack
452 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700453 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000454}
455
reed@google.com64d62952013-01-18 17:49:28 +0000456static void build_path_170666(SkPath& path) {
457 path.moveTo(17.9459f, 21.6344f);
458 path.lineTo(139.545f, -47.8105f);
459 path.lineTo(139.545f, -47.8105f);
460 path.lineTo(131.07f, -47.3888f);
461 path.lineTo(131.07f, -47.3888f);
462 path.lineTo(122.586f, -46.9532f);
463 path.lineTo(122.586f, -46.9532f);
464 path.lineTo(18076.6f, 31390.9f);
465 path.lineTo(18076.6f, 31390.9f);
466 path.lineTo(18085.1f, 31390.5f);
467 path.lineTo(18085.1f, 31390.5f);
468 path.lineTo(18076.6f, 31390.9f);
469 path.lineTo(18076.6f, 31390.9f);
470 path.lineTo(17955, 31460.3f);
471 path.lineTo(17955, 31460.3f);
472 path.lineTo(17963.5f, 31459.9f);
473 path.lineTo(17963.5f, 31459.9f);
474 path.lineTo(17971.9f, 31459.5f);
475 path.lineTo(17971.9f, 31459.5f);
476 path.lineTo(17.9551f, 21.6205f);
477 path.lineTo(17.9551f, 21.6205f);
478 path.lineTo(9.47091f, 22.0561f);
479 path.lineTo(9.47091f, 22.0561f);
480 path.lineTo(17.9459f, 21.6344f);
481 path.lineTo(17.9459f, 21.6344f);
482 path.close();path.moveTo(0.995934f, 22.4779f);
483 path.lineTo(0.986725f, 22.4918f);
484 path.lineTo(0.986725f, 22.4918f);
485 path.lineTo(17955, 31460.4f);
486 path.lineTo(17955, 31460.4f);
487 path.lineTo(17971.9f, 31459.5f);
488 path.lineTo(17971.9f, 31459.5f);
489 path.lineTo(18093.6f, 31390.1f);
490 path.lineTo(18093.6f, 31390.1f);
491 path.lineTo(18093.6f, 31390);
492 path.lineTo(18093.6f, 31390);
493 path.lineTo(139.555f, -47.8244f);
494 path.lineTo(139.555f, -47.8244f);
495 path.lineTo(122.595f, -46.9671f);
496 path.lineTo(122.595f, -46.9671f);
497 path.lineTo(0.995934f, 22.4779f);
498 path.lineTo(0.995934f, 22.4779f);
499 path.close();
500 path.moveTo(5.43941f, 25.5223f);
501 path.lineTo(798267, -28871.1f);
502 path.lineTo(798267, -28871.1f);
503 path.lineTo(3.12512e+06f, -113102);
504 path.lineTo(3.12512e+06f, -113102);
505 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
506 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
507 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
508 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
509 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
510 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
511 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
512 path.lineTo(2.78271e+08f, -1.00733e+07f);
513 path.lineTo(2.78271e+08f, -1.00733e+07f);
514 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
515 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
516 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
517 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
518 path.lineTo(2.77473e+08f, -1.00444e+07f);
519 path.lineTo(2.77473e+08f, -1.00444e+07f);
520 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
521 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
522 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
523 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
524 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
525 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
526 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
527 path.lineTo(798284, -28872);
528 path.lineTo(798284, -28872);
529 path.lineTo(22.4044f, 24.6677f);
530 path.lineTo(22.4044f, 24.6677f);
531 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
532 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
533 path.close();
534}
535
536static void build_path_simple_170666(SkPath& path) {
537 path.moveTo(126.677f, 24.1591f);
538 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
539}
540
541// This used to assert in the SK_DEBUG build, as the clip step would fail with
542// too-few interations in our cubic-line intersection code. That code now runs
543// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000544static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000545 SkPath path;
546 SkPaint paint;
547 paint.setAntiAlias(true);
548
reede8f30622016-03-23 18:59:25 -0700549 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000550
reed@google.com64d62952013-01-18 17:49:28 +0000551 build_path_simple_170666(path);
552 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000553
reed@google.com64d62952013-01-18 17:49:28 +0000554 build_path_170666(path);
555 surface->getCanvas()->drawPath(path, paint);
556}
557
caryclarke8c56662015-07-14 11:19:26 -0700558
559static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
560 SkScalar tx, SkScalar ty, SkScalar scale) {
561 SkPath smallPath;
562 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
563 bool smallConvex = smallPath.isConvex();
564 SkPath largePath;
565 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
566 SkMatrix matrix;
567 matrix.reset();
568 matrix.preTranslate(100, 100);
569 matrix.preScale(scale, scale);
570 largePath.transform(matrix);
571 bool largeConvex = largePath.isConvex();
572 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
573}
574
575static void test_crbug_493450(skiatest::Reporter* reporter) {
576 const char reducedCase[] =
577 "M0,0"
578 "L0.0002, 0"
579 "L0.0002, 0.0002"
580 "L0.0001, 0.0001"
581 "L0,0.0002"
582 "Z";
583 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
584 const char originalFiddleData[] =
585 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
586 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
587 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
588 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
589 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
590 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
591 826357.3384828606f);
592}
593
594static void test_crbug_495894(skiatest::Reporter* reporter) {
595 const char originalFiddleData[] =
596 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
597 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
598 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
599 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
600 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
601 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
602 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
603 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
604 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
605 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
606 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
607 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
608 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
609 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
610 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
611 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
612 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
613 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
614 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
615 "L-0.33993994441916414,-0.11288906492739594Z";
616 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
617 65536);
618}
619
mbarbella99600d02016-06-01 15:39:47 -0700620static void test_crbug_613918() {
621 SkPath path;
622 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
623 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
624 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
625
626 // This call could lead to an assert or uninitialized read due to a failure
627 // to check the return value from SkCubicClipper::ChopMonoAtY.
628 path.contains(-1.84817e-08f, 1.15465e-08f);
629}
630
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000631static void test_addrect(skiatest::Reporter* reporter) {
632 SkPath path;
633 path.lineTo(0, 0);
634 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700635 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000636
637 path.reset();
638 path.lineTo(FLT_EPSILON, FLT_EPSILON);
639 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700640 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000641
642 path.reset();
643 path.quadTo(0, 0, 0, 0);
644 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700645 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000646
647 path.reset();
648 path.conicTo(0, 0, 0, 0, 0.5f);
649 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700650 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000651
652 path.reset();
653 path.cubicTo(0, 0, 0, 0, 0, 0);
654 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700655 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000656}
657
reed@google.coma8790de2012-10-24 21:04:04 +0000658// Make sure we stay non-finite once we get there (unless we reset or rewind).
659static void test_addrect_isfinite(skiatest::Reporter* reporter) {
660 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000661
reed@google.coma8790de2012-10-24 21:04:04 +0000662 path.addRect(SkRect::MakeWH(50, 100));
663 REPORTER_ASSERT(reporter, path.isFinite());
664
665 path.moveTo(0, 0);
666 path.lineTo(SK_ScalarInfinity, 42);
667 REPORTER_ASSERT(reporter, !path.isFinite());
668
669 path.addRect(SkRect::MakeWH(50, 100));
670 REPORTER_ASSERT(reporter, !path.isFinite());
671
672 path.reset();
673 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000674
reed@google.coma8790de2012-10-24 21:04:04 +0000675 path.addRect(SkRect::MakeWH(50, 100));
676 REPORTER_ASSERT(reporter, path.isFinite());
677}
678
reed@google.com848148e2013-01-15 15:51:59 +0000679static void build_big_path(SkPath* path, bool reducedCase) {
680 if (reducedCase) {
681 path->moveTo(577330, 1971.72f);
682 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
683 } else {
684 path->moveTo(60.1631f, 7.70567f);
685 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
686 path->lineTo(577379, 1977.77f);
687 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
688 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
689 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
690 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
691 path->quadTo(577198, 1972, 577238, 1971.31f);
692 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
693 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
694 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
695 path->moveTo(306.718f, -32.912f);
696 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
697 }
698}
699
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000700static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700701 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000702
703 // This path used to assert, because our cubic-chopping code incorrectly
704 // moved control points after the chop. This test should be run in SK_DEBUG
705 // mode to ensure that we no long assert.
706 SkPath path;
707 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
708 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000709
reed@google.com848148e2013-01-15 15:51:59 +0000710 SkPaint paint;
711 for (int doAA = 0; doAA <= 1; ++doAA) {
712 paint.setAntiAlias(SkToBool(doAA));
713 surface->getCanvas()->drawPath(path, paint);
714 }
715 }
716}
717
reed3b2fb982015-07-29 08:37:13 -0700718static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
719 if (expected != bounds) {
720 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
721 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
722 expected.left(), expected.top(), expected.right(), expected.bottom());
723 }
724}
725
reed91f283b2015-07-28 06:00:50 -0700726static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
727 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700728#if 0
729 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
730 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700731 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
732
733 path.moveTo(-5, -8);
734 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
735
736 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
737 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
738
739 path.moveTo(1, 2);
740 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700741#else
742 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
743
744 path.moveTo(-5, -8); // should set the bounds
745 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
746
747 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
748 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
749
750 path.moveTo(1, 2); // don't expect this to have changed the bounds
751 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
752#endif
reed91f283b2015-07-28 06:00:50 -0700753}
754
reedb1b12f82016-07-13 10:56:53 -0700755#include "SkSurface.h"
756static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
757 SkPath path;
758 path.moveTo(0, 0);
759 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
760
761 SkPaint paint;
762 paint.setAntiAlias(true);
763
764 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
765 surf->getCanvas()->drawPath(path, paint);
766}
767
reed@google.com8cae8352012-09-14 15:18:41 +0000768// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
769// which triggered an assert, from a tricky cubic. This test replicates that
770// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
771// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000772static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000773 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000774 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
775 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
776 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
777 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000778 };
779
780 SkPath path;
781 path.moveTo(pts[0]);
782 path.cubicTo(pts[1], pts[2], pts[3]);
783
784 SkPaint paint;
785 paint.setAntiAlias(true);
786
reede8f30622016-03-23 18:59:25 -0700787 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000788}
reed@android.com3abec1d2009-03-02 05:36:20 +0000789
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000790// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
791//
792static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
793 SkPath path;
794 path.quadTo(157, 366, 286, 208);
795 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000797 SkMatrix matrix;
798 matrix.setScale(1000*1000, 1000*1000);
799
800 // Be sure that path::transform correctly updates isFinite and the bounds
801 // if the transformation overflows. The previous bug was that isFinite was
802 // set to true in this case, but the bounds were not set to empty (which
803 // they should be).
804 while (path.isFinite()) {
805 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
806 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
807 path.transform(matrix);
808 }
809 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
810
811 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
812 path.transform(matrix);
813 // we need to still be non-finite
814 REPORTER_ASSERT(reporter, !path.isFinite());
815 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
816}
817
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000818static void add_corner_arc(SkPath* path, const SkRect& rect,
819 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000820 int startAngle)
821{
822
823 SkScalar rx = SkMinScalar(rect.width(), xIn);
824 SkScalar ry = SkMinScalar(rect.height(), yIn);
825
826 SkRect arcRect;
827 arcRect.set(-rx, -ry, rx, ry);
828 switch (startAngle) {
829 case 0:
830 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
831 break;
832 case 90:
833 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
834 break;
835 case 180:
836 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
837 break;
838 case 270:
839 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
840 break;
841 default:
842 break;
843 }
844
845 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
846}
847
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000848static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000849 SkScalar xCorner, SkScalar yCorner) {
850 // we are lazy here and use the same x & y for each corner
851 add_corner_arc(path, r, xCorner, yCorner, 270);
852 add_corner_arc(path, r, xCorner, yCorner, 0);
853 add_corner_arc(path, r, xCorner, yCorner, 90);
854 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000855 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000856}
857
858// Chrome creates its own round rects with each corner possibly being different.
859// Performance will suffer if they are not convex.
860// Note: PathBench::ArbRoundRectBench performs almost exactly
861// the same test (but with drawing)
862static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000863 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000864 SkRect r;
865
866 for (int i = 0; i < 5000; ++i) {
867
robertphillips@google.com158618e2012-10-23 16:56:56 +0000868 SkScalar size = rand.nextUScalar1() * 30;
869 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000870 continue;
871 }
872 r.fLeft = rand.nextUScalar1() * 300;
873 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000874 r.fRight = r.fLeft + 2 * size;
875 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000876
877 SkPath temp;
878
879 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
880
881 REPORTER_ASSERT(reporter, temp.isConvex());
882 }
883}
884
robertphillips@google.com158618e2012-10-23 16:56:56 +0000885// Chrome will sometimes create a 0 radius round rect. The degenerate
886// quads prevent the path from being converted to a rect
887// Note: PathBench::ArbRoundRectBench performs almost exactly
888// the same test (but with drawing)
889static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000890 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000891 SkRect r;
892
893 for (int i = 0; i < 5000; ++i) {
894
895 SkScalar size = rand.nextUScalar1() * 30;
896 if (size < SK_Scalar1) {
897 continue;
898 }
899 r.fLeft = rand.nextUScalar1() * 300;
900 r.fTop = rand.nextUScalar1() * 300;
901 r.fRight = r.fLeft + 2 * size;
902 r.fBottom = r.fTop + 2 * size;
903
904 SkPath temp;
905
906 make_arb_round_rect(&temp, r, 0, 0);
907
robertphillips@google.com158618e2012-10-23 16:56:56 +0000908 SkRect result;
909 REPORTER_ASSERT(reporter, temp.isRect(&result));
910 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000911 }
912}
913
reed@google.com0bb18bb2012-07-26 15:20:36 +0000914static void test_rect_isfinite(skiatest::Reporter* reporter) {
915 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000916 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000917 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918
reed@google.com0bb18bb2012-07-26 15:20:36 +0000919 SkRect r;
920 r.setEmpty();
921 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000922 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000923 REPORTER_ASSERT(reporter, !r.isFinite());
924 r.set(0, 0, nan, 0);
925 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000926
reed@google.com0bb18bb2012-07-26 15:20:36 +0000927 SkPoint pts[] = {
928 { 0, 0 },
929 { SK_Scalar1, 0 },
930 { 0, SK_Scalar1 },
931 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000932
reed@google.com0bb18bb2012-07-26 15:20:36 +0000933 bool isFine = r.setBoundsCheck(pts, 3);
934 REPORTER_ASSERT(reporter, isFine);
935 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000936
reed@google.com0bb18bb2012-07-26 15:20:36 +0000937 pts[1].set(inf, 0);
938 isFine = r.setBoundsCheck(pts, 3);
939 REPORTER_ASSERT(reporter, !isFine);
940 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000941
reed@google.com0bb18bb2012-07-26 15:20:36 +0000942 pts[1].set(nan, 0);
943 isFine = r.setBoundsCheck(pts, 3);
944 REPORTER_ASSERT(reporter, !isFine);
945 REPORTER_ASSERT(reporter, r.isEmpty());
946}
947
948static void test_path_isfinite(skiatest::Reporter* reporter) {
949 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000950 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000951 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000952
reed@google.com0bb18bb2012-07-26 15:20:36 +0000953 SkPath path;
954 REPORTER_ASSERT(reporter, path.isFinite());
955
956 path.reset();
957 REPORTER_ASSERT(reporter, path.isFinite());
958
959 path.reset();
960 path.moveTo(SK_Scalar1, 0);
961 REPORTER_ASSERT(reporter, path.isFinite());
962
963 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000964 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000965 REPORTER_ASSERT(reporter, !path.isFinite());
966
967 path.reset();
968 path.moveTo(nan, 0);
969 REPORTER_ASSERT(reporter, !path.isFinite());
970}
971
972static void test_isfinite(skiatest::Reporter* reporter) {
973 test_rect_isfinite(reporter);
974 test_path_isfinite(reporter);
975}
976
fsb1475b02016-01-20 09:51:07 -0800977static void test_islastcontourclosed(skiatest::Reporter* reporter) {
978 SkPath path;
979 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
980 path.moveTo(0, 0);
981 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
982 path.close();
983 REPORTER_ASSERT(reporter, path.isLastContourClosed());
984 path.lineTo(100, 100);
985 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
986 path.moveTo(200, 200);
987 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
988 path.close();
989 REPORTER_ASSERT(reporter, path.isLastContourClosed());
990 path.moveTo(0, 0);
991 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
992}
993
reed@google.com744faba2012-05-29 19:54:52 +0000994// assert that we always
995// start with a moveTo
996// only have 1 moveTo
997// only have Lines after that
998// end with a single close
999// only have (at most) 1 close
1000//
1001static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001002 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +00001003 SkPath::RawIter iter(path);
1004 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +00001005
1006 bool firstTime = true;
1007 bool foundClose = false;
1008 for (;;) {
1009 switch (iter.next(pts)) {
1010 case SkPath::kMove_Verb:
1011 REPORTER_ASSERT(reporter, firstTime);
1012 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
1013 srcPts++;
1014 firstTime = false;
1015 break;
1016 case SkPath::kLine_Verb:
1017 REPORTER_ASSERT(reporter, !firstTime);
1018 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
1019 srcPts++;
1020 break;
1021 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001022 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +00001023 break;
reed@google.com277c3f82013-05-31 15:17:50 +00001024 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001025 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +00001026 break;
reed@google.com744faba2012-05-29 19:54:52 +00001027 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001028 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +00001029 break;
1030 case SkPath::kClose_Verb:
1031 REPORTER_ASSERT(reporter, !firstTime);
1032 REPORTER_ASSERT(reporter, !foundClose);
1033 REPORTER_ASSERT(reporter, expectClose);
1034 foundClose = true;
1035 break;
1036 case SkPath::kDone_Verb:
1037 goto DONE;
1038 }
1039 }
1040DONE:
1041 REPORTER_ASSERT(reporter, foundClose == expectClose);
1042}
1043
1044static void test_addPoly(skiatest::Reporter* reporter) {
1045 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001046 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047
reed@google.com744faba2012-05-29 19:54:52 +00001048 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1049 pts[i].fX = rand.nextSScalar1();
1050 pts[i].fY = rand.nextSScalar1();
1051 }
1052
1053 for (int doClose = 0; doClose <= 1; ++doClose) {
1054 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1055 SkPath path;
bsalomon98806072014-12-12 15:11:17 -08001056 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001057 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +00001058 }
1059 }
1060}
1061
reed@google.com8b06f1a2012-05-29 12:03:46 +00001062static void test_strokerec(skiatest::Reporter* reporter) {
1063 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1064 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001065
reed@google.com8b06f1a2012-05-29 12:03:46 +00001066 rec.setHairlineStyle();
1067 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001068
reed@google.com8b06f1a2012-05-29 12:03:46 +00001069 rec.setStrokeStyle(SK_Scalar1, false);
1070 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001071
reed@google.com8b06f1a2012-05-29 12:03:46 +00001072 rec.setStrokeStyle(SK_Scalar1, true);
1073 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001074
reed@google.com8b06f1a2012-05-29 12:03:46 +00001075 rec.setStrokeStyle(0, false);
1076 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001077
reed@google.com8b06f1a2012-05-29 12:03:46 +00001078 rec.setStrokeStyle(0, true);
1079 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1080}
1081
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001082// Set this for paths that don't have a consistent direction such as a bowtie.
1083// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001084const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001085
1086static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001087 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001088 if (expected == kDontCheckDir) {
1089 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001090 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001091 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1092
reed026beb52015-06-10 14:23:15 -07001093 SkPathPriv::FirstDirection dir;
1094 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001095 REPORTER_ASSERT(reporter, dir == expected);
1096 } else {
reed026beb52015-06-10 14:23:15 -07001097 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001098 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001099}
1100
reed@google.com3e71a882012-01-10 18:44:37 +00001101static void test_direction(skiatest::Reporter* reporter) {
1102 size_t i;
1103 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001104 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001105 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1106 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1107 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001108
1109 static const char* gDegen[] = {
1110 "M 10 10",
1111 "M 10 10 M 20 20",
1112 "M 10 10 L 20 20",
1113 "M 10 10 L 10 10 L 10 10",
1114 "M 10 10 Q 10 10 10 10",
1115 "M 10 10 C 10 10 10 10 10 10",
1116 };
1117 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1118 path.reset();
1119 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1120 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001121 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001122 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001123
reed@google.com3e71a882012-01-10 18:44:37 +00001124 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001125 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001126 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001127 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +00001128 // rect with top two corners replaced by cubics with identical middle
1129 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001130 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1131 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001132 };
1133 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1134 path.reset();
1135 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1136 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001137 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001138 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001139
reed@google.com3e71a882012-01-10 18:44:37 +00001140 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001141 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001142 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001143 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +00001144 // rect with top two corners replaced by cubics with identical middle
1145 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001146 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1147 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001148 };
1149 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1150 path.reset();
1151 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1152 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001153 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001154 }
reed@google.comac8543f2012-01-30 20:51:25 +00001155
1156 // Test two donuts, each wound a different direction. Only the outer contour
1157 // determines the cheap direction
1158 path.reset();
1159 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1160 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001161 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001162
reed@google.comac8543f2012-01-30 20:51:25 +00001163 path.reset();
1164 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1165 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001166 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001167
1168 // triangle with one point really far from the origin.
1169 path.reset();
1170 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001171 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001172 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1173 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001174 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001175
1176 path.reset();
1177 path.conicTo(20, 0, 20, 20, 0.5f);
1178 path.close();
reed026beb52015-06-10 14:23:15 -07001179 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001180
1181 path.reset();
1182 path.lineTo(1, 1e7f);
1183 path.lineTo(1e7f, 2e7f);
1184 path.close();
1185 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001186 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001187}
1188
reed@google.comffdb0182011-11-14 19:29:14 +00001189static void add_rect(SkPath* path, const SkRect& r) {
1190 path->moveTo(r.fLeft, r.fTop);
1191 path->lineTo(r.fRight, r.fTop);
1192 path->lineTo(r.fRight, r.fBottom);
1193 path->lineTo(r.fLeft, r.fBottom);
1194 path->close();
1195}
1196
1197static void test_bounds(skiatest::Reporter* reporter) {
1198 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001199 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1200 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1201 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1202 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001203 };
1204
1205 SkPath path0, path1;
1206 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1207 path0.addRect(rects[i]);
1208 add_rect(&path1, rects[i]);
1209 }
1210
1211 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1212}
1213
reed@google.com55b5f4b2011-09-07 12:23:41 +00001214static void stroke_cubic(const SkPoint pts[4]) {
1215 SkPath path;
1216 path.moveTo(pts[0]);
1217 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001218
reed@google.com55b5f4b2011-09-07 12:23:41 +00001219 SkPaint paint;
1220 paint.setStyle(SkPaint::kStroke_Style);
1221 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001222
reed@google.com55b5f4b2011-09-07 12:23:41 +00001223 SkPath fill;
1224 paint.getFillPath(path, &fill);
1225}
1226
1227// just ensure this can run w/o any SkASSERTS firing in the debug build
1228// we used to assert due to differences in how we determine a degenerate vector
1229// but that was fixed with the introduction of SkPoint::CanNormalize
1230static void stroke_tiny_cubic() {
1231 SkPoint p0[] = {
1232 { 372.0f, 92.0f },
1233 { 372.0f, 92.0f },
1234 { 372.0f, 92.0f },
1235 { 372.0f, 92.0f },
1236 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001237
reed@google.com55b5f4b2011-09-07 12:23:41 +00001238 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001239
reed@google.com55b5f4b2011-09-07 12:23:41 +00001240 SkPoint p1[] = {
1241 { 372.0f, 92.0f },
1242 { 372.0007f, 92.000755f },
1243 { 371.99927f, 92.003922f },
1244 { 371.99826f, 92.003899f },
1245 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001246
reed@google.com55b5f4b2011-09-07 12:23:41 +00001247 stroke_cubic(p1);
1248}
1249
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001250static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1251 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001252 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001253 SkPoint mv;
1254 SkPoint pts[4];
1255 SkPath::Verb v;
1256 int nMT = 0;
1257 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001258 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001259 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1260 switch (v) {
1261 case SkPath::kMove_Verb:
1262 mv = pts[0];
1263 ++nMT;
1264 break;
1265 case SkPath::kClose_Verb:
1266 REPORTER_ASSERT(reporter, mv == pts[0]);
1267 ++nCL;
1268 break;
1269 default:
1270 break;
1271 }
1272 }
1273 // if we force a close on the interator we should have a close
1274 // for every moveTo
1275 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1276 }
1277}
1278
1279static void test_close(skiatest::Reporter* reporter) {
1280 SkPath closePt;
1281 closePt.moveTo(0, 0);
1282 closePt.close();
1283 check_close(reporter, closePt);
1284
1285 SkPath openPt;
1286 openPt.moveTo(0, 0);
1287 check_close(reporter, openPt);
1288
1289 SkPath empty;
1290 check_close(reporter, empty);
1291 empty.close();
1292 check_close(reporter, empty);
1293
1294 SkPath rect;
1295 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1296 check_close(reporter, rect);
1297 rect.close();
1298 check_close(reporter, rect);
1299
1300 SkPath quad;
1301 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1302 check_close(reporter, quad);
1303 quad.close();
1304 check_close(reporter, quad);
1305
1306 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001307 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001308 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1309 check_close(reporter, cubic);
1310 cubic.close();
1311 check_close(reporter, cubic);
1312
1313 SkPath line;
1314 line.moveTo(SK_Scalar1, SK_Scalar1);
1315 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1316 check_close(reporter, line);
1317 line.close();
1318 check_close(reporter, line);
1319
1320 SkPath rect2;
1321 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1322 rect2.close();
1323 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1324 check_close(reporter, rect2);
1325 rect2.close();
1326 check_close(reporter, rect2);
1327
1328 SkPath oval3;
1329 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1330 oval3.close();
1331 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1332 check_close(reporter, oval3);
1333 oval3.close();
1334 check_close(reporter, oval3);
1335
1336 SkPath moves;
1337 moves.moveTo(SK_Scalar1, SK_Scalar1);
1338 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1339 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1340 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1341 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001342
1343 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001344}
1345
reed@google.com7c424812011-05-15 04:38:34 +00001346static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1347 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001348 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1349 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001350 REPORTER_ASSERT(reporter, c == expected);
1351}
1352
caryclarkb4216502015-03-02 13:02:34 -08001353static void test_path_crbug389050(skiatest::Reporter* reporter) {
1354 SkPath tinyConvexPolygon;
1355 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1356 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1357 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1358 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1359 tinyConvexPolygon.close();
1360 tinyConvexPolygon.getConvexity();
1361 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001362 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001363
1364 SkPath platTriangle;
1365 platTriangle.moveTo(0, 0);
1366 platTriangle.lineTo(200, 0);
1367 platTriangle.lineTo(100, 0.04f);
1368 platTriangle.close();
1369 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001370 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001371
1372 platTriangle.reset();
1373 platTriangle.moveTo(0, 0);
1374 platTriangle.lineTo(200, 0);
1375 platTriangle.lineTo(100, 0.03f);
1376 platTriangle.close();
1377 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001378 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001379}
1380
reed@google.com7c424812011-05-15 04:38:34 +00001381static void test_convexity2(skiatest::Reporter* reporter) {
1382 SkPath pt;
1383 pt.moveTo(0, 0);
1384 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001385 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001386 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001387
reed@google.com7c424812011-05-15 04:38:34 +00001388 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001389 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1390 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001391 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001392 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001393 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001394
reed@google.com7c424812011-05-15 04:38:34 +00001395 SkPath triLeft;
1396 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001397 triLeft.lineTo(SK_Scalar1, 0);
1398 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001399 triLeft.close();
1400 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001401 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001402
reed@google.com7c424812011-05-15 04:38:34 +00001403 SkPath triRight;
1404 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001405 triRight.lineTo(-SK_Scalar1, 0);
1406 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001407 triRight.close();
1408 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001409 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001410
reed@google.com7c424812011-05-15 04:38:34 +00001411 SkPath square;
1412 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001413 square.lineTo(SK_Scalar1, 0);
1414 square.lineTo(SK_Scalar1, SK_Scalar1);
1415 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001416 square.close();
1417 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001418 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001419
reed@google.com7c424812011-05-15 04:38:34 +00001420 SkPath redundantSquare;
1421 redundantSquare.moveTo(0, 0);
1422 redundantSquare.lineTo(0, 0);
1423 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001424 redundantSquare.lineTo(SK_Scalar1, 0);
1425 redundantSquare.lineTo(SK_Scalar1, 0);
1426 redundantSquare.lineTo(SK_Scalar1, 0);
1427 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1428 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1429 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1430 redundantSquare.lineTo(0, SK_Scalar1);
1431 redundantSquare.lineTo(0, SK_Scalar1);
1432 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001433 redundantSquare.close();
1434 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001435 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001436
reed@google.com7c424812011-05-15 04:38:34 +00001437 SkPath bowTie;
1438 bowTie.moveTo(0, 0);
1439 bowTie.lineTo(0, 0);
1440 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001441 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1442 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1443 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1444 bowTie.lineTo(SK_Scalar1, 0);
1445 bowTie.lineTo(SK_Scalar1, 0);
1446 bowTie.lineTo(SK_Scalar1, 0);
1447 bowTie.lineTo(0, SK_Scalar1);
1448 bowTie.lineTo(0, SK_Scalar1);
1449 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001450 bowTie.close();
1451 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001452 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001453
reed@google.com7c424812011-05-15 04:38:34 +00001454 SkPath spiral;
1455 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001456 spiral.lineTo(100*SK_Scalar1, 0);
1457 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1458 spiral.lineTo(0, 100*SK_Scalar1);
1459 spiral.lineTo(0, 50*SK_Scalar1);
1460 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1461 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001462 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001463 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001464 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001465
reed@google.com7c424812011-05-15 04:38:34 +00001466 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001467 dent.moveTo(0, 0);
1468 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1469 dent.lineTo(0, 100*SK_Scalar1);
1470 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1471 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001472 dent.close();
1473 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001474 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001475
halcanary6950de62015-11-07 05:29:00 -08001476 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001477 SkPath strokedSin;
1478 for (int i = 0; i < 2000; i++) {
1479 SkScalar x = SkIntToScalar(i) / 2;
1480 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1481 if (0 == i) {
1482 strokedSin.moveTo(x, y);
1483 } else {
1484 strokedSin.lineTo(x, y);
1485 }
1486 }
1487 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1488 stroke.setStrokeStyle(2 * SK_Scalar1);
1489 stroke.applyToPath(&strokedSin, strokedSin);
1490 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1491 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001492
1493 // http://crbug.com/412640
1494 SkPath degenerateConcave;
1495 degenerateConcave.moveTo(148.67912f, 191.875f);
1496 degenerateConcave.lineTo(470.37695f, 7.5f);
1497 degenerateConcave.lineTo(148.67912f, 191.875f);
1498 degenerateConcave.lineTo(41.446522f, 376.25f);
1499 degenerateConcave.lineTo(-55.971577f, 460.0f);
1500 degenerateConcave.lineTo(41.446522f, 376.25f);
1501 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001502 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001503
1504 // http://crbug.com/433683
1505 SkPath badFirstVector;
1506 badFirstVector.moveTo(501.087708f, 319.610352f);
1507 badFirstVector.lineTo(501.087708f, 319.610352f);
1508 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1509 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1510 badFirstVector.lineTo(301.557678f, 98.044601f);
1511 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1512 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1513 badFirstVector.lineTo(504.912292f, 316.389648f);
1514 badFirstVector.lineTo(501.087708f, 319.610352f);
1515 badFirstVector.close();
1516 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001517}
1518
reed@android.com6b82d1a2009-06-03 02:35:01 +00001519static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1520 const SkRect& bounds) {
1521 REPORTER_ASSERT(reporter, p.isConvex());
1522 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001523
reed@android.com6b82d1a2009-06-03 02:35:01 +00001524 SkPath p2(p);
1525 REPORTER_ASSERT(reporter, p2.isConvex());
1526 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1527
1528 SkPath other;
1529 other.swap(p2);
1530 REPORTER_ASSERT(reporter, other.isConvex());
1531 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1532}
1533
reed@google.com04863fa2011-05-15 04:08:24 +00001534static void setFromString(SkPath* path, const char str[]) {
1535 bool first = true;
1536 while (str) {
1537 SkScalar x, y;
1538 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001539 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001540 break;
1541 }
1542 str = SkParse::FindScalar(str, &y);
1543 SkASSERT(str);
1544 if (first) {
1545 path->moveTo(x, y);
1546 first = false;
1547 } else {
1548 path->lineTo(x, y);
1549 }
1550 }
1551}
1552
1553static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001554 SkPath path;
1555
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001556 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001557 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001558 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001559 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001560 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1561
reed@google.com04863fa2011-05-15 04:08:24 +00001562 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001563 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001564 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001565 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001566
reed@google.com04863fa2011-05-15 04:08:24 +00001567 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001568 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001569 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001570 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001571
liyuqianbdabcc42016-11-03 11:40:19 -07001572 path.reset();
1573 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1574 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1575
reed@google.com04863fa2011-05-15 04:08:24 +00001576 static const struct {
reed026beb52015-06-10 14:23:15 -07001577 const char* fPathStr;
1578 SkPath::Convexity fExpectedConvexity;
1579 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001580 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001581 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1582 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1583 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1584 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1585 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1586 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001587 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001588 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001589 };
1590
1591 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1592 SkPath path;
1593 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001594 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1595 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001596 // check after setting the initial convex and direction
1597 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1598 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001599 SkPathPriv::FirstDirection dir;
1600 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1601 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001602 ^ foundDir);
1603 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1604 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1605 }
1606 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1607 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001608 }
caryclarkd3d1a982014-12-08 04:57:38 -08001609
1610 static const SkPoint nonFinitePts[] = {
1611 { SK_ScalarInfinity, 0 },
1612 { 0, SK_ScalarInfinity },
1613 { SK_ScalarInfinity, SK_ScalarInfinity },
1614 { SK_ScalarNegativeInfinity, 0},
1615 { 0, SK_ScalarNegativeInfinity },
1616 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1617 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1618 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1619 { SK_ScalarNaN, 0 },
1620 { 0, SK_ScalarNaN },
1621 { SK_ScalarNaN, SK_ScalarNaN },
1622 };
1623
1624 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1625
1626 static const SkPoint finitePts[] = {
1627 { SK_ScalarMax, 0 },
1628 { 0, SK_ScalarMax },
1629 { SK_ScalarMax, SK_ScalarMax },
1630 { SK_ScalarMin, 0 },
1631 { 0, SK_ScalarMin },
1632 { SK_ScalarMin, SK_ScalarMin },
1633 };
1634
1635 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1636
1637 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1638 int i = (int) (index % nonFinitePtsCount);
1639 int f = (int) (index % finitePtsCount);
1640 int g = (int) ((f + 1) % finitePtsCount);
1641 path.reset();
1642 switch (index % 13) {
1643 case 0: path.lineTo(nonFinitePts[i]); break;
1644 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1645 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1646 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1647 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1648 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1649 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1650 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1651 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1652 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1653 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1654 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1655 case 12: path.moveTo(nonFinitePts[i]); break;
1656 }
1657 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1658 }
1659
1660 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1661 int f = (int) (index % finitePtsCount);
1662 int g = (int) ((f + 1) % finitePtsCount);
1663 path.reset();
1664 int curveSelect = index % 11;
1665 switch (curveSelect) {
1666 case 0: path.moveTo(finitePts[f]); break;
1667 case 1: path.lineTo(finitePts[f]); break;
1668 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1669 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1670 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1671 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1672 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1673 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1674 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1675 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1676 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1677 }
1678 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1679 : SkPath::kUnknown_Convexity);
1680 }
1681
reed@google.com04863fa2011-05-15 04:08:24 +00001682}
1683
reed@google.com7e6c4d12012-05-10 14:05:43 +00001684static void test_isLine(skiatest::Reporter* reporter) {
1685 SkPath path;
1686 SkPoint pts[2];
1687 const SkScalar value = SkIntToScalar(5);
1688
halcanary96fcdcc2015-08-27 07:41:13 -07001689 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001690
reed@google.com7e6c4d12012-05-10 14:05:43 +00001691 // set some non-zero values
1692 pts[0].set(value, value);
1693 pts[1].set(value, value);
1694 REPORTER_ASSERT(reporter, !path.isLine(pts));
1695 // check that pts was untouched
1696 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1697 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1698
1699 const SkScalar moveX = SkIntToScalar(1);
1700 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001701 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001702
1703 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001704 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001705 REPORTER_ASSERT(reporter, !path.isLine(pts));
1706 // check that pts was untouched
1707 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1708 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1709
1710 const SkScalar lineX = SkIntToScalar(2);
1711 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001712 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001713
1714 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001715 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001716
1717 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1718 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1719 REPORTER_ASSERT(reporter, path.isLine(pts));
1720 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1721 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1722
1723 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001724 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001725 REPORTER_ASSERT(reporter, !path.isLine(pts));
1726 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1727 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001728
1729 path.reset();
1730 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001731 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001732}
1733
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001734static void test_conservativelyContains(skiatest::Reporter* reporter) {
1735 SkPath path;
1736
1737 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1738 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1739
1740 // A circle that bounds kBaseRect (with a significant amount of slop)
1741 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001742 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001743 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1744
1745 // round-rect radii
1746 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001747
caryclark@google.com56f233a2012-11-19 13:06:06 +00001748 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001749 SkRect fQueryRect;
1750 bool fInRect;
1751 bool fInCircle;
1752 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001753 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001754 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001755 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001756
1757 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001758 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1759 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1760 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1761 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001762 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001763
1764 // rects with edges off by one from kBaseRect's edges
1765 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1766 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001767 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001768 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1769 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001770 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001771 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1772 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001773 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001774 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1775 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001776 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001777 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1778 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001779 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001780 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1781 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001782 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001783 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1784 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001785 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001786
1787 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001788 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1789 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1790 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1791 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001792
1793 // far away rect
1794 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1795 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001796 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001797
1798 // very large rect containing kBaseRect
1799 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1800 kBaseRect.fTop - 5 * kBaseRect.height(),
1801 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001802 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001803
1804 // skinny rect that spans same y-range as kBaseRect
1805 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1806 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001807 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001808
1809 // short rect that spans same x-range as kBaseRect
1810 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001811 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001812
1813 // skinny rect that spans slightly larger y-range than kBaseRect
1814 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1815 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001816 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001817
1818 // short rect that spans slightly larger x-range than kBaseRect
1819 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1820 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001821 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001822 };
1823
1824 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001825 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001826 SkRect qRect = kQueries[q].fQueryRect;
1827 if (inv & 0x1) {
1828 SkTSwap(qRect.fLeft, qRect.fRight);
1829 }
1830 if (inv & 0x2) {
1831 SkTSwap(qRect.fTop, qRect.fBottom);
1832 }
1833 for (int d = 0; d < 2; ++d) {
1834 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1835 path.reset();
1836 path.addRect(kBaseRect, dir);
1837 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1838 path.conservativelyContainsRect(qRect));
1839
1840 path.reset();
1841 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1842 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1843 path.conservativelyContainsRect(qRect));
1844
1845 path.reset();
1846 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1847 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1848 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001849
1850 path.reset();
1851 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1852 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1853 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1854 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1855 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1856 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1857 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1858 path.close();
1859 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1860 path.conservativelyContainsRect(qRect));
1861
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001862 }
1863 // Slightly non-convex shape, shouldn't contain any rects.
1864 path.reset();
1865 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001866 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001867 path.lineTo(SkIntToScalar(100), 0);
1868 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1869 path.lineTo(0, SkIntToScalar(100));
1870 path.close();
1871 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1872 }
1873 }
1874
1875 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1876 path.reset();
1877 path.moveTo(0, 0);
1878 path.lineTo(SkIntToScalar(100), 0);
1879 path.lineTo(0, SkIntToScalar(100));
1880
1881 // inside, on along top edge
1882 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1883 SkIntToScalar(10),
1884 SkIntToScalar(10))));
1885 // above
1886 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1887 SkRect::MakeXYWH(SkIntToScalar(50),
1888 SkIntToScalar(-10),
1889 SkIntToScalar(10),
1890 SkIntToScalar(10))));
1891 // to the left
1892 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1893 SkIntToScalar(5),
1894 SkIntToScalar(5),
1895 SkIntToScalar(5))));
1896
1897 // outside the diagonal edge
1898 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1899 SkIntToScalar(200),
1900 SkIntToScalar(20),
1901 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001902
bsalomonb17c1292014-08-28 14:04:55 -07001903
1904 // Test that multiple move commands do not cause asserts.
1905
1906 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001907 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001908 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1909#if 0
1910 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1911 SkIntToScalar(10),
1912 SkIntToScalar(10))));
1913#endif
1914
1915 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001916 path.reset();
1917 path.moveTo(100, 100);
1918 path.moveTo(0, 0);
1919 path.lineTo(SkIntToScalar(100), 0);
1920 path.lineTo(0, SkIntToScalar(100));
1921
1922 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1923 SkIntToScalar(10),
1924 SkIntToScalar(10))));
1925
bsalomonb17c1292014-08-28 14:04:55 -07001926 // Test that multiple move commands do not cause asserts and that the function
1927 // is not confused by the multiple moves.
1928 path.reset();
1929 path.moveTo(0, 0);
1930 path.lineTo(SkIntToScalar(100), 0);
1931 path.lineTo(0, SkIntToScalar(100));
1932 path.moveTo(0, SkIntToScalar(200));
1933 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1934 path.lineTo(0, SkIntToScalar(300));
1935
1936 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1937 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1938 SkIntToScalar(10),
1939 SkIntToScalar(10))));
1940
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001941 path.reset();
1942 path.lineTo(100, 100);
1943 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001944}
1945
reed@google.comf32322b2013-10-16 15:14:04 +00001946static void test_isRect_open_close(skiatest::Reporter* reporter) {
1947 SkPath path;
1948 bool isClosed;
1949
1950 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001951 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001952
halcanary96fcdcc2015-08-27 07:41:13 -07001953 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001954 REPORTER_ASSERT(reporter, isClosed);
1955}
1956
caryclark@google.comf1316942011-07-26 19:54:45 +00001957// Simple isRect test is inline TestPath, below.
1958// test_isRect provides more extensive testing.
1959static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001960 test_isRect_open_close(reporter);
1961
caryclark@google.comf1316942011-07-26 19:54:45 +00001962 // passing tests (all moveTo / lineTo...
1963 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1964 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1965 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1966 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1967 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1968 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1969 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1970 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1971 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001972 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1973 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001974 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1975 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1976 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001977 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001978
caryclark@google.comf1316942011-07-26 19:54:45 +00001979 // failing tests
1980 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1981 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1982 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1983 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1984 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1985 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1986 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1987 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001988 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1989 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1990 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001991
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001992 // no close, but we should detect them as fillably the same as a rect
1993 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1994 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1995 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1996
1997 // like c2, but we double-back on ourselves
1998 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1999 // like c2, but we overshoot the start point
2000 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2001 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00002002
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002003 struct IsRectTest {
2004 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002005 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002006 bool fClose;
2007 bool fIsRect;
2008 } tests[] = {
2009 { r1, SK_ARRAY_COUNT(r1), true, true },
2010 { r2, SK_ARRAY_COUNT(r2), true, true },
2011 { r3, SK_ARRAY_COUNT(r3), true, true },
2012 { r4, SK_ARRAY_COUNT(r4), true, true },
2013 { r5, SK_ARRAY_COUNT(r5), true, true },
2014 { r6, SK_ARRAY_COUNT(r6), true, true },
2015 { r7, SK_ARRAY_COUNT(r7), true, true },
2016 { r8, SK_ARRAY_COUNT(r8), true, true },
2017 { r9, SK_ARRAY_COUNT(r9), true, true },
2018 { ra, SK_ARRAY_COUNT(ra), true, true },
2019 { rb, SK_ARRAY_COUNT(rb), true, true },
2020 { rc, SK_ARRAY_COUNT(rc), true, true },
2021 { rd, SK_ARRAY_COUNT(rd), true, true },
2022 { re, SK_ARRAY_COUNT(re), true, true },
2023 { rf, SK_ARRAY_COUNT(rf), true, true },
2024
2025 { f1, SK_ARRAY_COUNT(f1), true, false },
2026 { f2, SK_ARRAY_COUNT(f2), true, false },
2027 { f3, SK_ARRAY_COUNT(f3), true, false },
2028 { f4, SK_ARRAY_COUNT(f4), true, false },
2029 { f5, SK_ARRAY_COUNT(f5), true, false },
2030 { f6, SK_ARRAY_COUNT(f6), true, false },
2031 { f7, SK_ARRAY_COUNT(f7), true, false },
2032 { f8, SK_ARRAY_COUNT(f8), true, false },
2033 { f9, SK_ARRAY_COUNT(f9), true, false },
2034 { fa, SK_ARRAY_COUNT(fa), true, false },
2035 { fb, SK_ARRAY_COUNT(fb), true, false },
2036
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002037 { c1, SK_ARRAY_COUNT(c1), false, true },
2038 { c2, SK_ARRAY_COUNT(c2), false, true },
2039 { c3, SK_ARRAY_COUNT(c3), false, true },
2040
2041 { d1, SK_ARRAY_COUNT(d1), false, false },
2042 { d2, SK_ARRAY_COUNT(d2), false, false },
2043 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00002044 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002045
2046 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002047 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002048 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2049 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002050 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2051 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2052 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002053 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002054 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002055 path.close();
2056 }
halcanary96fcdcc2015-08-27 07:41:13 -07002057 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002058
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002059 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002060 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002061 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002062 SkPath::Direction direction;
2063 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002064 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002065 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002066 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2067 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002068 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002069 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002070 } else {
2071 SkRect computed;
2072 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002073 bool isClosed = (bool)-1;
2074 SkPath::Direction direction = (SkPath::Direction) - 1;
2075 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002076 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2077 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002078 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2079 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002080 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002081 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002082
caryclark@google.comf1316942011-07-26 19:54:45 +00002083 // fail, close then line
2084 SkPath path1;
2085 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002086 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002087 path1.lineTo(r1[index].fX, r1[index].fY);
2088 }
2089 path1.close();
2090 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002091 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002092
caryclark@google.comf1316942011-07-26 19:54:45 +00002093 // fail, move in the middle
2094 path1.reset();
2095 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002096 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002097 if (index == 2) {
2098 path1.moveTo(1, .5f);
2099 }
2100 path1.lineTo(r1[index].fX, r1[index].fY);
2101 }
2102 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002103 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002104
2105 // fail, move on the edge
2106 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002107 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002108 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2109 path1.lineTo(r1[index].fX, r1[index].fY);
2110 }
2111 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002112 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002113
caryclark@google.comf1316942011-07-26 19:54:45 +00002114 // fail, quad
2115 path1.reset();
2116 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002117 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002118 if (index == 2) {
2119 path1.quadTo(1, .5f, 1, .5f);
2120 }
2121 path1.lineTo(r1[index].fX, r1[index].fY);
2122 }
2123 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002124 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002125
caryclark@google.comf1316942011-07-26 19:54:45 +00002126 // fail, cubic
2127 path1.reset();
2128 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002129 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002130 if (index == 2) {
2131 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2132 }
2133 path1.lineTo(r1[index].fX, r1[index].fY);
2134 }
2135 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002136 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002137}
2138
bsalomonedc743a2016-06-01 09:42:31 -07002139static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2140 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2141 SkRect r = SkRect::MakeEmpty();
2142 SkPath::Direction d = SkPath::kCCW_Direction;
2143 unsigned s = ~0U;
2144
2145 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2146 REPORTER_ASSERT(reporter, r == rect);
2147 REPORTER_ASSERT(reporter, d == dir);
2148 REPORTER_ASSERT(reporter, s == start);
2149}
2150
2151static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2152 SkRect r = SkRect::MakeEmpty();
2153 SkPath::Direction d = SkPath::kCCW_Direction;
2154 unsigned s = ~0U;
2155
2156 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2157 const SkRect emptyRect = SkRect::MakeEmpty();
2158 SkPath path;
2159 for (int start = 0; start < 4; ++start) {
2160 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2161 SkPath path;
2162 path.addRect(testRect, dir, start);
2163 check_simple_closed_rect(reporter, path, testRect, dir, start);
2164 path.close();
2165 check_simple_closed_rect(reporter, path, testRect, dir, start);
2166 SkPath path2 = path;
2167 path2.lineTo(10, 10);
2168 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2169 path2 = path;
2170 path2.moveTo(10, 10);
2171 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2172 path2 = path;
2173 path2.addRect(testRect, dir, start);
2174 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2175 // Make the path by hand, manually closing it.
2176 path2.reset();
2177 SkPath::RawIter iter(path);
2178 SkPath::Verb v;
2179 SkPoint verbPts[4];
2180 SkPoint firstPt = {0.f, 0.f};
2181 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2182 switch(v) {
2183 case SkPath::kMove_Verb:
2184 firstPt = verbPts[0];
2185 path2.moveTo(verbPts[0]);
2186 break;
2187 case SkPath::kLine_Verb:
2188 path2.lineTo(verbPts[1]);
2189 break;
2190 default:
2191 break;
2192 }
2193 }
2194 // We haven't closed it yet...
2195 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2196 // ... now we do and test again.
2197 path2.lineTo(firstPt);
2198 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2199 // A redundant close shouldn't cause a failure.
2200 path2.close();
2201 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2202 // Degenerate point and line rects are not allowed
2203 path2.reset();
2204 path2.addRect(emptyRect, dir, start);
2205 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2206 SkRect degenRect = testRect;
2207 degenRect.fLeft = degenRect.fRight;
2208 path2.reset();
2209 path2.addRect(degenRect, dir, start);
2210 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2211 degenRect = testRect;
2212 degenRect.fTop = degenRect.fBottom;
2213 path2.reset();
2214 path2.addRect(degenRect, dir, start);
2215 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2216 // An inverted rect makes a rect path, but changes the winding dir and start point.
2217 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2218 ? SkPath::kCCW_Direction
2219 : SkPath::kCW_Direction;
2220 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2221 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2222 SkRect swapRect = testRect;
2223 SkTSwap(swapRect.fLeft, swapRect.fRight);
2224 path2.reset();
2225 path2.addRect(swapRect, dir, start);
2226 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2227 swapRect = testRect;
2228 SkTSwap(swapRect.fTop, swapRect.fBottom);
2229 path2.reset();
2230 path2.addRect(swapRect, dir, start);
2231 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2232 }
2233 }
bsalomon057ae8a2016-07-24 05:37:26 -07002234 // down, up, left, close
2235 path.reset();
2236 path.moveTo(1, 1);
2237 path.lineTo(1, 2);
2238 path.lineTo(1, 1);
2239 path.lineTo(0, 1);
2240 SkRect rect;
2241 SkPath::Direction dir;
2242 unsigned start;
2243 path.close();
2244 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2245 // right, left, up, close
2246 path.reset();
2247 path.moveTo(1, 1);
2248 path.lineTo(2, 1);
2249 path.lineTo(1, 1);
2250 path.lineTo(1, 0);
2251 path.close();
2252 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2253 // parallelogram with horizontal edges
2254 path.reset();
2255 path.moveTo(1, 0);
2256 path.lineTo(3, 0);
2257 path.lineTo(2, 1);
2258 path.lineTo(0, 1);
2259 path.close();
2260 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2261 // parallelogram with vertical edges
2262 path.reset();
2263 path.moveTo(0, 1);
2264 path.lineTo(0, 3);
2265 path.lineTo(1, 2);
2266 path.lineTo(1, 0);
2267 path.close();
2268 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2269
bsalomonedc743a2016-06-01 09:42:31 -07002270}
2271
caryclark95bc5f32015-04-08 08:34:15 -07002272static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002273 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002274 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002275 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2276 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2277 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002278 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002279 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2280 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2281 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2282 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002283 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2284 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002285 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2286 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2287 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002288
2289 // failing tests
2290 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2291 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2292 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2293 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2294 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2295 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2296 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2297 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2298
caryclark95bc5f32015-04-08 08:34:15 -07002299 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002300 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2301 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2302
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002303 struct IsNestedRectTest {
2304 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002305 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002306 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002307 bool fClose;
2308 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2309 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002310 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2311 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2312 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2313 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2314 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2315 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2316 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2317 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2318 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2319 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2320 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2321 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2322 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2323 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002324
reed026beb52015-06-10 14:23:15 -07002325 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2326 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2327 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2328 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2329 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2330 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2331 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2332 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002333
reed026beb52015-06-10 14:23:15 -07002334 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2335 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002336 };
2337
2338 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002339 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002340 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002341 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2342 SkPath path;
2343 if (rectFirst) {
2344 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2345 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002346 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2347 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2348 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002349 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002350 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002351 path.close();
2352 }
2353 if (!rectFirst) {
2354 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2355 }
caryclark95bc5f32015-04-08 08:34:15 -07002356 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002357 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002358 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002359 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002360 SkPathPriv::FirstDirection expectedDirs[2];
2361 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002362 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002363 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002364 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2365 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002366 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002367 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002368 } else {
reed026beb52015-06-10 14:23:15 -07002369 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002370 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002371 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002372 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002373 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2374 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002375 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2376 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002377 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002378 }
2379
2380 // fail, close then line
2381 SkPath path1;
2382 if (rectFirst) {
2383 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2384 }
2385 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002386 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002387 path1.lineTo(r1[index].fX, r1[index].fY);
2388 }
2389 path1.close();
2390 path1.lineTo(1, 0);
2391 if (!rectFirst) {
2392 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2393 }
halcanary96fcdcc2015-08-27 07:41:13 -07002394 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002395
2396 // fail, move in the middle
2397 path1.reset();
2398 if (rectFirst) {
2399 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2400 }
2401 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002402 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002403 if (index == 2) {
2404 path1.moveTo(1, .5f);
2405 }
2406 path1.lineTo(r1[index].fX, r1[index].fY);
2407 }
2408 path1.close();
2409 if (!rectFirst) {
2410 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2411 }
halcanary96fcdcc2015-08-27 07:41:13 -07002412 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002413
2414 // fail, move on the edge
2415 path1.reset();
2416 if (rectFirst) {
2417 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2418 }
bsalomon98806072014-12-12 15:11:17 -08002419 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002420 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2421 path1.lineTo(r1[index].fX, r1[index].fY);
2422 }
2423 path1.close();
2424 if (!rectFirst) {
2425 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2426 }
halcanary96fcdcc2015-08-27 07:41:13 -07002427 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002428
2429 // fail, quad
2430 path1.reset();
2431 if (rectFirst) {
2432 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2433 }
2434 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002435 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002436 if (index == 2) {
2437 path1.quadTo(1, .5f, 1, .5f);
2438 }
2439 path1.lineTo(r1[index].fX, r1[index].fY);
2440 }
2441 path1.close();
2442 if (!rectFirst) {
2443 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2444 }
halcanary96fcdcc2015-08-27 07:41:13 -07002445 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002446
2447 // fail, cubic
2448 path1.reset();
2449 if (rectFirst) {
2450 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2451 }
2452 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002453 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002454 if (index == 2) {
2455 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2456 }
2457 path1.lineTo(r1[index].fX, r1[index].fY);
2458 }
2459 path1.close();
2460 if (!rectFirst) {
2461 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2462 }
halcanary96fcdcc2015-08-27 07:41:13 -07002463 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002464
caryclark@google.com56f233a2012-11-19 13:06:06 +00002465 // fail, not nested
2466 path1.reset();
2467 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2468 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002469 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002470 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002471
caryclark95bc5f32015-04-08 08:34:15 -07002472 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2473 SkPath path;
2474 path.moveTo(0, 0);
2475 path.lineTo(10, 0);
2476 path.lineTo(10, 10);
2477 path.lineTo(0, 10);
2478 path.lineTo(0, 0);
2479 path.moveTo(1, 1);
2480 path.lineTo(9, 1);
2481 path.lineTo(9, 9);
2482 path.lineTo(1, 9);
2483 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002484 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002485
caryclark@google.combfe90372012-11-21 13:56:20 +00002486 // pass, stroke rect
2487 SkPath src, dst;
2488 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2489 SkPaint strokePaint;
2490 strokePaint.setStyle(SkPaint::kStroke_Style);
2491 strokePaint.setStrokeWidth(2);
2492 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002493 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002494}
2495
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002496static void write_and_read_back(skiatest::Reporter* reporter,
2497 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002498 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002499 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002500 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002501 SkAutoMalloc storage(size);
2502 writer.flatten(storage.get());
2503 SkReader32 reader(storage.get(), size);
2504
2505 SkPath readBack;
2506 REPORTER_ASSERT(reporter, readBack != p);
2507 reader.readPath(&readBack);
2508 REPORTER_ASSERT(reporter, readBack == p);
2509
rmistry@google.comd6176b02012-08-23 18:14:13 +00002510 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002511 p.getConvexityOrUnknown());
2512
bsalomon78d58d12016-05-27 09:17:04 -07002513 SkRect oval0, oval1;
2514 SkPath::Direction dir0, dir1;
2515 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002516 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002517 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2518 REPORTER_ASSERT(reporter, oval0 == oval1);
2519 REPORTER_ASSERT(reporter, dir0 == dir1);
2520 REPORTER_ASSERT(reporter, start0 == start1);
2521 }
2522 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2523 SkRRect rrect0, rrect1;
2524 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2525 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2526 REPORTER_ASSERT(reporter, dir0 == dir1);
2527 REPORTER_ASSERT(reporter, start0 == start1);
2528 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002529 const SkRect& origBounds = p.getBounds();
2530 const SkRect& readBackBounds = readBack.getBounds();
2531
2532 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2533}
2534
reed@google.com53effc52011-09-21 19:05:12 +00002535static void test_flattening(skiatest::Reporter* reporter) {
2536 SkPath p;
2537
2538 static const SkPoint pts[] = {
2539 { 0, 0 },
2540 { SkIntToScalar(10), SkIntToScalar(10) },
2541 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2542 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2543 };
2544 p.moveTo(pts[0]);
2545 p.lineTo(pts[1]);
2546 p.quadTo(pts[2], pts[3]);
2547 p.cubicTo(pts[4], pts[5], pts[6]);
2548
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002549 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002550
2551 // create a buffer that should be much larger than the path so we don't
2552 // kill our stack if writer goes too far.
2553 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002554 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002555 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002556 REPORTER_ASSERT(reporter, size1 == size2);
2557
2558 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002559 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002560 REPORTER_ASSERT(reporter, size1 == size3);
2561 REPORTER_ASSERT(reporter, p == p2);
2562
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002563 size3 = p2.readFromMemory(buffer, 0);
2564 REPORTER_ASSERT(reporter, !size3);
2565
2566 SkPath tooShort;
2567 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2568 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2569
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002570 char buffer2[1024];
2571 size3 = p2.writeToMemory(buffer2);
2572 REPORTER_ASSERT(reporter, size1 == size3);
2573 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002574
2575 // test persistence of the oval flag & convexity
2576 {
2577 SkPath oval;
2578 SkRect rect = SkRect::MakeWH(10, 10);
2579 oval.addOval(rect);
2580
2581 write_and_read_back(reporter, oval);
2582 }
reed@google.com53effc52011-09-21 19:05:12 +00002583}
2584
2585static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002586 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002587
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002588#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002589 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002590 { 0, 0 }, // move
2591 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2592 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2593 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2594#if CONIC_PERSPECTIVE_BUG_FIXED
2595 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2596#endif
reed@google.com53effc52011-09-21 19:05:12 +00002597 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002598 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002599
reed@google.com53effc52011-09-21 19:05:12 +00002600 p.moveTo(pts[0]);
2601 p.lineTo(pts[1]);
2602 p.quadTo(pts[2], pts[3]);
2603 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002604#if CONIC_PERSPECTIVE_BUG_FIXED
2605 p.conicTo(pts[4], pts[5], 0.5f);
2606#endif
2607 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002608
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002609 {
2610 SkMatrix matrix;
2611 matrix.reset();
2612 SkPath p1;
2613 p.transform(matrix, &p1);
2614 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002615 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002616
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002617
2618 {
2619 SkMatrix matrix;
2620 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2621
2622 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2623
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002624 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002625 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002626 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002627 REPORTER_ASSERT(reporter, kPtCount == count);
2628 for (int i = 0; i < count; ++i) {
2629 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2630 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2631 }
2632 }
2633
2634 {
2635 SkMatrix matrix;
2636 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002637 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002638
2639 SkPath p1;
2640 p1.moveTo(SkPoint::Make(0, 0));
2641
2642 p.transform(matrix, &p1);
2643 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002644 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002645 SkRect pBounds = p.getBounds();
2646 SkRect p1Bounds = p1.getBounds();
2647 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2648 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2649 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2650 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2651 }
2652
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002653 p.reset();
2654 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002655
2656 {
2657 SkMatrix matrix;
2658 matrix.reset();
2659 SkPath p1;
2660 p1.moveTo(SkPoint::Make(0, 0));
2661
2662 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002663 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002664 }
2665
2666
2667 {
2668 SkMatrix matrix;
2669 matrix.reset();
2670 matrix.setScaleX(-1);
2671 SkPath p1;
2672 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2673
2674 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002675 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002676 }
2677
2678 {
2679 SkMatrix matrix;
2680 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2681 SkPath p1;
2682 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2683
2684 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002685 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002686 }
reed@google.com53effc52011-09-21 19:05:12 +00002687}
2688
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002689static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002690 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002691 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002692
caryclark@google.com56f233a2012-11-19 13:06:06 +00002693 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002694 const char* testPath;
2695 const size_t numResultPts;
2696 const SkRect resultBound;
2697 const SkPath::Verb* resultVerbs;
2698 const size_t numResultVerbs;
2699 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002700
schenney@chromium.org7e963602012-06-13 17:05:43 +00002701 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2702 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2703 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2704 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2705 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2706 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2707 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2708 static const SkPath::Verb resultVerbs8[] = {
2709 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2710 };
2711 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2712 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2713 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2714 static const SkPath::Verb resultVerbs12[] = {
2715 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2716 };
2717 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2718 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2719 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2720 static const SkPath::Verb resultVerbs16[] = {
2721 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2722 };
2723 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002724 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002725 { "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 -07002726 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002727 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2728 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2729 { "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) },
2730 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2731 { "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) },
2732 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2733 { "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) },
2734 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2735 { "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) },
2736 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2737 { "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 +00002738 SK_ARRAY_COUNT(resultVerbs14)
2739 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002740 { "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) },
2741 { "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 +00002742 SK_ARRAY_COUNT(resultVerbs16)
2743 }
2744 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002745
schenney@chromium.org7e963602012-06-13 17:05:43 +00002746 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2747 p.reset();
2748 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2749 REPORTER_ASSERT(reporter, valid);
2750 REPORTER_ASSERT(reporter, !p.isEmpty());
2751 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2752 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2753 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2754 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2755 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2756 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002757 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002758}
2759
2760struct SegmentInfo {
2761 SkPath fPath;
2762 int fPointCount;
2763};
2764
reed@google.com10296cc2011-09-21 12:29:05 +00002765#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2766
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002767static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002768 SkPath p, p2;
2769
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002770 p.moveTo(0, 0);
2771 p.quadTo(100, 100, 200, 200);
2772 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2773 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002774 p2 = p;
2775 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002776 p.cubicTo(100, 100, 200, 200, 300, 300);
2777 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2778 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002779 p2 = p;
2780 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2781
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002782 p.reset();
2783 p.moveTo(0, 0);
2784 p.cubicTo(100, 100, 200, 200, 300, 300);
2785 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002786 p2 = p;
2787 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002788
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002789 REPORTER_ASSERT(reporter, !p.isEmpty());
2790}
2791
2792static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002793 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002794 SkPoint pts[4];
2795
2796 // Test an iterator with no path
2797 SkPath::Iter noPathIter;
2798 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002799
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002800 // Test that setting an empty path works
2801 noPathIter.setPath(p, false);
2802 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002803
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002804 // Test that close path makes no difference for an empty path
2805 noPathIter.setPath(p, true);
2806 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002807
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002808 // Test an iterator with an initial empty path
2809 SkPath::Iter iter(p, false);
2810 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2811
2812 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002813 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002814 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2815
rmistry@google.comd6176b02012-08-23 18:14:13 +00002816
schenney@chromium.org7e963602012-06-13 17:05:43 +00002817 struct iterTestData {
2818 const char* testPath;
2819 const bool forceClose;
2820 const bool consumeDegenerates;
2821 const size_t* numResultPtsPerVerb;
2822 const SkPoint* resultPts;
2823 const SkPath::Verb* resultVerbs;
2824 const size_t numResultVerbs;
2825 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002826
schenney@chromium.org7e963602012-06-13 17:05:43 +00002827 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2828 static const SkPath::Verb resultVerbs2[] = {
2829 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2830 };
2831 static const SkPath::Verb resultVerbs3[] = {
2832 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2833 };
2834 static const SkPath::Verb resultVerbs4[] = {
2835 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2836 };
2837 static const SkPath::Verb resultVerbs5[] = {
2838 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2839 };
2840 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002841 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2842 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2843 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2844 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002845 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002846 static const SkPoint resultPts2[] = {
2847 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2848 };
2849 static const SkPoint resultPts3[] = {
2850 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2851 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2852 };
2853 static const SkPoint resultPts4[] = {
2854 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2855 };
2856 static const SkPoint resultPts5[] = {
2857 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2858 };
2859 static const struct iterTestData gIterTests[] = {
2860 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002861 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2862 { "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 +00002863 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2864 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2865 { "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) },
2866 { "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 +00002867 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2868 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2869 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2870 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2871 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2872 { "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 +00002873 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002874
schenney@chromium.org7e963602012-06-13 17:05:43 +00002875 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2876 p.reset();
2877 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2878 REPORTER_ASSERT(reporter, valid);
2879 iter.setPath(p, gIterTests[i].forceClose);
2880 int j = 0, l = 0;
2881 do {
2882 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2883 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2884 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2885 }
2886 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2887 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2888 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002889
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002890 p.reset();
2891 iter.setPath(p, false);
2892 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2893 p.lineTo(1, 1);
2894 p.close();
2895 iter.setPath(p, false);
2896 REPORTER_ASSERT(reporter, iter.isClosedContour());
2897 p.reset();
2898 iter.setPath(p, true);
2899 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2900 p.lineTo(1, 1);
2901 iter.setPath(p, true);
2902 REPORTER_ASSERT(reporter, iter.isClosedContour());
2903 p.moveTo(0, 0);
2904 p.lineTo(2, 2);
2905 iter.setPath(p, false);
2906 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2907
2908 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2909 // check to see if the result is correct.
2910 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2911 p.reset();
2912 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2913 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2914 iter.setPath(p, true);
2915 iter.next(pts, false);
2916 iter.next(pts, false);
2917 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2918 }
2919
2920 p.reset();
2921 p.quadTo(0, 0, 0, 0);
2922 iter.setPath(p, false);
2923 iter.next(pts, false);
2924 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2925 iter.setPath(p, false);
2926 iter.next(pts, false);
2927 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2928
2929 p.reset();
2930 p.conicTo(0, 0, 0, 0, 0.5f);
2931 iter.setPath(p, false);
2932 iter.next(pts, false);
2933 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2934 iter.setPath(p, false);
2935 iter.next(pts, false);
2936 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2937
2938 p.reset();
2939 p.cubicTo(0, 0, 0, 0, 0, 0);
2940 iter.setPath(p, false);
2941 iter.next(pts, false);
2942 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2943 iter.setPath(p, false);
2944 iter.next(pts, false);
2945 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2946
2947 p.moveTo(1, 1); // add a trailing moveto
2948 iter.setPath(p, false);
2949 iter.next(pts, false);
2950 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2951 iter.setPath(p, false);
2952 iter.next(pts, false);
2953 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2954
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002955 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002956
2957 // Test out mixed degenerate and non-degenerate geometry with Conics
2958 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2959 SkRect r = SkRect::MakeWH(100, 100);
2960 SkRRect rr;
2961 rr.setRectRadii(r, radii);
2962 p.reset();
2963 p.addRRect(rr);
2964 iter.setPath(p, false);
2965 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2966 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2967 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2968 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2969 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002970}
2971
2972static void test_raw_iter(skiatest::Reporter* reporter) {
2973 SkPath p;
2974 SkPoint pts[4];
2975
2976 // Test an iterator with no path
2977 SkPath::RawIter noPathIter;
2978 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2979 // Test that setting an empty path works
2980 noPathIter.setPath(p);
2981 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002982
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002983 // Test an iterator with an initial empty path
2984 SkPath::RawIter iter(p);
2985 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2986
2987 // Test that a move-only path returns the move.
2988 p.moveTo(SK_Scalar1, 0);
2989 iter.setPath(p);
2990 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2991 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2992 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2993 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2994
2995 // No matter how many moves we add, we should get them all back
2996 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2997 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2998 iter.setPath(p);
2999 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3000 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3001 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3002 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3003 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3004 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3005 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3006 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3007 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3008 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3009
3010 // Initial close is never ever stored
3011 p.reset();
3012 p.close();
3013 iter.setPath(p);
3014 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3015
3016 // Move/close sequences
3017 p.reset();
3018 p.close(); // Not stored, no purpose
3019 p.moveTo(SK_Scalar1, 0);
3020 p.close();
3021 p.close(); // Not stored, no purpose
3022 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3023 p.close();
3024 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3025 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3026 p.close();
3027 iter.setPath(p);
3028 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3029 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3030 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3031 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003032 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3033 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3034 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3035 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003036 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3037 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3038 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3039 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3040 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3041 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3042 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003043 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3044
3045 // Generate random paths and verify
3046 SkPoint randomPts[25];
3047 for (int i = 0; i < 5; ++i) {
3048 for (int j = 0; j < 5; ++j) {
3049 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3050 }
3051 }
3052
3053 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003054 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003055 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003056 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003057 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003058
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003059 for (int i = 0; i < 500; ++i) {
3060 p.reset();
3061 bool lastWasClose = true;
3062 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003063 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003064 int numPoints = 0;
3065 int numVerbs = (rand.nextU() >> 16) % 10;
3066 int numIterVerbs = 0;
3067 for (int j = 0; j < numVerbs; ++j) {
3068 do {
3069 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3070 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003071 switch (nextVerb) {
3072 case SkPath::kMove_Verb:
3073 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3074 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003075 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003076 numPoints += 1;
3077 lastWasClose = false;
3078 haveMoveTo = true;
3079 break;
3080 case SkPath::kLine_Verb:
3081 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003082 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003083 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3084 haveMoveTo = true;
3085 }
3086 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3087 p.lineTo(expectedPts[numPoints]);
3088 numPoints += 1;
3089 lastWasClose = false;
3090 break;
3091 case SkPath::kQuad_Verb:
3092 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003093 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003094 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3095 haveMoveTo = true;
3096 }
3097 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3098 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3099 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3100 numPoints += 2;
3101 lastWasClose = false;
3102 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003103 case SkPath::kConic_Verb:
3104 if (!haveMoveTo) {
3105 expectedPts[numPoints++] = lastMoveToPt;
3106 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3107 haveMoveTo = true;
3108 }
3109 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3110 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3111 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3112 rand.nextUScalar1() * 4);
3113 numPoints += 2;
3114 lastWasClose = false;
3115 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003116 case SkPath::kCubic_Verb:
3117 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003118 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003119 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3120 haveMoveTo = true;
3121 }
3122 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3123 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3124 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3125 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3126 expectedPts[numPoints + 2]);
3127 numPoints += 3;
3128 lastWasClose = false;
3129 break;
3130 case SkPath::kClose_Verb:
3131 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003132 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003133 lastWasClose = true;
3134 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003135 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003136 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003137 }
3138 expectedVerbs[numIterVerbs++] = nextVerb;
3139 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003140
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003141 iter.setPath(p);
3142 numVerbs = numIterVerbs;
3143 numIterVerbs = 0;
3144 int numIterPts = 0;
3145 SkPoint lastMoveTo;
3146 SkPoint lastPt;
3147 lastMoveTo.set(0, 0);
3148 lastPt.set(0, 0);
3149 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3150 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3151 numIterVerbs++;
3152 switch (nextVerb) {
3153 case SkPath::kMove_Verb:
3154 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3155 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3156 lastPt = lastMoveTo = pts[0];
3157 numIterPts += 1;
3158 break;
3159 case SkPath::kLine_Verb:
3160 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3161 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3162 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3163 lastPt = pts[1];
3164 numIterPts += 1;
3165 break;
3166 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003167 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003168 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3169 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3170 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3171 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3172 lastPt = pts[2];
3173 numIterPts += 2;
3174 break;
3175 case SkPath::kCubic_Verb:
3176 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3177 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3178 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3179 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3180 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3181 lastPt = pts[3];
3182 numIterPts += 3;
3183 break;
3184 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003185 lastPt = lastMoveTo;
3186 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003187 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003188 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003189 }
3190 }
3191 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3192 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3193 }
3194}
3195
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003196static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003197 const SkPath& path,
3198 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003199 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003200 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003201 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003202 SkPath::Direction isOvalDir;
3203 unsigned isOvalStart;
3204 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003205 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003206 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3207 SkPath tmpPath;
3208 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3209 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003210 }
bsalomon78d58d12016-05-27 09:17:04 -07003211 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003212}
3213
3214static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003215 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003216 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003217 SkPath tmp;
3218
3219 SkMatrix m;
3220 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3221 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003222 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003223 if (SkPathPriv::kCCW_FirstDirection == dir) {
3224 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003225 } else {
reed026beb52015-06-10 14:23:15 -07003226 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3227 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003228 }
3229 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003230}
3231
3232static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003233 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003234 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003235 SkPath tmp;
3236
3237 // translate at small offset
3238 SkMatrix m;
3239 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3240 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003241 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003242
3243 tmp.reset();
3244 m.reset();
3245
3246 // translate at a relatively big offset
3247 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3248 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003249 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003250}
3251
3252static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003253 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003254 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003255 for (int angle = 0; angle < 360; ++angle) {
3256 SkPath tmp;
3257 SkMatrix m;
3258 m.setRotate(SkIntToScalar(angle));
3259 path.transform(m, &tmp);
3260
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003261 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003262 // degrees is not an oval anymore, this can be improved. we made this
3263 // for the simplicity of our implementation.
3264 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003265 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003266 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003267 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003268 }
3269 }
3270}
3271
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003272static void test_circle_mirror_x(skiatest::Reporter* reporter,
3273 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003274 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003275 SkPath tmp;
3276 SkMatrix m;
3277 m.reset();
3278 m.setScaleX(-SK_Scalar1);
3279 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003280 if (SkPathPriv::kCW_FirstDirection == dir) {
3281 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003282 } else {
reed026beb52015-06-10 14:23:15 -07003283 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3284 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003285 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003286 check_for_circle(reporter, tmp, true, dir);
3287}
3288
3289static void test_circle_mirror_y(skiatest::Reporter* reporter,
3290 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003291 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003292 SkPath tmp;
3293 SkMatrix m;
3294 m.reset();
3295 m.setScaleY(-SK_Scalar1);
3296 path.transform(m, &tmp);
3297
reed026beb52015-06-10 14:23:15 -07003298 if (SkPathPriv::kCW_FirstDirection == dir) {
3299 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003300 } else {
reed026beb52015-06-10 14:23:15 -07003301 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3302 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003303 }
3304
3305 check_for_circle(reporter, tmp, true, dir);
3306}
3307
3308static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3309 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003310 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003311 SkPath tmp;
3312 SkMatrix m;
3313 m.reset();
3314 m.setScaleX(-SK_Scalar1);
3315 m.setScaleY(-SK_Scalar1);
3316 path.transform(m, &tmp);
3317
3318 check_for_circle(reporter, tmp, true, dir);
3319}
3320
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003321static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003322 SkPath::Direction inDir) {
3323 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003324 SkPath path;
3325
3326 // circle at origin
reed026beb52015-06-10 14:23:15 -07003327 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3328
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003329 check_for_circle(reporter, path, true, dir);
3330 test_circle_rotate(reporter, path, dir);
3331 test_circle_translate(reporter, path, dir);
3332 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003333 test_circle_mirror_x(reporter, path, dir);
3334 test_circle_mirror_y(reporter, path, dir);
3335 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003336
3337 // circle at an offset at (10, 10)
3338 path.reset();
3339 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003340 SkIntToScalar(20), inDir);
3341
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003342 check_for_circle(reporter, path, true, dir);
3343 test_circle_rotate(reporter, path, dir);
3344 test_circle_translate(reporter, path, dir);
3345 test_circle_skew(reporter, path, dir);
3346 test_circle_mirror_x(reporter, path, dir);
3347 test_circle_mirror_y(reporter, path, dir);
3348 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003349
3350 // Try different starting points for the contour.
3351 for (unsigned start = 0; start < 4; ++start) {
3352 path.reset();
3353 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3354 test_circle_rotate(reporter, path, dir);
3355 test_circle_translate(reporter, path, dir);
3356 test_circle_skew(reporter, path, dir);
3357 test_circle_mirror_x(reporter, path, dir);
3358 test_circle_mirror_y(reporter, path, dir);
3359 test_circle_mirror_xy(reporter, path, dir);
3360 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003361}
3362
3363static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3364 SkPath path;
3365 SkPath circle;
3366 SkPath rect;
3367 SkPath empty;
3368
reed026beb52015-06-10 14:23:15 -07003369 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3370 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003371
3372 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003373 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3374 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3375
3376 SkMatrix translate;
3377 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3378
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003379 // Although all the path concatenation related operations leave
3380 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003381
3382 // empty + circle (translate)
3383 path = empty;
3384 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003385 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003386
3387 // circle + empty (translate)
3388 path = circle;
3389 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003390
reed026beb52015-06-10 14:23:15 -07003391 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003392
3393 // test reverseAddPath
3394 path = circle;
3395 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003396 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003397}
3398
3399static void test_circle(skiatest::Reporter* reporter) {
3400 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3401 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3402
3403 // multiple addCircle()
3404 SkPath path;
3405 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3406 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003407 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003408
3409 // some extra lineTo() would make isOval() fail
3410 path.reset();
3411 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3412 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003413 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003414
3415 // not back to the original point
3416 path.reset();
3417 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3418 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003419 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003420
3421 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003422
3423 // test negative radius
3424 path.reset();
3425 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3426 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003427}
3428
3429static void test_oval(skiatest::Reporter* reporter) {
3430 SkRect rect;
3431 SkMatrix m;
3432 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003433 unsigned start = 0;
3434 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003435
3436 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3437 path.addOval(rect);
3438
bsalomon78d58d12016-05-27 09:17:04 -07003439 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003440 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003441
3442 m.setRotate(SkIntToScalar(90));
3443 SkPath tmp;
3444 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003445 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3446 // is unchanged.
3447 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3448 REPORTER_ASSERT(reporter, 2 == start);
3449 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003450
3451 m.reset();
3452 m.setRotate(SkIntToScalar(30));
3453 tmp.reset();
3454 path.transform(m, &tmp);
3455 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003456 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003457
3458 // since empty path being transformed.
3459 path.reset();
3460 tmp.reset();
3461 m.reset();
3462 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003463 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003464
3465 // empty path is not an oval
3466 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003467 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003468
3469 // only has moveTo()s
3470 tmp.reset();
3471 tmp.moveTo(0, 0);
3472 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003473 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003474
3475 // mimic WebKit's calling convention,
3476 // call moveTo() first and then call addOval()
3477 path.reset();
3478 path.moveTo(0, 0);
3479 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003480 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003481
3482 // copy path
3483 path.reset();
3484 tmp.reset();
3485 tmp.addOval(rect);
3486 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003487 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3488 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3489 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003490}
3491
bungeman@google.coma5809a32013-06-21 15:13:34 +00003492static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3493 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003494
reed@android.com3abec1d2009-03-02 05:36:20 +00003495 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003496 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003497 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003498 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003499 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003500 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3501 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003502 REPORTER_ASSERT(reporter, p == empty);
3503 REPORTER_ASSERT(reporter, !(p != empty));
3504}
3505
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003506static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3507 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003508 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003509 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003510 path->setConvexity(SkPath::kUnknown_Convexity);
3511 REPORTER_ASSERT(reporter, path->isConvex());
3512 path->reset();
3513}
3514
caryclarkd3d1a982014-12-08 04:57:38 -08003515static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3516 SkPath::Direction dir) {
3517 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003518 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003519 path->setConvexity(SkPath::kUnknown_Convexity);
3520 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3521 path->reset();
3522}
3523
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003524static void test_rrect(skiatest::Reporter* reporter) {
3525 SkPath p;
3526 SkRRect rr;
3527 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3528 SkRect r = {10, 20, 30, 40};
3529 rr.setRectRadii(r, radii);
3530 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003531 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003532 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003533 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003534 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003535 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003536 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003537 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003538 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003539 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003540 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003541 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3542 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3543 SkVector save = radii[i];
3544 radii[i].set(0, 0);
3545 rr.setRectRadii(r, radii);
3546 p.addRRect(rr);
3547 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3548 radii[i] = save;
3549 }
3550 p.addRoundRect(r, 0, 0);
3551 SkRect returnedRect;
3552 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3553 REPORTER_ASSERT(reporter, returnedRect == r);
3554 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3555 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3556 rr.setRectRadii(r, zeroRadii);
3557 p.addRRect(rr);
3558 bool closed;
3559 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003560 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003561 REPORTER_ASSERT(reporter, closed);
3562 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3563 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3564 p.addRRect(rr, SkPath::kCW_Direction);
3565 p.addRRect(rr, SkPath::kCW_Direction);
3566 REPORTER_ASSERT(reporter, !p.isConvex());
3567 p.reset();
3568 p.addRRect(rr, SkPath::kCCW_Direction);
3569 p.addRRect(rr, SkPath::kCCW_Direction);
3570 REPORTER_ASSERT(reporter, !p.isConvex());
3571 p.reset();
3572 SkRect emptyR = {10, 20, 10, 30};
3573 rr.setRectRadii(emptyR, radii);
3574 p.addRRect(rr);
3575 REPORTER_ASSERT(reporter, p.isEmpty());
3576 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3577 rr.setRectRadii(largeR, radii);
3578 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003579 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003580
3581 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003582 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3583 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003584 REPORTER_ASSERT(reporter, rr.isEmpty());
3585
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003586 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3587 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3588 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003589}
3590
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003591static void test_arc(skiatest::Reporter* reporter) {
3592 SkPath p;
3593 SkRect emptyOval = {10, 20, 30, 20};
3594 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3595 p.addArc(emptyOval, 1, 2);
3596 REPORTER_ASSERT(reporter, p.isEmpty());
3597 p.reset();
3598 SkRect oval = {10, 20, 30, 40};
3599 p.addArc(oval, 1, 0);
3600 REPORTER_ASSERT(reporter, p.isEmpty());
3601 p.reset();
3602 SkPath cwOval;
3603 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003604 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003605 REPORTER_ASSERT(reporter, p == cwOval);
3606 p.reset();
3607 SkPath ccwOval;
3608 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003609 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003610 REPORTER_ASSERT(reporter, p == ccwOval);
3611 p.reset();
3612 p.addArc(oval, 1, 180);
3613 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003614 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003615 p.setConvexity(SkPath::kUnknown_Convexity);
3616 REPORTER_ASSERT(reporter, p.isConvex());
3617}
3618
bsalomon1978ce22016-05-31 10:42:16 -07003619static inline SkScalar oval_start_index_to_angle(unsigned start) {
3620 switch (start) {
3621 case 0:
3622 return 270.f;
3623 case 1:
3624 return 0.f;
3625 case 2:
3626 return 90.f;
3627 case 3:
3628 return 180.f;
3629 default:
3630 return -1.f;
3631 }
3632}
3633
3634static inline SkScalar canonical_start_angle(float angle) {
3635 while (angle < 0.f) {
3636 angle += 360.f;
3637 }
3638 while (angle >= 360.f) {
3639 angle -= 360.f;
3640 }
3641 return angle;
3642}
3643
3644static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3645 const SkPath& path) {
3646 SkRect r = SkRect::MakeEmpty();
3647 SkPath::Direction d = SkPath::kCCW_Direction;
3648 unsigned s = ~0U;
3649 bool isOval = path.isOval(&r, &d, &s);
3650 REPORTER_ASSERT(reporter, isOval);
3651 SkPath recreatedPath;
3652 recreatedPath.addOval(r, d, s);
3653 REPORTER_ASSERT(reporter, path == recreatedPath);
3654 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3655 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3656}
3657
3658static void test_arc_ovals(skiatest::Reporter* reporter) {
3659 SkRect oval = SkRect::MakeWH(10, 20);
3660 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3661 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3662 SkPath path;
3663 path.addArc(oval, start, sweep);
3664 // SkPath's interfaces for inserting and extracting ovals only allow contours
3665 // to start at multiples of 90 degrees.
3666 if (std::fmod(start, 90.f) == 0) {
3667 check_oval_arc(reporter, start, sweep, path);
3668 } else {
3669 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3670 }
3671 }
3672 // Test start angles that are nearly at valid oval start angles.
3673 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3674 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3675 SkPath path;
3676 path.addArc(oval, start + delta, sweep);
3677 check_oval_arc(reporter, start, sweep, path);
3678 }
3679 }
3680 }
3681}
3682
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003683static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3684 SkScalar x0, SkScalar y0) {
3685 SkPoint pts[4];
3686 SkPath::Verb v = iter->next(pts);
3687 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3688 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3689 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3690}
3691
3692static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3693 SkScalar x1, SkScalar y1) {
3694 SkPoint pts[4];
3695 SkPath::Verb v = iter->next(pts);
3696 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3697 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3698 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3699}
3700
3701static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3702 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3703 SkPoint pts[4];
3704 SkPath::Verb v = iter->next(pts);
3705 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3706 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3707 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3708 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3709 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3710}
3711
3712static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3713 SkPoint pts[4];
3714 SkPath::Verb v = iter->next(pts);
3715 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3716}
3717
3718static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3719 check_done(reporter, p, iter);
3720 p->reset();
3721}
3722
3723static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3724 SkScalar x0, SkScalar y0) {
3725 SkPath::RawIter iter(*p);
3726 check_move(reporter, &iter, x0, y0);
3727 check_done_and_reset(reporter, p, &iter);
3728}
3729
3730static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3731 SkScalar x1, SkScalar y1) {
3732 SkPath::RawIter iter(*p);
3733 check_move(reporter, &iter, 0, 0);
3734 check_line(reporter, &iter, x1, y1);
3735 check_done_and_reset(reporter, p, &iter);
3736}
3737
3738static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3739 SkScalar x1, SkScalar y1) {
3740 SkPath::RawIter iter(*p);
3741 check_move(reporter, &iter, 0, 0);
3742 check_line(reporter, &iter, x1, y1);
3743 check_done(reporter, p, &iter);
3744}
3745
3746static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3747 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3748 SkPath::RawIter iter(*p);
3749 check_move(reporter, &iter, 0, 0);
3750 check_line(reporter, &iter, x1, y1);
3751 check_line(reporter, &iter, x2, y2);
3752 check_done_and_reset(reporter, p, &iter);
3753}
3754
3755static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3756 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3757 SkPath::RawIter iter(*p);
3758 check_move(reporter, &iter, 0, 0);
3759 check_quad(reporter, &iter, x1, y1, x2, y2);
3760 check_done_and_reset(reporter, p, &iter);
3761}
3762
reedd5d27d92015-02-09 13:54:43 -08003763static bool nearly_equal(const SkRect& a, const SkRect& b) {
3764 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3765 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3766 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3767 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3768}
3769
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003770static void test_arcTo(skiatest::Reporter* reporter) {
3771 SkPath p;
3772 p.arcTo(0, 0, 1, 2, 1);
3773 check_path_is_line_and_reset(reporter, &p, 0, 0);
3774 p.arcTo(1, 2, 1, 2, 1);
3775 check_path_is_line_and_reset(reporter, &p, 1, 2);
3776 p.arcTo(1, 2, 3, 4, 0);
3777 check_path_is_line_and_reset(reporter, &p, 1, 2);
3778 p.arcTo(1, 2, 0, 0, 1);
3779 check_path_is_line_and_reset(reporter, &p, 1, 2);
3780 p.arcTo(1, 0, 1, 1, 1);
3781 SkPoint pt;
3782 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3783 p.reset();
3784 p.arcTo(1, 0, 1, -1, 1);
3785 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3786 p.reset();
3787 SkRect oval = {1, 2, 3, 4};
3788 p.arcTo(oval, 0, 0, true);
3789 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3790 p.arcTo(oval, 0, 0, false);
3791 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3792 p.arcTo(oval, 360, 0, true);
3793 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3794 p.arcTo(oval, 360, 0, false);
3795 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003796
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003797 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003798 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003799 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003800 sweep += delta;
3801 delta /= 2;
3802 }
3803 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003804 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003805 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003806 sweep -= delta;
3807 delta /= 2;
3808 }
3809 SkRect noOvalWidth = {1, 2, 0, 3};
3810 p.reset();
3811 p.arcTo(noOvalWidth, 0, 360, false);
3812 REPORTER_ASSERT(reporter, p.isEmpty());
3813
3814 SkRect noOvalHeight = {1, 2, 3, 1};
3815 p.reset();
3816 p.arcTo(noOvalHeight, 0, 360, false);
3817 REPORTER_ASSERT(reporter, p.isEmpty());
3818}
3819
3820static void test_addPath(skiatest::Reporter* reporter) {
3821 SkPath p, q;
3822 p.lineTo(1, 2);
3823 q.moveTo(4, 4);
3824 q.lineTo(7, 8);
3825 q.conicTo(8, 7, 6, 5, 0.5f);
3826 q.quadTo(6, 7, 8, 6);
3827 q.cubicTo(5, 6, 7, 8, 7, 5);
3828 q.close();
3829 p.addPath(q, -4, -4);
3830 SkRect expected = {0, 0, 4, 4};
3831 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3832 p.reset();
3833 p.reverseAddPath(q);
3834 SkRect reverseExpected = {4, 4, 8, 8};
3835 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3836}
3837
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003838static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3839 SkPath p, q;
3840 if (explicitMoveTo) {
3841 p.moveTo(1, 1);
3842 }
3843 p.lineTo(1, 2);
3844 if (explicitMoveTo) {
3845 q.moveTo(2, 1);
3846 }
3847 q.lineTo(2, 2);
3848 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3849 uint8_t verbs[4];
3850 int verbcount = p.getVerbs(verbs, 4);
3851 REPORTER_ASSERT(reporter, verbcount == 4);
3852 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3853 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3854 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3855 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3856}
3857
3858static void test_extendClosedPath(skiatest::Reporter* reporter) {
3859 SkPath p, q;
3860 p.moveTo(1, 1);
3861 p.lineTo(1, 2);
3862 p.lineTo(2, 2);
3863 p.close();
3864 q.moveTo(2, 1);
3865 q.lineTo(2, 3);
3866 p.addPath(q, SkPath::kExtend_AddPathMode);
3867 uint8_t verbs[7];
3868 int verbcount = p.getVerbs(verbs, 7);
3869 REPORTER_ASSERT(reporter, verbcount == 7);
3870 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3871 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3872 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3873 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3874 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3875 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3876 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3877
3878 SkPoint pt;
3879 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3880 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3881 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3882}
3883
3884static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3885 SkPath p, q, r;
3886 // case 1: dst is empty
3887 p.moveTo(2, 1);
3888 p.lineTo(2, 3);
3889 q.addPath(p, mode);
3890 REPORTER_ASSERT(reporter, q == p);
3891 // case 2: src is empty
3892 p.addPath(r, mode);
3893 REPORTER_ASSERT(reporter, q == p);
3894 // case 3: src and dst are empty
3895 q.reset();
3896 q.addPath(r, mode);
3897 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003898}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003899
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003900static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3901 SkPath p;
3902 p.conicTo(1, 2, 3, 4, -1);
3903 check_path_is_line_and_reset(reporter, &p, 3, 4);
3904 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3905 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3906 p.conicTo(1, 2, 3, 4, 1);
3907 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3908}
3909
3910static void test_get_point(skiatest::Reporter* reporter) {
3911 SkPath p;
3912 SkPoint pt = p.getPoint(0);
3913 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003914 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003915 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3916 p.setLastPt(10, 10);
3917 pt = p.getPoint(0);
3918 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003919 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003920 p.rMoveTo(10, 10);
3921 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3922}
3923
3924static void test_contains(skiatest::Reporter* reporter) {
3925 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003926 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3927 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
3928 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
3929 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3930 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
3931 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 -08003932 // this may return true or false, depending on the platform's numerics, but it should not crash
3933 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003934
3935 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003936 p.setFillType(SkPath::kInverseWinding_FillType);
3937 REPORTER_ASSERT(reporter, p.contains(0, 0));
3938 p.setFillType(SkPath::kWinding_FillType);
3939 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3940 p.moveTo(4, 4);
3941 p.lineTo(6, 8);
3942 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003943 // test on edge
3944 REPORTER_ASSERT(reporter, p.contains(6, 4));
3945 REPORTER_ASSERT(reporter, p.contains(5, 6));
3946 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003947 // test quick reject
3948 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3949 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3950 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3951 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3952 // test various crossings in x
3953 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3954 REPORTER_ASSERT(reporter, p.contains(6, 7));
3955 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3956 p.reset();
3957 p.moveTo(4, 4);
3958 p.lineTo(8, 6);
3959 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003960 // test on edge
3961 REPORTER_ASSERT(reporter, p.contains(4, 6));
3962 REPORTER_ASSERT(reporter, p.contains(6, 5));
3963 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003964 // test various crossings in y
3965 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3966 REPORTER_ASSERT(reporter, p.contains(7, 6));
3967 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003968 p.reset();
3969 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003970 p.lineTo(8, 4);
3971 p.lineTo(8, 8);
3972 p.lineTo(4, 8);
3973 // test on vertices
3974 REPORTER_ASSERT(reporter, p.contains(4, 4));
3975 REPORTER_ASSERT(reporter, p.contains(8, 4));
3976 REPORTER_ASSERT(reporter, p.contains(8, 8));
3977 REPORTER_ASSERT(reporter, p.contains(4, 8));
3978 p.reset();
3979 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003980 p.lineTo(6, 8);
3981 p.lineTo(2, 8);
3982 // test on edge
3983 REPORTER_ASSERT(reporter, p.contains(5, 6));
3984 REPORTER_ASSERT(reporter, p.contains(4, 8));
3985 REPORTER_ASSERT(reporter, p.contains(3, 6));
3986 p.reset();
3987 p.moveTo(4, 4);
3988 p.lineTo(0, 6);
3989 p.lineTo(4, 8);
3990 // test on edge
3991 REPORTER_ASSERT(reporter, p.contains(2, 5));
3992 REPORTER_ASSERT(reporter, p.contains(2, 7));
3993 REPORTER_ASSERT(reporter, p.contains(4, 6));
3994 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3995 p.reset();
3996 p.moveTo(4, 0);
3997 p.lineTo(6, 4);
3998 p.lineTo(2, 4);
3999 p.moveTo(4, 0);
4000 p.lineTo(0, 8);
4001 p.lineTo(8, 8);
4002 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4003 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4004 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4005 REPORTER_ASSERT(reporter, p.contains(4, 4));
4006
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004007 // test quads
4008 p.reset();
4009 p.moveTo(4, 4);
4010 p.quadTo(6, 6, 8, 8);
4011 p.quadTo(6, 8, 4, 8);
4012 p.quadTo(4, 6, 4, 4);
4013 REPORTER_ASSERT(reporter, p.contains(5, 6));
4014 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004015 // test quad edge
4016 REPORTER_ASSERT(reporter, p.contains(5, 5));
4017 REPORTER_ASSERT(reporter, p.contains(5, 8));
4018 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08004019 // test quad endpoints
4020 REPORTER_ASSERT(reporter, p.contains(4, 4));
4021 REPORTER_ASSERT(reporter, p.contains(8, 8));
4022 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004023
4024 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004025 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4026 p.moveTo(qPts[0]);
4027 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4028 p.quadTo(qPts[index], qPts[index + 1]);
4029 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004030 REPORTER_ASSERT(reporter, p.contains(5, 6));
4031 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004032 // test quad edge
4033 SkPoint halfway;
4034 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4035 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4036 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4037 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004038
caryclark9aacd902015-12-14 08:38:09 -08004039 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004040 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004041 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4042 p.moveTo(kPts[0]);
4043 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4044 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4045 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004046 REPORTER_ASSERT(reporter, p.contains(5, 6));
4047 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004048 // test conic edge
4049 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4050 SkConic conic(&kPts[index], 0.5f);
4051 halfway = conic.evalAt(0.5f);
4052 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4053 }
caryclark9cb5d752015-12-18 04:35:24 -08004054 // test conic end points
4055 REPORTER_ASSERT(reporter, p.contains(4, 4));
4056 REPORTER_ASSERT(reporter, p.contains(8, 8));
4057 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004058
4059 // test cubics
4060 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4061 for (int i = 0; i < 3; ++i) {
4062 p.reset();
4063 p.setFillType(SkPath::kEvenOdd_FillType);
4064 p.moveTo(pts[i].fX, pts[i].fY);
4065 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);
4066 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);
4067 p.close();
4068 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4069 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004070 // test cubic edge
4071 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4072 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4073 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4074 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004075 // test cubic end points
4076 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4077 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4078 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004079 }
4080}
4081
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004082class PathRefTest_Private {
4083public:
4084 static void TestPathRef(skiatest::Reporter* reporter) {
4085 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004086
bungeman6bd52842016-10-27 09:30:08 -07004087 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004088
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004089 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004090
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004091 {
4092 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4093 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4094 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4095 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4096 for (int i = 0; i < kRepeatCnt; ++i) {
4097 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4098 }
4099 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004100 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004101
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004102 {
4103 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4104 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4105 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4106 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4107 for (int i = 0; i < kRepeatCnt; ++i) {
4108 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4109 }
4110 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004111 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004112
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004113 {
4114 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4115 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4116 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4117 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4118 for (int i = 0; i < kRepeatCnt; ++i) {
4119 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4120 }
4121 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004122 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004123
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004124 {
halcanary96fcdcc2015-08-27 07:41:13 -07004125 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004126 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4127 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4128 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4129 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4130 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004131 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004132 for (int i = 0; i < kRepeatCnt; ++i) {
4133 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4134 }
4135 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004136 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004137
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004138 {
4139 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4140 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4141 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4142 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4143 for (int i = 0; i < kRepeatCnt; ++i) {
4144 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4145 }
4146 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004147 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004148 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004149};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004150
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004151static void test_operatorEqual(skiatest::Reporter* reporter) {
4152 SkPath a;
4153 SkPath b;
4154 REPORTER_ASSERT(reporter, a == a);
4155 REPORTER_ASSERT(reporter, a == b);
4156 a.setFillType(SkPath::kInverseWinding_FillType);
4157 REPORTER_ASSERT(reporter, a != b);
4158 a.reset();
4159 REPORTER_ASSERT(reporter, a == b);
4160 a.lineTo(1, 1);
4161 REPORTER_ASSERT(reporter, a != b);
4162 a.reset();
4163 REPORTER_ASSERT(reporter, a == b);
4164 a.lineTo(1, 1);
4165 b.lineTo(1, 2);
4166 REPORTER_ASSERT(reporter, a != b);
4167 a.reset();
4168 a.lineTo(1, 2);
4169 REPORTER_ASSERT(reporter, a == b);
4170}
4171
caryclark66a5d8b2014-06-24 08:30:15 -07004172static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004173 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004174 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004175 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004176 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004177 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004178 if (strlen(str) > 0) {
4179 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4180 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004181 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004182 }
caryclark66a5d8b2014-06-24 08:30:15 -07004183}
4184
4185static void test_dump(skiatest::Reporter* reporter) {
4186 SkPath p;
Cary Clark9f67f042016-12-16 11:32:54 -05004187 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4188 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004189 p.moveTo(1, 2);
4190 p.lineTo(3, 4);
Cary Clark9f67f042016-12-16 11:32:54 -05004191 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4192 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004193 "path.lineTo(3, 4);\n");
Cary Clark9f67f042016-12-16 11:32:54 -05004194 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4195 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004196 "path.lineTo(3, 4);\n"
4197 "path.lineTo(1, 2);\n"
4198 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004199 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004200 p.setFillType(SkPath::kEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004201 p.moveTo(1, 2);
4202 p.quadTo(3, 4, 5, 6);
Cary Clark9f67f042016-12-16 11:32:54 -05004203 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4204 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004205 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004206 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004207 p.setFillType(SkPath::kInverseWinding_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004208 p.moveTo(1, 2);
4209 p.conicTo(3, 4, 5, 6, 0.5f);
Cary Clark9f67f042016-12-16 11:32:54 -05004210 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4211 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004212 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004213 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004214 p.setFillType(SkPath::kInverseEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004215 p.moveTo(1, 2);
4216 p.cubicTo(3, 4, 5, 6, 7, 8);
Cary Clark9f67f042016-12-16 11:32:54 -05004217 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4218 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004219 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4220 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004221 p.setFillType(SkPath::kWinding_FillType);
caryclarke9562592014-09-15 09:26:09 -07004222 p.moveTo(1, 2);
4223 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004224 compare_dump(reporter, p, false, true,
Cary Clark9f67f042016-12-16 11:32:54 -05004225 "path.setFillType(SkPath::kWinding_FillType);\n"
caryclark08fa28c2014-10-23 13:08:56 -07004226 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4227 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004228 p.reset();
4229 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4230 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Cary Clark9f67f042016-12-16 11:32:54 -05004231 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4232 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004233 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004234}
4235
senorblanco84cd6212015-08-04 10:01:58 -07004236namespace {
4237
4238class ChangeListener : public SkPathRef::GenIDChangeListener {
4239public:
4240 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4241 virtual ~ChangeListener() {}
4242 void onChange() override {
4243 *fChanged = true;
4244 }
4245private:
4246 bool* fChanged;
4247};
4248
4249}
4250
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004251class PathTest_Private {
4252public:
4253 static void TestPathTo(skiatest::Reporter* reporter) {
4254 SkPath p, q;
4255 p.lineTo(4, 4);
4256 p.reversePathTo(q);
4257 check_path_is_line(reporter, &p, 4, 4);
4258 q.moveTo(-4, -4);
4259 p.reversePathTo(q);
4260 check_path_is_line(reporter, &p, 4, 4);
4261 q.lineTo(7, 8);
4262 q.conicTo(8, 7, 6, 5, 0.5f);
4263 q.quadTo(6, 7, 8, 6);
4264 q.cubicTo(5, 6, 7, 8, 7, 5);
4265 q.close();
4266 p.reversePathTo(q);
4267 SkRect reverseExpected = {-4, -4, 8, 8};
4268 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4269 }
senorblanco84cd6212015-08-04 10:01:58 -07004270
4271 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4272 SkPath p;
4273
4274 bool changed = false;
4275 p.moveTo(0, 0);
4276
4277 // Check that listener is notified on moveTo().
4278
halcanary385fe4d2015-08-26 13:07:48 -07004279 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004280 REPORTER_ASSERT(reporter, !changed);
4281 p.moveTo(10, 0);
4282 REPORTER_ASSERT(reporter, changed);
4283
4284 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004285 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004286 REPORTER_ASSERT(reporter, !changed);
4287 p.lineTo(20, 0);
4288 REPORTER_ASSERT(reporter, changed);
4289
4290 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004291 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004292 REPORTER_ASSERT(reporter, !changed);
4293 p.reset();
4294 REPORTER_ASSERT(reporter, changed);
4295
4296 p.moveTo(0, 0);
4297
4298 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004299 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004300 REPORTER_ASSERT(reporter, !changed);
4301 p.rewind();
4302 REPORTER_ASSERT(reporter, changed);
4303
4304 // Check that listener is notified when pathref is deleted.
4305 {
4306 SkPath q;
4307 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004308 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004309 REPORTER_ASSERT(reporter, !changed);
4310 }
4311 // q went out of scope.
4312 REPORTER_ASSERT(reporter, changed);
4313 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004314};
4315
reed158fabb2016-07-20 10:06:59 -07004316static void test_crbug_629455(skiatest::Reporter* reporter) {
4317 SkPath path;
4318 path.moveTo(0, 0);
4319 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4320 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4321 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4322// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4323 path.lineTo(0, 0);
4324
4325 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4326 SkPaint paint;
4327 paint.setAntiAlias(true);
4328 surface->getCanvas()->drawPath(path, paint);
4329}
4330
liyuqianc78eff92016-11-09 11:18:30 -08004331static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4332 SkPath path;
4333 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4334 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4335 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4336 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4337 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4338 path.close();
4339
4340 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4341 SkPaint paint;
4342 paint.setAntiAlias(true);
4343 surface->getCanvas()->clipPath(path, true);
4344 surface->getCanvas()->drawRectCoords(0, 0, 100, 100, paint);
4345}
4346
Yuqian Lidb13a092016-11-29 10:29:22 -05004347static void test_path_crbugskia6003() {
4348 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4349 SkCanvas* canvas = surface->getCanvas();
4350 SkPaint paint;
4351 paint.setAntiAlias(true);
4352 SkPath path;
4353 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4354 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4355 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4356 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4357 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4358 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4359 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4360 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4361 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4362 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4363 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4364 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4365 path.close();
4366 canvas->clipPath(path, true);
4367 canvas->drawRectCoords(0, 0, 500, 500, paint);
4368}
4369
Yuqian Lic4f66af2016-11-11 09:36:53 -05004370static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4371 SkPath path;
4372 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4373 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4374 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4375 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4376 path.close();
4377
4378 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4379 SkPaint paint;
4380 paint.setAntiAlias(true);
4381 surface->getCanvas()->drawPath(path, paint);
4382}
4383
caryclark8e7b19d2016-02-18 04:11:48 -08004384static void test_interp(skiatest::Reporter* reporter) {
4385 SkPath p1, p2, out;
4386 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4387 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4388 REPORTER_ASSERT(reporter, p1 == out);
4389 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4390 REPORTER_ASSERT(reporter, p1 == out);
4391 p1.moveTo(0, 2);
4392 p1.lineTo(0, 4);
4393 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4394 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4395 p2.moveTo(6, 0);
4396 p2.lineTo(8, 0);
4397 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4398 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4399 REPORTER_ASSERT(reporter, p2 == out);
4400 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4401 REPORTER_ASSERT(reporter, p1 == out);
4402 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4403 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4404 p1.reset();
4405 p1.moveTo(4, 4);
4406 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4407 p2.reset();
4408 p2.moveTo(4, 2);
4409 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4410 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4411 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4412 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4413 p2.reset();
4414 p2.moveTo(4, 2);
4415 p2.conicTo(6, 3, 6, 5, 1);
4416 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4417 p2.reset();
4418 p2.moveTo(4, 4);
4419 p2.conicTo(5, 4, 5, 5, 0.5f);
4420 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4421}
4422
4423DEF_TEST(PathInterp, reporter) {
4424 test_interp(reporter);
4425}
4426
caryclark9aacd902015-12-14 08:38:09 -08004427DEF_TEST(PathContains, reporter) {
4428 test_contains(reporter);
4429}
4430
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004431DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004432 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004433 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004434 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004435 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004436 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004437 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004438 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004439 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004440 test_fuzz_crbug_662780();
Yuqian Li20079a92016-11-16 13:07:57 -05004441 test_mask_overflow();
Yuqian Lidb13a092016-11-29 10:29:22 -05004442 test_path_crbugskia6003();
Yuqian Li79252f72016-11-29 15:02:49 -05004443 test_fuzz_crbug_668907();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004444
bungeman@google.coma5809a32013-06-21 15:13:34 +00004445 SkTSize<SkScalar>::Make(3,4);
4446
4447 SkPath p, empty;
4448 SkRect bounds, bounds2;
4449 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004450
reed@android.comd252db02009-04-01 18:31:44 +00004451 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004452
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004453 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4454 SkPath& self = p;
4455 p = self;
4456
4457 // this triggers a code path in SkPath::swap which is otherwise unexercised
4458 p.swap(self);
4459
reed@android.com3abec1d2009-03-02 05:36:20 +00004460 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004461
reed@android.com6b82d1a2009-06-03 02:35:01 +00004462 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4463 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004464 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004465 REPORTER_ASSERT(reporter,
4466 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004467 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004468
reed@android.com6b82d1a2009-06-03 02:35:01 +00004469 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004470 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004471
reed@android.com6b82d1a2009-06-03 02:35:01 +00004472 p.addOval(bounds);
4473 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004474 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004475
bungeman@google.coma5809a32013-06-21 15:13:34 +00004476 p.rewind();
4477 test_empty(reporter, p);
4478
reed@android.com3abec1d2009-03-02 05:36:20 +00004479 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004480 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004481 // we have only lines
4482 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004483 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004484
bungeman@google.coma5809a32013-06-21 15:13:34 +00004485 REPORTER_ASSERT(reporter, p != empty);
4486 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004487
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004488 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004489 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4490 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004491 SkPoint pts[4];
4492 int count = p.getPoints(pts, 4);
4493 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004494 uint8_t verbs[6];
4495 verbs[5] = 0xff;
4496 p.getVerbs(verbs, 5);
4497 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4498 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4499 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4500 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4501 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4502 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004503 bounds2.set(pts, 4);
4504 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004505
reed@android.com3abec1d2009-03-02 05:36:20 +00004506 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4507 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004508 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004509
halcanary96fcdcc2015-08-27 07:41:13 -07004510 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004511 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004512 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4513 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004514
reed@android.com3abec1d2009-03-02 05:36:20 +00004515 // now force p to not be a rect
4516 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4517 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004518 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004519
robertphillipsfe7c4272014-12-29 11:36:39 -08004520 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4521 // path has a trailing moveTo. Please see crbug.com\445368)
4522 {
4523 SkRect r;
4524 p.reset();
4525 p.addRect(bounds);
4526 REPORTER_ASSERT(reporter, p.isRect(&r));
4527 REPORTER_ASSERT(reporter, r == bounds);
4528 // add a moveTo outside of our bounds
4529 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4530 REPORTER_ASSERT(reporter, p.isRect(&r));
4531 REPORTER_ASSERT(reporter, r == bounds);
4532 }
4533
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004534 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004535 test_isLine(reporter);
4536 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004537 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004538 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004539 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004540 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004541 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004542 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004543 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004544 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004545 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004546 test_flattening(reporter);
4547 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004548 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004549 test_iter(reporter);
4550 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004551 test_circle(reporter);
4552 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004553 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004554 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004555 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004556 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004557 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004558 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004559 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004560 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004561 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004562 test_tricky_cubic();
4563 test_clipped_cubic();
4564 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004565 test_crbug_493450(reporter);
4566 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004567 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004568 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004569 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004570 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004571 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004572 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004573 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004574 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004575 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004576 test_arcTo(reporter);
4577 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004578 test_addPathMode(reporter, false, false);
4579 test_addPathMode(reporter, true, false);
4580 test_addPathMode(reporter, false, true);
4581 test_addPathMode(reporter, true, true);
4582 test_extendClosedPath(reporter);
4583 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4584 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004585 test_conicTo_special_case(reporter);
4586 test_get_point(reporter);
4587 test_contains(reporter);
4588 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004589 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004590 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004591 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004592 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004593 test_path_crbugskia2820(reporter);
Yuqian Li9939bd62016-11-29 10:27:16 -05004594 test_path_crbugskia5995();
caryclark5ccef572015-03-02 10:07:56 -08004595 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004596 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004597 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004598 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004599}