blob: 483d14ec48c7d5e4d8b75497312bddef90c9e879 [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
piotaixrfac4e0e2014-08-26 11:59:04 -0700160/**
161 * In debug mode, this path was causing an assertion to fail in
162 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
163 */
164static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
165 SkPoint orig, p1, p2, p3;
166 orig = SkPoint::Make(1.f, 1.f);
167 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
168 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
169 p3 = SkPoint::Make(2.f, 2.f);
170
171 path->reset();
172 path->moveTo(orig);
173 path->cubicTo(p1, p2, p3);
174 path->close();
175}
176
177static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
178 SkPath path;
179 make_path_crbugskia2820(&path, reporter);
180
181 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
182 stroke.setStrokeStyle(2 * SK_Scalar1);
183 stroke.applyToPath(&path, path);
184}
185
reed@google.comcc8be772013-10-15 15:35:29 +0000186static void make_path0(SkPath* path) {
187 // from * https://code.google.com/p/skia/issues/detail?id=1706
188
189 path->moveTo(146.939f, 1012.84f);
190 path->lineTo(181.747f, 1009.18f);
191 path->lineTo(182.165f, 1013.16f);
192 path->lineTo(147.357f, 1016.82f);
193 path->lineTo(146.939f, 1012.84f);
194 path->close();
195}
196
197static void make_path1(SkPath* path) {
198 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
199}
200
201typedef void (*PathProc)(SkPath*);
202
203/*
204 * Regression test: we used to crash (overwrite internal storage) during
205 * construction of the region when the path was INVERSE. That is now fixed,
206 * so test these regions (which used to assert/crash).
207 *
208 * https://code.google.com/p/skia/issues/detail?id=1706
209 */
210static void test_path_to_region(skiatest::Reporter* reporter) {
211 PathProc procs[] = {
212 make_path0,
213 make_path1,
214 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000215
reed@google.comcc8be772013-10-15 15:35:29 +0000216 SkRegion clip;
217 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000218
reed@google.comcc8be772013-10-15 15:35:29 +0000219 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
220 SkPath path;
221 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000222
reed@google.comcc8be772013-10-15 15:35:29 +0000223 SkRegion rgn;
224 rgn.setPath(path, clip);
225 path.toggleInverseFillType();
226 rgn.setPath(path, clip);
227 }
228}
229
halcanary4dbbd042016-06-07 17:21:10 -0700230#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000231 #define SUPPRESS_VISIBILITY_WARNING
232#else
233 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
234#endif
235
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000236static void test_path_close_issue1474(skiatest::Reporter* reporter) {
237 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
238 // are relative to the point we close to, not relative to the point we close from.
239 SkPath path;
240 SkPoint last;
241
242 // Test rLineTo().
243 path.rLineTo(0, 100);
244 path.rLineTo(100, 0);
245 path.close(); // Returns us back to 0,0.
246 path.rLineTo(50, 50); // This should go to 50,50.
247
248 path.getLastPt(&last);
249 REPORTER_ASSERT(reporter, 50 == last.fX);
250 REPORTER_ASSERT(reporter, 50 == last.fY);
251
252 // Test rQuadTo().
253 path.rewind();
254 path.rLineTo(0, 100);
255 path.rLineTo(100, 0);
256 path.close();
257 path.rQuadTo(50, 50, 75, 75);
258
259 path.getLastPt(&last);
260 REPORTER_ASSERT(reporter, 75 == last.fX);
261 REPORTER_ASSERT(reporter, 75 == last.fY);
262
263 // Test rConicTo().
264 path.rewind();
265 path.rLineTo(0, 100);
266 path.rLineTo(100, 0);
267 path.close();
268 path.rConicTo(50, 50, 85, 85, 2);
269
270 path.getLastPt(&last);
271 REPORTER_ASSERT(reporter, 85 == last.fX);
272 REPORTER_ASSERT(reporter, 85 == last.fY);
273
274 // Test rCubicTo().
275 path.rewind();
276 path.rLineTo(0, 100);
277 path.rLineTo(100, 0);
278 path.close();
279 path.rCubicTo(50, 50, 85, 85, 95, 95);
280
281 path.getLastPt(&last);
282 REPORTER_ASSERT(reporter, 95 == last.fX);
283 REPORTER_ASSERT(reporter, 95 == last.fY);
284}
285
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000286static void test_gen_id(skiatest::Reporter* reporter) {
287 SkPath a, b;
288 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
289
290 a.moveTo(0, 0);
291 const uint32_t z = a.getGenerationID();
292 REPORTER_ASSERT(reporter, z != b.getGenerationID());
293
294 a.reset();
295 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
296
297 a.moveTo(1, 1);
298 const uint32_t y = a.getGenerationID();
299 REPORTER_ASSERT(reporter, z != y);
300
301 b.moveTo(2, 2);
302 const uint32_t x = b.getGenerationID();
303 REPORTER_ASSERT(reporter, x != y && x != z);
304
305 a.swap(b);
306 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
307
308 b = a;
309 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
310
311 SkPath c(a);
312 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
313
314 c.lineTo(3, 3);
315 const uint32_t w = c.getGenerationID();
316 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
317 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
318 REPORTER_ASSERT(reporter, w != x);
319
djsollen50da1d82015-02-17 09:09:53 -0800320#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000321 static bool kExpectGenIDToIgnoreFill = false;
322#else
323 static bool kExpectGenIDToIgnoreFill = true;
324#endif
325
326 c.toggleInverseFillType();
327 const uint32_t v = c.getGenerationID();
328 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
329
330 c.rewind();
331 REPORTER_ASSERT(reporter, v != c.getGenerationID());
332}
333
reed@google.com3eff3592013-05-08 21:08:21 +0000334// This used to assert in the debug build, as the edges did not all line-up.
335static void test_bad_cubic_crbug234190() {
336 SkPath path;
337 path.moveTo(13.8509f, 3.16858f);
338 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
339 -2.38991e+08f, -4.26573e+08f,
340 -2.41016e+08f, -4.30188e+08f);
341
342 SkPaint paint;
343 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700344 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000345 surface->getCanvas()->drawPath(path, paint);
346}
347
reed@google.com7a90daf2013-04-10 18:44:00 +0000348static void test_bad_cubic_crbug229478() {
349 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000350 { 4595.91064f, -11596.9873f },
351 { 4597.2168f, -11595.9414f },
352 { 4598.52344f, -11594.8955f },
353 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000354 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000355
reed@google.com7a90daf2013-04-10 18:44:00 +0000356 SkPath path;
357 path.moveTo(pts[0]);
358 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000359
reed@google.com7a90daf2013-04-10 18:44:00 +0000360 SkPaint paint;
361 paint.setStyle(SkPaint::kStroke_Style);
362 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000363
reed@google.com7a90daf2013-04-10 18:44:00 +0000364 SkPath dst;
365 // Before the fix, this would infinite-recurse, and run out of stack
366 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700367 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000368}
369
reed@google.com64d62952013-01-18 17:49:28 +0000370static void build_path_170666(SkPath& path) {
371 path.moveTo(17.9459f, 21.6344f);
372 path.lineTo(139.545f, -47.8105f);
373 path.lineTo(139.545f, -47.8105f);
374 path.lineTo(131.07f, -47.3888f);
375 path.lineTo(131.07f, -47.3888f);
376 path.lineTo(122.586f, -46.9532f);
377 path.lineTo(122.586f, -46.9532f);
378 path.lineTo(18076.6f, 31390.9f);
379 path.lineTo(18076.6f, 31390.9f);
380 path.lineTo(18085.1f, 31390.5f);
381 path.lineTo(18085.1f, 31390.5f);
382 path.lineTo(18076.6f, 31390.9f);
383 path.lineTo(18076.6f, 31390.9f);
384 path.lineTo(17955, 31460.3f);
385 path.lineTo(17955, 31460.3f);
386 path.lineTo(17963.5f, 31459.9f);
387 path.lineTo(17963.5f, 31459.9f);
388 path.lineTo(17971.9f, 31459.5f);
389 path.lineTo(17971.9f, 31459.5f);
390 path.lineTo(17.9551f, 21.6205f);
391 path.lineTo(17.9551f, 21.6205f);
392 path.lineTo(9.47091f, 22.0561f);
393 path.lineTo(9.47091f, 22.0561f);
394 path.lineTo(17.9459f, 21.6344f);
395 path.lineTo(17.9459f, 21.6344f);
396 path.close();path.moveTo(0.995934f, 22.4779f);
397 path.lineTo(0.986725f, 22.4918f);
398 path.lineTo(0.986725f, 22.4918f);
399 path.lineTo(17955, 31460.4f);
400 path.lineTo(17955, 31460.4f);
401 path.lineTo(17971.9f, 31459.5f);
402 path.lineTo(17971.9f, 31459.5f);
403 path.lineTo(18093.6f, 31390.1f);
404 path.lineTo(18093.6f, 31390.1f);
405 path.lineTo(18093.6f, 31390);
406 path.lineTo(18093.6f, 31390);
407 path.lineTo(139.555f, -47.8244f);
408 path.lineTo(139.555f, -47.8244f);
409 path.lineTo(122.595f, -46.9671f);
410 path.lineTo(122.595f, -46.9671f);
411 path.lineTo(0.995934f, 22.4779f);
412 path.lineTo(0.995934f, 22.4779f);
413 path.close();
414 path.moveTo(5.43941f, 25.5223f);
415 path.lineTo(798267, -28871.1f);
416 path.lineTo(798267, -28871.1f);
417 path.lineTo(3.12512e+06f, -113102);
418 path.lineTo(3.12512e+06f, -113102);
419 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
420 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
421 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
422 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
423 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
424 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
425 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
426 path.lineTo(2.78271e+08f, -1.00733e+07f);
427 path.lineTo(2.78271e+08f, -1.00733e+07f);
428 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
429 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
430 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
431 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
432 path.lineTo(2.77473e+08f, -1.00444e+07f);
433 path.lineTo(2.77473e+08f, -1.00444e+07f);
434 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
435 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
436 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
437 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
438 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
439 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
440 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
441 path.lineTo(798284, -28872);
442 path.lineTo(798284, -28872);
443 path.lineTo(22.4044f, 24.6677f);
444 path.lineTo(22.4044f, 24.6677f);
445 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
446 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
447 path.close();
448}
449
450static void build_path_simple_170666(SkPath& path) {
451 path.moveTo(126.677f, 24.1591f);
452 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
453}
454
455// This used to assert in the SK_DEBUG build, as the clip step would fail with
456// too-few interations in our cubic-line intersection code. That code now runs
457// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000458static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000459 SkPath path;
460 SkPaint paint;
461 paint.setAntiAlias(true);
462
reede8f30622016-03-23 18:59:25 -0700463 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000464
reed@google.com64d62952013-01-18 17:49:28 +0000465 build_path_simple_170666(path);
466 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000467
reed@google.com64d62952013-01-18 17:49:28 +0000468 build_path_170666(path);
469 surface->getCanvas()->drawPath(path, paint);
470}
471
caryclarke8c56662015-07-14 11:19:26 -0700472
473static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
474 SkScalar tx, SkScalar ty, SkScalar scale) {
475 SkPath smallPath;
476 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
477 bool smallConvex = smallPath.isConvex();
478 SkPath largePath;
479 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
480 SkMatrix matrix;
481 matrix.reset();
482 matrix.preTranslate(100, 100);
483 matrix.preScale(scale, scale);
484 largePath.transform(matrix);
485 bool largeConvex = largePath.isConvex();
486 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
487}
488
489static void test_crbug_493450(skiatest::Reporter* reporter) {
490 const char reducedCase[] =
491 "M0,0"
492 "L0.0002, 0"
493 "L0.0002, 0.0002"
494 "L0.0001, 0.0001"
495 "L0,0.0002"
496 "Z";
497 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
498 const char originalFiddleData[] =
499 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
500 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
501 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
502 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
503 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
504 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
505 826357.3384828606f);
506}
507
508static void test_crbug_495894(skiatest::Reporter* reporter) {
509 const char originalFiddleData[] =
510 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
511 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
512 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
513 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
514 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
515 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
516 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
517 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
518 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
519 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
520 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
521 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
522 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
523 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
524 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
525 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
526 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
527 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
528 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
529 "L-0.33993994441916414,-0.11288906492739594Z";
530 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
531 65536);
532}
533
mbarbella99600d02016-06-01 15:39:47 -0700534static void test_crbug_613918() {
535 SkPath path;
536 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
537 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
538 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
539
540 // This call could lead to an assert or uninitialized read due to a failure
541 // to check the return value from SkCubicClipper::ChopMonoAtY.
542 path.contains(-1.84817e-08f, 1.15465e-08f);
543}
544
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000545static void test_addrect(skiatest::Reporter* reporter) {
546 SkPath path;
547 path.lineTo(0, 0);
548 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700549 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000550
551 path.reset();
552 path.lineTo(FLT_EPSILON, FLT_EPSILON);
553 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700554 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000555
556 path.reset();
557 path.quadTo(0, 0, 0, 0);
558 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700559 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000560
561 path.reset();
562 path.conicTo(0, 0, 0, 0, 0.5f);
563 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700564 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000565
566 path.reset();
567 path.cubicTo(0, 0, 0, 0, 0, 0);
568 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700569 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000570}
571
reed@google.coma8790de2012-10-24 21:04:04 +0000572// Make sure we stay non-finite once we get there (unless we reset or rewind).
573static void test_addrect_isfinite(skiatest::Reporter* reporter) {
574 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000575
reed@google.coma8790de2012-10-24 21:04:04 +0000576 path.addRect(SkRect::MakeWH(50, 100));
577 REPORTER_ASSERT(reporter, path.isFinite());
578
579 path.moveTo(0, 0);
580 path.lineTo(SK_ScalarInfinity, 42);
581 REPORTER_ASSERT(reporter, !path.isFinite());
582
583 path.addRect(SkRect::MakeWH(50, 100));
584 REPORTER_ASSERT(reporter, !path.isFinite());
585
586 path.reset();
587 REPORTER_ASSERT(reporter, path.isFinite());
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
reed@google.com848148e2013-01-15 15:51:59 +0000593static void build_big_path(SkPath* path, bool reducedCase) {
594 if (reducedCase) {
595 path->moveTo(577330, 1971.72f);
596 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
597 } else {
598 path->moveTo(60.1631f, 7.70567f);
599 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
600 path->lineTo(577379, 1977.77f);
601 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
602 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
603 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
604 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
605 path->quadTo(577198, 1972, 577238, 1971.31f);
606 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
607 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
608 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
609 path->moveTo(306.718f, -32.912f);
610 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
611 }
612}
613
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000614static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700615 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000616
617 // This path used to assert, because our cubic-chopping code incorrectly
618 // moved control points after the chop. This test should be run in SK_DEBUG
619 // mode to ensure that we no long assert.
620 SkPath path;
621 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
622 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000623
reed@google.com848148e2013-01-15 15:51:59 +0000624 SkPaint paint;
625 for (int doAA = 0; doAA <= 1; ++doAA) {
626 paint.setAntiAlias(SkToBool(doAA));
627 surface->getCanvas()->drawPath(path, paint);
628 }
629 }
630}
631
reed3b2fb982015-07-29 08:37:13 -0700632static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
633 if (expected != bounds) {
634 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
635 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
636 expected.left(), expected.top(), expected.right(), expected.bottom());
637 }
638}
639
reed91f283b2015-07-28 06:00:50 -0700640static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
641 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700642#if 0
643 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
644 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700645 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
646
647 path.moveTo(-5, -8);
648 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
649
650 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
651 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
652
653 path.moveTo(1, 2);
654 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700655#else
656 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
657
658 path.moveTo(-5, -8); // should set the bounds
659 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
660
661 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
662 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
663
664 path.moveTo(1, 2); // don't expect this to have changed the bounds
665 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
666#endif
reed91f283b2015-07-28 06:00:50 -0700667}
668
reedb1b12f82016-07-13 10:56:53 -0700669#include "SkSurface.h"
670static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
671 SkPath path;
672 path.moveTo(0, 0);
673 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
674
675 SkPaint paint;
676 paint.setAntiAlias(true);
677
678 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
679 surf->getCanvas()->drawPath(path, paint);
680}
681
reed@google.com8cae8352012-09-14 15:18:41 +0000682// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
683// which triggered an assert, from a tricky cubic. This test replicates that
684// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
685// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000686static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000687 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000688 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
689 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
690 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
691 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000692 };
693
694 SkPath path;
695 path.moveTo(pts[0]);
696 path.cubicTo(pts[1], pts[2], pts[3]);
697
698 SkPaint paint;
699 paint.setAntiAlias(true);
700
reede8f30622016-03-23 18:59:25 -0700701 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000702}
reed@android.com3abec1d2009-03-02 05:36:20 +0000703
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000704// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
705//
706static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
707 SkPath path;
708 path.quadTo(157, 366, 286, 208);
709 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000710
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000711 SkMatrix matrix;
712 matrix.setScale(1000*1000, 1000*1000);
713
714 // Be sure that path::transform correctly updates isFinite and the bounds
715 // if the transformation overflows. The previous bug was that isFinite was
716 // set to true in this case, but the bounds were not set to empty (which
717 // they should be).
718 while (path.isFinite()) {
719 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
720 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
721 path.transform(matrix);
722 }
723 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
724
725 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
726 path.transform(matrix);
727 // we need to still be non-finite
728 REPORTER_ASSERT(reporter, !path.isFinite());
729 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
730}
731
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000732static void add_corner_arc(SkPath* path, const SkRect& rect,
733 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000734 int startAngle)
735{
736
737 SkScalar rx = SkMinScalar(rect.width(), xIn);
738 SkScalar ry = SkMinScalar(rect.height(), yIn);
739
740 SkRect arcRect;
741 arcRect.set(-rx, -ry, rx, ry);
742 switch (startAngle) {
743 case 0:
744 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
745 break;
746 case 90:
747 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
748 break;
749 case 180:
750 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
751 break;
752 case 270:
753 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
754 break;
755 default:
756 break;
757 }
758
759 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
760}
761
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000762static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000763 SkScalar xCorner, SkScalar yCorner) {
764 // we are lazy here and use the same x & y for each corner
765 add_corner_arc(path, r, xCorner, yCorner, 270);
766 add_corner_arc(path, r, xCorner, yCorner, 0);
767 add_corner_arc(path, r, xCorner, yCorner, 90);
768 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000769 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000770}
771
772// Chrome creates its own round rects with each corner possibly being different.
773// Performance will suffer if they are not convex.
774// Note: PathBench::ArbRoundRectBench performs almost exactly
775// the same test (but with drawing)
776static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000777 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000778 SkRect r;
779
780 for (int i = 0; i < 5000; ++i) {
781
robertphillips@google.com158618e2012-10-23 16:56:56 +0000782 SkScalar size = rand.nextUScalar1() * 30;
783 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000784 continue;
785 }
786 r.fLeft = rand.nextUScalar1() * 300;
787 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000788 r.fRight = r.fLeft + 2 * size;
789 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000790
791 SkPath temp;
792
793 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
794
795 REPORTER_ASSERT(reporter, temp.isConvex());
796 }
797}
798
robertphillips@google.com158618e2012-10-23 16:56:56 +0000799// Chrome will sometimes create a 0 radius round rect. The degenerate
800// quads prevent the path from being converted to a rect
801// Note: PathBench::ArbRoundRectBench performs almost exactly
802// the same test (but with drawing)
803static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000804 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000805 SkRect r;
806
807 for (int i = 0; i < 5000; ++i) {
808
809 SkScalar size = rand.nextUScalar1() * 30;
810 if (size < SK_Scalar1) {
811 continue;
812 }
813 r.fLeft = rand.nextUScalar1() * 300;
814 r.fTop = rand.nextUScalar1() * 300;
815 r.fRight = r.fLeft + 2 * size;
816 r.fBottom = r.fTop + 2 * size;
817
818 SkPath temp;
819
820 make_arb_round_rect(&temp, r, 0, 0);
821
robertphillips@google.com158618e2012-10-23 16:56:56 +0000822 SkRect result;
823 REPORTER_ASSERT(reporter, temp.isRect(&result));
824 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000825 }
826}
827
reed@google.com0bb18bb2012-07-26 15:20:36 +0000828static void test_rect_isfinite(skiatest::Reporter* reporter) {
829 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000830 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000831 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000832
reed@google.com0bb18bb2012-07-26 15:20:36 +0000833 SkRect r;
834 r.setEmpty();
835 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000836 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000837 REPORTER_ASSERT(reporter, !r.isFinite());
838 r.set(0, 0, nan, 0);
839 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000840
reed@google.com0bb18bb2012-07-26 15:20:36 +0000841 SkPoint pts[] = {
842 { 0, 0 },
843 { SK_Scalar1, 0 },
844 { 0, SK_Scalar1 },
845 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000846
reed@google.com0bb18bb2012-07-26 15:20:36 +0000847 bool isFine = r.setBoundsCheck(pts, 3);
848 REPORTER_ASSERT(reporter, isFine);
849 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000850
reed@google.com0bb18bb2012-07-26 15:20:36 +0000851 pts[1].set(inf, 0);
852 isFine = r.setBoundsCheck(pts, 3);
853 REPORTER_ASSERT(reporter, !isFine);
854 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000855
reed@google.com0bb18bb2012-07-26 15:20:36 +0000856 pts[1].set(nan, 0);
857 isFine = r.setBoundsCheck(pts, 3);
858 REPORTER_ASSERT(reporter, !isFine);
859 REPORTER_ASSERT(reporter, r.isEmpty());
860}
861
862static void test_path_isfinite(skiatest::Reporter* reporter) {
863 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000864 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000865 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000866
reed@google.com0bb18bb2012-07-26 15:20:36 +0000867 SkPath path;
868 REPORTER_ASSERT(reporter, path.isFinite());
869
870 path.reset();
871 REPORTER_ASSERT(reporter, path.isFinite());
872
873 path.reset();
874 path.moveTo(SK_Scalar1, 0);
875 REPORTER_ASSERT(reporter, path.isFinite());
876
877 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000878 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000879 REPORTER_ASSERT(reporter, !path.isFinite());
880
881 path.reset();
882 path.moveTo(nan, 0);
883 REPORTER_ASSERT(reporter, !path.isFinite());
884}
885
886static void test_isfinite(skiatest::Reporter* reporter) {
887 test_rect_isfinite(reporter);
888 test_path_isfinite(reporter);
889}
890
fsb1475b02016-01-20 09:51:07 -0800891static void test_islastcontourclosed(skiatest::Reporter* reporter) {
892 SkPath path;
893 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
894 path.moveTo(0, 0);
895 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
896 path.close();
897 REPORTER_ASSERT(reporter, path.isLastContourClosed());
898 path.lineTo(100, 100);
899 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
900 path.moveTo(200, 200);
901 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
902 path.close();
903 REPORTER_ASSERT(reporter, path.isLastContourClosed());
904 path.moveTo(0, 0);
905 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
906}
907
reed@google.com744faba2012-05-29 19:54:52 +0000908// assert that we always
909// start with a moveTo
910// only have 1 moveTo
911// only have Lines after that
912// end with a single close
913// only have (at most) 1 close
914//
915static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000916 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000917 SkPath::RawIter iter(path);
918 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000919
920 bool firstTime = true;
921 bool foundClose = false;
922 for (;;) {
923 switch (iter.next(pts)) {
924 case SkPath::kMove_Verb:
925 REPORTER_ASSERT(reporter, firstTime);
926 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
927 srcPts++;
928 firstTime = false;
929 break;
930 case SkPath::kLine_Verb:
931 REPORTER_ASSERT(reporter, !firstTime);
932 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
933 srcPts++;
934 break;
935 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000936 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000937 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000938 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000939 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000940 break;
reed@google.com744faba2012-05-29 19:54:52 +0000941 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000942 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000943 break;
944 case SkPath::kClose_Verb:
945 REPORTER_ASSERT(reporter, !firstTime);
946 REPORTER_ASSERT(reporter, !foundClose);
947 REPORTER_ASSERT(reporter, expectClose);
948 foundClose = true;
949 break;
950 case SkPath::kDone_Verb:
951 goto DONE;
952 }
953 }
954DONE:
955 REPORTER_ASSERT(reporter, foundClose == expectClose);
956}
957
958static void test_addPoly(skiatest::Reporter* reporter) {
959 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000960 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000961
reed@google.com744faba2012-05-29 19:54:52 +0000962 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
963 pts[i].fX = rand.nextSScalar1();
964 pts[i].fY = rand.nextSScalar1();
965 }
966
967 for (int doClose = 0; doClose <= 1; ++doClose) {
968 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
969 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800970 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000971 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000972 }
973 }
974}
975
reed@google.com8b06f1a2012-05-29 12:03:46 +0000976static void test_strokerec(skiatest::Reporter* reporter) {
977 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
978 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000979
reed@google.com8b06f1a2012-05-29 12:03:46 +0000980 rec.setHairlineStyle();
981 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000982
reed@google.com8b06f1a2012-05-29 12:03:46 +0000983 rec.setStrokeStyle(SK_Scalar1, false);
984 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000985
reed@google.com8b06f1a2012-05-29 12:03:46 +0000986 rec.setStrokeStyle(SK_Scalar1, true);
987 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000988
reed@google.com8b06f1a2012-05-29 12:03:46 +0000989 rec.setStrokeStyle(0, false);
990 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000991
reed@google.com8b06f1a2012-05-29 12:03:46 +0000992 rec.setStrokeStyle(0, true);
993 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
994}
995
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000996// Set this for paths that don't have a consistent direction such as a bowtie.
997// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700998const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000999
1000static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001001 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001002 if (expected == kDontCheckDir) {
1003 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001004 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001005 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1006
reed026beb52015-06-10 14:23:15 -07001007 SkPathPriv::FirstDirection dir;
1008 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001009 REPORTER_ASSERT(reporter, dir == expected);
1010 } else {
reed026beb52015-06-10 14:23:15 -07001011 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001012 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001013}
1014
reed@google.com3e71a882012-01-10 18:44:37 +00001015static void test_direction(skiatest::Reporter* reporter) {
1016 size_t i;
1017 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001018 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001019 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1020 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1021 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001022
1023 static const char* gDegen[] = {
1024 "M 10 10",
1025 "M 10 10 M 20 20",
1026 "M 10 10 L 20 20",
1027 "M 10 10 L 10 10 L 10 10",
1028 "M 10 10 Q 10 10 10 10",
1029 "M 10 10 C 10 10 10 10 10 10",
1030 };
1031 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1032 path.reset();
1033 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1034 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001035 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001036 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001037
reed@google.com3e71a882012-01-10 18:44:37 +00001038 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001039 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001040 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001041 "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 +00001042 // rect with top two corners replaced by cubics with identical middle
1043 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001044 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1045 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001046 };
1047 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1048 path.reset();
1049 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1050 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001051 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001052 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001053
reed@google.com3e71a882012-01-10 18:44:37 +00001054 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001055 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001056 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001057 "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 +00001058 // rect with top two corners replaced by cubics with identical middle
1059 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001060 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1061 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001062 };
1063 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1064 path.reset();
1065 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1066 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001067 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001068 }
reed@google.comac8543f2012-01-30 20:51:25 +00001069
1070 // Test two donuts, each wound a different direction. Only the outer contour
1071 // determines the cheap direction
1072 path.reset();
1073 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1074 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001075 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001076
reed@google.comac8543f2012-01-30 20:51:25 +00001077 path.reset();
1078 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1079 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001080 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001081
1082 // triangle with one point really far from the origin.
1083 path.reset();
1084 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001085 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001086 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1087 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001088 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001089
1090 path.reset();
1091 path.conicTo(20, 0, 20, 20, 0.5f);
1092 path.close();
reed026beb52015-06-10 14:23:15 -07001093 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001094
1095 path.reset();
1096 path.lineTo(1, 1e7f);
1097 path.lineTo(1e7f, 2e7f);
1098 path.close();
1099 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001100 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001101}
1102
reed@google.comffdb0182011-11-14 19:29:14 +00001103static void add_rect(SkPath* path, const SkRect& r) {
1104 path->moveTo(r.fLeft, r.fTop);
1105 path->lineTo(r.fRight, r.fTop);
1106 path->lineTo(r.fRight, r.fBottom);
1107 path->lineTo(r.fLeft, r.fBottom);
1108 path->close();
1109}
1110
1111static void test_bounds(skiatest::Reporter* reporter) {
1112 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001113 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1114 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1115 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1116 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001117 };
1118
1119 SkPath path0, path1;
1120 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1121 path0.addRect(rects[i]);
1122 add_rect(&path1, rects[i]);
1123 }
1124
1125 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1126}
1127
reed@google.com55b5f4b2011-09-07 12:23:41 +00001128static void stroke_cubic(const SkPoint pts[4]) {
1129 SkPath path;
1130 path.moveTo(pts[0]);
1131 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001132
reed@google.com55b5f4b2011-09-07 12:23:41 +00001133 SkPaint paint;
1134 paint.setStyle(SkPaint::kStroke_Style);
1135 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001136
reed@google.com55b5f4b2011-09-07 12:23:41 +00001137 SkPath fill;
1138 paint.getFillPath(path, &fill);
1139}
1140
1141// just ensure this can run w/o any SkASSERTS firing in the debug build
1142// we used to assert due to differences in how we determine a degenerate vector
1143// but that was fixed with the introduction of SkPoint::CanNormalize
1144static void stroke_tiny_cubic() {
1145 SkPoint p0[] = {
1146 { 372.0f, 92.0f },
1147 { 372.0f, 92.0f },
1148 { 372.0f, 92.0f },
1149 { 372.0f, 92.0f },
1150 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001151
reed@google.com55b5f4b2011-09-07 12:23:41 +00001152 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001153
reed@google.com55b5f4b2011-09-07 12:23:41 +00001154 SkPoint p1[] = {
1155 { 372.0f, 92.0f },
1156 { 372.0007f, 92.000755f },
1157 { 371.99927f, 92.003922f },
1158 { 371.99826f, 92.003899f },
1159 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001160
reed@google.com55b5f4b2011-09-07 12:23:41 +00001161 stroke_cubic(p1);
1162}
1163
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001164static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1165 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001166 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001167 SkPoint mv;
1168 SkPoint pts[4];
1169 SkPath::Verb v;
1170 int nMT = 0;
1171 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001172 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001173 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1174 switch (v) {
1175 case SkPath::kMove_Verb:
1176 mv = pts[0];
1177 ++nMT;
1178 break;
1179 case SkPath::kClose_Verb:
1180 REPORTER_ASSERT(reporter, mv == pts[0]);
1181 ++nCL;
1182 break;
1183 default:
1184 break;
1185 }
1186 }
1187 // if we force a close on the interator we should have a close
1188 // for every moveTo
1189 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1190 }
1191}
1192
1193static void test_close(skiatest::Reporter* reporter) {
1194 SkPath closePt;
1195 closePt.moveTo(0, 0);
1196 closePt.close();
1197 check_close(reporter, closePt);
1198
1199 SkPath openPt;
1200 openPt.moveTo(0, 0);
1201 check_close(reporter, openPt);
1202
1203 SkPath empty;
1204 check_close(reporter, empty);
1205 empty.close();
1206 check_close(reporter, empty);
1207
1208 SkPath rect;
1209 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1210 check_close(reporter, rect);
1211 rect.close();
1212 check_close(reporter, rect);
1213
1214 SkPath quad;
1215 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1216 check_close(reporter, quad);
1217 quad.close();
1218 check_close(reporter, quad);
1219
1220 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001221 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001222 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1223 check_close(reporter, cubic);
1224 cubic.close();
1225 check_close(reporter, cubic);
1226
1227 SkPath line;
1228 line.moveTo(SK_Scalar1, SK_Scalar1);
1229 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1230 check_close(reporter, line);
1231 line.close();
1232 check_close(reporter, line);
1233
1234 SkPath rect2;
1235 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1236 rect2.close();
1237 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1238 check_close(reporter, rect2);
1239 rect2.close();
1240 check_close(reporter, rect2);
1241
1242 SkPath oval3;
1243 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1244 oval3.close();
1245 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1246 check_close(reporter, oval3);
1247 oval3.close();
1248 check_close(reporter, oval3);
1249
1250 SkPath moves;
1251 moves.moveTo(SK_Scalar1, SK_Scalar1);
1252 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1253 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1254 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1255 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001256
1257 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001258}
1259
reed@google.com7c424812011-05-15 04:38:34 +00001260static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1261 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001262 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1263 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001264 REPORTER_ASSERT(reporter, c == expected);
1265}
1266
caryclarkb4216502015-03-02 13:02:34 -08001267static void test_path_crbug389050(skiatest::Reporter* reporter) {
1268 SkPath tinyConvexPolygon;
1269 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1270 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1271 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1272 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1273 tinyConvexPolygon.close();
1274 tinyConvexPolygon.getConvexity();
1275 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001276 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001277
1278 SkPath platTriangle;
1279 platTriangle.moveTo(0, 0);
1280 platTriangle.lineTo(200, 0);
1281 platTriangle.lineTo(100, 0.04f);
1282 platTriangle.close();
1283 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001284 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001285
1286 platTriangle.reset();
1287 platTriangle.moveTo(0, 0);
1288 platTriangle.lineTo(200, 0);
1289 platTriangle.lineTo(100, 0.03f);
1290 platTriangle.close();
1291 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001292 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001293}
1294
reed@google.com7c424812011-05-15 04:38:34 +00001295static void test_convexity2(skiatest::Reporter* reporter) {
1296 SkPath pt;
1297 pt.moveTo(0, 0);
1298 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001299 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001300 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001301
reed@google.com7c424812011-05-15 04:38:34 +00001302 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001303 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1304 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001305 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001306 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001307 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001308
reed@google.com7c424812011-05-15 04:38:34 +00001309 SkPath triLeft;
1310 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001311 triLeft.lineTo(SK_Scalar1, 0);
1312 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001313 triLeft.close();
1314 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001315 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001316
reed@google.com7c424812011-05-15 04:38:34 +00001317 SkPath triRight;
1318 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001319 triRight.lineTo(-SK_Scalar1, 0);
1320 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001321 triRight.close();
1322 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001323 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001324
reed@google.com7c424812011-05-15 04:38:34 +00001325 SkPath square;
1326 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001327 square.lineTo(SK_Scalar1, 0);
1328 square.lineTo(SK_Scalar1, SK_Scalar1);
1329 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001330 square.close();
1331 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001332 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001333
reed@google.com7c424812011-05-15 04:38:34 +00001334 SkPath redundantSquare;
1335 redundantSquare.moveTo(0, 0);
1336 redundantSquare.lineTo(0, 0);
1337 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001338 redundantSquare.lineTo(SK_Scalar1, 0);
1339 redundantSquare.lineTo(SK_Scalar1, 0);
1340 redundantSquare.lineTo(SK_Scalar1, 0);
1341 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1342 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1343 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1344 redundantSquare.lineTo(0, SK_Scalar1);
1345 redundantSquare.lineTo(0, SK_Scalar1);
1346 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001347 redundantSquare.close();
1348 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001349 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001350
reed@google.com7c424812011-05-15 04:38:34 +00001351 SkPath bowTie;
1352 bowTie.moveTo(0, 0);
1353 bowTie.lineTo(0, 0);
1354 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001355 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1356 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1357 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1358 bowTie.lineTo(SK_Scalar1, 0);
1359 bowTie.lineTo(SK_Scalar1, 0);
1360 bowTie.lineTo(SK_Scalar1, 0);
1361 bowTie.lineTo(0, SK_Scalar1);
1362 bowTie.lineTo(0, SK_Scalar1);
1363 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001364 bowTie.close();
1365 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001366 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001367
reed@google.com7c424812011-05-15 04:38:34 +00001368 SkPath spiral;
1369 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001370 spiral.lineTo(100*SK_Scalar1, 0);
1371 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1372 spiral.lineTo(0, 100*SK_Scalar1);
1373 spiral.lineTo(0, 50*SK_Scalar1);
1374 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1375 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001376 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001377 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001378 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001379
reed@google.com7c424812011-05-15 04:38:34 +00001380 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001381 dent.moveTo(0, 0);
1382 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1383 dent.lineTo(0, 100*SK_Scalar1);
1384 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1385 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001386 dent.close();
1387 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001388 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001389
halcanary6950de62015-11-07 05:29:00 -08001390 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001391 SkPath strokedSin;
1392 for (int i = 0; i < 2000; i++) {
1393 SkScalar x = SkIntToScalar(i) / 2;
1394 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1395 if (0 == i) {
1396 strokedSin.moveTo(x, y);
1397 } else {
1398 strokedSin.lineTo(x, y);
1399 }
1400 }
1401 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1402 stroke.setStrokeStyle(2 * SK_Scalar1);
1403 stroke.applyToPath(&strokedSin, strokedSin);
1404 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1405 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001406
1407 // http://crbug.com/412640
1408 SkPath degenerateConcave;
1409 degenerateConcave.moveTo(148.67912f, 191.875f);
1410 degenerateConcave.lineTo(470.37695f, 7.5f);
1411 degenerateConcave.lineTo(148.67912f, 191.875f);
1412 degenerateConcave.lineTo(41.446522f, 376.25f);
1413 degenerateConcave.lineTo(-55.971577f, 460.0f);
1414 degenerateConcave.lineTo(41.446522f, 376.25f);
1415 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001416 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001417
1418 // http://crbug.com/433683
1419 SkPath badFirstVector;
1420 badFirstVector.moveTo(501.087708f, 319.610352f);
1421 badFirstVector.lineTo(501.087708f, 319.610352f);
1422 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1423 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1424 badFirstVector.lineTo(301.557678f, 98.044601f);
1425 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1426 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1427 badFirstVector.lineTo(504.912292f, 316.389648f);
1428 badFirstVector.lineTo(501.087708f, 319.610352f);
1429 badFirstVector.close();
1430 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001431}
1432
reed@android.com6b82d1a2009-06-03 02:35:01 +00001433static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1434 const SkRect& bounds) {
1435 REPORTER_ASSERT(reporter, p.isConvex());
1436 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001437
reed@android.com6b82d1a2009-06-03 02:35:01 +00001438 SkPath p2(p);
1439 REPORTER_ASSERT(reporter, p2.isConvex());
1440 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1441
1442 SkPath other;
1443 other.swap(p2);
1444 REPORTER_ASSERT(reporter, other.isConvex());
1445 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1446}
1447
reed@google.com04863fa2011-05-15 04:08:24 +00001448static void setFromString(SkPath* path, const char str[]) {
1449 bool first = true;
1450 while (str) {
1451 SkScalar x, y;
1452 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001453 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001454 break;
1455 }
1456 str = SkParse::FindScalar(str, &y);
1457 SkASSERT(str);
1458 if (first) {
1459 path->moveTo(x, y);
1460 first = false;
1461 } else {
1462 path->lineTo(x, y);
1463 }
1464 }
1465}
1466
1467static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001468 SkPath path;
1469
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001470 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001471 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001472 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001473 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001474 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1475
reed@google.com04863fa2011-05-15 04:08:24 +00001476 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001477 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001478 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001479 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001480
reed@google.com04863fa2011-05-15 04:08:24 +00001481 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001482 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001483 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001484 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001485
reed@google.com04863fa2011-05-15 04:08:24 +00001486 static const struct {
reed026beb52015-06-10 14:23:15 -07001487 const char* fPathStr;
1488 SkPath::Convexity fExpectedConvexity;
1489 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001490 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001491 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1492 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1493 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1494 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1495 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1496 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001497 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001498 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001499 };
1500
1501 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1502 SkPath path;
1503 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001504 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1505 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001506 // check after setting the initial convex and direction
1507 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1508 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001509 SkPathPriv::FirstDirection dir;
1510 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1511 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001512 ^ foundDir);
1513 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1514 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1515 }
1516 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1517 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001518 }
caryclarkd3d1a982014-12-08 04:57:38 -08001519
1520 static const SkPoint nonFinitePts[] = {
1521 { SK_ScalarInfinity, 0 },
1522 { 0, SK_ScalarInfinity },
1523 { SK_ScalarInfinity, SK_ScalarInfinity },
1524 { SK_ScalarNegativeInfinity, 0},
1525 { 0, SK_ScalarNegativeInfinity },
1526 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1527 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1528 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1529 { SK_ScalarNaN, 0 },
1530 { 0, SK_ScalarNaN },
1531 { SK_ScalarNaN, SK_ScalarNaN },
1532 };
1533
1534 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1535
1536 static const SkPoint finitePts[] = {
1537 { SK_ScalarMax, 0 },
1538 { 0, SK_ScalarMax },
1539 { SK_ScalarMax, SK_ScalarMax },
1540 { SK_ScalarMin, 0 },
1541 { 0, SK_ScalarMin },
1542 { SK_ScalarMin, SK_ScalarMin },
1543 };
1544
1545 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1546
1547 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1548 int i = (int) (index % nonFinitePtsCount);
1549 int f = (int) (index % finitePtsCount);
1550 int g = (int) ((f + 1) % finitePtsCount);
1551 path.reset();
1552 switch (index % 13) {
1553 case 0: path.lineTo(nonFinitePts[i]); break;
1554 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1555 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1556 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1557 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1558 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1559 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1560 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1561 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1562 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1563 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1564 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1565 case 12: path.moveTo(nonFinitePts[i]); break;
1566 }
1567 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1568 }
1569
1570 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1571 int f = (int) (index % finitePtsCount);
1572 int g = (int) ((f + 1) % finitePtsCount);
1573 path.reset();
1574 int curveSelect = index % 11;
1575 switch (curveSelect) {
1576 case 0: path.moveTo(finitePts[f]); break;
1577 case 1: path.lineTo(finitePts[f]); break;
1578 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1579 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1580 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1581 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1582 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1583 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1584 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1585 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1586 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1587 }
1588 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1589 : SkPath::kUnknown_Convexity);
1590 }
1591
reed@google.com04863fa2011-05-15 04:08:24 +00001592}
1593
reed@google.com7e6c4d12012-05-10 14:05:43 +00001594static void test_isLine(skiatest::Reporter* reporter) {
1595 SkPath path;
1596 SkPoint pts[2];
1597 const SkScalar value = SkIntToScalar(5);
1598
halcanary96fcdcc2015-08-27 07:41:13 -07001599 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001600
reed@google.com7e6c4d12012-05-10 14:05:43 +00001601 // set some non-zero values
1602 pts[0].set(value, value);
1603 pts[1].set(value, value);
1604 REPORTER_ASSERT(reporter, !path.isLine(pts));
1605 // check that pts was untouched
1606 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1607 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1608
1609 const SkScalar moveX = SkIntToScalar(1);
1610 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001611 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001612
1613 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001614 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001615 REPORTER_ASSERT(reporter, !path.isLine(pts));
1616 // check that pts was untouched
1617 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1618 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1619
1620 const SkScalar lineX = SkIntToScalar(2);
1621 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001622 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001623
1624 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001625 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001626
1627 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1628 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1629 REPORTER_ASSERT(reporter, path.isLine(pts));
1630 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1631 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1632
1633 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001634 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001635 REPORTER_ASSERT(reporter, !path.isLine(pts));
1636 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1637 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001638
1639 path.reset();
1640 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001641 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001642}
1643
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001644static void test_conservativelyContains(skiatest::Reporter* reporter) {
1645 SkPath path;
1646
1647 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1648 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1649
1650 // A circle that bounds kBaseRect (with a significant amount of slop)
1651 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001652 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001653 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1654
1655 // round-rect radii
1656 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001657
caryclark@google.com56f233a2012-11-19 13:06:06 +00001658 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001659 SkRect fQueryRect;
1660 bool fInRect;
1661 bool fInCircle;
1662 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001663 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001664 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001665 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001666
1667 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001668 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1669 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1670 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1671 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001672 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001673
1674 // rects with edges off by one from kBaseRect's edges
1675 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1676 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001677 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001678 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1679 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001680 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001681 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1682 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001683 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001684 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1685 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001686 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001687 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1688 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001689 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001690 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1691 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001692 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001693 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1694 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001695 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001696
1697 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001698 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1699 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1700 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1701 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001702
1703 // far away rect
1704 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1705 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001706 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001707
1708 // very large rect containing kBaseRect
1709 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1710 kBaseRect.fTop - 5 * kBaseRect.height(),
1711 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001712 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001713
1714 // skinny rect that spans same y-range as kBaseRect
1715 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1716 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001717 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001718
1719 // short rect that spans same x-range as kBaseRect
1720 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001721 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001722
1723 // skinny rect that spans slightly larger y-range than kBaseRect
1724 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1725 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001726 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001727
1728 // short rect that spans slightly larger x-range than kBaseRect
1729 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1730 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001731 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001732 };
1733
1734 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001735 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001736 SkRect qRect = kQueries[q].fQueryRect;
1737 if (inv & 0x1) {
1738 SkTSwap(qRect.fLeft, qRect.fRight);
1739 }
1740 if (inv & 0x2) {
1741 SkTSwap(qRect.fTop, qRect.fBottom);
1742 }
1743 for (int d = 0; d < 2; ++d) {
1744 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1745 path.reset();
1746 path.addRect(kBaseRect, dir);
1747 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1748 path.conservativelyContainsRect(qRect));
1749
1750 path.reset();
1751 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1752 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1753 path.conservativelyContainsRect(qRect));
1754
1755 path.reset();
1756 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1757 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1758 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001759
1760 path.reset();
1761 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1762 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1763 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1764 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1765 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1766 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1767 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1768 path.close();
1769 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1770 path.conservativelyContainsRect(qRect));
1771
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001772 }
1773 // Slightly non-convex shape, shouldn't contain any rects.
1774 path.reset();
1775 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001776 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001777 path.lineTo(SkIntToScalar(100), 0);
1778 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1779 path.lineTo(0, SkIntToScalar(100));
1780 path.close();
1781 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1782 }
1783 }
1784
1785 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1786 path.reset();
1787 path.moveTo(0, 0);
1788 path.lineTo(SkIntToScalar(100), 0);
1789 path.lineTo(0, SkIntToScalar(100));
1790
1791 // inside, on along top edge
1792 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1793 SkIntToScalar(10),
1794 SkIntToScalar(10))));
1795 // above
1796 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1797 SkRect::MakeXYWH(SkIntToScalar(50),
1798 SkIntToScalar(-10),
1799 SkIntToScalar(10),
1800 SkIntToScalar(10))));
1801 // to the left
1802 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1803 SkIntToScalar(5),
1804 SkIntToScalar(5),
1805 SkIntToScalar(5))));
1806
1807 // outside the diagonal edge
1808 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1809 SkIntToScalar(200),
1810 SkIntToScalar(20),
1811 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001812
bsalomonb17c1292014-08-28 14:04:55 -07001813
1814 // Test that multiple move commands do not cause asserts.
1815
1816 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001817 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001818 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1819#if 0
1820 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1821 SkIntToScalar(10),
1822 SkIntToScalar(10))));
1823#endif
1824
1825 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001826 path.reset();
1827 path.moveTo(100, 100);
1828 path.moveTo(0, 0);
1829 path.lineTo(SkIntToScalar(100), 0);
1830 path.lineTo(0, SkIntToScalar(100));
1831
1832 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1833 SkIntToScalar(10),
1834 SkIntToScalar(10))));
1835
bsalomonb17c1292014-08-28 14:04:55 -07001836 // Test that multiple move commands do not cause asserts and that the function
1837 // is not confused by the multiple moves.
1838 path.reset();
1839 path.moveTo(0, 0);
1840 path.lineTo(SkIntToScalar(100), 0);
1841 path.lineTo(0, SkIntToScalar(100));
1842 path.moveTo(0, SkIntToScalar(200));
1843 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1844 path.lineTo(0, SkIntToScalar(300));
1845
1846 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1847 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1848 SkIntToScalar(10),
1849 SkIntToScalar(10))));
1850
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001851 path.reset();
1852 path.lineTo(100, 100);
1853 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001854}
1855
reed@google.comf32322b2013-10-16 15:14:04 +00001856static void test_isRect_open_close(skiatest::Reporter* reporter) {
1857 SkPath path;
1858 bool isClosed;
1859
1860 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001861 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001862
halcanary96fcdcc2015-08-27 07:41:13 -07001863 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001864 REPORTER_ASSERT(reporter, isClosed);
1865}
1866
caryclark@google.comf1316942011-07-26 19:54:45 +00001867// Simple isRect test is inline TestPath, below.
1868// test_isRect provides more extensive testing.
1869static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001870 test_isRect_open_close(reporter);
1871
caryclark@google.comf1316942011-07-26 19:54:45 +00001872 // passing tests (all moveTo / lineTo...
1873 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1874 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1875 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1876 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1877 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1878 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1879 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1880 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1881 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001882 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1883 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 +00001884 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1885 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1886 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001887 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001888
caryclark@google.comf1316942011-07-26 19:54:45 +00001889 // failing tests
1890 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1891 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1892 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1893 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1894 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1895 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1896 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1897 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001898 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1899 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1900 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001901
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001902 // no close, but we should detect them as fillably the same as a rect
1903 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1904 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1905 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1906
1907 // like c2, but we double-back on ourselves
1908 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1909 // like c2, but we overshoot the start point
1910 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1911 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001912
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001913 struct IsRectTest {
1914 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001915 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001916 bool fClose;
1917 bool fIsRect;
1918 } tests[] = {
1919 { r1, SK_ARRAY_COUNT(r1), true, true },
1920 { r2, SK_ARRAY_COUNT(r2), true, true },
1921 { r3, SK_ARRAY_COUNT(r3), true, true },
1922 { r4, SK_ARRAY_COUNT(r4), true, true },
1923 { r5, SK_ARRAY_COUNT(r5), true, true },
1924 { r6, SK_ARRAY_COUNT(r6), true, true },
1925 { r7, SK_ARRAY_COUNT(r7), true, true },
1926 { r8, SK_ARRAY_COUNT(r8), true, true },
1927 { r9, SK_ARRAY_COUNT(r9), true, true },
1928 { ra, SK_ARRAY_COUNT(ra), true, true },
1929 { rb, SK_ARRAY_COUNT(rb), true, true },
1930 { rc, SK_ARRAY_COUNT(rc), true, true },
1931 { rd, SK_ARRAY_COUNT(rd), true, true },
1932 { re, SK_ARRAY_COUNT(re), true, true },
1933 { rf, SK_ARRAY_COUNT(rf), true, true },
1934
1935 { f1, SK_ARRAY_COUNT(f1), true, false },
1936 { f2, SK_ARRAY_COUNT(f2), true, false },
1937 { f3, SK_ARRAY_COUNT(f3), true, false },
1938 { f4, SK_ARRAY_COUNT(f4), true, false },
1939 { f5, SK_ARRAY_COUNT(f5), true, false },
1940 { f6, SK_ARRAY_COUNT(f6), true, false },
1941 { f7, SK_ARRAY_COUNT(f7), true, false },
1942 { f8, SK_ARRAY_COUNT(f8), true, false },
1943 { f9, SK_ARRAY_COUNT(f9), true, false },
1944 { fa, SK_ARRAY_COUNT(fa), true, false },
1945 { fb, SK_ARRAY_COUNT(fb), true, false },
1946
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001947 { c1, SK_ARRAY_COUNT(c1), false, true },
1948 { c2, SK_ARRAY_COUNT(c2), false, true },
1949 { c3, SK_ARRAY_COUNT(c3), false, true },
1950
1951 { d1, SK_ARRAY_COUNT(d1), false, false },
1952 { d2, SK_ARRAY_COUNT(d2), false, false },
1953 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001954 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001955
1956 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001957 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001958 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1959 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001960 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1961 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1962 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001963 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001964 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001965 path.close();
1966 }
halcanary96fcdcc2015-08-27 07:41:13 -07001967 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001968
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001969 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001970 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001971 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001972 SkPath::Direction direction;
1973 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001974 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001975 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001976 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1977 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001978 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001979 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001980 } else {
1981 SkRect computed;
1982 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001983 bool isClosed = (bool)-1;
1984 SkPath::Direction direction = (SkPath::Direction) - 1;
1985 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001986 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1987 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001988 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1989 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001990 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001991 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001992
caryclark@google.comf1316942011-07-26 19:54:45 +00001993 // fail, close then line
1994 SkPath path1;
1995 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001996 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001997 path1.lineTo(r1[index].fX, r1[index].fY);
1998 }
1999 path1.close();
2000 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002001 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002002
caryclark@google.comf1316942011-07-26 19:54:45 +00002003 // fail, move in the middle
2004 path1.reset();
2005 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002006 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002007 if (index == 2) {
2008 path1.moveTo(1, .5f);
2009 }
2010 path1.lineTo(r1[index].fX, r1[index].fY);
2011 }
2012 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002013 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002014
2015 // fail, move on the edge
2016 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002017 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002018 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2019 path1.lineTo(r1[index].fX, r1[index].fY);
2020 }
2021 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002022 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002023
caryclark@google.comf1316942011-07-26 19:54:45 +00002024 // fail, quad
2025 path1.reset();
2026 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002027 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002028 if (index == 2) {
2029 path1.quadTo(1, .5f, 1, .5f);
2030 }
2031 path1.lineTo(r1[index].fX, r1[index].fY);
2032 }
2033 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002034 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002035
caryclark@google.comf1316942011-07-26 19:54:45 +00002036 // fail, cubic
2037 path1.reset();
2038 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002039 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002040 if (index == 2) {
2041 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2042 }
2043 path1.lineTo(r1[index].fX, r1[index].fY);
2044 }
2045 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002046 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002047}
2048
bsalomonedc743a2016-06-01 09:42:31 -07002049static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2050 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2051 SkRect r = SkRect::MakeEmpty();
2052 SkPath::Direction d = SkPath::kCCW_Direction;
2053 unsigned s = ~0U;
2054
2055 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2056 REPORTER_ASSERT(reporter, r == rect);
2057 REPORTER_ASSERT(reporter, d == dir);
2058 REPORTER_ASSERT(reporter, s == start);
2059}
2060
2061static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2062 SkRect r = SkRect::MakeEmpty();
2063 SkPath::Direction d = SkPath::kCCW_Direction;
2064 unsigned s = ~0U;
2065
2066 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2067 const SkRect emptyRect = SkRect::MakeEmpty();
2068 SkPath path;
2069 for (int start = 0; start < 4; ++start) {
2070 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2071 SkPath path;
2072 path.addRect(testRect, dir, start);
2073 check_simple_closed_rect(reporter, path, testRect, dir, start);
2074 path.close();
2075 check_simple_closed_rect(reporter, path, testRect, dir, start);
2076 SkPath path2 = path;
2077 path2.lineTo(10, 10);
2078 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2079 path2 = path;
2080 path2.moveTo(10, 10);
2081 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2082 path2 = path;
2083 path2.addRect(testRect, dir, start);
2084 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2085 // Make the path by hand, manually closing it.
2086 path2.reset();
2087 SkPath::RawIter iter(path);
2088 SkPath::Verb v;
2089 SkPoint verbPts[4];
2090 SkPoint firstPt = {0.f, 0.f};
2091 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2092 switch(v) {
2093 case SkPath::kMove_Verb:
2094 firstPt = verbPts[0];
2095 path2.moveTo(verbPts[0]);
2096 break;
2097 case SkPath::kLine_Verb:
2098 path2.lineTo(verbPts[1]);
2099 break;
2100 default:
2101 break;
2102 }
2103 }
2104 // We haven't closed it yet...
2105 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2106 // ... now we do and test again.
2107 path2.lineTo(firstPt);
2108 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2109 // A redundant close shouldn't cause a failure.
2110 path2.close();
2111 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2112 // Degenerate point and line rects are not allowed
2113 path2.reset();
2114 path2.addRect(emptyRect, dir, start);
2115 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2116 SkRect degenRect = testRect;
2117 degenRect.fLeft = degenRect.fRight;
2118 path2.reset();
2119 path2.addRect(degenRect, dir, start);
2120 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2121 degenRect = testRect;
2122 degenRect.fTop = degenRect.fBottom;
2123 path2.reset();
2124 path2.addRect(degenRect, dir, start);
2125 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2126 // An inverted rect makes a rect path, but changes the winding dir and start point.
2127 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2128 ? SkPath::kCCW_Direction
2129 : SkPath::kCW_Direction;
2130 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2131 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2132 SkRect swapRect = testRect;
2133 SkTSwap(swapRect.fLeft, swapRect.fRight);
2134 path2.reset();
2135 path2.addRect(swapRect, dir, start);
2136 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2137 swapRect = testRect;
2138 SkTSwap(swapRect.fTop, swapRect.fBottom);
2139 path2.reset();
2140 path2.addRect(swapRect, dir, start);
2141 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2142 }
2143 }
bsalomon057ae8a2016-07-24 05:37:26 -07002144 // down, up, left, close
2145 path.reset();
2146 path.moveTo(1, 1);
2147 path.lineTo(1, 2);
2148 path.lineTo(1, 1);
2149 path.lineTo(0, 1);
2150 SkRect rect;
2151 SkPath::Direction dir;
2152 unsigned start;
2153 path.close();
2154 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2155 // right, left, up, close
2156 path.reset();
2157 path.moveTo(1, 1);
2158 path.lineTo(2, 1);
2159 path.lineTo(1, 1);
2160 path.lineTo(1, 0);
2161 path.close();
2162 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2163 // parallelogram with horizontal edges
2164 path.reset();
2165 path.moveTo(1, 0);
2166 path.lineTo(3, 0);
2167 path.lineTo(2, 1);
2168 path.lineTo(0, 1);
2169 path.close();
2170 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2171 // parallelogram with vertical edges
2172 path.reset();
2173 path.moveTo(0, 1);
2174 path.lineTo(0, 3);
2175 path.lineTo(1, 2);
2176 path.lineTo(1, 0);
2177 path.close();
2178 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2179
bsalomonedc743a2016-06-01 09:42:31 -07002180}
2181
caryclark95bc5f32015-04-08 08:34:15 -07002182static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002183 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002184 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002185 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2186 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2187 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002188 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002189 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2190 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2191 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2192 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002193 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2194 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 +00002195 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2196 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2197 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002198
2199 // failing tests
2200 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2201 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2202 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2203 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2204 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2205 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2206 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2207 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2208
caryclark95bc5f32015-04-08 08:34:15 -07002209 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002210 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2211 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2212
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002213 struct IsNestedRectTest {
2214 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002215 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002216 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002217 bool fClose;
2218 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2219 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002220 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2221 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2222 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2223 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2224 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2225 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2226 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2227 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2228 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2229 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2230 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2231 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2232 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2233 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002234
reed026beb52015-06-10 14:23:15 -07002235 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2236 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2237 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2238 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2239 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2240 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2241 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2242 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002243
reed026beb52015-06-10 14:23:15 -07002244 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2245 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002246 };
2247
2248 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002249 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002250 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002251 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2252 SkPath path;
2253 if (rectFirst) {
2254 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2255 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002256 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2257 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2258 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002259 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002260 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002261 path.close();
2262 }
2263 if (!rectFirst) {
2264 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2265 }
caryclark95bc5f32015-04-08 08:34:15 -07002266 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002267 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002268 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002269 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002270 SkPathPriv::FirstDirection expectedDirs[2];
2271 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002272 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002273 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002274 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2275 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002276 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002277 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002278 } else {
reed026beb52015-06-10 14:23:15 -07002279 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002280 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002281 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002282 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002283 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2284 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002285 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2286 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002287 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002288 }
2289
2290 // fail, close then line
2291 SkPath path1;
2292 if (rectFirst) {
2293 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2294 }
2295 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002296 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002297 path1.lineTo(r1[index].fX, r1[index].fY);
2298 }
2299 path1.close();
2300 path1.lineTo(1, 0);
2301 if (!rectFirst) {
2302 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2303 }
halcanary96fcdcc2015-08-27 07:41:13 -07002304 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002305
2306 // fail, move in the middle
2307 path1.reset();
2308 if (rectFirst) {
2309 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2310 }
2311 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002312 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002313 if (index == 2) {
2314 path1.moveTo(1, .5f);
2315 }
2316 path1.lineTo(r1[index].fX, r1[index].fY);
2317 }
2318 path1.close();
2319 if (!rectFirst) {
2320 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2321 }
halcanary96fcdcc2015-08-27 07:41:13 -07002322 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002323
2324 // fail, move on the edge
2325 path1.reset();
2326 if (rectFirst) {
2327 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2328 }
bsalomon98806072014-12-12 15:11:17 -08002329 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002330 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2331 path1.lineTo(r1[index].fX, r1[index].fY);
2332 }
2333 path1.close();
2334 if (!rectFirst) {
2335 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2336 }
halcanary96fcdcc2015-08-27 07:41:13 -07002337 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002338
2339 // fail, quad
2340 path1.reset();
2341 if (rectFirst) {
2342 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2343 }
2344 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002345 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002346 if (index == 2) {
2347 path1.quadTo(1, .5f, 1, .5f);
2348 }
2349 path1.lineTo(r1[index].fX, r1[index].fY);
2350 }
2351 path1.close();
2352 if (!rectFirst) {
2353 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2354 }
halcanary96fcdcc2015-08-27 07:41:13 -07002355 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002356
2357 // fail, cubic
2358 path1.reset();
2359 if (rectFirst) {
2360 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2361 }
2362 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002363 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002364 if (index == 2) {
2365 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2366 }
2367 path1.lineTo(r1[index].fX, r1[index].fY);
2368 }
2369 path1.close();
2370 if (!rectFirst) {
2371 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2372 }
halcanary96fcdcc2015-08-27 07:41:13 -07002373 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002374
caryclark@google.com56f233a2012-11-19 13:06:06 +00002375 // fail, not nested
2376 path1.reset();
2377 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2378 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002379 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002380 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002381
caryclark95bc5f32015-04-08 08:34:15 -07002382 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2383 SkPath path;
2384 path.moveTo(0, 0);
2385 path.lineTo(10, 0);
2386 path.lineTo(10, 10);
2387 path.lineTo(0, 10);
2388 path.lineTo(0, 0);
2389 path.moveTo(1, 1);
2390 path.lineTo(9, 1);
2391 path.lineTo(9, 9);
2392 path.lineTo(1, 9);
2393 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002394 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002395
caryclark@google.combfe90372012-11-21 13:56:20 +00002396 // pass, stroke rect
2397 SkPath src, dst;
2398 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2399 SkPaint strokePaint;
2400 strokePaint.setStyle(SkPaint::kStroke_Style);
2401 strokePaint.setStrokeWidth(2);
2402 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002403 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002404}
2405
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002406static void write_and_read_back(skiatest::Reporter* reporter,
2407 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002408 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002409 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002410 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002411 SkAutoMalloc storage(size);
2412 writer.flatten(storage.get());
2413 SkReader32 reader(storage.get(), size);
2414
2415 SkPath readBack;
2416 REPORTER_ASSERT(reporter, readBack != p);
2417 reader.readPath(&readBack);
2418 REPORTER_ASSERT(reporter, readBack == p);
2419
rmistry@google.comd6176b02012-08-23 18:14:13 +00002420 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002421 p.getConvexityOrUnknown());
2422
bsalomon78d58d12016-05-27 09:17:04 -07002423 SkRect oval0, oval1;
2424 SkPath::Direction dir0, dir1;
2425 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002426 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002427 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2428 REPORTER_ASSERT(reporter, oval0 == oval1);
2429 REPORTER_ASSERT(reporter, dir0 == dir1);
2430 REPORTER_ASSERT(reporter, start0 == start1);
2431 }
2432 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2433 SkRRect rrect0, rrect1;
2434 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2435 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2436 REPORTER_ASSERT(reporter, dir0 == dir1);
2437 REPORTER_ASSERT(reporter, start0 == start1);
2438 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002439 const SkRect& origBounds = p.getBounds();
2440 const SkRect& readBackBounds = readBack.getBounds();
2441
2442 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2443}
2444
reed@google.com53effc52011-09-21 19:05:12 +00002445static void test_flattening(skiatest::Reporter* reporter) {
2446 SkPath p;
2447
2448 static const SkPoint pts[] = {
2449 { 0, 0 },
2450 { SkIntToScalar(10), SkIntToScalar(10) },
2451 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2452 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2453 };
2454 p.moveTo(pts[0]);
2455 p.lineTo(pts[1]);
2456 p.quadTo(pts[2], pts[3]);
2457 p.cubicTo(pts[4], pts[5], pts[6]);
2458
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002459 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002460
2461 // create a buffer that should be much larger than the path so we don't
2462 // kill our stack if writer goes too far.
2463 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002464 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002465 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002466 REPORTER_ASSERT(reporter, size1 == size2);
2467
2468 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002469 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002470 REPORTER_ASSERT(reporter, size1 == size3);
2471 REPORTER_ASSERT(reporter, p == p2);
2472
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002473 size3 = p2.readFromMemory(buffer, 0);
2474 REPORTER_ASSERT(reporter, !size3);
2475
2476 SkPath tooShort;
2477 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2478 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2479
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002480 char buffer2[1024];
2481 size3 = p2.writeToMemory(buffer2);
2482 REPORTER_ASSERT(reporter, size1 == size3);
2483 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002484
2485 // test persistence of the oval flag & convexity
2486 {
2487 SkPath oval;
2488 SkRect rect = SkRect::MakeWH(10, 10);
2489 oval.addOval(rect);
2490
2491 write_and_read_back(reporter, oval);
2492 }
reed@google.com53effc52011-09-21 19:05:12 +00002493}
2494
2495static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002496 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002497
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002498#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002499 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002500 { 0, 0 }, // move
2501 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2502 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2503 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2504#if CONIC_PERSPECTIVE_BUG_FIXED
2505 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2506#endif
reed@google.com53effc52011-09-21 19:05:12 +00002507 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002508 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002509
reed@google.com53effc52011-09-21 19:05:12 +00002510 p.moveTo(pts[0]);
2511 p.lineTo(pts[1]);
2512 p.quadTo(pts[2], pts[3]);
2513 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002514#if CONIC_PERSPECTIVE_BUG_FIXED
2515 p.conicTo(pts[4], pts[5], 0.5f);
2516#endif
2517 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002518
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002519 {
2520 SkMatrix matrix;
2521 matrix.reset();
2522 SkPath p1;
2523 p.transform(matrix, &p1);
2524 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002525 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002526
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002527
2528 {
2529 SkMatrix matrix;
2530 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2531
2532 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2533
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002534 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002535 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002536 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002537 REPORTER_ASSERT(reporter, kPtCount == count);
2538 for (int i = 0; i < count; ++i) {
2539 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2540 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2541 }
2542 }
2543
2544 {
2545 SkMatrix matrix;
2546 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002547 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002548
2549 SkPath p1;
2550 p1.moveTo(SkPoint::Make(0, 0));
2551
2552 p.transform(matrix, &p1);
2553 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002554 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002555 SkRect pBounds = p.getBounds();
2556 SkRect p1Bounds = p1.getBounds();
2557 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2558 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2559 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2560 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2561 }
2562
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002563 p.reset();
2564 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002565
2566 {
2567 SkMatrix matrix;
2568 matrix.reset();
2569 SkPath p1;
2570 p1.moveTo(SkPoint::Make(0, 0));
2571
2572 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002573 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002574 }
2575
2576
2577 {
2578 SkMatrix matrix;
2579 matrix.reset();
2580 matrix.setScaleX(-1);
2581 SkPath p1;
2582 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2583
2584 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002585 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002586 }
2587
2588 {
2589 SkMatrix matrix;
2590 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2591 SkPath p1;
2592 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2593
2594 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002595 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002596 }
reed@google.com53effc52011-09-21 19:05:12 +00002597}
2598
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002599static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002600 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002601 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002602
caryclark@google.com56f233a2012-11-19 13:06:06 +00002603 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002604 const char* testPath;
2605 const size_t numResultPts;
2606 const SkRect resultBound;
2607 const SkPath::Verb* resultVerbs;
2608 const size_t numResultVerbs;
2609 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002610
schenney@chromium.org7e963602012-06-13 17:05:43 +00002611 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2612 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2613 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2614 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2615 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2616 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2617 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2618 static const SkPath::Verb resultVerbs8[] = {
2619 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2620 };
2621 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2622 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2623 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2624 static const SkPath::Verb resultVerbs12[] = {
2625 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2626 };
2627 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2628 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2629 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2630 static const SkPath::Verb resultVerbs16[] = {
2631 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2632 };
2633 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002634 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002635 { "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 -07002636 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002637 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2638 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2639 { "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) },
2640 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2641 { "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) },
2642 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2643 { "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) },
2644 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2645 { "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) },
2646 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2647 { "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 +00002648 SK_ARRAY_COUNT(resultVerbs14)
2649 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002650 { "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) },
2651 { "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 +00002652 SK_ARRAY_COUNT(resultVerbs16)
2653 }
2654 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002655
schenney@chromium.org7e963602012-06-13 17:05:43 +00002656 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2657 p.reset();
2658 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2659 REPORTER_ASSERT(reporter, valid);
2660 REPORTER_ASSERT(reporter, !p.isEmpty());
2661 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2662 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2663 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2664 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2665 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2666 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002667 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002668}
2669
2670struct SegmentInfo {
2671 SkPath fPath;
2672 int fPointCount;
2673};
2674
reed@google.com10296cc2011-09-21 12:29:05 +00002675#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2676
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002677static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002678 SkPath p, p2;
2679
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002680 p.moveTo(0, 0);
2681 p.quadTo(100, 100, 200, 200);
2682 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2683 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002684 p2 = p;
2685 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002686 p.cubicTo(100, 100, 200, 200, 300, 300);
2687 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2688 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002689 p2 = p;
2690 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2691
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002692 p.reset();
2693 p.moveTo(0, 0);
2694 p.cubicTo(100, 100, 200, 200, 300, 300);
2695 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002696 p2 = p;
2697 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002698
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002699 REPORTER_ASSERT(reporter, !p.isEmpty());
2700}
2701
2702static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002703 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002704 SkPoint pts[4];
2705
2706 // Test an iterator with no path
2707 SkPath::Iter noPathIter;
2708 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002709
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002710 // Test that setting an empty path works
2711 noPathIter.setPath(p, false);
2712 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002713
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002714 // Test that close path makes no difference for an empty path
2715 noPathIter.setPath(p, true);
2716 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002717
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002718 // Test an iterator with an initial empty path
2719 SkPath::Iter iter(p, false);
2720 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2721
2722 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002723 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002724 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2725
rmistry@google.comd6176b02012-08-23 18:14:13 +00002726
schenney@chromium.org7e963602012-06-13 17:05:43 +00002727 struct iterTestData {
2728 const char* testPath;
2729 const bool forceClose;
2730 const bool consumeDegenerates;
2731 const size_t* numResultPtsPerVerb;
2732 const SkPoint* resultPts;
2733 const SkPath::Verb* resultVerbs;
2734 const size_t numResultVerbs;
2735 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002736
schenney@chromium.org7e963602012-06-13 17:05:43 +00002737 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2738 static const SkPath::Verb resultVerbs2[] = {
2739 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2740 };
2741 static const SkPath::Verb resultVerbs3[] = {
2742 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2743 };
2744 static const SkPath::Verb resultVerbs4[] = {
2745 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2746 };
2747 static const SkPath::Verb resultVerbs5[] = {
2748 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2749 };
2750 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002751 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2752 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2753 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2754 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002755 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002756 static const SkPoint resultPts2[] = {
2757 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2758 };
2759 static const SkPoint resultPts3[] = {
2760 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2761 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2762 };
2763 static const SkPoint resultPts4[] = {
2764 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2765 };
2766 static const SkPoint resultPts5[] = {
2767 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2768 };
2769 static const struct iterTestData gIterTests[] = {
2770 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002771 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2772 { "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 +00002773 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2774 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2775 { "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) },
2776 { "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 +00002777 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2778 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2779 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2780 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2781 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2782 { "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 +00002783 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002784
schenney@chromium.org7e963602012-06-13 17:05:43 +00002785 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2786 p.reset();
2787 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2788 REPORTER_ASSERT(reporter, valid);
2789 iter.setPath(p, gIterTests[i].forceClose);
2790 int j = 0, l = 0;
2791 do {
2792 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2793 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2794 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2795 }
2796 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2797 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2798 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002799
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002800 p.reset();
2801 iter.setPath(p, false);
2802 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2803 p.lineTo(1, 1);
2804 p.close();
2805 iter.setPath(p, false);
2806 REPORTER_ASSERT(reporter, iter.isClosedContour());
2807 p.reset();
2808 iter.setPath(p, true);
2809 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2810 p.lineTo(1, 1);
2811 iter.setPath(p, true);
2812 REPORTER_ASSERT(reporter, iter.isClosedContour());
2813 p.moveTo(0, 0);
2814 p.lineTo(2, 2);
2815 iter.setPath(p, false);
2816 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2817
2818 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2819 // check to see if the result is correct.
2820 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2821 p.reset();
2822 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2823 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2824 iter.setPath(p, true);
2825 iter.next(pts, false);
2826 iter.next(pts, false);
2827 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2828 }
2829
2830 p.reset();
2831 p.quadTo(0, 0, 0, 0);
2832 iter.setPath(p, false);
2833 iter.next(pts, false);
2834 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2835 iter.setPath(p, false);
2836 iter.next(pts, false);
2837 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2838
2839 p.reset();
2840 p.conicTo(0, 0, 0, 0, 0.5f);
2841 iter.setPath(p, false);
2842 iter.next(pts, false);
2843 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2844 iter.setPath(p, false);
2845 iter.next(pts, false);
2846 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2847
2848 p.reset();
2849 p.cubicTo(0, 0, 0, 0, 0, 0);
2850 iter.setPath(p, false);
2851 iter.next(pts, false);
2852 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2853 iter.setPath(p, false);
2854 iter.next(pts, false);
2855 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2856
2857 p.moveTo(1, 1); // add a trailing moveto
2858 iter.setPath(p, false);
2859 iter.next(pts, false);
2860 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2861 iter.setPath(p, false);
2862 iter.next(pts, false);
2863 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2864
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002865 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002866
2867 // Test out mixed degenerate and non-degenerate geometry with Conics
2868 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2869 SkRect r = SkRect::MakeWH(100, 100);
2870 SkRRect rr;
2871 rr.setRectRadii(r, radii);
2872 p.reset();
2873 p.addRRect(rr);
2874 iter.setPath(p, false);
2875 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2876 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2877 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2878 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2879 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002880}
2881
2882static void test_raw_iter(skiatest::Reporter* reporter) {
2883 SkPath p;
2884 SkPoint pts[4];
2885
2886 // Test an iterator with no path
2887 SkPath::RawIter noPathIter;
2888 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2889 // Test that setting an empty path works
2890 noPathIter.setPath(p);
2891 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002892
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002893 // Test an iterator with an initial empty path
2894 SkPath::RawIter iter(p);
2895 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2896
2897 // Test that a move-only path returns the move.
2898 p.moveTo(SK_Scalar1, 0);
2899 iter.setPath(p);
2900 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2901 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2902 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2903 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2904
2905 // No matter how many moves we add, we should get them all back
2906 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2907 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2908 iter.setPath(p);
2909 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2910 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2911 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2912 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2913 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2914 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2915 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2916 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2917 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2918 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2919
2920 // Initial close is never ever stored
2921 p.reset();
2922 p.close();
2923 iter.setPath(p);
2924 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2925
2926 // Move/close sequences
2927 p.reset();
2928 p.close(); // Not stored, no purpose
2929 p.moveTo(SK_Scalar1, 0);
2930 p.close();
2931 p.close(); // Not stored, no purpose
2932 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2933 p.close();
2934 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2935 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2936 p.close();
2937 iter.setPath(p);
2938 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2939 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2940 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2941 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002942 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2943 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2944 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2945 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002946 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2947 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2948 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2949 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2950 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2951 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2952 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002953 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2954
2955 // Generate random paths and verify
2956 SkPoint randomPts[25];
2957 for (int i = 0; i < 5; ++i) {
2958 for (int j = 0; j < 5; ++j) {
2959 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2960 }
2961 }
2962
2963 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002964 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002965 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002966 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002967 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002968
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002969 for (int i = 0; i < 500; ++i) {
2970 p.reset();
2971 bool lastWasClose = true;
2972 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002973 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002974 int numPoints = 0;
2975 int numVerbs = (rand.nextU() >> 16) % 10;
2976 int numIterVerbs = 0;
2977 for (int j = 0; j < numVerbs; ++j) {
2978 do {
2979 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2980 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002981 switch (nextVerb) {
2982 case SkPath::kMove_Verb:
2983 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2984 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002985 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002986 numPoints += 1;
2987 lastWasClose = false;
2988 haveMoveTo = true;
2989 break;
2990 case SkPath::kLine_Verb:
2991 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002992 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002993 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2994 haveMoveTo = true;
2995 }
2996 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2997 p.lineTo(expectedPts[numPoints]);
2998 numPoints += 1;
2999 lastWasClose = false;
3000 break;
3001 case SkPath::kQuad_Verb:
3002 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003003 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003004 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3005 haveMoveTo = true;
3006 }
3007 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3008 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3009 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3010 numPoints += 2;
3011 lastWasClose = false;
3012 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003013 case SkPath::kConic_Verb:
3014 if (!haveMoveTo) {
3015 expectedPts[numPoints++] = lastMoveToPt;
3016 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3017 haveMoveTo = true;
3018 }
3019 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3020 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3021 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3022 rand.nextUScalar1() * 4);
3023 numPoints += 2;
3024 lastWasClose = false;
3025 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003026 case SkPath::kCubic_Verb:
3027 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003028 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003029 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 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3035 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3036 expectedPts[numPoints + 2]);
3037 numPoints += 3;
3038 lastWasClose = false;
3039 break;
3040 case SkPath::kClose_Verb:
3041 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003042 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003043 lastWasClose = true;
3044 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003045 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003046 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003047 }
3048 expectedVerbs[numIterVerbs++] = nextVerb;
3049 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003050
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003051 iter.setPath(p);
3052 numVerbs = numIterVerbs;
3053 numIterVerbs = 0;
3054 int numIterPts = 0;
3055 SkPoint lastMoveTo;
3056 SkPoint lastPt;
3057 lastMoveTo.set(0, 0);
3058 lastPt.set(0, 0);
3059 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3060 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3061 numIterVerbs++;
3062 switch (nextVerb) {
3063 case SkPath::kMove_Verb:
3064 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3065 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3066 lastPt = lastMoveTo = pts[0];
3067 numIterPts += 1;
3068 break;
3069 case SkPath::kLine_Verb:
3070 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3071 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3072 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3073 lastPt = pts[1];
3074 numIterPts += 1;
3075 break;
3076 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003077 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003078 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3079 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3080 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3081 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3082 lastPt = pts[2];
3083 numIterPts += 2;
3084 break;
3085 case SkPath::kCubic_Verb:
3086 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3087 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3088 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3089 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3090 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3091 lastPt = pts[3];
3092 numIterPts += 3;
3093 break;
3094 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003095 lastPt = lastMoveTo;
3096 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003097 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003098 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003099 }
3100 }
3101 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3102 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3103 }
3104}
3105
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003106static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003107 const SkPath& path,
3108 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003109 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003110 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003111 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003112 SkPath::Direction isOvalDir;
3113 unsigned isOvalStart;
3114 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003115 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003116 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3117 SkPath tmpPath;
3118 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3119 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003120 }
bsalomon78d58d12016-05-27 09:17:04 -07003121 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003122}
3123
3124static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003125 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003126 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003127 SkPath tmp;
3128
3129 SkMatrix m;
3130 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3131 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003132 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003133 if (SkPathPriv::kCCW_FirstDirection == dir) {
3134 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003135 } else {
reed026beb52015-06-10 14:23:15 -07003136 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3137 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003138 }
3139 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003140}
3141
3142static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003143 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003144 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003145 SkPath tmp;
3146
3147 // translate at small offset
3148 SkMatrix m;
3149 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3150 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003151 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003152
3153 tmp.reset();
3154 m.reset();
3155
3156 // translate at a relatively big offset
3157 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3158 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003159 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003160}
3161
3162static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003163 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003164 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003165 for (int angle = 0; angle < 360; ++angle) {
3166 SkPath tmp;
3167 SkMatrix m;
3168 m.setRotate(SkIntToScalar(angle));
3169 path.transform(m, &tmp);
3170
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003171 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003172 // degrees is not an oval anymore, this can be improved. we made this
3173 // for the simplicity of our implementation.
3174 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003175 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003176 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003177 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003178 }
3179 }
3180}
3181
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003182static void test_circle_mirror_x(skiatest::Reporter* reporter,
3183 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003184 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003185 SkPath tmp;
3186 SkMatrix m;
3187 m.reset();
3188 m.setScaleX(-SK_Scalar1);
3189 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003190 if (SkPathPriv::kCW_FirstDirection == dir) {
3191 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003192 } else {
reed026beb52015-06-10 14:23:15 -07003193 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3194 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003195 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003196 check_for_circle(reporter, tmp, true, dir);
3197}
3198
3199static void test_circle_mirror_y(skiatest::Reporter* reporter,
3200 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003201 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003202 SkPath tmp;
3203 SkMatrix m;
3204 m.reset();
3205 m.setScaleY(-SK_Scalar1);
3206 path.transform(m, &tmp);
3207
reed026beb52015-06-10 14:23:15 -07003208 if (SkPathPriv::kCW_FirstDirection == dir) {
3209 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003210 } else {
reed026beb52015-06-10 14:23:15 -07003211 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3212 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003213 }
3214
3215 check_for_circle(reporter, tmp, true, dir);
3216}
3217
3218static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3219 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003220 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003221 SkPath tmp;
3222 SkMatrix m;
3223 m.reset();
3224 m.setScaleX(-SK_Scalar1);
3225 m.setScaleY(-SK_Scalar1);
3226 path.transform(m, &tmp);
3227
3228 check_for_circle(reporter, tmp, true, dir);
3229}
3230
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003231static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003232 SkPath::Direction inDir) {
3233 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003234 SkPath path;
3235
3236 // circle at origin
reed026beb52015-06-10 14:23:15 -07003237 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3238
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003239 check_for_circle(reporter, path, true, dir);
3240 test_circle_rotate(reporter, path, dir);
3241 test_circle_translate(reporter, path, dir);
3242 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003243 test_circle_mirror_x(reporter, path, dir);
3244 test_circle_mirror_y(reporter, path, dir);
3245 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003246
3247 // circle at an offset at (10, 10)
3248 path.reset();
3249 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003250 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);
3256 test_circle_mirror_x(reporter, path, dir);
3257 test_circle_mirror_y(reporter, path, dir);
3258 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003259
3260 // Try different starting points for the contour.
3261 for (unsigned start = 0; start < 4; ++start) {
3262 path.reset();
3263 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3264 test_circle_rotate(reporter, path, dir);
3265 test_circle_translate(reporter, path, dir);
3266 test_circle_skew(reporter, path, dir);
3267 test_circle_mirror_x(reporter, path, dir);
3268 test_circle_mirror_y(reporter, path, dir);
3269 test_circle_mirror_xy(reporter, path, dir);
3270 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003271}
3272
3273static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3274 SkPath path;
3275 SkPath circle;
3276 SkPath rect;
3277 SkPath empty;
3278
reed026beb52015-06-10 14:23:15 -07003279 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3280 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003281
3282 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003283 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3284 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3285
3286 SkMatrix translate;
3287 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3288
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003289 // Although all the path concatenation related operations leave
3290 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003291
3292 // empty + circle (translate)
3293 path = empty;
3294 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003295 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003296
3297 // circle + empty (translate)
3298 path = circle;
3299 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003300
reed026beb52015-06-10 14:23:15 -07003301 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003302
3303 // test reverseAddPath
3304 path = circle;
3305 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003306 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003307}
3308
3309static void test_circle(skiatest::Reporter* reporter) {
3310 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3311 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3312
3313 // multiple addCircle()
3314 SkPath path;
3315 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3316 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003317 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003318
3319 // some extra lineTo() would make isOval() fail
3320 path.reset();
3321 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3322 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003323 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003324
3325 // not back to the original point
3326 path.reset();
3327 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3328 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003329 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003330
3331 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003332
3333 // test negative radius
3334 path.reset();
3335 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3336 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003337}
3338
3339static void test_oval(skiatest::Reporter* reporter) {
3340 SkRect rect;
3341 SkMatrix m;
3342 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003343 unsigned start = 0;
3344 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003345
3346 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3347 path.addOval(rect);
3348
bsalomon78d58d12016-05-27 09:17:04 -07003349 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003350 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003351
3352 m.setRotate(SkIntToScalar(90));
3353 SkPath tmp;
3354 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003355 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3356 // is unchanged.
3357 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3358 REPORTER_ASSERT(reporter, 2 == start);
3359 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003360
3361 m.reset();
3362 m.setRotate(SkIntToScalar(30));
3363 tmp.reset();
3364 path.transform(m, &tmp);
3365 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003366 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003367
3368 // since empty path being transformed.
3369 path.reset();
3370 tmp.reset();
3371 m.reset();
3372 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003373 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003374
3375 // empty path is not an oval
3376 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003377 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003378
3379 // only has moveTo()s
3380 tmp.reset();
3381 tmp.moveTo(0, 0);
3382 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003383 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003384
3385 // mimic WebKit's calling convention,
3386 // call moveTo() first and then call addOval()
3387 path.reset();
3388 path.moveTo(0, 0);
3389 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003390 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003391
3392 // copy path
3393 path.reset();
3394 tmp.reset();
3395 tmp.addOval(rect);
3396 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003397 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3398 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3399 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003400}
3401
bungeman@google.coma5809a32013-06-21 15:13:34 +00003402static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3403 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003404
reed@android.com3abec1d2009-03-02 05:36:20 +00003405 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003406 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003407 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003408 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003409 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003410 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3411 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003412 REPORTER_ASSERT(reporter, p == empty);
3413 REPORTER_ASSERT(reporter, !(p != empty));
3414}
3415
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003416static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3417 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003418 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003419 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003420 path->setConvexity(SkPath::kUnknown_Convexity);
3421 REPORTER_ASSERT(reporter, path->isConvex());
3422 path->reset();
3423}
3424
caryclarkd3d1a982014-12-08 04:57:38 -08003425static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3426 SkPath::Direction dir) {
3427 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003428 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003429 path->setConvexity(SkPath::kUnknown_Convexity);
3430 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3431 path->reset();
3432}
3433
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003434static void test_rrect(skiatest::Reporter* reporter) {
3435 SkPath p;
3436 SkRRect rr;
3437 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3438 SkRect r = {10, 20, 30, 40};
3439 rr.setRectRadii(r, radii);
3440 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003441 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003442 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003443 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003444 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003445 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003446 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003447 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003448 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003449 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003450 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003451 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3452 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3453 SkVector save = radii[i];
3454 radii[i].set(0, 0);
3455 rr.setRectRadii(r, radii);
3456 p.addRRect(rr);
3457 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3458 radii[i] = save;
3459 }
3460 p.addRoundRect(r, 0, 0);
3461 SkRect returnedRect;
3462 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3463 REPORTER_ASSERT(reporter, returnedRect == r);
3464 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3465 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3466 rr.setRectRadii(r, zeroRadii);
3467 p.addRRect(rr);
3468 bool closed;
3469 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003470 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003471 REPORTER_ASSERT(reporter, closed);
3472 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3473 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3474 p.addRRect(rr, SkPath::kCW_Direction);
3475 p.addRRect(rr, SkPath::kCW_Direction);
3476 REPORTER_ASSERT(reporter, !p.isConvex());
3477 p.reset();
3478 p.addRRect(rr, SkPath::kCCW_Direction);
3479 p.addRRect(rr, SkPath::kCCW_Direction);
3480 REPORTER_ASSERT(reporter, !p.isConvex());
3481 p.reset();
3482 SkRect emptyR = {10, 20, 10, 30};
3483 rr.setRectRadii(emptyR, radii);
3484 p.addRRect(rr);
3485 REPORTER_ASSERT(reporter, p.isEmpty());
3486 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3487 rr.setRectRadii(largeR, radii);
3488 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003489 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003490
3491 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003492 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3493 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003494 REPORTER_ASSERT(reporter, rr.isEmpty());
3495
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003496 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3497 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3498 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003499}
3500
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003501static void test_arc(skiatest::Reporter* reporter) {
3502 SkPath p;
3503 SkRect emptyOval = {10, 20, 30, 20};
3504 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3505 p.addArc(emptyOval, 1, 2);
3506 REPORTER_ASSERT(reporter, p.isEmpty());
3507 p.reset();
3508 SkRect oval = {10, 20, 30, 40};
3509 p.addArc(oval, 1, 0);
3510 REPORTER_ASSERT(reporter, p.isEmpty());
3511 p.reset();
3512 SkPath cwOval;
3513 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003514 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003515 REPORTER_ASSERT(reporter, p == cwOval);
3516 p.reset();
3517 SkPath ccwOval;
3518 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003519 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003520 REPORTER_ASSERT(reporter, p == ccwOval);
3521 p.reset();
3522 p.addArc(oval, 1, 180);
3523 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003524 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003525 p.setConvexity(SkPath::kUnknown_Convexity);
3526 REPORTER_ASSERT(reporter, p.isConvex());
3527}
3528
bsalomon1978ce22016-05-31 10:42:16 -07003529static inline SkScalar oval_start_index_to_angle(unsigned start) {
3530 switch (start) {
3531 case 0:
3532 return 270.f;
3533 case 1:
3534 return 0.f;
3535 case 2:
3536 return 90.f;
3537 case 3:
3538 return 180.f;
3539 default:
3540 return -1.f;
3541 }
3542}
3543
3544static inline SkScalar canonical_start_angle(float angle) {
3545 while (angle < 0.f) {
3546 angle += 360.f;
3547 }
3548 while (angle >= 360.f) {
3549 angle -= 360.f;
3550 }
3551 return angle;
3552}
3553
3554static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3555 const SkPath& path) {
3556 SkRect r = SkRect::MakeEmpty();
3557 SkPath::Direction d = SkPath::kCCW_Direction;
3558 unsigned s = ~0U;
3559 bool isOval = path.isOval(&r, &d, &s);
3560 REPORTER_ASSERT(reporter, isOval);
3561 SkPath recreatedPath;
3562 recreatedPath.addOval(r, d, s);
3563 REPORTER_ASSERT(reporter, path == recreatedPath);
3564 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3565 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3566}
3567
3568static void test_arc_ovals(skiatest::Reporter* reporter) {
3569 SkRect oval = SkRect::MakeWH(10, 20);
3570 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3571 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3572 SkPath path;
3573 path.addArc(oval, start, sweep);
3574 // SkPath's interfaces for inserting and extracting ovals only allow contours
3575 // to start at multiples of 90 degrees.
3576 if (std::fmod(start, 90.f) == 0) {
3577 check_oval_arc(reporter, start, sweep, path);
3578 } else {
3579 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3580 }
3581 }
3582 // Test start angles that are nearly at valid oval start angles.
3583 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3584 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3585 SkPath path;
3586 path.addArc(oval, start + delta, sweep);
3587 check_oval_arc(reporter, start, sweep, path);
3588 }
3589 }
3590 }
3591}
3592
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003593static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3594 SkScalar x0, SkScalar y0) {
3595 SkPoint pts[4];
3596 SkPath::Verb v = iter->next(pts);
3597 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3598 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3599 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3600}
3601
3602static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3603 SkScalar x1, SkScalar y1) {
3604 SkPoint pts[4];
3605 SkPath::Verb v = iter->next(pts);
3606 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3607 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3608 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3609}
3610
3611static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3612 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3613 SkPoint pts[4];
3614 SkPath::Verb v = iter->next(pts);
3615 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3616 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3617 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3618 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3619 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3620}
3621
3622static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3623 SkPoint pts[4];
3624 SkPath::Verb v = iter->next(pts);
3625 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3626}
3627
3628static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3629 check_done(reporter, p, iter);
3630 p->reset();
3631}
3632
3633static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3634 SkScalar x0, SkScalar y0) {
3635 SkPath::RawIter iter(*p);
3636 check_move(reporter, &iter, x0, y0);
3637 check_done_and_reset(reporter, p, &iter);
3638}
3639
3640static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3641 SkScalar x1, SkScalar y1) {
3642 SkPath::RawIter iter(*p);
3643 check_move(reporter, &iter, 0, 0);
3644 check_line(reporter, &iter, x1, y1);
3645 check_done_and_reset(reporter, p, &iter);
3646}
3647
3648static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3649 SkScalar x1, SkScalar y1) {
3650 SkPath::RawIter iter(*p);
3651 check_move(reporter, &iter, 0, 0);
3652 check_line(reporter, &iter, x1, y1);
3653 check_done(reporter, p, &iter);
3654}
3655
3656static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3657 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3658 SkPath::RawIter iter(*p);
3659 check_move(reporter, &iter, 0, 0);
3660 check_line(reporter, &iter, x1, y1);
3661 check_line(reporter, &iter, x2, y2);
3662 check_done_and_reset(reporter, p, &iter);
3663}
3664
3665static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3666 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3667 SkPath::RawIter iter(*p);
3668 check_move(reporter, &iter, 0, 0);
3669 check_quad(reporter, &iter, x1, y1, x2, y2);
3670 check_done_and_reset(reporter, p, &iter);
3671}
3672
reedd5d27d92015-02-09 13:54:43 -08003673static bool nearly_equal(const SkRect& a, const SkRect& b) {
3674 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3675 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3676 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3677 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3678}
3679
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003680static void test_arcTo(skiatest::Reporter* reporter) {
3681 SkPath p;
3682 p.arcTo(0, 0, 1, 2, 1);
3683 check_path_is_line_and_reset(reporter, &p, 0, 0);
3684 p.arcTo(1, 2, 1, 2, 1);
3685 check_path_is_line_and_reset(reporter, &p, 1, 2);
3686 p.arcTo(1, 2, 3, 4, 0);
3687 check_path_is_line_and_reset(reporter, &p, 1, 2);
3688 p.arcTo(1, 2, 0, 0, 1);
3689 check_path_is_line_and_reset(reporter, &p, 1, 2);
3690 p.arcTo(1, 0, 1, 1, 1);
3691 SkPoint pt;
3692 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3693 p.reset();
3694 p.arcTo(1, 0, 1, -1, 1);
3695 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3696 p.reset();
3697 SkRect oval = {1, 2, 3, 4};
3698 p.arcTo(oval, 0, 0, true);
3699 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3700 p.arcTo(oval, 0, 0, false);
3701 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3702 p.arcTo(oval, 360, 0, true);
3703 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3704 p.arcTo(oval, 360, 0, false);
3705 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003706
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003707 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003708 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003709 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003710 sweep += delta;
3711 delta /= 2;
3712 }
3713 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003714 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003715 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003716 sweep -= delta;
3717 delta /= 2;
3718 }
3719 SkRect noOvalWidth = {1, 2, 0, 3};
3720 p.reset();
3721 p.arcTo(noOvalWidth, 0, 360, false);
3722 REPORTER_ASSERT(reporter, p.isEmpty());
3723
3724 SkRect noOvalHeight = {1, 2, 3, 1};
3725 p.reset();
3726 p.arcTo(noOvalHeight, 0, 360, false);
3727 REPORTER_ASSERT(reporter, p.isEmpty());
3728}
3729
3730static void test_addPath(skiatest::Reporter* reporter) {
3731 SkPath p, q;
3732 p.lineTo(1, 2);
3733 q.moveTo(4, 4);
3734 q.lineTo(7, 8);
3735 q.conicTo(8, 7, 6, 5, 0.5f);
3736 q.quadTo(6, 7, 8, 6);
3737 q.cubicTo(5, 6, 7, 8, 7, 5);
3738 q.close();
3739 p.addPath(q, -4, -4);
3740 SkRect expected = {0, 0, 4, 4};
3741 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3742 p.reset();
3743 p.reverseAddPath(q);
3744 SkRect reverseExpected = {4, 4, 8, 8};
3745 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3746}
3747
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003748static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3749 SkPath p, q;
3750 if (explicitMoveTo) {
3751 p.moveTo(1, 1);
3752 }
3753 p.lineTo(1, 2);
3754 if (explicitMoveTo) {
3755 q.moveTo(2, 1);
3756 }
3757 q.lineTo(2, 2);
3758 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3759 uint8_t verbs[4];
3760 int verbcount = p.getVerbs(verbs, 4);
3761 REPORTER_ASSERT(reporter, verbcount == 4);
3762 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3763 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3764 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3765 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3766}
3767
3768static void test_extendClosedPath(skiatest::Reporter* reporter) {
3769 SkPath p, q;
3770 p.moveTo(1, 1);
3771 p.lineTo(1, 2);
3772 p.lineTo(2, 2);
3773 p.close();
3774 q.moveTo(2, 1);
3775 q.lineTo(2, 3);
3776 p.addPath(q, SkPath::kExtend_AddPathMode);
3777 uint8_t verbs[7];
3778 int verbcount = p.getVerbs(verbs, 7);
3779 REPORTER_ASSERT(reporter, verbcount == 7);
3780 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3781 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3782 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3783 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3784 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3785 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3786 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3787
3788 SkPoint pt;
3789 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3790 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3791 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3792}
3793
3794static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3795 SkPath p, q, r;
3796 // case 1: dst is empty
3797 p.moveTo(2, 1);
3798 p.lineTo(2, 3);
3799 q.addPath(p, mode);
3800 REPORTER_ASSERT(reporter, q == p);
3801 // case 2: src is empty
3802 p.addPath(r, mode);
3803 REPORTER_ASSERT(reporter, q == p);
3804 // case 3: src and dst are empty
3805 q.reset();
3806 q.addPath(r, mode);
3807 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003808}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003809
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003810static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3811 SkPath p;
3812 p.conicTo(1, 2, 3, 4, -1);
3813 check_path_is_line_and_reset(reporter, &p, 3, 4);
3814 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3815 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3816 p.conicTo(1, 2, 3, 4, 1);
3817 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3818}
3819
3820static void test_get_point(skiatest::Reporter* reporter) {
3821 SkPath p;
3822 SkPoint pt = p.getPoint(0);
3823 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003824 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003825 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3826 p.setLastPt(10, 10);
3827 pt = p.getPoint(0);
3828 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003829 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003830 p.rMoveTo(10, 10);
3831 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3832}
3833
3834static void test_contains(skiatest::Reporter* reporter) {
3835 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003836 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3837 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
3838 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
3839 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3840 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
3841 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 -08003842 // this may return true or false, depending on the platform's numerics, but it should not crash
3843 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003844
3845 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003846 p.setFillType(SkPath::kInverseWinding_FillType);
3847 REPORTER_ASSERT(reporter, p.contains(0, 0));
3848 p.setFillType(SkPath::kWinding_FillType);
3849 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3850 p.moveTo(4, 4);
3851 p.lineTo(6, 8);
3852 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003853 // test on edge
3854 REPORTER_ASSERT(reporter, p.contains(6, 4));
3855 REPORTER_ASSERT(reporter, p.contains(5, 6));
3856 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003857 // test quick reject
3858 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3859 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3860 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3861 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3862 // test various crossings in x
3863 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3864 REPORTER_ASSERT(reporter, p.contains(6, 7));
3865 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3866 p.reset();
3867 p.moveTo(4, 4);
3868 p.lineTo(8, 6);
3869 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003870 // test on edge
3871 REPORTER_ASSERT(reporter, p.contains(4, 6));
3872 REPORTER_ASSERT(reporter, p.contains(6, 5));
3873 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003874 // test various crossings in y
3875 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3876 REPORTER_ASSERT(reporter, p.contains(7, 6));
3877 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003878 p.reset();
3879 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003880 p.lineTo(8, 4);
3881 p.lineTo(8, 8);
3882 p.lineTo(4, 8);
3883 // test on vertices
3884 REPORTER_ASSERT(reporter, p.contains(4, 4));
3885 REPORTER_ASSERT(reporter, p.contains(8, 4));
3886 REPORTER_ASSERT(reporter, p.contains(8, 8));
3887 REPORTER_ASSERT(reporter, p.contains(4, 8));
3888 p.reset();
3889 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003890 p.lineTo(6, 8);
3891 p.lineTo(2, 8);
3892 // test on edge
3893 REPORTER_ASSERT(reporter, p.contains(5, 6));
3894 REPORTER_ASSERT(reporter, p.contains(4, 8));
3895 REPORTER_ASSERT(reporter, p.contains(3, 6));
3896 p.reset();
3897 p.moveTo(4, 4);
3898 p.lineTo(0, 6);
3899 p.lineTo(4, 8);
3900 // test on edge
3901 REPORTER_ASSERT(reporter, p.contains(2, 5));
3902 REPORTER_ASSERT(reporter, p.contains(2, 7));
3903 REPORTER_ASSERT(reporter, p.contains(4, 6));
3904 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3905 p.reset();
3906 p.moveTo(4, 0);
3907 p.lineTo(6, 4);
3908 p.lineTo(2, 4);
3909 p.moveTo(4, 0);
3910 p.lineTo(0, 8);
3911 p.lineTo(8, 8);
3912 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3913 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3914 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3915 REPORTER_ASSERT(reporter, p.contains(4, 4));
3916
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003917 // test quads
3918 p.reset();
3919 p.moveTo(4, 4);
3920 p.quadTo(6, 6, 8, 8);
3921 p.quadTo(6, 8, 4, 8);
3922 p.quadTo(4, 6, 4, 4);
3923 REPORTER_ASSERT(reporter, p.contains(5, 6));
3924 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003925 // test quad edge
3926 REPORTER_ASSERT(reporter, p.contains(5, 5));
3927 REPORTER_ASSERT(reporter, p.contains(5, 8));
3928 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003929 // test quad endpoints
3930 REPORTER_ASSERT(reporter, p.contains(4, 4));
3931 REPORTER_ASSERT(reporter, p.contains(8, 8));
3932 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003933
3934 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003935 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3936 p.moveTo(qPts[0]);
3937 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3938 p.quadTo(qPts[index], qPts[index + 1]);
3939 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003940 REPORTER_ASSERT(reporter, p.contains(5, 6));
3941 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003942 // test quad edge
3943 SkPoint halfway;
3944 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3945 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3946 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3947 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003948
caryclark9aacd902015-12-14 08:38:09 -08003949 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003950 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003951 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3952 p.moveTo(kPts[0]);
3953 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3954 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3955 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003956 REPORTER_ASSERT(reporter, p.contains(5, 6));
3957 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003958 // test conic edge
3959 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3960 SkConic conic(&kPts[index], 0.5f);
3961 halfway = conic.evalAt(0.5f);
3962 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3963 }
caryclark9cb5d752015-12-18 04:35:24 -08003964 // test conic end points
3965 REPORTER_ASSERT(reporter, p.contains(4, 4));
3966 REPORTER_ASSERT(reporter, p.contains(8, 8));
3967 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003968
3969 // test cubics
3970 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3971 for (int i = 0; i < 3; ++i) {
3972 p.reset();
3973 p.setFillType(SkPath::kEvenOdd_FillType);
3974 p.moveTo(pts[i].fX, pts[i].fY);
3975 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);
3976 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);
3977 p.close();
3978 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3979 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003980 // test cubic edge
3981 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3982 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3983 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3984 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003985 // test cubic end points
3986 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3987 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3988 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003989 }
3990}
3991
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003992class PathRefTest_Private {
3993public:
3994 static void TestPathRef(skiatest::Reporter* reporter) {
3995 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003996
halcanary385fe4d2015-08-26 13:07:48 -07003997 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003998
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003999 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004000
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004001 {
4002 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4003 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4004 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4005 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4006 for (int i = 0; i < kRepeatCnt; ++i) {
4007 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4008 }
4009 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004010 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004011
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004012 {
4013 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4014 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4015 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4016 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4017 for (int i = 0; i < kRepeatCnt; ++i) {
4018 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4019 }
4020 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004021 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004022
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004023 {
4024 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4025 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4026 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4027 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4028 for (int i = 0; i < kRepeatCnt; ++i) {
4029 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4030 }
4031 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004032 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004033
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004034 {
halcanary96fcdcc2015-08-27 07:41:13 -07004035 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004036 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4037 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4038 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4039 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4040 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004041 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004042 for (int i = 0; i < kRepeatCnt; ++i) {
4043 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4044 }
4045 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004046 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004047
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004048 {
4049 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4050 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4051 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4052 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4053 for (int i = 0; i < kRepeatCnt; ++i) {
4054 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4055 }
4056 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004057 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004058 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004059};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004060
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004061static void test_operatorEqual(skiatest::Reporter* reporter) {
4062 SkPath a;
4063 SkPath b;
4064 REPORTER_ASSERT(reporter, a == a);
4065 REPORTER_ASSERT(reporter, a == b);
4066 a.setFillType(SkPath::kInverseWinding_FillType);
4067 REPORTER_ASSERT(reporter, a != b);
4068 a.reset();
4069 REPORTER_ASSERT(reporter, a == b);
4070 a.lineTo(1, 1);
4071 REPORTER_ASSERT(reporter, a != b);
4072 a.reset();
4073 REPORTER_ASSERT(reporter, a == b);
4074 a.lineTo(1, 1);
4075 b.lineTo(1, 2);
4076 REPORTER_ASSERT(reporter, a != b);
4077 a.reset();
4078 a.lineTo(1, 2);
4079 REPORTER_ASSERT(reporter, a == b);
4080}
4081
caryclark66a5d8b2014-06-24 08:30:15 -07004082static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004083 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004084 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004085 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004086 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004087 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004088 if (strlen(str) > 0) {
4089 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4090 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004091 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004092 }
caryclark66a5d8b2014-06-24 08:30:15 -07004093}
4094
4095static void test_dump(skiatest::Reporter* reporter) {
4096 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07004097 compare_dump(reporter, p, false, false, "");
4098 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07004099 p.moveTo(1, 2);
4100 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07004101 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4102 "path.lineTo(3, 4);\n");
4103 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
4104 "path.lineTo(3, 4);\n"
4105 "path.lineTo(1, 2);\n"
4106 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004107 p.reset();
4108 p.moveTo(1, 2);
4109 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07004110 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4111 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004112 p.reset();
4113 p.moveTo(1, 2);
4114 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07004115 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4116 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004117 p.reset();
4118 p.moveTo(1, 2);
4119 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07004120 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4121 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4122 p.reset();
4123 p.moveTo(1, 2);
4124 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004125 compare_dump(reporter, p, false, true,
4126 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4127 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004128 p.reset();
4129 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4130 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4131 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4132 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004133}
4134
senorblanco84cd6212015-08-04 10:01:58 -07004135namespace {
4136
4137class ChangeListener : public SkPathRef::GenIDChangeListener {
4138public:
4139 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4140 virtual ~ChangeListener() {}
4141 void onChange() override {
4142 *fChanged = true;
4143 }
4144private:
4145 bool* fChanged;
4146};
4147
4148}
4149
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004150class PathTest_Private {
4151public:
4152 static void TestPathTo(skiatest::Reporter* reporter) {
4153 SkPath p, q;
4154 p.lineTo(4, 4);
4155 p.reversePathTo(q);
4156 check_path_is_line(reporter, &p, 4, 4);
4157 q.moveTo(-4, -4);
4158 p.reversePathTo(q);
4159 check_path_is_line(reporter, &p, 4, 4);
4160 q.lineTo(7, 8);
4161 q.conicTo(8, 7, 6, 5, 0.5f);
4162 q.quadTo(6, 7, 8, 6);
4163 q.cubicTo(5, 6, 7, 8, 7, 5);
4164 q.close();
4165 p.reversePathTo(q);
4166 SkRect reverseExpected = {-4, -4, 8, 8};
4167 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4168 }
senorblanco84cd6212015-08-04 10:01:58 -07004169
4170 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4171 SkPath p;
4172
4173 bool changed = false;
4174 p.moveTo(0, 0);
4175
4176 // Check that listener is notified on moveTo().
4177
halcanary385fe4d2015-08-26 13:07:48 -07004178 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004179 REPORTER_ASSERT(reporter, !changed);
4180 p.moveTo(10, 0);
4181 REPORTER_ASSERT(reporter, changed);
4182
4183 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004184 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004185 REPORTER_ASSERT(reporter, !changed);
4186 p.lineTo(20, 0);
4187 REPORTER_ASSERT(reporter, changed);
4188
4189 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004190 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004191 REPORTER_ASSERT(reporter, !changed);
4192 p.reset();
4193 REPORTER_ASSERT(reporter, changed);
4194
4195 p.moveTo(0, 0);
4196
4197 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004198 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004199 REPORTER_ASSERT(reporter, !changed);
4200 p.rewind();
4201 REPORTER_ASSERT(reporter, changed);
4202
4203 // Check that listener is notified when pathref is deleted.
4204 {
4205 SkPath q;
4206 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004207 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004208 REPORTER_ASSERT(reporter, !changed);
4209 }
4210 // q went out of scope.
4211 REPORTER_ASSERT(reporter, changed);
4212 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004213};
4214
reed158fabb2016-07-20 10:06:59 -07004215static void test_crbug_629455(skiatest::Reporter* reporter) {
4216 SkPath path;
4217 path.moveTo(0, 0);
4218 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4219 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4220 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4221// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4222 path.lineTo(0, 0);
4223
4224 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4225 SkPaint paint;
4226 paint.setAntiAlias(true);
4227 surface->getCanvas()->drawPath(path, paint);
4228}
4229
caryclark8e7b19d2016-02-18 04:11:48 -08004230static void test_interp(skiatest::Reporter* reporter) {
4231 SkPath p1, p2, out;
4232 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4233 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4234 REPORTER_ASSERT(reporter, p1 == out);
4235 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4236 REPORTER_ASSERT(reporter, p1 == out);
4237 p1.moveTo(0, 2);
4238 p1.lineTo(0, 4);
4239 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4240 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4241 p2.moveTo(6, 0);
4242 p2.lineTo(8, 0);
4243 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4244 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4245 REPORTER_ASSERT(reporter, p2 == out);
4246 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4247 REPORTER_ASSERT(reporter, p1 == out);
4248 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4249 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4250 p1.reset();
4251 p1.moveTo(4, 4);
4252 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4253 p2.reset();
4254 p2.moveTo(4, 2);
4255 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4256 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4257 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4258 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4259 p2.reset();
4260 p2.moveTo(4, 2);
4261 p2.conicTo(6, 3, 6, 5, 1);
4262 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4263 p2.reset();
4264 p2.moveTo(4, 4);
4265 p2.conicTo(5, 4, 5, 5, 0.5f);
4266 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4267}
4268
4269DEF_TEST(PathInterp, reporter) {
4270 test_interp(reporter);
4271}
4272
caryclark9aacd902015-12-14 08:38:09 -08004273DEF_TEST(PathContains, reporter) {
4274 test_contains(reporter);
4275}
4276
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004277DEF_TEST(Paths, reporter) {
caryclarke97fe832016-09-06 08:54:10 -07004278 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004279 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004280 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004281 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004282 test_path_crbug364224();
4283
bungeman@google.coma5809a32013-06-21 15:13:34 +00004284 SkTSize<SkScalar>::Make(3,4);
4285
4286 SkPath p, empty;
4287 SkRect bounds, bounds2;
4288 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004289
reed@android.comd252db02009-04-01 18:31:44 +00004290 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004291
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004292 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4293 SkPath& self = p;
4294 p = self;
4295
4296 // this triggers a code path in SkPath::swap which is otherwise unexercised
4297 p.swap(self);
4298
reed@android.com3abec1d2009-03-02 05:36:20 +00004299 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004300
reed@android.com6b82d1a2009-06-03 02:35:01 +00004301 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4302 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004303 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004304 REPORTER_ASSERT(reporter,
4305 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004306 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004307
reed@android.com6b82d1a2009-06-03 02:35:01 +00004308 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004309 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004310
reed@android.com6b82d1a2009-06-03 02:35:01 +00004311 p.addOval(bounds);
4312 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004313 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004314
bungeman@google.coma5809a32013-06-21 15:13:34 +00004315 p.rewind();
4316 test_empty(reporter, p);
4317
reed@android.com3abec1d2009-03-02 05:36:20 +00004318 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004319 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004320 // we have only lines
4321 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004322 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004323
bungeman@google.coma5809a32013-06-21 15:13:34 +00004324 REPORTER_ASSERT(reporter, p != empty);
4325 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004326
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004327 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004328 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4329 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004330 SkPoint pts[4];
4331 int count = p.getPoints(pts, 4);
4332 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004333 uint8_t verbs[6];
4334 verbs[5] = 0xff;
4335 p.getVerbs(verbs, 5);
4336 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4337 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4338 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4339 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4340 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4341 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004342 bounds2.set(pts, 4);
4343 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004344
reed@android.com3abec1d2009-03-02 05:36:20 +00004345 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4346 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004347 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004348
halcanary96fcdcc2015-08-27 07:41:13 -07004349 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004350 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004351 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4352 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004353
reed@android.com3abec1d2009-03-02 05:36:20 +00004354 // now force p to not be a rect
4355 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4356 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004357 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004358
robertphillipsfe7c4272014-12-29 11:36:39 -08004359 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4360 // path has a trailing moveTo. Please see crbug.com\445368)
4361 {
4362 SkRect r;
4363 p.reset();
4364 p.addRect(bounds);
4365 REPORTER_ASSERT(reporter, p.isRect(&r));
4366 REPORTER_ASSERT(reporter, r == bounds);
4367 // add a moveTo outside of our bounds
4368 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4369 REPORTER_ASSERT(reporter, p.isRect(&r));
4370 REPORTER_ASSERT(reporter, r == bounds);
4371 }
4372
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004373 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004374 test_isLine(reporter);
4375 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004376 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004377 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004378 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004379 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004380 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004381 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004382 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004383 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004384 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004385 test_flattening(reporter);
4386 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004387 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004388 test_iter(reporter);
4389 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004390 test_circle(reporter);
4391 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004392 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004393 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004394 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004395 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004396 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004397 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004398 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004399 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004400 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004401 test_tricky_cubic();
4402 test_clipped_cubic();
4403 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004404 test_crbug_493450(reporter);
4405 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004406 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004407 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004408 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004409 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004410 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004411 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004412 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004413 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004414 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004415 test_arcTo(reporter);
4416 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004417 test_addPathMode(reporter, false, false);
4418 test_addPathMode(reporter, true, false);
4419 test_addPathMode(reporter, false, true);
4420 test_addPathMode(reporter, true, true);
4421 test_extendClosedPath(reporter);
4422 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4423 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004424 test_conicTo_special_case(reporter);
4425 test_get_point(reporter);
4426 test_contains(reporter);
4427 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004428 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004429 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004430 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004431 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004432 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004433 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004434 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004435 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004436 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004437}