blob: 99a4e5b5af8a24b7fc5922d97708a8850e875f5d [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
piotaixrfac4e0e2014-08-26 11:59:04 -0700173/**
174 * In debug mode, this path was causing an assertion to fail in
175 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
176 */
177static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
178 SkPoint orig, p1, p2, p3;
179 orig = SkPoint::Make(1.f, 1.f);
180 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
181 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
182 p3 = SkPoint::Make(2.f, 2.f);
183
184 path->reset();
185 path->moveTo(orig);
186 path->cubicTo(p1, p2, p3);
187 path->close();
188}
189
190static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
191 SkPath path;
192 make_path_crbugskia2820(&path, reporter);
193
194 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
195 stroke.setStrokeStyle(2 * SK_Scalar1);
196 stroke.applyToPath(&path, path);
197}
198
reed@google.comcc8be772013-10-15 15:35:29 +0000199static void make_path0(SkPath* path) {
200 // from * https://code.google.com/p/skia/issues/detail?id=1706
201
202 path->moveTo(146.939f, 1012.84f);
203 path->lineTo(181.747f, 1009.18f);
204 path->lineTo(182.165f, 1013.16f);
205 path->lineTo(147.357f, 1016.82f);
206 path->lineTo(146.939f, 1012.84f);
207 path->close();
208}
209
210static void make_path1(SkPath* path) {
211 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
212}
213
214typedef void (*PathProc)(SkPath*);
215
216/*
217 * Regression test: we used to crash (overwrite internal storage) during
218 * construction of the region when the path was INVERSE. That is now fixed,
219 * so test these regions (which used to assert/crash).
220 *
221 * https://code.google.com/p/skia/issues/detail?id=1706
222 */
223static void test_path_to_region(skiatest::Reporter* reporter) {
224 PathProc procs[] = {
225 make_path0,
226 make_path1,
227 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000228
reed@google.comcc8be772013-10-15 15:35:29 +0000229 SkRegion clip;
230 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000231
reed@google.comcc8be772013-10-15 15:35:29 +0000232 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
233 SkPath path;
234 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000235
reed@google.comcc8be772013-10-15 15:35:29 +0000236 SkRegion rgn;
237 rgn.setPath(path, clip);
238 path.toggleInverseFillType();
239 rgn.setPath(path, clip);
240 }
241}
242
halcanary4dbbd042016-06-07 17:21:10 -0700243#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000244 #define SUPPRESS_VISIBILITY_WARNING
245#else
246 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
247#endif
248
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000249static void test_path_close_issue1474(skiatest::Reporter* reporter) {
250 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
251 // are relative to the point we close to, not relative to the point we close from.
252 SkPath path;
253 SkPoint last;
254
255 // Test rLineTo().
256 path.rLineTo(0, 100);
257 path.rLineTo(100, 0);
258 path.close(); // Returns us back to 0,0.
259 path.rLineTo(50, 50); // This should go to 50,50.
260
261 path.getLastPt(&last);
262 REPORTER_ASSERT(reporter, 50 == last.fX);
263 REPORTER_ASSERT(reporter, 50 == last.fY);
264
265 // Test rQuadTo().
266 path.rewind();
267 path.rLineTo(0, 100);
268 path.rLineTo(100, 0);
269 path.close();
270 path.rQuadTo(50, 50, 75, 75);
271
272 path.getLastPt(&last);
273 REPORTER_ASSERT(reporter, 75 == last.fX);
274 REPORTER_ASSERT(reporter, 75 == last.fY);
275
276 // Test rConicTo().
277 path.rewind();
278 path.rLineTo(0, 100);
279 path.rLineTo(100, 0);
280 path.close();
281 path.rConicTo(50, 50, 85, 85, 2);
282
283 path.getLastPt(&last);
284 REPORTER_ASSERT(reporter, 85 == last.fX);
285 REPORTER_ASSERT(reporter, 85 == last.fY);
286
287 // Test rCubicTo().
288 path.rewind();
289 path.rLineTo(0, 100);
290 path.rLineTo(100, 0);
291 path.close();
292 path.rCubicTo(50, 50, 85, 85, 95, 95);
293
294 path.getLastPt(&last);
295 REPORTER_ASSERT(reporter, 95 == last.fX);
296 REPORTER_ASSERT(reporter, 95 == last.fY);
297}
298
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000299static void test_gen_id(skiatest::Reporter* reporter) {
300 SkPath a, b;
301 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
302
303 a.moveTo(0, 0);
304 const uint32_t z = a.getGenerationID();
305 REPORTER_ASSERT(reporter, z != b.getGenerationID());
306
307 a.reset();
308 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
309
310 a.moveTo(1, 1);
311 const uint32_t y = a.getGenerationID();
312 REPORTER_ASSERT(reporter, z != y);
313
314 b.moveTo(2, 2);
315 const uint32_t x = b.getGenerationID();
316 REPORTER_ASSERT(reporter, x != y && x != z);
317
318 a.swap(b);
319 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
320
321 b = a;
322 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
323
324 SkPath c(a);
325 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
326
327 c.lineTo(3, 3);
328 const uint32_t w = c.getGenerationID();
329 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
330 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
331 REPORTER_ASSERT(reporter, w != x);
332
djsollen50da1d82015-02-17 09:09:53 -0800333#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000334 static bool kExpectGenIDToIgnoreFill = false;
335#else
336 static bool kExpectGenIDToIgnoreFill = true;
337#endif
338
339 c.toggleInverseFillType();
340 const uint32_t v = c.getGenerationID();
341 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
342
343 c.rewind();
344 REPORTER_ASSERT(reporter, v != c.getGenerationID());
345}
346
reed@google.com3eff3592013-05-08 21:08:21 +0000347// This used to assert in the debug build, as the edges did not all line-up.
348static void test_bad_cubic_crbug234190() {
349 SkPath path;
350 path.moveTo(13.8509f, 3.16858f);
351 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
352 -2.38991e+08f, -4.26573e+08f,
353 -2.41016e+08f, -4.30188e+08f);
354
355 SkPaint paint;
356 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700357 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000358 surface->getCanvas()->drawPath(path, paint);
359}
360
reed@google.com7a90daf2013-04-10 18:44:00 +0000361static void test_bad_cubic_crbug229478() {
362 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000363 { 4595.91064f, -11596.9873f },
364 { 4597.2168f, -11595.9414f },
365 { 4598.52344f, -11594.8955f },
366 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000367 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000368
reed@google.com7a90daf2013-04-10 18:44:00 +0000369 SkPath path;
370 path.moveTo(pts[0]);
371 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000372
reed@google.com7a90daf2013-04-10 18:44:00 +0000373 SkPaint paint;
374 paint.setStyle(SkPaint::kStroke_Style);
375 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000376
reed@google.com7a90daf2013-04-10 18:44:00 +0000377 SkPath dst;
378 // Before the fix, this would infinite-recurse, and run out of stack
379 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700380 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000381}
382
reed@google.com64d62952013-01-18 17:49:28 +0000383static void build_path_170666(SkPath& path) {
384 path.moveTo(17.9459f, 21.6344f);
385 path.lineTo(139.545f, -47.8105f);
386 path.lineTo(139.545f, -47.8105f);
387 path.lineTo(131.07f, -47.3888f);
388 path.lineTo(131.07f, -47.3888f);
389 path.lineTo(122.586f, -46.9532f);
390 path.lineTo(122.586f, -46.9532f);
391 path.lineTo(18076.6f, 31390.9f);
392 path.lineTo(18076.6f, 31390.9f);
393 path.lineTo(18085.1f, 31390.5f);
394 path.lineTo(18085.1f, 31390.5f);
395 path.lineTo(18076.6f, 31390.9f);
396 path.lineTo(18076.6f, 31390.9f);
397 path.lineTo(17955, 31460.3f);
398 path.lineTo(17955, 31460.3f);
399 path.lineTo(17963.5f, 31459.9f);
400 path.lineTo(17963.5f, 31459.9f);
401 path.lineTo(17971.9f, 31459.5f);
402 path.lineTo(17971.9f, 31459.5f);
403 path.lineTo(17.9551f, 21.6205f);
404 path.lineTo(17.9551f, 21.6205f);
405 path.lineTo(9.47091f, 22.0561f);
406 path.lineTo(9.47091f, 22.0561f);
407 path.lineTo(17.9459f, 21.6344f);
408 path.lineTo(17.9459f, 21.6344f);
409 path.close();path.moveTo(0.995934f, 22.4779f);
410 path.lineTo(0.986725f, 22.4918f);
411 path.lineTo(0.986725f, 22.4918f);
412 path.lineTo(17955, 31460.4f);
413 path.lineTo(17955, 31460.4f);
414 path.lineTo(17971.9f, 31459.5f);
415 path.lineTo(17971.9f, 31459.5f);
416 path.lineTo(18093.6f, 31390.1f);
417 path.lineTo(18093.6f, 31390.1f);
418 path.lineTo(18093.6f, 31390);
419 path.lineTo(18093.6f, 31390);
420 path.lineTo(139.555f, -47.8244f);
421 path.lineTo(139.555f, -47.8244f);
422 path.lineTo(122.595f, -46.9671f);
423 path.lineTo(122.595f, -46.9671f);
424 path.lineTo(0.995934f, 22.4779f);
425 path.lineTo(0.995934f, 22.4779f);
426 path.close();
427 path.moveTo(5.43941f, 25.5223f);
428 path.lineTo(798267, -28871.1f);
429 path.lineTo(798267, -28871.1f);
430 path.lineTo(3.12512e+06f, -113102);
431 path.lineTo(3.12512e+06f, -113102);
432 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
433 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
434 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
435 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
436 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
437 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
438 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
439 path.lineTo(2.78271e+08f, -1.00733e+07f);
440 path.lineTo(2.78271e+08f, -1.00733e+07f);
441 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
442 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
443 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
444 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
445 path.lineTo(2.77473e+08f, -1.00444e+07f);
446 path.lineTo(2.77473e+08f, -1.00444e+07f);
447 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
448 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
449 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
450 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
451 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
452 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
453 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
454 path.lineTo(798284, -28872);
455 path.lineTo(798284, -28872);
456 path.lineTo(22.4044f, 24.6677f);
457 path.lineTo(22.4044f, 24.6677f);
458 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
459 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
460 path.close();
461}
462
463static void build_path_simple_170666(SkPath& path) {
464 path.moveTo(126.677f, 24.1591f);
465 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
466}
467
468// This used to assert in the SK_DEBUG build, as the clip step would fail with
469// too-few interations in our cubic-line intersection code. That code now runs
470// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000471static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000472 SkPath path;
473 SkPaint paint;
474 paint.setAntiAlias(true);
475
reede8f30622016-03-23 18:59:25 -0700476 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000477
reed@google.com64d62952013-01-18 17:49:28 +0000478 build_path_simple_170666(path);
479 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000480
reed@google.com64d62952013-01-18 17:49:28 +0000481 build_path_170666(path);
482 surface->getCanvas()->drawPath(path, paint);
483}
484
caryclarke8c56662015-07-14 11:19:26 -0700485
486static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
487 SkScalar tx, SkScalar ty, SkScalar scale) {
488 SkPath smallPath;
489 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
490 bool smallConvex = smallPath.isConvex();
491 SkPath largePath;
492 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
493 SkMatrix matrix;
494 matrix.reset();
495 matrix.preTranslate(100, 100);
496 matrix.preScale(scale, scale);
497 largePath.transform(matrix);
498 bool largeConvex = largePath.isConvex();
499 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
500}
501
502static void test_crbug_493450(skiatest::Reporter* reporter) {
503 const char reducedCase[] =
504 "M0,0"
505 "L0.0002, 0"
506 "L0.0002, 0.0002"
507 "L0.0001, 0.0001"
508 "L0,0.0002"
509 "Z";
510 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
511 const char originalFiddleData[] =
512 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
513 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
514 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
515 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
516 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
517 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
518 826357.3384828606f);
519}
520
521static void test_crbug_495894(skiatest::Reporter* reporter) {
522 const char originalFiddleData[] =
523 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
524 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
525 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
526 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
527 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
528 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
529 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
530 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
531 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
532 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
533 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
534 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
535 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
536 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
537 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
538 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
539 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
540 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
541 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
542 "L-0.33993994441916414,-0.11288906492739594Z";
543 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
544 65536);
545}
546
mbarbella99600d02016-06-01 15:39:47 -0700547static void test_crbug_613918() {
548 SkPath path;
549 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
550 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
551 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
552
553 // This call could lead to an assert or uninitialized read due to a failure
554 // to check the return value from SkCubicClipper::ChopMonoAtY.
555 path.contains(-1.84817e-08f, 1.15465e-08f);
556}
557
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000558static void test_addrect(skiatest::Reporter* reporter) {
559 SkPath path;
560 path.lineTo(0, 0);
561 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700562 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000563
564 path.reset();
565 path.lineTo(FLT_EPSILON, FLT_EPSILON);
566 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700567 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000568
569 path.reset();
570 path.quadTo(0, 0, 0, 0);
571 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700572 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000573
574 path.reset();
575 path.conicTo(0, 0, 0, 0, 0.5f);
576 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700577 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000578
579 path.reset();
580 path.cubicTo(0, 0, 0, 0, 0, 0);
581 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700582 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000583}
584
reed@google.coma8790de2012-10-24 21:04:04 +0000585// Make sure we stay non-finite once we get there (unless we reset or rewind).
586static void test_addrect_isfinite(skiatest::Reporter* reporter) {
587 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000588
reed@google.coma8790de2012-10-24 21:04:04 +0000589 path.addRect(SkRect::MakeWH(50, 100));
590 REPORTER_ASSERT(reporter, path.isFinite());
591
592 path.moveTo(0, 0);
593 path.lineTo(SK_ScalarInfinity, 42);
594 REPORTER_ASSERT(reporter, !path.isFinite());
595
596 path.addRect(SkRect::MakeWH(50, 100));
597 REPORTER_ASSERT(reporter, !path.isFinite());
598
599 path.reset();
600 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000601
reed@google.coma8790de2012-10-24 21:04:04 +0000602 path.addRect(SkRect::MakeWH(50, 100));
603 REPORTER_ASSERT(reporter, path.isFinite());
604}
605
reed@google.com848148e2013-01-15 15:51:59 +0000606static void build_big_path(SkPath* path, bool reducedCase) {
607 if (reducedCase) {
608 path->moveTo(577330, 1971.72f);
609 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
610 } else {
611 path->moveTo(60.1631f, 7.70567f);
612 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
613 path->lineTo(577379, 1977.77f);
614 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
615 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
616 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
617 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
618 path->quadTo(577198, 1972, 577238, 1971.31f);
619 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
620 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
621 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
622 path->moveTo(306.718f, -32.912f);
623 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
624 }
625}
626
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000627static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700628 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000629
630 // This path used to assert, because our cubic-chopping code incorrectly
631 // moved control points after the chop. This test should be run in SK_DEBUG
632 // mode to ensure that we no long assert.
633 SkPath path;
634 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
635 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000636
reed@google.com848148e2013-01-15 15:51:59 +0000637 SkPaint paint;
638 for (int doAA = 0; doAA <= 1; ++doAA) {
639 paint.setAntiAlias(SkToBool(doAA));
640 surface->getCanvas()->drawPath(path, paint);
641 }
642 }
643}
644
reed3b2fb982015-07-29 08:37:13 -0700645static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
646 if (expected != bounds) {
647 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
648 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
649 expected.left(), expected.top(), expected.right(), expected.bottom());
650 }
651}
652
reed91f283b2015-07-28 06:00:50 -0700653static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
654 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700655#if 0
656 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
657 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700658 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
659
660 path.moveTo(-5, -8);
661 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
662
663 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
664 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
665
666 path.moveTo(1, 2);
667 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700668#else
669 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
670
671 path.moveTo(-5, -8); // should set the bounds
672 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
673
674 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
675 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
676
677 path.moveTo(1, 2); // don't expect this to have changed the bounds
678 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
679#endif
reed91f283b2015-07-28 06:00:50 -0700680}
681
reedb1b12f82016-07-13 10:56:53 -0700682#include "SkSurface.h"
683static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
684 SkPath path;
685 path.moveTo(0, 0);
686 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
687
688 SkPaint paint;
689 paint.setAntiAlias(true);
690
691 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
692 surf->getCanvas()->drawPath(path, paint);
693}
694
reed@google.com8cae8352012-09-14 15:18:41 +0000695// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
696// which triggered an assert, from a tricky cubic. This test replicates that
697// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
698// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000699static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000700 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000701 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
702 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
703 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
704 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000705 };
706
707 SkPath path;
708 path.moveTo(pts[0]);
709 path.cubicTo(pts[1], pts[2], pts[3]);
710
711 SkPaint paint;
712 paint.setAntiAlias(true);
713
reede8f30622016-03-23 18:59:25 -0700714 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000715}
reed@android.com3abec1d2009-03-02 05:36:20 +0000716
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000717// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
718//
719static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
720 SkPath path;
721 path.quadTo(157, 366, 286, 208);
722 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000723
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000724 SkMatrix matrix;
725 matrix.setScale(1000*1000, 1000*1000);
726
727 // Be sure that path::transform correctly updates isFinite and the bounds
728 // if the transformation overflows. The previous bug was that isFinite was
729 // set to true in this case, but the bounds were not set to empty (which
730 // they should be).
731 while (path.isFinite()) {
732 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
733 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
734 path.transform(matrix);
735 }
736 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
737
738 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
739 path.transform(matrix);
740 // we need to still be non-finite
741 REPORTER_ASSERT(reporter, !path.isFinite());
742 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
743}
744
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000745static void add_corner_arc(SkPath* path, const SkRect& rect,
746 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000747 int startAngle)
748{
749
750 SkScalar rx = SkMinScalar(rect.width(), xIn);
751 SkScalar ry = SkMinScalar(rect.height(), yIn);
752
753 SkRect arcRect;
754 arcRect.set(-rx, -ry, rx, ry);
755 switch (startAngle) {
756 case 0:
757 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
758 break;
759 case 90:
760 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
761 break;
762 case 180:
763 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
764 break;
765 case 270:
766 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
767 break;
768 default:
769 break;
770 }
771
772 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
773}
774
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000775static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000776 SkScalar xCorner, SkScalar yCorner) {
777 // we are lazy here and use the same x & y for each corner
778 add_corner_arc(path, r, xCorner, yCorner, 270);
779 add_corner_arc(path, r, xCorner, yCorner, 0);
780 add_corner_arc(path, r, xCorner, yCorner, 90);
781 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000782 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000783}
784
785// Chrome creates its own round rects with each corner possibly being different.
786// Performance will suffer if they are not convex.
787// Note: PathBench::ArbRoundRectBench performs almost exactly
788// the same test (but with drawing)
789static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000790 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000791 SkRect r;
792
793 for (int i = 0; i < 5000; ++i) {
794
robertphillips@google.com158618e2012-10-23 16:56:56 +0000795 SkScalar size = rand.nextUScalar1() * 30;
796 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000797 continue;
798 }
799 r.fLeft = rand.nextUScalar1() * 300;
800 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000801 r.fRight = r.fLeft + 2 * size;
802 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000803
804 SkPath temp;
805
806 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
807
808 REPORTER_ASSERT(reporter, temp.isConvex());
809 }
810}
811
robertphillips@google.com158618e2012-10-23 16:56:56 +0000812// Chrome will sometimes create a 0 radius round rect. The degenerate
813// quads prevent the path from being converted to a rect
814// Note: PathBench::ArbRoundRectBench performs almost exactly
815// the same test (but with drawing)
816static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000817 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000818 SkRect r;
819
820 for (int i = 0; i < 5000; ++i) {
821
822 SkScalar size = rand.nextUScalar1() * 30;
823 if (size < SK_Scalar1) {
824 continue;
825 }
826 r.fLeft = rand.nextUScalar1() * 300;
827 r.fTop = rand.nextUScalar1() * 300;
828 r.fRight = r.fLeft + 2 * size;
829 r.fBottom = r.fTop + 2 * size;
830
831 SkPath temp;
832
833 make_arb_round_rect(&temp, r, 0, 0);
834
robertphillips@google.com158618e2012-10-23 16:56:56 +0000835 SkRect result;
836 REPORTER_ASSERT(reporter, temp.isRect(&result));
837 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000838 }
839}
840
reed@google.com0bb18bb2012-07-26 15:20:36 +0000841static void test_rect_isfinite(skiatest::Reporter* reporter) {
842 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000843 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000844 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000845
reed@google.com0bb18bb2012-07-26 15:20:36 +0000846 SkRect r;
847 r.setEmpty();
848 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000849 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000850 REPORTER_ASSERT(reporter, !r.isFinite());
851 r.set(0, 0, nan, 0);
852 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000853
reed@google.com0bb18bb2012-07-26 15:20:36 +0000854 SkPoint pts[] = {
855 { 0, 0 },
856 { SK_Scalar1, 0 },
857 { 0, SK_Scalar1 },
858 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000859
reed@google.com0bb18bb2012-07-26 15:20:36 +0000860 bool isFine = r.setBoundsCheck(pts, 3);
861 REPORTER_ASSERT(reporter, isFine);
862 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000863
reed@google.com0bb18bb2012-07-26 15:20:36 +0000864 pts[1].set(inf, 0);
865 isFine = r.setBoundsCheck(pts, 3);
866 REPORTER_ASSERT(reporter, !isFine);
867 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000868
reed@google.com0bb18bb2012-07-26 15:20:36 +0000869 pts[1].set(nan, 0);
870 isFine = r.setBoundsCheck(pts, 3);
871 REPORTER_ASSERT(reporter, !isFine);
872 REPORTER_ASSERT(reporter, r.isEmpty());
873}
874
875static void test_path_isfinite(skiatest::Reporter* reporter) {
876 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000877 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000878 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000879
reed@google.com0bb18bb2012-07-26 15:20:36 +0000880 SkPath path;
881 REPORTER_ASSERT(reporter, path.isFinite());
882
883 path.reset();
884 REPORTER_ASSERT(reporter, path.isFinite());
885
886 path.reset();
887 path.moveTo(SK_Scalar1, 0);
888 REPORTER_ASSERT(reporter, path.isFinite());
889
890 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000891 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000892 REPORTER_ASSERT(reporter, !path.isFinite());
893
894 path.reset();
895 path.moveTo(nan, 0);
896 REPORTER_ASSERT(reporter, !path.isFinite());
897}
898
899static void test_isfinite(skiatest::Reporter* reporter) {
900 test_rect_isfinite(reporter);
901 test_path_isfinite(reporter);
902}
903
fsb1475b02016-01-20 09:51:07 -0800904static void test_islastcontourclosed(skiatest::Reporter* reporter) {
905 SkPath path;
906 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
907 path.moveTo(0, 0);
908 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
909 path.close();
910 REPORTER_ASSERT(reporter, path.isLastContourClosed());
911 path.lineTo(100, 100);
912 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
913 path.moveTo(200, 200);
914 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
915 path.close();
916 REPORTER_ASSERT(reporter, path.isLastContourClosed());
917 path.moveTo(0, 0);
918 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
919}
920
reed@google.com744faba2012-05-29 19:54:52 +0000921// assert that we always
922// start with a moveTo
923// only have 1 moveTo
924// only have Lines after that
925// end with a single close
926// only have (at most) 1 close
927//
928static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000929 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000930 SkPath::RawIter iter(path);
931 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000932
933 bool firstTime = true;
934 bool foundClose = false;
935 for (;;) {
936 switch (iter.next(pts)) {
937 case SkPath::kMove_Verb:
938 REPORTER_ASSERT(reporter, firstTime);
939 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
940 srcPts++;
941 firstTime = false;
942 break;
943 case SkPath::kLine_Verb:
944 REPORTER_ASSERT(reporter, !firstTime);
945 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
946 srcPts++;
947 break;
948 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000949 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000950 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000951 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000952 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000953 break;
reed@google.com744faba2012-05-29 19:54:52 +0000954 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000955 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000956 break;
957 case SkPath::kClose_Verb:
958 REPORTER_ASSERT(reporter, !firstTime);
959 REPORTER_ASSERT(reporter, !foundClose);
960 REPORTER_ASSERT(reporter, expectClose);
961 foundClose = true;
962 break;
963 case SkPath::kDone_Verb:
964 goto DONE;
965 }
966 }
967DONE:
968 REPORTER_ASSERT(reporter, foundClose == expectClose);
969}
970
971static void test_addPoly(skiatest::Reporter* reporter) {
972 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000973 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000974
reed@google.com744faba2012-05-29 19:54:52 +0000975 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
976 pts[i].fX = rand.nextSScalar1();
977 pts[i].fY = rand.nextSScalar1();
978 }
979
980 for (int doClose = 0; doClose <= 1; ++doClose) {
981 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
982 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800983 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000984 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000985 }
986 }
987}
988
reed@google.com8b06f1a2012-05-29 12:03:46 +0000989static void test_strokerec(skiatest::Reporter* reporter) {
990 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
991 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000992
reed@google.com8b06f1a2012-05-29 12:03:46 +0000993 rec.setHairlineStyle();
994 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000995
reed@google.com8b06f1a2012-05-29 12:03:46 +0000996 rec.setStrokeStyle(SK_Scalar1, false);
997 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000998
reed@google.com8b06f1a2012-05-29 12:03:46 +0000999 rec.setStrokeStyle(SK_Scalar1, true);
1000 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001001
reed@google.com8b06f1a2012-05-29 12:03:46 +00001002 rec.setStrokeStyle(0, false);
1003 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001004
reed@google.com8b06f1a2012-05-29 12:03:46 +00001005 rec.setStrokeStyle(0, true);
1006 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1007}
1008
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001009// Set this for paths that don't have a consistent direction such as a bowtie.
1010// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001011const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001012
1013static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001014 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001015 if (expected == kDontCheckDir) {
1016 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001017 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001018 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1019
reed026beb52015-06-10 14:23:15 -07001020 SkPathPriv::FirstDirection dir;
1021 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001022 REPORTER_ASSERT(reporter, dir == expected);
1023 } else {
reed026beb52015-06-10 14:23:15 -07001024 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001025 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001026}
1027
reed@google.com3e71a882012-01-10 18:44:37 +00001028static void test_direction(skiatest::Reporter* reporter) {
1029 size_t i;
1030 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001031 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001032 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1033 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1034 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001035
1036 static const char* gDegen[] = {
1037 "M 10 10",
1038 "M 10 10 M 20 20",
1039 "M 10 10 L 20 20",
1040 "M 10 10 L 10 10 L 10 10",
1041 "M 10 10 Q 10 10 10 10",
1042 "M 10 10 C 10 10 10 10 10 10",
1043 };
1044 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1045 path.reset();
1046 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1047 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001048 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001049 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001050
reed@google.com3e71a882012-01-10 18:44:37 +00001051 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001052 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001053 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001054 "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 +00001055 // rect with top two corners replaced by cubics with identical middle
1056 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001057 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1058 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001059 };
1060 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1061 path.reset();
1062 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1063 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001064 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001065 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001066
reed@google.com3e71a882012-01-10 18:44:37 +00001067 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001068 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001069 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001070 "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 +00001071 // rect with top two corners replaced by cubics with identical middle
1072 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001073 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1074 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001075 };
1076 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1077 path.reset();
1078 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1079 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001080 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001081 }
reed@google.comac8543f2012-01-30 20:51:25 +00001082
1083 // Test two donuts, each wound a different direction. Only the outer contour
1084 // determines the cheap direction
1085 path.reset();
1086 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1087 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001088 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001089
reed@google.comac8543f2012-01-30 20:51:25 +00001090 path.reset();
1091 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1092 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001093 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001094
1095 // triangle with one point really far from the origin.
1096 path.reset();
1097 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001098 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001099 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1100 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001101 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001102
1103 path.reset();
1104 path.conicTo(20, 0, 20, 20, 0.5f);
1105 path.close();
reed026beb52015-06-10 14:23:15 -07001106 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001107
1108 path.reset();
1109 path.lineTo(1, 1e7f);
1110 path.lineTo(1e7f, 2e7f);
1111 path.close();
1112 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001113 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001114}
1115
reed@google.comffdb0182011-11-14 19:29:14 +00001116static void add_rect(SkPath* path, const SkRect& r) {
1117 path->moveTo(r.fLeft, r.fTop);
1118 path->lineTo(r.fRight, r.fTop);
1119 path->lineTo(r.fRight, r.fBottom);
1120 path->lineTo(r.fLeft, r.fBottom);
1121 path->close();
1122}
1123
1124static void test_bounds(skiatest::Reporter* reporter) {
1125 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001126 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1127 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1128 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1129 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001130 };
1131
1132 SkPath path0, path1;
1133 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1134 path0.addRect(rects[i]);
1135 add_rect(&path1, rects[i]);
1136 }
1137
1138 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1139}
1140
reed@google.com55b5f4b2011-09-07 12:23:41 +00001141static void stroke_cubic(const SkPoint pts[4]) {
1142 SkPath path;
1143 path.moveTo(pts[0]);
1144 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001145
reed@google.com55b5f4b2011-09-07 12:23:41 +00001146 SkPaint paint;
1147 paint.setStyle(SkPaint::kStroke_Style);
1148 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001149
reed@google.com55b5f4b2011-09-07 12:23:41 +00001150 SkPath fill;
1151 paint.getFillPath(path, &fill);
1152}
1153
1154// just ensure this can run w/o any SkASSERTS firing in the debug build
1155// we used to assert due to differences in how we determine a degenerate vector
1156// but that was fixed with the introduction of SkPoint::CanNormalize
1157static void stroke_tiny_cubic() {
1158 SkPoint p0[] = {
1159 { 372.0f, 92.0f },
1160 { 372.0f, 92.0f },
1161 { 372.0f, 92.0f },
1162 { 372.0f, 92.0f },
1163 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001164
reed@google.com55b5f4b2011-09-07 12:23:41 +00001165 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001166
reed@google.com55b5f4b2011-09-07 12:23:41 +00001167 SkPoint p1[] = {
1168 { 372.0f, 92.0f },
1169 { 372.0007f, 92.000755f },
1170 { 371.99927f, 92.003922f },
1171 { 371.99826f, 92.003899f },
1172 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001173
reed@google.com55b5f4b2011-09-07 12:23:41 +00001174 stroke_cubic(p1);
1175}
1176
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001177static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1178 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001179 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001180 SkPoint mv;
1181 SkPoint pts[4];
1182 SkPath::Verb v;
1183 int nMT = 0;
1184 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001185 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001186 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1187 switch (v) {
1188 case SkPath::kMove_Verb:
1189 mv = pts[0];
1190 ++nMT;
1191 break;
1192 case SkPath::kClose_Verb:
1193 REPORTER_ASSERT(reporter, mv == pts[0]);
1194 ++nCL;
1195 break;
1196 default:
1197 break;
1198 }
1199 }
1200 // if we force a close on the interator we should have a close
1201 // for every moveTo
1202 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1203 }
1204}
1205
1206static void test_close(skiatest::Reporter* reporter) {
1207 SkPath closePt;
1208 closePt.moveTo(0, 0);
1209 closePt.close();
1210 check_close(reporter, closePt);
1211
1212 SkPath openPt;
1213 openPt.moveTo(0, 0);
1214 check_close(reporter, openPt);
1215
1216 SkPath empty;
1217 check_close(reporter, empty);
1218 empty.close();
1219 check_close(reporter, empty);
1220
1221 SkPath rect;
1222 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1223 check_close(reporter, rect);
1224 rect.close();
1225 check_close(reporter, rect);
1226
1227 SkPath quad;
1228 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1229 check_close(reporter, quad);
1230 quad.close();
1231 check_close(reporter, quad);
1232
1233 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001234 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001235 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1236 check_close(reporter, cubic);
1237 cubic.close();
1238 check_close(reporter, cubic);
1239
1240 SkPath line;
1241 line.moveTo(SK_Scalar1, SK_Scalar1);
1242 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1243 check_close(reporter, line);
1244 line.close();
1245 check_close(reporter, line);
1246
1247 SkPath rect2;
1248 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1249 rect2.close();
1250 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1251 check_close(reporter, rect2);
1252 rect2.close();
1253 check_close(reporter, rect2);
1254
1255 SkPath oval3;
1256 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1257 oval3.close();
1258 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1259 check_close(reporter, oval3);
1260 oval3.close();
1261 check_close(reporter, oval3);
1262
1263 SkPath moves;
1264 moves.moveTo(SK_Scalar1, SK_Scalar1);
1265 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1266 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1267 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1268 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001269
1270 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001271}
1272
reed@google.com7c424812011-05-15 04:38:34 +00001273static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1274 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001275 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1276 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001277 REPORTER_ASSERT(reporter, c == expected);
1278}
1279
caryclarkb4216502015-03-02 13:02:34 -08001280static void test_path_crbug389050(skiatest::Reporter* reporter) {
1281 SkPath tinyConvexPolygon;
1282 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1283 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1284 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1285 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1286 tinyConvexPolygon.close();
1287 tinyConvexPolygon.getConvexity();
1288 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001289 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001290
1291 SkPath platTriangle;
1292 platTriangle.moveTo(0, 0);
1293 platTriangle.lineTo(200, 0);
1294 platTriangle.lineTo(100, 0.04f);
1295 platTriangle.close();
1296 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001297 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001298
1299 platTriangle.reset();
1300 platTriangle.moveTo(0, 0);
1301 platTriangle.lineTo(200, 0);
1302 platTriangle.lineTo(100, 0.03f);
1303 platTriangle.close();
1304 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001305 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001306}
1307
reed@google.com7c424812011-05-15 04:38:34 +00001308static void test_convexity2(skiatest::Reporter* reporter) {
1309 SkPath pt;
1310 pt.moveTo(0, 0);
1311 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001312 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001313 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001314
reed@google.com7c424812011-05-15 04:38:34 +00001315 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001316 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1317 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001318 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001319 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001320 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001321
reed@google.com7c424812011-05-15 04:38:34 +00001322 SkPath triLeft;
1323 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001324 triLeft.lineTo(SK_Scalar1, 0);
1325 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001326 triLeft.close();
1327 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001328 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001329
reed@google.com7c424812011-05-15 04:38:34 +00001330 SkPath triRight;
1331 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001332 triRight.lineTo(-SK_Scalar1, 0);
1333 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001334 triRight.close();
1335 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001336 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001337
reed@google.com7c424812011-05-15 04:38:34 +00001338 SkPath square;
1339 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001340 square.lineTo(SK_Scalar1, 0);
1341 square.lineTo(SK_Scalar1, SK_Scalar1);
1342 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001343 square.close();
1344 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001345 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001346
reed@google.com7c424812011-05-15 04:38:34 +00001347 SkPath redundantSquare;
1348 redundantSquare.moveTo(0, 0);
1349 redundantSquare.lineTo(0, 0);
1350 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001351 redundantSquare.lineTo(SK_Scalar1, 0);
1352 redundantSquare.lineTo(SK_Scalar1, 0);
1353 redundantSquare.lineTo(SK_Scalar1, 0);
1354 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1355 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1356 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1357 redundantSquare.lineTo(0, SK_Scalar1);
1358 redundantSquare.lineTo(0, SK_Scalar1);
1359 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001360 redundantSquare.close();
1361 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001362 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001363
reed@google.com7c424812011-05-15 04:38:34 +00001364 SkPath bowTie;
1365 bowTie.moveTo(0, 0);
1366 bowTie.lineTo(0, 0);
1367 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001368 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1369 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1370 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1371 bowTie.lineTo(SK_Scalar1, 0);
1372 bowTie.lineTo(SK_Scalar1, 0);
1373 bowTie.lineTo(SK_Scalar1, 0);
1374 bowTie.lineTo(0, SK_Scalar1);
1375 bowTie.lineTo(0, SK_Scalar1);
1376 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001377 bowTie.close();
1378 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001379 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001380
reed@google.com7c424812011-05-15 04:38:34 +00001381 SkPath spiral;
1382 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001383 spiral.lineTo(100*SK_Scalar1, 0);
1384 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1385 spiral.lineTo(0, 100*SK_Scalar1);
1386 spiral.lineTo(0, 50*SK_Scalar1);
1387 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1388 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001389 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001390 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001391 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001392
reed@google.com7c424812011-05-15 04:38:34 +00001393 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001394 dent.moveTo(0, 0);
1395 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1396 dent.lineTo(0, 100*SK_Scalar1);
1397 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1398 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001399 dent.close();
1400 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001401 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001402
halcanary6950de62015-11-07 05:29:00 -08001403 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001404 SkPath strokedSin;
1405 for (int i = 0; i < 2000; i++) {
1406 SkScalar x = SkIntToScalar(i) / 2;
1407 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1408 if (0 == i) {
1409 strokedSin.moveTo(x, y);
1410 } else {
1411 strokedSin.lineTo(x, y);
1412 }
1413 }
1414 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1415 stroke.setStrokeStyle(2 * SK_Scalar1);
1416 stroke.applyToPath(&strokedSin, strokedSin);
1417 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1418 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001419
1420 // http://crbug.com/412640
1421 SkPath degenerateConcave;
1422 degenerateConcave.moveTo(148.67912f, 191.875f);
1423 degenerateConcave.lineTo(470.37695f, 7.5f);
1424 degenerateConcave.lineTo(148.67912f, 191.875f);
1425 degenerateConcave.lineTo(41.446522f, 376.25f);
1426 degenerateConcave.lineTo(-55.971577f, 460.0f);
1427 degenerateConcave.lineTo(41.446522f, 376.25f);
1428 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001429 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001430
1431 // http://crbug.com/433683
1432 SkPath badFirstVector;
1433 badFirstVector.moveTo(501.087708f, 319.610352f);
1434 badFirstVector.lineTo(501.087708f, 319.610352f);
1435 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1436 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1437 badFirstVector.lineTo(301.557678f, 98.044601f);
1438 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1439 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1440 badFirstVector.lineTo(504.912292f, 316.389648f);
1441 badFirstVector.lineTo(501.087708f, 319.610352f);
1442 badFirstVector.close();
1443 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001444}
1445
reed@android.com6b82d1a2009-06-03 02:35:01 +00001446static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1447 const SkRect& bounds) {
1448 REPORTER_ASSERT(reporter, p.isConvex());
1449 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001450
reed@android.com6b82d1a2009-06-03 02:35:01 +00001451 SkPath p2(p);
1452 REPORTER_ASSERT(reporter, p2.isConvex());
1453 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1454
1455 SkPath other;
1456 other.swap(p2);
1457 REPORTER_ASSERT(reporter, other.isConvex());
1458 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1459}
1460
reed@google.com04863fa2011-05-15 04:08:24 +00001461static void setFromString(SkPath* path, const char str[]) {
1462 bool first = true;
1463 while (str) {
1464 SkScalar x, y;
1465 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001466 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001467 break;
1468 }
1469 str = SkParse::FindScalar(str, &y);
1470 SkASSERT(str);
1471 if (first) {
1472 path->moveTo(x, y);
1473 first = false;
1474 } else {
1475 path->lineTo(x, y);
1476 }
1477 }
1478}
1479
1480static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001481 SkPath path;
1482
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001483 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001484 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001485 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001486 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001487 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1488
reed@google.com04863fa2011-05-15 04:08:24 +00001489 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001490 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001491 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001492 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001493
reed@google.com04863fa2011-05-15 04:08:24 +00001494 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001495 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001496 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001497 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001498
reed@google.com04863fa2011-05-15 04:08:24 +00001499 static const struct {
reed026beb52015-06-10 14:23:15 -07001500 const char* fPathStr;
1501 SkPath::Convexity fExpectedConvexity;
1502 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001503 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001504 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1505 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1506 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1507 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1508 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1509 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001510 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001511 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001512 };
1513
1514 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1515 SkPath path;
1516 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001517 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1518 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001519 // check after setting the initial convex and direction
1520 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1521 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001522 SkPathPriv::FirstDirection dir;
1523 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1524 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001525 ^ foundDir);
1526 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1527 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1528 }
1529 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1530 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001531 }
caryclarkd3d1a982014-12-08 04:57:38 -08001532
1533 static const SkPoint nonFinitePts[] = {
1534 { SK_ScalarInfinity, 0 },
1535 { 0, SK_ScalarInfinity },
1536 { SK_ScalarInfinity, SK_ScalarInfinity },
1537 { SK_ScalarNegativeInfinity, 0},
1538 { 0, SK_ScalarNegativeInfinity },
1539 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1540 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1541 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1542 { SK_ScalarNaN, 0 },
1543 { 0, SK_ScalarNaN },
1544 { SK_ScalarNaN, SK_ScalarNaN },
1545 };
1546
1547 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1548
1549 static const SkPoint finitePts[] = {
1550 { SK_ScalarMax, 0 },
1551 { 0, SK_ScalarMax },
1552 { SK_ScalarMax, SK_ScalarMax },
1553 { SK_ScalarMin, 0 },
1554 { 0, SK_ScalarMin },
1555 { SK_ScalarMin, SK_ScalarMin },
1556 };
1557
1558 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1559
1560 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1561 int i = (int) (index % nonFinitePtsCount);
1562 int f = (int) (index % finitePtsCount);
1563 int g = (int) ((f + 1) % finitePtsCount);
1564 path.reset();
1565 switch (index % 13) {
1566 case 0: path.lineTo(nonFinitePts[i]); break;
1567 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1568 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1569 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1570 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1571 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1572 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1573 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1574 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1575 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1576 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1577 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1578 case 12: path.moveTo(nonFinitePts[i]); break;
1579 }
1580 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1581 }
1582
1583 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1584 int f = (int) (index % finitePtsCount);
1585 int g = (int) ((f + 1) % finitePtsCount);
1586 path.reset();
1587 int curveSelect = index % 11;
1588 switch (curveSelect) {
1589 case 0: path.moveTo(finitePts[f]); break;
1590 case 1: path.lineTo(finitePts[f]); break;
1591 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1592 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1593 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1594 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1595 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1596 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1597 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1598 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1599 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1600 }
1601 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1602 : SkPath::kUnknown_Convexity);
1603 }
1604
reed@google.com04863fa2011-05-15 04:08:24 +00001605}
1606
reed@google.com7e6c4d12012-05-10 14:05:43 +00001607static void test_isLine(skiatest::Reporter* reporter) {
1608 SkPath path;
1609 SkPoint pts[2];
1610 const SkScalar value = SkIntToScalar(5);
1611
halcanary96fcdcc2015-08-27 07:41:13 -07001612 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001613
reed@google.com7e6c4d12012-05-10 14:05:43 +00001614 // set some non-zero values
1615 pts[0].set(value, value);
1616 pts[1].set(value, value);
1617 REPORTER_ASSERT(reporter, !path.isLine(pts));
1618 // check that pts was untouched
1619 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1620 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1621
1622 const SkScalar moveX = SkIntToScalar(1);
1623 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001624 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001625
1626 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001627 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001628 REPORTER_ASSERT(reporter, !path.isLine(pts));
1629 // check that pts was untouched
1630 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1631 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1632
1633 const SkScalar lineX = SkIntToScalar(2);
1634 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001635 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001636
1637 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001638 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001639
1640 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1641 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1642 REPORTER_ASSERT(reporter, path.isLine(pts));
1643 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1644 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1645
1646 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001647 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001648 REPORTER_ASSERT(reporter, !path.isLine(pts));
1649 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1650 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001651
1652 path.reset();
1653 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001654 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001655}
1656
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001657static void test_conservativelyContains(skiatest::Reporter* reporter) {
1658 SkPath path;
1659
1660 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1661 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1662
1663 // A circle that bounds kBaseRect (with a significant amount of slop)
1664 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001665 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001666 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1667
1668 // round-rect radii
1669 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001670
caryclark@google.com56f233a2012-11-19 13:06:06 +00001671 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001672 SkRect fQueryRect;
1673 bool fInRect;
1674 bool fInCircle;
1675 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001676 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001677 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001678 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001679
1680 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001681 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1682 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1683 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1684 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001685 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001686
1687 // rects with edges off by one from kBaseRect's edges
1688 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1689 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001690 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001691 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1692 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001693 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001694 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1695 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001696 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001697 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1698 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001699 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001700 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1701 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001702 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001703 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1704 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001705 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001706 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1707 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001708 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001709
1710 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001711 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1712 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1713 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1714 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001715
1716 // far away rect
1717 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1718 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001719 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001720
1721 // very large rect containing kBaseRect
1722 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1723 kBaseRect.fTop - 5 * kBaseRect.height(),
1724 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001725 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001726
1727 // skinny rect that spans same y-range as kBaseRect
1728 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1729 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001730 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001731
1732 // short rect that spans same x-range as kBaseRect
1733 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001734 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001735
1736 // skinny rect that spans slightly larger y-range than kBaseRect
1737 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1738 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001739 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001740
1741 // short rect that spans slightly larger x-range than kBaseRect
1742 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1743 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001744 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001745 };
1746
1747 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001748 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001749 SkRect qRect = kQueries[q].fQueryRect;
1750 if (inv & 0x1) {
1751 SkTSwap(qRect.fLeft, qRect.fRight);
1752 }
1753 if (inv & 0x2) {
1754 SkTSwap(qRect.fTop, qRect.fBottom);
1755 }
1756 for (int d = 0; d < 2; ++d) {
1757 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1758 path.reset();
1759 path.addRect(kBaseRect, dir);
1760 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1761 path.conservativelyContainsRect(qRect));
1762
1763 path.reset();
1764 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1765 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1766 path.conservativelyContainsRect(qRect));
1767
1768 path.reset();
1769 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1770 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1771 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001772
1773 path.reset();
1774 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1775 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1776 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1777 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1778 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1779 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1780 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1781 path.close();
1782 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1783 path.conservativelyContainsRect(qRect));
1784
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001785 }
1786 // Slightly non-convex shape, shouldn't contain any rects.
1787 path.reset();
1788 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001789 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001790 path.lineTo(SkIntToScalar(100), 0);
1791 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1792 path.lineTo(0, SkIntToScalar(100));
1793 path.close();
1794 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1795 }
1796 }
1797
1798 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1799 path.reset();
1800 path.moveTo(0, 0);
1801 path.lineTo(SkIntToScalar(100), 0);
1802 path.lineTo(0, SkIntToScalar(100));
1803
1804 // inside, on along top edge
1805 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1806 SkIntToScalar(10),
1807 SkIntToScalar(10))));
1808 // above
1809 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1810 SkRect::MakeXYWH(SkIntToScalar(50),
1811 SkIntToScalar(-10),
1812 SkIntToScalar(10),
1813 SkIntToScalar(10))));
1814 // to the left
1815 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1816 SkIntToScalar(5),
1817 SkIntToScalar(5),
1818 SkIntToScalar(5))));
1819
1820 // outside the diagonal edge
1821 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1822 SkIntToScalar(200),
1823 SkIntToScalar(20),
1824 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001825
bsalomonb17c1292014-08-28 14:04:55 -07001826
1827 // Test that multiple move commands do not cause asserts.
1828
1829 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001830 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001831 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1832#if 0
1833 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1834 SkIntToScalar(10),
1835 SkIntToScalar(10))));
1836#endif
1837
1838 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001839 path.reset();
1840 path.moveTo(100, 100);
1841 path.moveTo(0, 0);
1842 path.lineTo(SkIntToScalar(100), 0);
1843 path.lineTo(0, SkIntToScalar(100));
1844
1845 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1846 SkIntToScalar(10),
1847 SkIntToScalar(10))));
1848
bsalomonb17c1292014-08-28 14:04:55 -07001849 // Test that multiple move commands do not cause asserts and that the function
1850 // is not confused by the multiple moves.
1851 path.reset();
1852 path.moveTo(0, 0);
1853 path.lineTo(SkIntToScalar(100), 0);
1854 path.lineTo(0, SkIntToScalar(100));
1855 path.moveTo(0, SkIntToScalar(200));
1856 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1857 path.lineTo(0, SkIntToScalar(300));
1858
1859 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1860 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1861 SkIntToScalar(10),
1862 SkIntToScalar(10))));
1863
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001864 path.reset();
1865 path.lineTo(100, 100);
1866 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001867}
1868
reed@google.comf32322b2013-10-16 15:14:04 +00001869static void test_isRect_open_close(skiatest::Reporter* reporter) {
1870 SkPath path;
1871 bool isClosed;
1872
1873 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001874 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001875
halcanary96fcdcc2015-08-27 07:41:13 -07001876 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001877 REPORTER_ASSERT(reporter, isClosed);
1878}
1879
caryclark@google.comf1316942011-07-26 19:54:45 +00001880// Simple isRect test is inline TestPath, below.
1881// test_isRect provides more extensive testing.
1882static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001883 test_isRect_open_close(reporter);
1884
caryclark@google.comf1316942011-07-26 19:54:45 +00001885 // passing tests (all moveTo / lineTo...
1886 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1887 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1888 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1889 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1890 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1891 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1892 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1893 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1894 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001895 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1896 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 +00001897 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1898 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1899 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001900 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001901
caryclark@google.comf1316942011-07-26 19:54:45 +00001902 // failing tests
1903 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1904 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1905 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1906 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1907 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1908 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1909 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1910 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001911 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1912 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1913 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001914
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001915 // no close, but we should detect them as fillably the same as a rect
1916 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1917 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1918 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1919
1920 // like c2, but we double-back on ourselves
1921 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1922 // like c2, but we overshoot the start point
1923 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1924 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001925
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001926 struct IsRectTest {
1927 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001928 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001929 bool fClose;
1930 bool fIsRect;
1931 } tests[] = {
1932 { r1, SK_ARRAY_COUNT(r1), true, true },
1933 { r2, SK_ARRAY_COUNT(r2), true, true },
1934 { r3, SK_ARRAY_COUNT(r3), true, true },
1935 { r4, SK_ARRAY_COUNT(r4), true, true },
1936 { r5, SK_ARRAY_COUNT(r5), true, true },
1937 { r6, SK_ARRAY_COUNT(r6), true, true },
1938 { r7, SK_ARRAY_COUNT(r7), true, true },
1939 { r8, SK_ARRAY_COUNT(r8), true, true },
1940 { r9, SK_ARRAY_COUNT(r9), true, true },
1941 { ra, SK_ARRAY_COUNT(ra), true, true },
1942 { rb, SK_ARRAY_COUNT(rb), true, true },
1943 { rc, SK_ARRAY_COUNT(rc), true, true },
1944 { rd, SK_ARRAY_COUNT(rd), true, true },
1945 { re, SK_ARRAY_COUNT(re), true, true },
1946 { rf, SK_ARRAY_COUNT(rf), true, true },
1947
1948 { f1, SK_ARRAY_COUNT(f1), true, false },
1949 { f2, SK_ARRAY_COUNT(f2), true, false },
1950 { f3, SK_ARRAY_COUNT(f3), true, false },
1951 { f4, SK_ARRAY_COUNT(f4), true, false },
1952 { f5, SK_ARRAY_COUNT(f5), true, false },
1953 { f6, SK_ARRAY_COUNT(f6), true, false },
1954 { f7, SK_ARRAY_COUNT(f7), true, false },
1955 { f8, SK_ARRAY_COUNT(f8), true, false },
1956 { f9, SK_ARRAY_COUNT(f9), true, false },
1957 { fa, SK_ARRAY_COUNT(fa), true, false },
1958 { fb, SK_ARRAY_COUNT(fb), true, false },
1959
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001960 { c1, SK_ARRAY_COUNT(c1), false, true },
1961 { c2, SK_ARRAY_COUNT(c2), false, true },
1962 { c3, SK_ARRAY_COUNT(c3), false, true },
1963
1964 { d1, SK_ARRAY_COUNT(d1), false, false },
1965 { d2, SK_ARRAY_COUNT(d2), false, false },
1966 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001967 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001968
1969 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001970 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001971 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1972 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001973 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1974 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1975 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001976 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001977 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001978 path.close();
1979 }
halcanary96fcdcc2015-08-27 07:41:13 -07001980 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001981
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001982 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001983 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001984 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001985 SkPath::Direction direction;
1986 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001987 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001988 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001989 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1990 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001991 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001992 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001993 } else {
1994 SkRect computed;
1995 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001996 bool isClosed = (bool)-1;
1997 SkPath::Direction direction = (SkPath::Direction) - 1;
1998 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001999 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2000 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002001 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2002 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002003 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002004 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002005
caryclark@google.comf1316942011-07-26 19:54:45 +00002006 // fail, close then line
2007 SkPath path1;
2008 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002009 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002010 path1.lineTo(r1[index].fX, r1[index].fY);
2011 }
2012 path1.close();
2013 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002014 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002015
caryclark@google.comf1316942011-07-26 19:54:45 +00002016 // fail, move in the middle
2017 path1.reset();
2018 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002019 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002020 if (index == 2) {
2021 path1.moveTo(1, .5f);
2022 }
2023 path1.lineTo(r1[index].fX, r1[index].fY);
2024 }
2025 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002026 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002027
2028 // fail, move on the edge
2029 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002030 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002031 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2032 path1.lineTo(r1[index].fX, r1[index].fY);
2033 }
2034 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002035 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002036
caryclark@google.comf1316942011-07-26 19:54:45 +00002037 // fail, quad
2038 path1.reset();
2039 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002040 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002041 if (index == 2) {
2042 path1.quadTo(1, .5f, 1, .5f);
2043 }
2044 path1.lineTo(r1[index].fX, r1[index].fY);
2045 }
2046 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002047 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002048
caryclark@google.comf1316942011-07-26 19:54:45 +00002049 // fail, cubic
2050 path1.reset();
2051 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002052 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002053 if (index == 2) {
2054 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2055 }
2056 path1.lineTo(r1[index].fX, r1[index].fY);
2057 }
2058 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002059 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002060}
2061
bsalomonedc743a2016-06-01 09:42:31 -07002062static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2063 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2064 SkRect r = SkRect::MakeEmpty();
2065 SkPath::Direction d = SkPath::kCCW_Direction;
2066 unsigned s = ~0U;
2067
2068 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2069 REPORTER_ASSERT(reporter, r == rect);
2070 REPORTER_ASSERT(reporter, d == dir);
2071 REPORTER_ASSERT(reporter, s == start);
2072}
2073
2074static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2075 SkRect r = SkRect::MakeEmpty();
2076 SkPath::Direction d = SkPath::kCCW_Direction;
2077 unsigned s = ~0U;
2078
2079 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2080 const SkRect emptyRect = SkRect::MakeEmpty();
2081 SkPath path;
2082 for (int start = 0; start < 4; ++start) {
2083 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2084 SkPath path;
2085 path.addRect(testRect, dir, start);
2086 check_simple_closed_rect(reporter, path, testRect, dir, start);
2087 path.close();
2088 check_simple_closed_rect(reporter, path, testRect, dir, start);
2089 SkPath path2 = path;
2090 path2.lineTo(10, 10);
2091 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2092 path2 = path;
2093 path2.moveTo(10, 10);
2094 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2095 path2 = path;
2096 path2.addRect(testRect, dir, start);
2097 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2098 // Make the path by hand, manually closing it.
2099 path2.reset();
2100 SkPath::RawIter iter(path);
2101 SkPath::Verb v;
2102 SkPoint verbPts[4];
2103 SkPoint firstPt = {0.f, 0.f};
2104 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2105 switch(v) {
2106 case SkPath::kMove_Verb:
2107 firstPt = verbPts[0];
2108 path2.moveTo(verbPts[0]);
2109 break;
2110 case SkPath::kLine_Verb:
2111 path2.lineTo(verbPts[1]);
2112 break;
2113 default:
2114 break;
2115 }
2116 }
2117 // We haven't closed it yet...
2118 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2119 // ... now we do and test again.
2120 path2.lineTo(firstPt);
2121 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2122 // A redundant close shouldn't cause a failure.
2123 path2.close();
2124 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2125 // Degenerate point and line rects are not allowed
2126 path2.reset();
2127 path2.addRect(emptyRect, dir, start);
2128 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2129 SkRect degenRect = testRect;
2130 degenRect.fLeft = degenRect.fRight;
2131 path2.reset();
2132 path2.addRect(degenRect, dir, start);
2133 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2134 degenRect = testRect;
2135 degenRect.fTop = degenRect.fBottom;
2136 path2.reset();
2137 path2.addRect(degenRect, dir, start);
2138 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2139 // An inverted rect makes a rect path, but changes the winding dir and start point.
2140 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2141 ? SkPath::kCCW_Direction
2142 : SkPath::kCW_Direction;
2143 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2144 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2145 SkRect swapRect = testRect;
2146 SkTSwap(swapRect.fLeft, swapRect.fRight);
2147 path2.reset();
2148 path2.addRect(swapRect, dir, start);
2149 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2150 swapRect = testRect;
2151 SkTSwap(swapRect.fTop, swapRect.fBottom);
2152 path2.reset();
2153 path2.addRect(swapRect, dir, start);
2154 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2155 }
2156 }
bsalomon057ae8a2016-07-24 05:37:26 -07002157 // down, up, left, close
2158 path.reset();
2159 path.moveTo(1, 1);
2160 path.lineTo(1, 2);
2161 path.lineTo(1, 1);
2162 path.lineTo(0, 1);
2163 SkRect rect;
2164 SkPath::Direction dir;
2165 unsigned start;
2166 path.close();
2167 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2168 // right, left, up, close
2169 path.reset();
2170 path.moveTo(1, 1);
2171 path.lineTo(2, 1);
2172 path.lineTo(1, 1);
2173 path.lineTo(1, 0);
2174 path.close();
2175 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2176 // parallelogram with horizontal edges
2177 path.reset();
2178 path.moveTo(1, 0);
2179 path.lineTo(3, 0);
2180 path.lineTo(2, 1);
2181 path.lineTo(0, 1);
2182 path.close();
2183 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2184 // parallelogram with vertical edges
2185 path.reset();
2186 path.moveTo(0, 1);
2187 path.lineTo(0, 3);
2188 path.lineTo(1, 2);
2189 path.lineTo(1, 0);
2190 path.close();
2191 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2192
bsalomonedc743a2016-06-01 09:42:31 -07002193}
2194
caryclark95bc5f32015-04-08 08:34:15 -07002195static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002196 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002197 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002198 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2199 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2200 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002201 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002202 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2203 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2204 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2205 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002206 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2207 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 +00002208 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2209 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2210 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002211
2212 // failing tests
2213 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2214 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2215 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2216 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2217 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2218 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2219 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2220 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2221
caryclark95bc5f32015-04-08 08:34:15 -07002222 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002223 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2224 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2225
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002226 struct IsNestedRectTest {
2227 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002228 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002229 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002230 bool fClose;
2231 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2232 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002233 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2234 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2235 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2236 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2237 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2238 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2239 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2240 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2241 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2242 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2243 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2244 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2245 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2246 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002247
reed026beb52015-06-10 14:23:15 -07002248 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2249 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2250 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2251 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2252 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2253 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2254 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2255 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002256
reed026beb52015-06-10 14:23:15 -07002257 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2258 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002259 };
2260
2261 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002262 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002263 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002264 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2265 SkPath path;
2266 if (rectFirst) {
2267 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2268 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002269 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2270 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2271 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002272 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002273 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002274 path.close();
2275 }
2276 if (!rectFirst) {
2277 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2278 }
caryclark95bc5f32015-04-08 08:34:15 -07002279 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002280 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002281 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002282 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002283 SkPathPriv::FirstDirection expectedDirs[2];
2284 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002285 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002286 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002287 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2288 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002289 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002290 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002291 } else {
reed026beb52015-06-10 14:23:15 -07002292 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002293 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002294 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002295 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002296 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2297 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002298 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2299 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002300 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002301 }
2302
2303 // fail, close then line
2304 SkPath path1;
2305 if (rectFirst) {
2306 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2307 }
2308 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002309 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002310 path1.lineTo(r1[index].fX, r1[index].fY);
2311 }
2312 path1.close();
2313 path1.lineTo(1, 0);
2314 if (!rectFirst) {
2315 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2316 }
halcanary96fcdcc2015-08-27 07:41:13 -07002317 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002318
2319 // fail, move in the middle
2320 path1.reset();
2321 if (rectFirst) {
2322 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2323 }
2324 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002325 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002326 if (index == 2) {
2327 path1.moveTo(1, .5f);
2328 }
2329 path1.lineTo(r1[index].fX, r1[index].fY);
2330 }
2331 path1.close();
2332 if (!rectFirst) {
2333 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2334 }
halcanary96fcdcc2015-08-27 07:41:13 -07002335 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002336
2337 // fail, move on the edge
2338 path1.reset();
2339 if (rectFirst) {
2340 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2341 }
bsalomon98806072014-12-12 15:11:17 -08002342 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002343 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2344 path1.lineTo(r1[index].fX, r1[index].fY);
2345 }
2346 path1.close();
2347 if (!rectFirst) {
2348 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2349 }
halcanary96fcdcc2015-08-27 07:41:13 -07002350 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002351
2352 // fail, quad
2353 path1.reset();
2354 if (rectFirst) {
2355 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2356 }
2357 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002358 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002359 if (index == 2) {
2360 path1.quadTo(1, .5f, 1, .5f);
2361 }
2362 path1.lineTo(r1[index].fX, r1[index].fY);
2363 }
2364 path1.close();
2365 if (!rectFirst) {
2366 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2367 }
halcanary96fcdcc2015-08-27 07:41:13 -07002368 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002369
2370 // fail, cubic
2371 path1.reset();
2372 if (rectFirst) {
2373 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2374 }
2375 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002376 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002377 if (index == 2) {
2378 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2379 }
2380 path1.lineTo(r1[index].fX, r1[index].fY);
2381 }
2382 path1.close();
2383 if (!rectFirst) {
2384 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2385 }
halcanary96fcdcc2015-08-27 07:41:13 -07002386 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002387
caryclark@google.com56f233a2012-11-19 13:06:06 +00002388 // fail, not nested
2389 path1.reset();
2390 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2391 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002392 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002393 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002394
caryclark95bc5f32015-04-08 08:34:15 -07002395 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2396 SkPath path;
2397 path.moveTo(0, 0);
2398 path.lineTo(10, 0);
2399 path.lineTo(10, 10);
2400 path.lineTo(0, 10);
2401 path.lineTo(0, 0);
2402 path.moveTo(1, 1);
2403 path.lineTo(9, 1);
2404 path.lineTo(9, 9);
2405 path.lineTo(1, 9);
2406 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002407 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002408
caryclark@google.combfe90372012-11-21 13:56:20 +00002409 // pass, stroke rect
2410 SkPath src, dst;
2411 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2412 SkPaint strokePaint;
2413 strokePaint.setStyle(SkPaint::kStroke_Style);
2414 strokePaint.setStrokeWidth(2);
2415 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002416 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002417}
2418
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002419static void write_and_read_back(skiatest::Reporter* reporter,
2420 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002421 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002422 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002423 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002424 SkAutoMalloc storage(size);
2425 writer.flatten(storage.get());
2426 SkReader32 reader(storage.get(), size);
2427
2428 SkPath readBack;
2429 REPORTER_ASSERT(reporter, readBack != p);
2430 reader.readPath(&readBack);
2431 REPORTER_ASSERT(reporter, readBack == p);
2432
rmistry@google.comd6176b02012-08-23 18:14:13 +00002433 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002434 p.getConvexityOrUnknown());
2435
bsalomon78d58d12016-05-27 09:17:04 -07002436 SkRect oval0, oval1;
2437 SkPath::Direction dir0, dir1;
2438 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002439 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002440 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2441 REPORTER_ASSERT(reporter, oval0 == oval1);
2442 REPORTER_ASSERT(reporter, dir0 == dir1);
2443 REPORTER_ASSERT(reporter, start0 == start1);
2444 }
2445 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2446 SkRRect rrect0, rrect1;
2447 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2448 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2449 REPORTER_ASSERT(reporter, dir0 == dir1);
2450 REPORTER_ASSERT(reporter, start0 == start1);
2451 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002452 const SkRect& origBounds = p.getBounds();
2453 const SkRect& readBackBounds = readBack.getBounds();
2454
2455 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2456}
2457
reed@google.com53effc52011-09-21 19:05:12 +00002458static void test_flattening(skiatest::Reporter* reporter) {
2459 SkPath p;
2460
2461 static const SkPoint pts[] = {
2462 { 0, 0 },
2463 { SkIntToScalar(10), SkIntToScalar(10) },
2464 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2465 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2466 };
2467 p.moveTo(pts[0]);
2468 p.lineTo(pts[1]);
2469 p.quadTo(pts[2], pts[3]);
2470 p.cubicTo(pts[4], pts[5], pts[6]);
2471
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002472 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002473
2474 // create a buffer that should be much larger than the path so we don't
2475 // kill our stack if writer goes too far.
2476 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002477 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002478 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002479 REPORTER_ASSERT(reporter, size1 == size2);
2480
2481 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002482 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002483 REPORTER_ASSERT(reporter, size1 == size3);
2484 REPORTER_ASSERT(reporter, p == p2);
2485
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002486 size3 = p2.readFromMemory(buffer, 0);
2487 REPORTER_ASSERT(reporter, !size3);
2488
2489 SkPath tooShort;
2490 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2491 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2492
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002493 char buffer2[1024];
2494 size3 = p2.writeToMemory(buffer2);
2495 REPORTER_ASSERT(reporter, size1 == size3);
2496 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002497
2498 // test persistence of the oval flag & convexity
2499 {
2500 SkPath oval;
2501 SkRect rect = SkRect::MakeWH(10, 10);
2502 oval.addOval(rect);
2503
2504 write_and_read_back(reporter, oval);
2505 }
reed@google.com53effc52011-09-21 19:05:12 +00002506}
2507
2508static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002509 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002510
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002511#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002512 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002513 { 0, 0 }, // move
2514 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2515 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2516 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2517#if CONIC_PERSPECTIVE_BUG_FIXED
2518 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2519#endif
reed@google.com53effc52011-09-21 19:05:12 +00002520 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002521 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002522
reed@google.com53effc52011-09-21 19:05:12 +00002523 p.moveTo(pts[0]);
2524 p.lineTo(pts[1]);
2525 p.quadTo(pts[2], pts[3]);
2526 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002527#if CONIC_PERSPECTIVE_BUG_FIXED
2528 p.conicTo(pts[4], pts[5], 0.5f);
2529#endif
2530 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002531
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002532 {
2533 SkMatrix matrix;
2534 matrix.reset();
2535 SkPath p1;
2536 p.transform(matrix, &p1);
2537 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002538 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002539
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002540
2541 {
2542 SkMatrix matrix;
2543 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2544
2545 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2546
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002547 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002548 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002549 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002550 REPORTER_ASSERT(reporter, kPtCount == count);
2551 for (int i = 0; i < count; ++i) {
2552 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2553 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2554 }
2555 }
2556
2557 {
2558 SkMatrix matrix;
2559 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002560 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002561
2562 SkPath p1;
2563 p1.moveTo(SkPoint::Make(0, 0));
2564
2565 p.transform(matrix, &p1);
2566 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002567 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002568 SkRect pBounds = p.getBounds();
2569 SkRect p1Bounds = p1.getBounds();
2570 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2571 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2572 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2573 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2574 }
2575
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002576 p.reset();
2577 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002578
2579 {
2580 SkMatrix matrix;
2581 matrix.reset();
2582 SkPath p1;
2583 p1.moveTo(SkPoint::Make(0, 0));
2584
2585 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002586 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002587 }
2588
2589
2590 {
2591 SkMatrix matrix;
2592 matrix.reset();
2593 matrix.setScaleX(-1);
2594 SkPath p1;
2595 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2596
2597 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002598 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002599 }
2600
2601 {
2602 SkMatrix matrix;
2603 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2604 SkPath p1;
2605 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2606
2607 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002608 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002609 }
reed@google.com53effc52011-09-21 19:05:12 +00002610}
2611
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002612static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002613 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002614 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002615
caryclark@google.com56f233a2012-11-19 13:06:06 +00002616 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002617 const char* testPath;
2618 const size_t numResultPts;
2619 const SkRect resultBound;
2620 const SkPath::Verb* resultVerbs;
2621 const size_t numResultVerbs;
2622 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002623
schenney@chromium.org7e963602012-06-13 17:05:43 +00002624 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2625 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2626 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2627 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2628 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2629 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2630 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2631 static const SkPath::Verb resultVerbs8[] = {
2632 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2633 };
2634 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2635 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2636 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2637 static const SkPath::Verb resultVerbs12[] = {
2638 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2639 };
2640 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2641 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2642 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2643 static const SkPath::Verb resultVerbs16[] = {
2644 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2645 };
2646 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002647 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002648 { "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 -07002649 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002650 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2651 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2652 { "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) },
2653 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2654 { "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) },
2655 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2656 { "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) },
2657 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2658 { "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) },
2659 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2660 { "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 +00002661 SK_ARRAY_COUNT(resultVerbs14)
2662 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002663 { "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) },
2664 { "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 +00002665 SK_ARRAY_COUNT(resultVerbs16)
2666 }
2667 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002668
schenney@chromium.org7e963602012-06-13 17:05:43 +00002669 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2670 p.reset();
2671 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2672 REPORTER_ASSERT(reporter, valid);
2673 REPORTER_ASSERT(reporter, !p.isEmpty());
2674 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2675 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2676 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2677 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2678 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2679 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002680 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002681}
2682
2683struct SegmentInfo {
2684 SkPath fPath;
2685 int fPointCount;
2686};
2687
reed@google.com10296cc2011-09-21 12:29:05 +00002688#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2689
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002690static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002691 SkPath p, p2;
2692
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002693 p.moveTo(0, 0);
2694 p.quadTo(100, 100, 200, 200);
2695 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2696 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002697 p2 = p;
2698 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002699 p.cubicTo(100, 100, 200, 200, 300, 300);
2700 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2701 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002702 p2 = p;
2703 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2704
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002705 p.reset();
2706 p.moveTo(0, 0);
2707 p.cubicTo(100, 100, 200, 200, 300, 300);
2708 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002709 p2 = p;
2710 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002711
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002712 REPORTER_ASSERT(reporter, !p.isEmpty());
2713}
2714
2715static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002716 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002717 SkPoint pts[4];
2718
2719 // Test an iterator with no path
2720 SkPath::Iter noPathIter;
2721 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002722
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002723 // Test that setting an empty path works
2724 noPathIter.setPath(p, false);
2725 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002726
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002727 // Test that close path makes no difference for an empty path
2728 noPathIter.setPath(p, true);
2729 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002730
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002731 // Test an iterator with an initial empty path
2732 SkPath::Iter iter(p, false);
2733 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2734
2735 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002736 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002737 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2738
rmistry@google.comd6176b02012-08-23 18:14:13 +00002739
schenney@chromium.org7e963602012-06-13 17:05:43 +00002740 struct iterTestData {
2741 const char* testPath;
2742 const bool forceClose;
2743 const bool consumeDegenerates;
2744 const size_t* numResultPtsPerVerb;
2745 const SkPoint* resultPts;
2746 const SkPath::Verb* resultVerbs;
2747 const size_t numResultVerbs;
2748 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002749
schenney@chromium.org7e963602012-06-13 17:05:43 +00002750 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2751 static const SkPath::Verb resultVerbs2[] = {
2752 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2753 };
2754 static const SkPath::Verb resultVerbs3[] = {
2755 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2756 };
2757 static const SkPath::Verb resultVerbs4[] = {
2758 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2759 };
2760 static const SkPath::Verb resultVerbs5[] = {
2761 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2762 };
2763 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002764 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2765 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2766 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2767 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002768 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002769 static const SkPoint resultPts2[] = {
2770 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2771 };
2772 static const SkPoint resultPts3[] = {
2773 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2774 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2775 };
2776 static const SkPoint resultPts4[] = {
2777 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2778 };
2779 static const SkPoint resultPts5[] = {
2780 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2781 };
2782 static const struct iterTestData gIterTests[] = {
2783 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002784 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2785 { "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 +00002786 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2787 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2788 { "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) },
2789 { "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 +00002790 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2791 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2792 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2793 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2794 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2795 { "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 +00002796 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002797
schenney@chromium.org7e963602012-06-13 17:05:43 +00002798 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2799 p.reset();
2800 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2801 REPORTER_ASSERT(reporter, valid);
2802 iter.setPath(p, gIterTests[i].forceClose);
2803 int j = 0, l = 0;
2804 do {
2805 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2806 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2807 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2808 }
2809 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2810 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2811 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002812
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002813 p.reset();
2814 iter.setPath(p, false);
2815 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2816 p.lineTo(1, 1);
2817 p.close();
2818 iter.setPath(p, false);
2819 REPORTER_ASSERT(reporter, iter.isClosedContour());
2820 p.reset();
2821 iter.setPath(p, true);
2822 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2823 p.lineTo(1, 1);
2824 iter.setPath(p, true);
2825 REPORTER_ASSERT(reporter, iter.isClosedContour());
2826 p.moveTo(0, 0);
2827 p.lineTo(2, 2);
2828 iter.setPath(p, false);
2829 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2830
2831 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2832 // check to see if the result is correct.
2833 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2834 p.reset();
2835 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2836 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2837 iter.setPath(p, true);
2838 iter.next(pts, false);
2839 iter.next(pts, false);
2840 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2841 }
2842
2843 p.reset();
2844 p.quadTo(0, 0, 0, 0);
2845 iter.setPath(p, false);
2846 iter.next(pts, false);
2847 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2848 iter.setPath(p, false);
2849 iter.next(pts, false);
2850 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2851
2852 p.reset();
2853 p.conicTo(0, 0, 0, 0, 0.5f);
2854 iter.setPath(p, false);
2855 iter.next(pts, false);
2856 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2857 iter.setPath(p, false);
2858 iter.next(pts, false);
2859 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2860
2861 p.reset();
2862 p.cubicTo(0, 0, 0, 0, 0, 0);
2863 iter.setPath(p, false);
2864 iter.next(pts, false);
2865 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2866 iter.setPath(p, false);
2867 iter.next(pts, false);
2868 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2869
2870 p.moveTo(1, 1); // add a trailing moveto
2871 iter.setPath(p, false);
2872 iter.next(pts, false);
2873 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2874 iter.setPath(p, false);
2875 iter.next(pts, false);
2876 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2877
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002878 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002879
2880 // Test out mixed degenerate and non-degenerate geometry with Conics
2881 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2882 SkRect r = SkRect::MakeWH(100, 100);
2883 SkRRect rr;
2884 rr.setRectRadii(r, radii);
2885 p.reset();
2886 p.addRRect(rr);
2887 iter.setPath(p, false);
2888 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2889 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2890 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2891 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2892 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002893}
2894
2895static void test_raw_iter(skiatest::Reporter* reporter) {
2896 SkPath p;
2897 SkPoint pts[4];
2898
2899 // Test an iterator with no path
2900 SkPath::RawIter noPathIter;
2901 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2902 // Test that setting an empty path works
2903 noPathIter.setPath(p);
2904 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002905
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002906 // Test an iterator with an initial empty path
2907 SkPath::RawIter iter(p);
2908 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2909
2910 // Test that a move-only path returns the move.
2911 p.moveTo(SK_Scalar1, 0);
2912 iter.setPath(p);
2913 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2914 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2915 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2916 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2917
2918 // No matter how many moves we add, we should get them all back
2919 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2920 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2921 iter.setPath(p);
2922 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2923 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2924 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2925 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2926 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2927 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2928 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2929 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2930 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2931 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2932
2933 // Initial close is never ever stored
2934 p.reset();
2935 p.close();
2936 iter.setPath(p);
2937 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2938
2939 // Move/close sequences
2940 p.reset();
2941 p.close(); // Not stored, no purpose
2942 p.moveTo(SK_Scalar1, 0);
2943 p.close();
2944 p.close(); // Not stored, no purpose
2945 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2946 p.close();
2947 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2948 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2949 p.close();
2950 iter.setPath(p);
2951 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2952 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2953 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2954 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002955 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2956 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2957 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2958 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002959 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2960 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2961 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2962 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2963 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2964 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2965 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002966 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2967
2968 // Generate random paths and verify
2969 SkPoint randomPts[25];
2970 for (int i = 0; i < 5; ++i) {
2971 for (int j = 0; j < 5; ++j) {
2972 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2973 }
2974 }
2975
2976 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002977 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002978 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002979 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002980 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002981
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002982 for (int i = 0; i < 500; ++i) {
2983 p.reset();
2984 bool lastWasClose = true;
2985 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002986 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002987 int numPoints = 0;
2988 int numVerbs = (rand.nextU() >> 16) % 10;
2989 int numIterVerbs = 0;
2990 for (int j = 0; j < numVerbs; ++j) {
2991 do {
2992 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2993 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002994 switch (nextVerb) {
2995 case SkPath::kMove_Verb:
2996 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2997 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002998 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002999 numPoints += 1;
3000 lastWasClose = false;
3001 haveMoveTo = true;
3002 break;
3003 case SkPath::kLine_Verb:
3004 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003005 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003006 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3007 haveMoveTo = true;
3008 }
3009 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3010 p.lineTo(expectedPts[numPoints]);
3011 numPoints += 1;
3012 lastWasClose = false;
3013 break;
3014 case SkPath::kQuad_Verb:
3015 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003016 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003017 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3018 haveMoveTo = true;
3019 }
3020 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3021 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3022 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3023 numPoints += 2;
3024 lastWasClose = false;
3025 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003026 case SkPath::kConic_Verb:
3027 if (!haveMoveTo) {
3028 expectedPts[numPoints++] = lastMoveToPt;
3029 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3030 haveMoveTo = true;
3031 }
3032 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3033 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3034 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3035 rand.nextUScalar1() * 4);
3036 numPoints += 2;
3037 lastWasClose = false;
3038 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003039 case SkPath::kCubic_Verb:
3040 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003041 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003042 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3043 haveMoveTo = true;
3044 }
3045 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3046 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3047 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3048 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3049 expectedPts[numPoints + 2]);
3050 numPoints += 3;
3051 lastWasClose = false;
3052 break;
3053 case SkPath::kClose_Verb:
3054 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003055 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003056 lastWasClose = true;
3057 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003058 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003059 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003060 }
3061 expectedVerbs[numIterVerbs++] = nextVerb;
3062 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003063
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003064 iter.setPath(p);
3065 numVerbs = numIterVerbs;
3066 numIterVerbs = 0;
3067 int numIterPts = 0;
3068 SkPoint lastMoveTo;
3069 SkPoint lastPt;
3070 lastMoveTo.set(0, 0);
3071 lastPt.set(0, 0);
3072 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3073 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3074 numIterVerbs++;
3075 switch (nextVerb) {
3076 case SkPath::kMove_Verb:
3077 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3078 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3079 lastPt = lastMoveTo = pts[0];
3080 numIterPts += 1;
3081 break;
3082 case SkPath::kLine_Verb:
3083 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3084 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3085 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3086 lastPt = pts[1];
3087 numIterPts += 1;
3088 break;
3089 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003090 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003091 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3092 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3093 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3094 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3095 lastPt = pts[2];
3096 numIterPts += 2;
3097 break;
3098 case SkPath::kCubic_Verb:
3099 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3100 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3101 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3102 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3103 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3104 lastPt = pts[3];
3105 numIterPts += 3;
3106 break;
3107 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003108 lastPt = lastMoveTo;
3109 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003110 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003111 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003112 }
3113 }
3114 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3115 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3116 }
3117}
3118
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003119static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003120 const SkPath& path,
3121 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003122 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003123 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003124 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003125 SkPath::Direction isOvalDir;
3126 unsigned isOvalStart;
3127 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003128 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003129 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3130 SkPath tmpPath;
3131 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3132 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003133 }
bsalomon78d58d12016-05-27 09:17:04 -07003134 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003135}
3136
3137static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003138 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003139 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003140 SkPath tmp;
3141
3142 SkMatrix m;
3143 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3144 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003145 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003146 if (SkPathPriv::kCCW_FirstDirection == dir) {
3147 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003148 } else {
reed026beb52015-06-10 14:23:15 -07003149 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3150 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003151 }
3152 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003153}
3154
3155static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003156 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003157 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003158 SkPath tmp;
3159
3160 // translate at small offset
3161 SkMatrix m;
3162 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3163 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003164 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003165
3166 tmp.reset();
3167 m.reset();
3168
3169 // translate at a relatively big offset
3170 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3171 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003172 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003173}
3174
3175static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003176 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003177 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003178 for (int angle = 0; angle < 360; ++angle) {
3179 SkPath tmp;
3180 SkMatrix m;
3181 m.setRotate(SkIntToScalar(angle));
3182 path.transform(m, &tmp);
3183
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003184 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003185 // degrees is not an oval anymore, this can be improved. we made this
3186 // for the simplicity of our implementation.
3187 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003188 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003189 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003190 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003191 }
3192 }
3193}
3194
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003195static void test_circle_mirror_x(skiatest::Reporter* reporter,
3196 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003197 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003198 SkPath tmp;
3199 SkMatrix m;
3200 m.reset();
3201 m.setScaleX(-SK_Scalar1);
3202 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003203 if (SkPathPriv::kCW_FirstDirection == dir) {
3204 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003205 } else {
reed026beb52015-06-10 14:23:15 -07003206 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3207 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003208 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003209 check_for_circle(reporter, tmp, true, dir);
3210}
3211
3212static void test_circle_mirror_y(skiatest::Reporter* reporter,
3213 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003214 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003215 SkPath tmp;
3216 SkMatrix m;
3217 m.reset();
3218 m.setScaleY(-SK_Scalar1);
3219 path.transform(m, &tmp);
3220
reed026beb52015-06-10 14:23:15 -07003221 if (SkPathPriv::kCW_FirstDirection == dir) {
3222 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003223 } else {
reed026beb52015-06-10 14:23:15 -07003224 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3225 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003226 }
3227
3228 check_for_circle(reporter, tmp, true, dir);
3229}
3230
3231static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3232 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003233 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003234 SkPath tmp;
3235 SkMatrix m;
3236 m.reset();
3237 m.setScaleX(-SK_Scalar1);
3238 m.setScaleY(-SK_Scalar1);
3239 path.transform(m, &tmp);
3240
3241 check_for_circle(reporter, tmp, true, dir);
3242}
3243
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003244static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003245 SkPath::Direction inDir) {
3246 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003247 SkPath path;
3248
3249 // circle at origin
reed026beb52015-06-10 14:23:15 -07003250 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3251
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003252 check_for_circle(reporter, path, true, dir);
3253 test_circle_rotate(reporter, path, dir);
3254 test_circle_translate(reporter, path, dir);
3255 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003256 test_circle_mirror_x(reporter, path, dir);
3257 test_circle_mirror_y(reporter, path, dir);
3258 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003259
3260 // circle at an offset at (10, 10)
3261 path.reset();
3262 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003263 SkIntToScalar(20), inDir);
3264
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003265 check_for_circle(reporter, path, true, dir);
3266 test_circle_rotate(reporter, path, dir);
3267 test_circle_translate(reporter, path, dir);
3268 test_circle_skew(reporter, path, dir);
3269 test_circle_mirror_x(reporter, path, dir);
3270 test_circle_mirror_y(reporter, path, dir);
3271 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003272
3273 // Try different starting points for the contour.
3274 for (unsigned start = 0; start < 4; ++start) {
3275 path.reset();
3276 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3277 test_circle_rotate(reporter, path, dir);
3278 test_circle_translate(reporter, path, dir);
3279 test_circle_skew(reporter, path, dir);
3280 test_circle_mirror_x(reporter, path, dir);
3281 test_circle_mirror_y(reporter, path, dir);
3282 test_circle_mirror_xy(reporter, path, dir);
3283 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003284}
3285
3286static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3287 SkPath path;
3288 SkPath circle;
3289 SkPath rect;
3290 SkPath empty;
3291
reed026beb52015-06-10 14:23:15 -07003292 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3293 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003294
3295 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003296 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3297 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3298
3299 SkMatrix translate;
3300 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3301
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003302 // Although all the path concatenation related operations leave
3303 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003304
3305 // empty + circle (translate)
3306 path = empty;
3307 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003308 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003309
3310 // circle + empty (translate)
3311 path = circle;
3312 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003313
reed026beb52015-06-10 14:23:15 -07003314 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003315
3316 // test reverseAddPath
3317 path = circle;
3318 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003319 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003320}
3321
3322static void test_circle(skiatest::Reporter* reporter) {
3323 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3324 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3325
3326 // multiple addCircle()
3327 SkPath path;
3328 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3329 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003330 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003331
3332 // some extra lineTo() would make isOval() fail
3333 path.reset();
3334 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3335 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003336 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003337
3338 // not back to the original point
3339 path.reset();
3340 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3341 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003342 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003343
3344 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003345
3346 // test negative radius
3347 path.reset();
3348 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3349 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003350}
3351
3352static void test_oval(skiatest::Reporter* reporter) {
3353 SkRect rect;
3354 SkMatrix m;
3355 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003356 unsigned start = 0;
3357 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003358
3359 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3360 path.addOval(rect);
3361
bsalomon78d58d12016-05-27 09:17:04 -07003362 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003363 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003364
3365 m.setRotate(SkIntToScalar(90));
3366 SkPath tmp;
3367 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003368 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3369 // is unchanged.
3370 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3371 REPORTER_ASSERT(reporter, 2 == start);
3372 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003373
3374 m.reset();
3375 m.setRotate(SkIntToScalar(30));
3376 tmp.reset();
3377 path.transform(m, &tmp);
3378 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003379 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003380
3381 // since empty path being transformed.
3382 path.reset();
3383 tmp.reset();
3384 m.reset();
3385 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003386 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003387
3388 // empty path is not an oval
3389 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003390 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003391
3392 // only has moveTo()s
3393 tmp.reset();
3394 tmp.moveTo(0, 0);
3395 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003396 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003397
3398 // mimic WebKit's calling convention,
3399 // call moveTo() first and then call addOval()
3400 path.reset();
3401 path.moveTo(0, 0);
3402 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003403 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003404
3405 // copy path
3406 path.reset();
3407 tmp.reset();
3408 tmp.addOval(rect);
3409 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003410 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3411 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3412 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003413}
3414
bungeman@google.coma5809a32013-06-21 15:13:34 +00003415static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3416 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003417
reed@android.com3abec1d2009-03-02 05:36:20 +00003418 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003419 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003420 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003421 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003422 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003423 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3424 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003425 REPORTER_ASSERT(reporter, p == empty);
3426 REPORTER_ASSERT(reporter, !(p != empty));
3427}
3428
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003429static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3430 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003431 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003432 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003433 path->setConvexity(SkPath::kUnknown_Convexity);
3434 REPORTER_ASSERT(reporter, path->isConvex());
3435 path->reset();
3436}
3437
caryclarkd3d1a982014-12-08 04:57:38 -08003438static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3439 SkPath::Direction dir) {
3440 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003441 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003442 path->setConvexity(SkPath::kUnknown_Convexity);
3443 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3444 path->reset();
3445}
3446
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003447static void test_rrect(skiatest::Reporter* reporter) {
3448 SkPath p;
3449 SkRRect rr;
3450 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3451 SkRect r = {10, 20, 30, 40};
3452 rr.setRectRadii(r, radii);
3453 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003454 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003455 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003456 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003457 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003458 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003459 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003460 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003461 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003462 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003463 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003464 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3465 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3466 SkVector save = radii[i];
3467 radii[i].set(0, 0);
3468 rr.setRectRadii(r, radii);
3469 p.addRRect(rr);
3470 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3471 radii[i] = save;
3472 }
3473 p.addRoundRect(r, 0, 0);
3474 SkRect returnedRect;
3475 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3476 REPORTER_ASSERT(reporter, returnedRect == r);
3477 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3478 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3479 rr.setRectRadii(r, zeroRadii);
3480 p.addRRect(rr);
3481 bool closed;
3482 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003483 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003484 REPORTER_ASSERT(reporter, closed);
3485 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3486 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3487 p.addRRect(rr, SkPath::kCW_Direction);
3488 p.addRRect(rr, SkPath::kCW_Direction);
3489 REPORTER_ASSERT(reporter, !p.isConvex());
3490 p.reset();
3491 p.addRRect(rr, SkPath::kCCW_Direction);
3492 p.addRRect(rr, SkPath::kCCW_Direction);
3493 REPORTER_ASSERT(reporter, !p.isConvex());
3494 p.reset();
3495 SkRect emptyR = {10, 20, 10, 30};
3496 rr.setRectRadii(emptyR, radii);
3497 p.addRRect(rr);
3498 REPORTER_ASSERT(reporter, p.isEmpty());
3499 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3500 rr.setRectRadii(largeR, radii);
3501 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003502 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003503
3504 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003505 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3506 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003507 REPORTER_ASSERT(reporter, rr.isEmpty());
3508
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003509 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3510 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3511 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003512}
3513
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003514static void test_arc(skiatest::Reporter* reporter) {
3515 SkPath p;
3516 SkRect emptyOval = {10, 20, 30, 20};
3517 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3518 p.addArc(emptyOval, 1, 2);
3519 REPORTER_ASSERT(reporter, p.isEmpty());
3520 p.reset();
3521 SkRect oval = {10, 20, 30, 40};
3522 p.addArc(oval, 1, 0);
3523 REPORTER_ASSERT(reporter, p.isEmpty());
3524 p.reset();
3525 SkPath cwOval;
3526 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003527 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003528 REPORTER_ASSERT(reporter, p == cwOval);
3529 p.reset();
3530 SkPath ccwOval;
3531 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003532 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003533 REPORTER_ASSERT(reporter, p == ccwOval);
3534 p.reset();
3535 p.addArc(oval, 1, 180);
3536 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003537 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003538 p.setConvexity(SkPath::kUnknown_Convexity);
3539 REPORTER_ASSERT(reporter, p.isConvex());
3540}
3541
bsalomon1978ce22016-05-31 10:42:16 -07003542static inline SkScalar oval_start_index_to_angle(unsigned start) {
3543 switch (start) {
3544 case 0:
3545 return 270.f;
3546 case 1:
3547 return 0.f;
3548 case 2:
3549 return 90.f;
3550 case 3:
3551 return 180.f;
3552 default:
3553 return -1.f;
3554 }
3555}
3556
3557static inline SkScalar canonical_start_angle(float angle) {
3558 while (angle < 0.f) {
3559 angle += 360.f;
3560 }
3561 while (angle >= 360.f) {
3562 angle -= 360.f;
3563 }
3564 return angle;
3565}
3566
3567static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3568 const SkPath& path) {
3569 SkRect r = SkRect::MakeEmpty();
3570 SkPath::Direction d = SkPath::kCCW_Direction;
3571 unsigned s = ~0U;
3572 bool isOval = path.isOval(&r, &d, &s);
3573 REPORTER_ASSERT(reporter, isOval);
3574 SkPath recreatedPath;
3575 recreatedPath.addOval(r, d, s);
3576 REPORTER_ASSERT(reporter, path == recreatedPath);
3577 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3578 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3579}
3580
3581static void test_arc_ovals(skiatest::Reporter* reporter) {
3582 SkRect oval = SkRect::MakeWH(10, 20);
3583 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3584 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3585 SkPath path;
3586 path.addArc(oval, start, sweep);
3587 // SkPath's interfaces for inserting and extracting ovals only allow contours
3588 // to start at multiples of 90 degrees.
3589 if (std::fmod(start, 90.f) == 0) {
3590 check_oval_arc(reporter, start, sweep, path);
3591 } else {
3592 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3593 }
3594 }
3595 // Test start angles that are nearly at valid oval start angles.
3596 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3597 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3598 SkPath path;
3599 path.addArc(oval, start + delta, sweep);
3600 check_oval_arc(reporter, start, sweep, path);
3601 }
3602 }
3603 }
3604}
3605
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003606static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3607 SkScalar x0, SkScalar y0) {
3608 SkPoint pts[4];
3609 SkPath::Verb v = iter->next(pts);
3610 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3611 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3612 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3613}
3614
3615static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3616 SkScalar x1, SkScalar y1) {
3617 SkPoint pts[4];
3618 SkPath::Verb v = iter->next(pts);
3619 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3620 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3621 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3622}
3623
3624static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3625 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3626 SkPoint pts[4];
3627 SkPath::Verb v = iter->next(pts);
3628 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3629 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3630 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3631 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3632 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3633}
3634
3635static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3636 SkPoint pts[4];
3637 SkPath::Verb v = iter->next(pts);
3638 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3639}
3640
3641static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3642 check_done(reporter, p, iter);
3643 p->reset();
3644}
3645
3646static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3647 SkScalar x0, SkScalar y0) {
3648 SkPath::RawIter iter(*p);
3649 check_move(reporter, &iter, x0, y0);
3650 check_done_and_reset(reporter, p, &iter);
3651}
3652
3653static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3654 SkScalar x1, SkScalar y1) {
3655 SkPath::RawIter iter(*p);
3656 check_move(reporter, &iter, 0, 0);
3657 check_line(reporter, &iter, x1, y1);
3658 check_done_and_reset(reporter, p, &iter);
3659}
3660
3661static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3662 SkScalar x1, SkScalar y1) {
3663 SkPath::RawIter iter(*p);
3664 check_move(reporter, &iter, 0, 0);
3665 check_line(reporter, &iter, x1, y1);
3666 check_done(reporter, p, &iter);
3667}
3668
3669static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3670 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3671 SkPath::RawIter iter(*p);
3672 check_move(reporter, &iter, 0, 0);
3673 check_line(reporter, &iter, x1, y1);
3674 check_line(reporter, &iter, x2, y2);
3675 check_done_and_reset(reporter, p, &iter);
3676}
3677
3678static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3679 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3680 SkPath::RawIter iter(*p);
3681 check_move(reporter, &iter, 0, 0);
3682 check_quad(reporter, &iter, x1, y1, x2, y2);
3683 check_done_and_reset(reporter, p, &iter);
3684}
3685
reedd5d27d92015-02-09 13:54:43 -08003686static bool nearly_equal(const SkRect& a, const SkRect& b) {
3687 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3688 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3689 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3690 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3691}
3692
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003693static void test_arcTo(skiatest::Reporter* reporter) {
3694 SkPath p;
3695 p.arcTo(0, 0, 1, 2, 1);
3696 check_path_is_line_and_reset(reporter, &p, 0, 0);
3697 p.arcTo(1, 2, 1, 2, 1);
3698 check_path_is_line_and_reset(reporter, &p, 1, 2);
3699 p.arcTo(1, 2, 3, 4, 0);
3700 check_path_is_line_and_reset(reporter, &p, 1, 2);
3701 p.arcTo(1, 2, 0, 0, 1);
3702 check_path_is_line_and_reset(reporter, &p, 1, 2);
3703 p.arcTo(1, 0, 1, 1, 1);
3704 SkPoint pt;
3705 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3706 p.reset();
3707 p.arcTo(1, 0, 1, -1, 1);
3708 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3709 p.reset();
3710 SkRect oval = {1, 2, 3, 4};
3711 p.arcTo(oval, 0, 0, true);
3712 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3713 p.arcTo(oval, 0, 0, false);
3714 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3715 p.arcTo(oval, 360, 0, true);
3716 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3717 p.arcTo(oval, 360, 0, false);
3718 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003719
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003720 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003721 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003722 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003723 sweep += delta;
3724 delta /= 2;
3725 }
3726 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003727 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003728 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003729 sweep -= delta;
3730 delta /= 2;
3731 }
3732 SkRect noOvalWidth = {1, 2, 0, 3};
3733 p.reset();
3734 p.arcTo(noOvalWidth, 0, 360, false);
3735 REPORTER_ASSERT(reporter, p.isEmpty());
3736
3737 SkRect noOvalHeight = {1, 2, 3, 1};
3738 p.reset();
3739 p.arcTo(noOvalHeight, 0, 360, false);
3740 REPORTER_ASSERT(reporter, p.isEmpty());
3741}
3742
3743static void test_addPath(skiatest::Reporter* reporter) {
3744 SkPath p, q;
3745 p.lineTo(1, 2);
3746 q.moveTo(4, 4);
3747 q.lineTo(7, 8);
3748 q.conicTo(8, 7, 6, 5, 0.5f);
3749 q.quadTo(6, 7, 8, 6);
3750 q.cubicTo(5, 6, 7, 8, 7, 5);
3751 q.close();
3752 p.addPath(q, -4, -4);
3753 SkRect expected = {0, 0, 4, 4};
3754 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3755 p.reset();
3756 p.reverseAddPath(q);
3757 SkRect reverseExpected = {4, 4, 8, 8};
3758 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3759}
3760
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003761static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3762 SkPath p, q;
3763 if (explicitMoveTo) {
3764 p.moveTo(1, 1);
3765 }
3766 p.lineTo(1, 2);
3767 if (explicitMoveTo) {
3768 q.moveTo(2, 1);
3769 }
3770 q.lineTo(2, 2);
3771 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3772 uint8_t verbs[4];
3773 int verbcount = p.getVerbs(verbs, 4);
3774 REPORTER_ASSERT(reporter, verbcount == 4);
3775 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3776 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3777 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3778 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3779}
3780
3781static void test_extendClosedPath(skiatest::Reporter* reporter) {
3782 SkPath p, q;
3783 p.moveTo(1, 1);
3784 p.lineTo(1, 2);
3785 p.lineTo(2, 2);
3786 p.close();
3787 q.moveTo(2, 1);
3788 q.lineTo(2, 3);
3789 p.addPath(q, SkPath::kExtend_AddPathMode);
3790 uint8_t verbs[7];
3791 int verbcount = p.getVerbs(verbs, 7);
3792 REPORTER_ASSERT(reporter, verbcount == 7);
3793 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3794 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3795 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3796 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3797 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3798 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3799 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3800
3801 SkPoint pt;
3802 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3803 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3804 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3805}
3806
3807static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3808 SkPath p, q, r;
3809 // case 1: dst is empty
3810 p.moveTo(2, 1);
3811 p.lineTo(2, 3);
3812 q.addPath(p, mode);
3813 REPORTER_ASSERT(reporter, q == p);
3814 // case 2: src is empty
3815 p.addPath(r, mode);
3816 REPORTER_ASSERT(reporter, q == p);
3817 // case 3: src and dst are empty
3818 q.reset();
3819 q.addPath(r, mode);
3820 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003821}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003822
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003823static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3824 SkPath p;
3825 p.conicTo(1, 2, 3, 4, -1);
3826 check_path_is_line_and_reset(reporter, &p, 3, 4);
3827 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3828 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3829 p.conicTo(1, 2, 3, 4, 1);
3830 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3831}
3832
3833static void test_get_point(skiatest::Reporter* reporter) {
3834 SkPath p;
3835 SkPoint pt = p.getPoint(0);
3836 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003837 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003838 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3839 p.setLastPt(10, 10);
3840 pt = p.getPoint(0);
3841 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003842 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003843 p.rMoveTo(10, 10);
3844 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3845}
3846
3847static void test_contains(skiatest::Reporter* reporter) {
3848 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003849 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3850 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
3851 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
3852 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3853 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
3854 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 -08003855 // this may return true or false, depending on the platform's numerics, but it should not crash
3856 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003857
3858 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003859 p.setFillType(SkPath::kInverseWinding_FillType);
3860 REPORTER_ASSERT(reporter, p.contains(0, 0));
3861 p.setFillType(SkPath::kWinding_FillType);
3862 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3863 p.moveTo(4, 4);
3864 p.lineTo(6, 8);
3865 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003866 // test on edge
3867 REPORTER_ASSERT(reporter, p.contains(6, 4));
3868 REPORTER_ASSERT(reporter, p.contains(5, 6));
3869 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003870 // test quick reject
3871 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3872 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3873 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3874 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3875 // test various crossings in x
3876 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3877 REPORTER_ASSERT(reporter, p.contains(6, 7));
3878 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3879 p.reset();
3880 p.moveTo(4, 4);
3881 p.lineTo(8, 6);
3882 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003883 // test on edge
3884 REPORTER_ASSERT(reporter, p.contains(4, 6));
3885 REPORTER_ASSERT(reporter, p.contains(6, 5));
3886 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003887 // test various crossings in y
3888 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3889 REPORTER_ASSERT(reporter, p.contains(7, 6));
3890 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003891 p.reset();
3892 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003893 p.lineTo(8, 4);
3894 p.lineTo(8, 8);
3895 p.lineTo(4, 8);
3896 // test on vertices
3897 REPORTER_ASSERT(reporter, p.contains(4, 4));
3898 REPORTER_ASSERT(reporter, p.contains(8, 4));
3899 REPORTER_ASSERT(reporter, p.contains(8, 8));
3900 REPORTER_ASSERT(reporter, p.contains(4, 8));
3901 p.reset();
3902 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003903 p.lineTo(6, 8);
3904 p.lineTo(2, 8);
3905 // test on edge
3906 REPORTER_ASSERT(reporter, p.contains(5, 6));
3907 REPORTER_ASSERT(reporter, p.contains(4, 8));
3908 REPORTER_ASSERT(reporter, p.contains(3, 6));
3909 p.reset();
3910 p.moveTo(4, 4);
3911 p.lineTo(0, 6);
3912 p.lineTo(4, 8);
3913 // test on edge
3914 REPORTER_ASSERT(reporter, p.contains(2, 5));
3915 REPORTER_ASSERT(reporter, p.contains(2, 7));
3916 REPORTER_ASSERT(reporter, p.contains(4, 6));
3917 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3918 p.reset();
3919 p.moveTo(4, 0);
3920 p.lineTo(6, 4);
3921 p.lineTo(2, 4);
3922 p.moveTo(4, 0);
3923 p.lineTo(0, 8);
3924 p.lineTo(8, 8);
3925 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3926 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3927 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3928 REPORTER_ASSERT(reporter, p.contains(4, 4));
3929
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003930 // test quads
3931 p.reset();
3932 p.moveTo(4, 4);
3933 p.quadTo(6, 6, 8, 8);
3934 p.quadTo(6, 8, 4, 8);
3935 p.quadTo(4, 6, 4, 4);
3936 REPORTER_ASSERT(reporter, p.contains(5, 6));
3937 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003938 // test quad edge
3939 REPORTER_ASSERT(reporter, p.contains(5, 5));
3940 REPORTER_ASSERT(reporter, p.contains(5, 8));
3941 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003942 // test quad endpoints
3943 REPORTER_ASSERT(reporter, p.contains(4, 4));
3944 REPORTER_ASSERT(reporter, p.contains(8, 8));
3945 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003946
3947 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003948 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3949 p.moveTo(qPts[0]);
3950 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3951 p.quadTo(qPts[index], qPts[index + 1]);
3952 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003953 REPORTER_ASSERT(reporter, p.contains(5, 6));
3954 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003955 // test quad edge
3956 SkPoint halfway;
3957 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3958 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3959 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3960 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003961
caryclark9aacd902015-12-14 08:38:09 -08003962 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003963 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003964 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3965 p.moveTo(kPts[0]);
3966 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3967 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3968 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003969 REPORTER_ASSERT(reporter, p.contains(5, 6));
3970 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003971 // test conic edge
3972 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3973 SkConic conic(&kPts[index], 0.5f);
3974 halfway = conic.evalAt(0.5f);
3975 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3976 }
caryclark9cb5d752015-12-18 04:35:24 -08003977 // test conic end points
3978 REPORTER_ASSERT(reporter, p.contains(4, 4));
3979 REPORTER_ASSERT(reporter, p.contains(8, 8));
3980 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003981
3982 // test cubics
3983 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3984 for (int i = 0; i < 3; ++i) {
3985 p.reset();
3986 p.setFillType(SkPath::kEvenOdd_FillType);
3987 p.moveTo(pts[i].fX, pts[i].fY);
3988 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);
3989 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);
3990 p.close();
3991 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3992 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003993 // test cubic edge
3994 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3995 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3996 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3997 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003998 // test cubic end points
3999 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4000 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4001 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004002 }
4003}
4004
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004005class PathRefTest_Private {
4006public:
4007 static void TestPathRef(skiatest::Reporter* reporter) {
4008 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004009
halcanary385fe4d2015-08-26 13:07:48 -07004010 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004011
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004012 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004013
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004014 {
4015 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4016 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4017 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4018 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4019 for (int i = 0; i < kRepeatCnt; ++i) {
4020 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4021 }
4022 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004023 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004024
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004025 {
4026 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4027 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4028 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4029 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4030 for (int i = 0; i < kRepeatCnt; ++i) {
4031 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4032 }
4033 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004034 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004035
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004036 {
4037 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4038 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4039 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4040 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4041 for (int i = 0; i < kRepeatCnt; ++i) {
4042 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4043 }
4044 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004045 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004046
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004047 {
halcanary96fcdcc2015-08-27 07:41:13 -07004048 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004049 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4050 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4051 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4052 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4053 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004054 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004055 for (int i = 0; i < kRepeatCnt; ++i) {
4056 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4057 }
4058 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004059 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004060
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004061 {
4062 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4063 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4064 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4065 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4066 for (int i = 0; i < kRepeatCnt; ++i) {
4067 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4068 }
4069 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004070 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004071 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004072};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004073
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004074static void test_operatorEqual(skiatest::Reporter* reporter) {
4075 SkPath a;
4076 SkPath b;
4077 REPORTER_ASSERT(reporter, a == a);
4078 REPORTER_ASSERT(reporter, a == b);
4079 a.setFillType(SkPath::kInverseWinding_FillType);
4080 REPORTER_ASSERT(reporter, a != b);
4081 a.reset();
4082 REPORTER_ASSERT(reporter, a == b);
4083 a.lineTo(1, 1);
4084 REPORTER_ASSERT(reporter, a != b);
4085 a.reset();
4086 REPORTER_ASSERT(reporter, a == b);
4087 a.lineTo(1, 1);
4088 b.lineTo(1, 2);
4089 REPORTER_ASSERT(reporter, a != b);
4090 a.reset();
4091 a.lineTo(1, 2);
4092 REPORTER_ASSERT(reporter, a == b);
4093}
4094
caryclark66a5d8b2014-06-24 08:30:15 -07004095static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004096 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004097 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004098 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004099 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004100 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004101 if (strlen(str) > 0) {
4102 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4103 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004104 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004105 }
caryclark66a5d8b2014-06-24 08:30:15 -07004106}
4107
4108static void test_dump(skiatest::Reporter* reporter) {
4109 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07004110 compare_dump(reporter, p, false, false, "");
4111 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07004112 p.moveTo(1, 2);
4113 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07004114 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4115 "path.lineTo(3, 4);\n");
4116 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
4117 "path.lineTo(3, 4);\n"
4118 "path.lineTo(1, 2);\n"
4119 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004120 p.reset();
4121 p.moveTo(1, 2);
4122 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07004123 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4124 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004125 p.reset();
4126 p.moveTo(1, 2);
4127 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07004128 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4129 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004130 p.reset();
4131 p.moveTo(1, 2);
4132 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07004133 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4134 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4135 p.reset();
4136 p.moveTo(1, 2);
4137 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004138 compare_dump(reporter, p, false, true,
4139 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4140 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004141 p.reset();
4142 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4143 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4144 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4145 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004146}
4147
senorblanco84cd6212015-08-04 10:01:58 -07004148namespace {
4149
4150class ChangeListener : public SkPathRef::GenIDChangeListener {
4151public:
4152 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4153 virtual ~ChangeListener() {}
4154 void onChange() override {
4155 *fChanged = true;
4156 }
4157private:
4158 bool* fChanged;
4159};
4160
4161}
4162
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004163class PathTest_Private {
4164public:
4165 static void TestPathTo(skiatest::Reporter* reporter) {
4166 SkPath p, q;
4167 p.lineTo(4, 4);
4168 p.reversePathTo(q);
4169 check_path_is_line(reporter, &p, 4, 4);
4170 q.moveTo(-4, -4);
4171 p.reversePathTo(q);
4172 check_path_is_line(reporter, &p, 4, 4);
4173 q.lineTo(7, 8);
4174 q.conicTo(8, 7, 6, 5, 0.5f);
4175 q.quadTo(6, 7, 8, 6);
4176 q.cubicTo(5, 6, 7, 8, 7, 5);
4177 q.close();
4178 p.reversePathTo(q);
4179 SkRect reverseExpected = {-4, -4, 8, 8};
4180 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4181 }
senorblanco84cd6212015-08-04 10:01:58 -07004182
4183 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4184 SkPath p;
4185
4186 bool changed = false;
4187 p.moveTo(0, 0);
4188
4189 // Check that listener is notified on moveTo().
4190
halcanary385fe4d2015-08-26 13:07:48 -07004191 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004192 REPORTER_ASSERT(reporter, !changed);
4193 p.moveTo(10, 0);
4194 REPORTER_ASSERT(reporter, changed);
4195
4196 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004197 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004198 REPORTER_ASSERT(reporter, !changed);
4199 p.lineTo(20, 0);
4200 REPORTER_ASSERT(reporter, changed);
4201
4202 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004203 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004204 REPORTER_ASSERT(reporter, !changed);
4205 p.reset();
4206 REPORTER_ASSERT(reporter, changed);
4207
4208 p.moveTo(0, 0);
4209
4210 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004211 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004212 REPORTER_ASSERT(reporter, !changed);
4213 p.rewind();
4214 REPORTER_ASSERT(reporter, changed);
4215
4216 // Check that listener is notified when pathref is deleted.
4217 {
4218 SkPath q;
4219 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004220 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004221 REPORTER_ASSERT(reporter, !changed);
4222 }
4223 // q went out of scope.
4224 REPORTER_ASSERT(reporter, changed);
4225 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004226};
4227
reed158fabb2016-07-20 10:06:59 -07004228static void test_crbug_629455(skiatest::Reporter* reporter) {
4229 SkPath path;
4230 path.moveTo(0, 0);
4231 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4232 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4233 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4234// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4235 path.lineTo(0, 0);
4236
4237 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4238 SkPaint paint;
4239 paint.setAntiAlias(true);
4240 surface->getCanvas()->drawPath(path, paint);
4241}
4242
caryclark8e7b19d2016-02-18 04:11:48 -08004243static void test_interp(skiatest::Reporter* reporter) {
4244 SkPath p1, p2, out;
4245 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4246 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4247 REPORTER_ASSERT(reporter, p1 == out);
4248 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4249 REPORTER_ASSERT(reporter, p1 == out);
4250 p1.moveTo(0, 2);
4251 p1.lineTo(0, 4);
4252 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4253 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4254 p2.moveTo(6, 0);
4255 p2.lineTo(8, 0);
4256 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4257 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4258 REPORTER_ASSERT(reporter, p2 == out);
4259 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4260 REPORTER_ASSERT(reporter, p1 == out);
4261 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4262 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4263 p1.reset();
4264 p1.moveTo(4, 4);
4265 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4266 p2.reset();
4267 p2.moveTo(4, 2);
4268 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4269 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4270 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4271 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4272 p2.reset();
4273 p2.moveTo(4, 2);
4274 p2.conicTo(6, 3, 6, 5, 1);
4275 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4276 p2.reset();
4277 p2.moveTo(4, 4);
4278 p2.conicTo(5, 4, 5, 5, 0.5f);
4279 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4280}
4281
4282DEF_TEST(PathInterp, reporter) {
4283 test_interp(reporter);
4284}
4285
caryclark9aacd902015-12-14 08:38:09 -08004286DEF_TEST(PathContains, reporter) {
4287 test_contains(reporter);
4288}
4289
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004290DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004291 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004292 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004293 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004294 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004295 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004296 test_path_crbug364224();
4297
bungeman@google.coma5809a32013-06-21 15:13:34 +00004298 SkTSize<SkScalar>::Make(3,4);
4299
4300 SkPath p, empty;
4301 SkRect bounds, bounds2;
4302 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004303
reed@android.comd252db02009-04-01 18:31:44 +00004304 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004305
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004306 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4307 SkPath& self = p;
4308 p = self;
4309
4310 // this triggers a code path in SkPath::swap which is otherwise unexercised
4311 p.swap(self);
4312
reed@android.com3abec1d2009-03-02 05:36:20 +00004313 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004314
reed@android.com6b82d1a2009-06-03 02:35:01 +00004315 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4316 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004317 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004318 REPORTER_ASSERT(reporter,
4319 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004320 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004321
reed@android.com6b82d1a2009-06-03 02:35:01 +00004322 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004323 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004324
reed@android.com6b82d1a2009-06-03 02:35:01 +00004325 p.addOval(bounds);
4326 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004327 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004328
bungeman@google.coma5809a32013-06-21 15:13:34 +00004329 p.rewind();
4330 test_empty(reporter, p);
4331
reed@android.com3abec1d2009-03-02 05:36:20 +00004332 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004333 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004334 // we have only lines
4335 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004336 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004337
bungeman@google.coma5809a32013-06-21 15:13:34 +00004338 REPORTER_ASSERT(reporter, p != empty);
4339 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004340
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004341 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004342 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4343 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004344 SkPoint pts[4];
4345 int count = p.getPoints(pts, 4);
4346 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004347 uint8_t verbs[6];
4348 verbs[5] = 0xff;
4349 p.getVerbs(verbs, 5);
4350 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4351 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4352 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4353 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4354 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4355 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004356 bounds2.set(pts, 4);
4357 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004358
reed@android.com3abec1d2009-03-02 05:36:20 +00004359 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4360 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004361 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004362
halcanary96fcdcc2015-08-27 07:41:13 -07004363 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004364 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004365 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4366 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004367
reed@android.com3abec1d2009-03-02 05:36:20 +00004368 // now force p to not be a rect
4369 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4370 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004371 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004372
robertphillipsfe7c4272014-12-29 11:36:39 -08004373 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4374 // path has a trailing moveTo. Please see crbug.com\445368)
4375 {
4376 SkRect r;
4377 p.reset();
4378 p.addRect(bounds);
4379 REPORTER_ASSERT(reporter, p.isRect(&r));
4380 REPORTER_ASSERT(reporter, r == bounds);
4381 // add a moveTo outside of our bounds
4382 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4383 REPORTER_ASSERT(reporter, p.isRect(&r));
4384 REPORTER_ASSERT(reporter, r == bounds);
4385 }
4386
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004387 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004388 test_isLine(reporter);
4389 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004390 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004391 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004392 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004393 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004394 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004395 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004396 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004397 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004398 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004399 test_flattening(reporter);
4400 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004401 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004402 test_iter(reporter);
4403 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004404 test_circle(reporter);
4405 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004406 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004407 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004408 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004409 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004410 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004411 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004412 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004413 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004414 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004415 test_tricky_cubic();
4416 test_clipped_cubic();
4417 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004418 test_crbug_493450(reporter);
4419 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004420 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004421 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004422 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004423 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004424 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004425 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004426 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004427 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004428 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004429 test_arcTo(reporter);
4430 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004431 test_addPathMode(reporter, false, false);
4432 test_addPathMode(reporter, true, false);
4433 test_addPathMode(reporter, false, true);
4434 test_addPathMode(reporter, true, true);
4435 test_extendClosedPath(reporter);
4436 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4437 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004438 test_conicTo_special_case(reporter);
4439 test_get_point(reporter);
4440 test_contains(reporter);
4441 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004442 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004443 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004444 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004445 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004446 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004447 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004448 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004449 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004450 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004451}