blob: 85bd80487405702838c19b0e48999e8380f1b8ac [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
bsalomon1978ce22016-05-31 10:42:16 -07008#include <cmath>
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -080010#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000011#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070014#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000015#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000017#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000018#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000019#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070020#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070021#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000022#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000023#include "SkTypes.h"
24#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000026
reed5bcbe912014-12-15 12:28:33 -080027static void set_radii(SkVector radii[4], int index, float rad) {
28 sk_bzero(radii, sizeof(SkVector) * 4);
29 radii[index].set(rad, rad);
30}
31
32static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33 const SkVector radii[4]) {
34 SkRRect rrect;
35 rrect.setRectRadii(bounds, radii);
36 REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38 SkPath path;
39 // this line should not assert in the debug build (from validate)
40 path.addRRect(rrect);
41 REPORTER_ASSERT(reporter, bounds == path.getBounds());
42}
43
caryclark5ccef572015-03-02 10:07:56 -080044static void test_skbug_3469(skiatest::Reporter* reporter) {
45 SkPath path;
46 path.moveTo(20, 20);
47 path.quadTo(20, 50, 80, 50);
48 path.quadTo(20, 50, 20, 80);
49 REPORTER_ASSERT(reporter, !path.isConvex());
50}
51
reed5bcbe912014-12-15 12:28:33 -080052static void test_skbug_3239(skiatest::Reporter* reporter) {
53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
56
57 const float rad = 33436320;
58
59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62 SkVector radii[4];
63 for (int i = 0; i < 4; ++i) {
64 set_radii(radii, i, rad);
65 test_add_rrect(reporter, rectx, radii);
66 test_add_rrect(reporter, recty, radii);
67 }
68}
69
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000070static void make_path_crbug364224(SkPath* path) {
71 path->reset();
72 path->moveTo(3.747501373f, 2.724499941f);
73 path->lineTo(3.747501373f, 3.75f);
74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75 path->lineTo(0.7475013733f, 4.0f);
76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77 path->lineTo(0.4975013733f, 1.0f);
78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79 path->lineTo(3.497501373f, 0.75f);
80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81 path->lineTo(3.715001345f, 0.5512499809f);
82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83 path->lineTo(0.7475013733f, 0.4999999702f);
84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85 path->lineTo(0.2475013733f, 3.75f);
86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87 path->lineTo(3.497501373f, 4.25f);
88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89 path->lineTo(3.997501373f, 2.474750042f);
90 path->lineTo(3.747501373f, 2.724499941f);
91 path->close();
92}
93
94static void make_path_crbug364224_simplified(SkPath* path) {
95 path->moveTo(3.747501373f, 2.724499941f);
96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97 path->close();
98}
99
reedff863bc2016-08-17 07:37:57 -0700100static void test_sect_with_horizontal_needs_pinning() {
101 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
102 // intersection.
103 SkPath path;
104 path.reset();
105 path.moveTo(-540000, -720000);
106 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
107 path.lineTo(1, 1);
108
109 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
110 SkPaint paint;
111 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
112}
113
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000114static void test_path_crbug364224() {
115 SkPath path;
116 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700117 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000118 SkCanvas* canvas = surface->getCanvas();
119
120 make_path_crbug364224_simplified(&path);
121 canvas->drawPath(path, paint);
122
123 make_path_crbug364224(&path);
124 canvas->drawPath(path, paint);
125}
126
caryclark3cebe242016-08-23 09:41:00 -0700127// this is a unit test instead of a GM because it doesn't draw anything
128static void test_fuzz_crbug_638223() {
129 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
130 SkCanvas* canvas = surface->getCanvas();
131 SkPath path;
132 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
133 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
134 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
135 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
caryclarke97fe832016-09-06 08:54:10 -0700141static void test_fuzz_crbug_643933() {
142 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
143 SkCanvas* canvas = surface->getCanvas();
144 SkPaint paint;
145 paint.setAntiAlias(true);
146 SkPath path;
147 path.moveTo(0, 0);
148 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
149 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
150 SkBits2Float(0x721aee0c)); // 3.0687e+30f
151 canvas->drawPath(path, paint);
152 path.reset();
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
155 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
156 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
157 canvas->drawPath(path, paint);
158}
159
caryclarkbac10462016-09-22 10:24:59 -0700160static void test_fuzz_crbug_647922() {
161 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
162 SkCanvas* canvas = surface->getCanvas();
163 SkPaint paint;
164 paint.setAntiAlias(true);
165 SkPath path;
166 path.moveTo(0, 0);
167 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
168 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
169 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
170 canvas->drawPath(path, paint);
171}
172
liyuqian041da382016-11-11 09:59:51 -0800173static void test_fuzz_crbug_662780() {
174 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
175 SkCanvas* canvas = surface->getCanvas();
176 SkPaint paint;
177 paint.setAntiAlias(true);
178 SkPath path;
179 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
180 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
181 // 8, 8, 8.00002f, 8, 0.707107f
182 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
183 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
184 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
185 // 308, 8, 308, 8, 0.707107f
186 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
187 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
188 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
189 // 308, 158, 308, 158, 0.707107f
190 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
191 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
192 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
193 // 8, 158, 8, 158, 0.707107f
194 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
195 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
196 path.close();
197 canvas->clipPath(path, true);
198 canvas->drawRectCoords(0, 0, 250, 250, paint);
199}
200
piotaixrfac4e0e2014-08-26 11:59:04 -0700201/**
202 * In debug mode, this path was causing an assertion to fail in
203 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
204 */
205static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
206 SkPoint orig, p1, p2, p3;
207 orig = SkPoint::Make(1.f, 1.f);
208 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
209 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
210 p3 = SkPoint::Make(2.f, 2.f);
211
212 path->reset();
213 path->moveTo(orig);
214 path->cubicTo(p1, p2, p3);
215 path->close();
216}
217
218static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
219 SkPath path;
220 make_path_crbugskia2820(&path, reporter);
221
222 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
223 stroke.setStrokeStyle(2 * SK_Scalar1);
224 stroke.applyToPath(&path, path);
225}
226
reed@google.comcc8be772013-10-15 15:35:29 +0000227static void make_path0(SkPath* path) {
228 // from * https://code.google.com/p/skia/issues/detail?id=1706
229
230 path->moveTo(146.939f, 1012.84f);
231 path->lineTo(181.747f, 1009.18f);
232 path->lineTo(182.165f, 1013.16f);
233 path->lineTo(147.357f, 1016.82f);
234 path->lineTo(146.939f, 1012.84f);
235 path->close();
236}
237
238static void make_path1(SkPath* path) {
239 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
240}
241
242typedef void (*PathProc)(SkPath*);
243
244/*
245 * Regression test: we used to crash (overwrite internal storage) during
246 * construction of the region when the path was INVERSE. That is now fixed,
247 * so test these regions (which used to assert/crash).
248 *
249 * https://code.google.com/p/skia/issues/detail?id=1706
250 */
251static void test_path_to_region(skiatest::Reporter* reporter) {
252 PathProc procs[] = {
253 make_path0,
254 make_path1,
255 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000256
reed@google.comcc8be772013-10-15 15:35:29 +0000257 SkRegion clip;
258 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000259
reed@google.comcc8be772013-10-15 15:35:29 +0000260 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
261 SkPath path;
262 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000263
reed@google.comcc8be772013-10-15 15:35:29 +0000264 SkRegion rgn;
265 rgn.setPath(path, clip);
266 path.toggleInverseFillType();
267 rgn.setPath(path, clip);
268 }
269}
270
halcanary4dbbd042016-06-07 17:21:10 -0700271#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000272 #define SUPPRESS_VISIBILITY_WARNING
273#else
274 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
275#endif
276
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000277static void test_path_close_issue1474(skiatest::Reporter* reporter) {
278 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
279 // are relative to the point we close to, not relative to the point we close from.
280 SkPath path;
281 SkPoint last;
282
283 // Test rLineTo().
284 path.rLineTo(0, 100);
285 path.rLineTo(100, 0);
286 path.close(); // Returns us back to 0,0.
287 path.rLineTo(50, 50); // This should go to 50,50.
288
289 path.getLastPt(&last);
290 REPORTER_ASSERT(reporter, 50 == last.fX);
291 REPORTER_ASSERT(reporter, 50 == last.fY);
292
293 // Test rQuadTo().
294 path.rewind();
295 path.rLineTo(0, 100);
296 path.rLineTo(100, 0);
297 path.close();
298 path.rQuadTo(50, 50, 75, 75);
299
300 path.getLastPt(&last);
301 REPORTER_ASSERT(reporter, 75 == last.fX);
302 REPORTER_ASSERT(reporter, 75 == last.fY);
303
304 // Test rConicTo().
305 path.rewind();
306 path.rLineTo(0, 100);
307 path.rLineTo(100, 0);
308 path.close();
309 path.rConicTo(50, 50, 85, 85, 2);
310
311 path.getLastPt(&last);
312 REPORTER_ASSERT(reporter, 85 == last.fX);
313 REPORTER_ASSERT(reporter, 85 == last.fY);
314
315 // Test rCubicTo().
316 path.rewind();
317 path.rLineTo(0, 100);
318 path.rLineTo(100, 0);
319 path.close();
320 path.rCubicTo(50, 50, 85, 85, 95, 95);
321
322 path.getLastPt(&last);
323 REPORTER_ASSERT(reporter, 95 == last.fX);
324 REPORTER_ASSERT(reporter, 95 == last.fY);
325}
326
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000327static void test_gen_id(skiatest::Reporter* reporter) {
328 SkPath a, b;
329 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
330
331 a.moveTo(0, 0);
332 const uint32_t z = a.getGenerationID();
333 REPORTER_ASSERT(reporter, z != b.getGenerationID());
334
335 a.reset();
336 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
337
338 a.moveTo(1, 1);
339 const uint32_t y = a.getGenerationID();
340 REPORTER_ASSERT(reporter, z != y);
341
342 b.moveTo(2, 2);
343 const uint32_t x = b.getGenerationID();
344 REPORTER_ASSERT(reporter, x != y && x != z);
345
346 a.swap(b);
347 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
348
349 b = a;
350 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
351
352 SkPath c(a);
353 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
354
355 c.lineTo(3, 3);
356 const uint32_t w = c.getGenerationID();
357 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
358 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
359 REPORTER_ASSERT(reporter, w != x);
360
djsollen50da1d82015-02-17 09:09:53 -0800361#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000362 static bool kExpectGenIDToIgnoreFill = false;
363#else
364 static bool kExpectGenIDToIgnoreFill = true;
365#endif
366
367 c.toggleInverseFillType();
368 const uint32_t v = c.getGenerationID();
369 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
370
371 c.rewind();
372 REPORTER_ASSERT(reporter, v != c.getGenerationID());
373}
374
reed@google.com3eff3592013-05-08 21:08:21 +0000375// This used to assert in the debug build, as the edges did not all line-up.
376static void test_bad_cubic_crbug234190() {
377 SkPath path;
378 path.moveTo(13.8509f, 3.16858f);
379 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
380 -2.38991e+08f, -4.26573e+08f,
381 -2.41016e+08f, -4.30188e+08f);
382
383 SkPaint paint;
384 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700385 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000386 surface->getCanvas()->drawPath(path, paint);
387}
388
reed@google.com7a90daf2013-04-10 18:44:00 +0000389static void test_bad_cubic_crbug229478() {
390 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000391 { 4595.91064f, -11596.9873f },
392 { 4597.2168f, -11595.9414f },
393 { 4598.52344f, -11594.8955f },
394 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000395 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000396
reed@google.com7a90daf2013-04-10 18:44:00 +0000397 SkPath path;
398 path.moveTo(pts[0]);
399 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000400
reed@google.com7a90daf2013-04-10 18:44:00 +0000401 SkPaint paint;
402 paint.setStyle(SkPaint::kStroke_Style);
403 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000404
reed@google.com7a90daf2013-04-10 18:44:00 +0000405 SkPath dst;
406 // Before the fix, this would infinite-recurse, and run out of stack
407 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700408 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000409}
410
reed@google.com64d62952013-01-18 17:49:28 +0000411static void build_path_170666(SkPath& path) {
412 path.moveTo(17.9459f, 21.6344f);
413 path.lineTo(139.545f, -47.8105f);
414 path.lineTo(139.545f, -47.8105f);
415 path.lineTo(131.07f, -47.3888f);
416 path.lineTo(131.07f, -47.3888f);
417 path.lineTo(122.586f, -46.9532f);
418 path.lineTo(122.586f, -46.9532f);
419 path.lineTo(18076.6f, 31390.9f);
420 path.lineTo(18076.6f, 31390.9f);
421 path.lineTo(18085.1f, 31390.5f);
422 path.lineTo(18085.1f, 31390.5f);
423 path.lineTo(18076.6f, 31390.9f);
424 path.lineTo(18076.6f, 31390.9f);
425 path.lineTo(17955, 31460.3f);
426 path.lineTo(17955, 31460.3f);
427 path.lineTo(17963.5f, 31459.9f);
428 path.lineTo(17963.5f, 31459.9f);
429 path.lineTo(17971.9f, 31459.5f);
430 path.lineTo(17971.9f, 31459.5f);
431 path.lineTo(17.9551f, 21.6205f);
432 path.lineTo(17.9551f, 21.6205f);
433 path.lineTo(9.47091f, 22.0561f);
434 path.lineTo(9.47091f, 22.0561f);
435 path.lineTo(17.9459f, 21.6344f);
436 path.lineTo(17.9459f, 21.6344f);
437 path.close();path.moveTo(0.995934f, 22.4779f);
438 path.lineTo(0.986725f, 22.4918f);
439 path.lineTo(0.986725f, 22.4918f);
440 path.lineTo(17955, 31460.4f);
441 path.lineTo(17955, 31460.4f);
442 path.lineTo(17971.9f, 31459.5f);
443 path.lineTo(17971.9f, 31459.5f);
444 path.lineTo(18093.6f, 31390.1f);
445 path.lineTo(18093.6f, 31390.1f);
446 path.lineTo(18093.6f, 31390);
447 path.lineTo(18093.6f, 31390);
448 path.lineTo(139.555f, -47.8244f);
449 path.lineTo(139.555f, -47.8244f);
450 path.lineTo(122.595f, -46.9671f);
451 path.lineTo(122.595f, -46.9671f);
452 path.lineTo(0.995934f, 22.4779f);
453 path.lineTo(0.995934f, 22.4779f);
454 path.close();
455 path.moveTo(5.43941f, 25.5223f);
456 path.lineTo(798267, -28871.1f);
457 path.lineTo(798267, -28871.1f);
458 path.lineTo(3.12512e+06f, -113102);
459 path.lineTo(3.12512e+06f, -113102);
460 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
461 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
462 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
463 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
464 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
465 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
466 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
467 path.lineTo(2.78271e+08f, -1.00733e+07f);
468 path.lineTo(2.78271e+08f, -1.00733e+07f);
469 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
470 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
471 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
472 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
473 path.lineTo(2.77473e+08f, -1.00444e+07f);
474 path.lineTo(2.77473e+08f, -1.00444e+07f);
475 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
476 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
477 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
478 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
479 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
480 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
481 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
482 path.lineTo(798284, -28872);
483 path.lineTo(798284, -28872);
484 path.lineTo(22.4044f, 24.6677f);
485 path.lineTo(22.4044f, 24.6677f);
486 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
487 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
488 path.close();
489}
490
491static void build_path_simple_170666(SkPath& path) {
492 path.moveTo(126.677f, 24.1591f);
493 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
494}
495
496// This used to assert in the SK_DEBUG build, as the clip step would fail with
497// too-few interations in our cubic-line intersection code. That code now runs
498// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000499static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000500 SkPath path;
501 SkPaint paint;
502 paint.setAntiAlias(true);
503
reede8f30622016-03-23 18:59:25 -0700504 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000505
reed@google.com64d62952013-01-18 17:49:28 +0000506 build_path_simple_170666(path);
507 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000508
reed@google.com64d62952013-01-18 17:49:28 +0000509 build_path_170666(path);
510 surface->getCanvas()->drawPath(path, paint);
511}
512
caryclarke8c56662015-07-14 11:19:26 -0700513
514static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
515 SkScalar tx, SkScalar ty, SkScalar scale) {
516 SkPath smallPath;
517 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
518 bool smallConvex = smallPath.isConvex();
519 SkPath largePath;
520 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
521 SkMatrix matrix;
522 matrix.reset();
523 matrix.preTranslate(100, 100);
524 matrix.preScale(scale, scale);
525 largePath.transform(matrix);
526 bool largeConvex = largePath.isConvex();
527 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
528}
529
530static void test_crbug_493450(skiatest::Reporter* reporter) {
531 const char reducedCase[] =
532 "M0,0"
533 "L0.0002, 0"
534 "L0.0002, 0.0002"
535 "L0.0001, 0.0001"
536 "L0,0.0002"
537 "Z";
538 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
539 const char originalFiddleData[] =
540 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
541 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
542 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
543 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
544 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
545 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
546 826357.3384828606f);
547}
548
549static void test_crbug_495894(skiatest::Reporter* reporter) {
550 const char originalFiddleData[] =
551 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
552 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
553 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
554 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
555 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
556 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
557 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
558 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
559 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
560 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
561 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
562 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
563 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
564 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
565 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
566 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
567 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
568 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
569 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
570 "L-0.33993994441916414,-0.11288906492739594Z";
571 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
572 65536);
573}
574
mbarbella99600d02016-06-01 15:39:47 -0700575static void test_crbug_613918() {
576 SkPath path;
577 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
578 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
579 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
580
581 // This call could lead to an assert or uninitialized read due to a failure
582 // to check the return value from SkCubicClipper::ChopMonoAtY.
583 path.contains(-1.84817e-08f, 1.15465e-08f);
584}
585
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000586static void test_addrect(skiatest::Reporter* reporter) {
587 SkPath path;
588 path.lineTo(0, 0);
589 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700590 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000591
592 path.reset();
593 path.lineTo(FLT_EPSILON, FLT_EPSILON);
594 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700595 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000596
597 path.reset();
598 path.quadTo(0, 0, 0, 0);
599 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700600 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000601
602 path.reset();
603 path.conicTo(0, 0, 0, 0, 0.5f);
604 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700605 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000606
607 path.reset();
608 path.cubicTo(0, 0, 0, 0, 0, 0);
609 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700610 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000611}
612
reed@google.coma8790de2012-10-24 21:04:04 +0000613// Make sure we stay non-finite once we get there (unless we reset or rewind).
614static void test_addrect_isfinite(skiatest::Reporter* reporter) {
615 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000616
reed@google.coma8790de2012-10-24 21:04:04 +0000617 path.addRect(SkRect::MakeWH(50, 100));
618 REPORTER_ASSERT(reporter, path.isFinite());
619
620 path.moveTo(0, 0);
621 path.lineTo(SK_ScalarInfinity, 42);
622 REPORTER_ASSERT(reporter, !path.isFinite());
623
624 path.addRect(SkRect::MakeWH(50, 100));
625 REPORTER_ASSERT(reporter, !path.isFinite());
626
627 path.reset();
628 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000629
reed@google.coma8790de2012-10-24 21:04:04 +0000630 path.addRect(SkRect::MakeWH(50, 100));
631 REPORTER_ASSERT(reporter, path.isFinite());
632}
633
reed@google.com848148e2013-01-15 15:51:59 +0000634static void build_big_path(SkPath* path, bool reducedCase) {
635 if (reducedCase) {
636 path->moveTo(577330, 1971.72f);
637 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
638 } else {
639 path->moveTo(60.1631f, 7.70567f);
640 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
641 path->lineTo(577379, 1977.77f);
642 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
643 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
644 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
645 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
646 path->quadTo(577198, 1972, 577238, 1971.31f);
647 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
648 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
649 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
650 path->moveTo(306.718f, -32.912f);
651 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
652 }
653}
654
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000655static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700656 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000657
658 // This path used to assert, because our cubic-chopping code incorrectly
659 // moved control points after the chop. This test should be run in SK_DEBUG
660 // mode to ensure that we no long assert.
661 SkPath path;
662 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
663 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000664
reed@google.com848148e2013-01-15 15:51:59 +0000665 SkPaint paint;
666 for (int doAA = 0; doAA <= 1; ++doAA) {
667 paint.setAntiAlias(SkToBool(doAA));
668 surface->getCanvas()->drawPath(path, paint);
669 }
670 }
671}
672
reed3b2fb982015-07-29 08:37:13 -0700673static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
674 if (expected != bounds) {
675 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
676 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
677 expected.left(), expected.top(), expected.right(), expected.bottom());
678 }
679}
680
reed91f283b2015-07-28 06:00:50 -0700681static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
682 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700683#if 0
684 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
685 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700686 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
687
688 path.moveTo(-5, -8);
689 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
690
691 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
692 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
693
694 path.moveTo(1, 2);
695 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700696#else
697 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
698
699 path.moveTo(-5, -8); // should set the bounds
700 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
701
702 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
703 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
704
705 path.moveTo(1, 2); // don't expect this to have changed the bounds
706 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
707#endif
reed91f283b2015-07-28 06:00:50 -0700708}
709
reedb1b12f82016-07-13 10:56:53 -0700710#include "SkSurface.h"
711static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
712 SkPath path;
713 path.moveTo(0, 0);
714 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
715
716 SkPaint paint;
717 paint.setAntiAlias(true);
718
719 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
720 surf->getCanvas()->drawPath(path, paint);
721}
722
reed@google.com8cae8352012-09-14 15:18:41 +0000723// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
724// which triggered an assert, from a tricky cubic. This test replicates that
725// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
726// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000727static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000728 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000729 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
730 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
731 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
732 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000733 };
734
735 SkPath path;
736 path.moveTo(pts[0]);
737 path.cubicTo(pts[1], pts[2], pts[3]);
738
739 SkPaint paint;
740 paint.setAntiAlias(true);
741
reede8f30622016-03-23 18:59:25 -0700742 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000743}
reed@android.com3abec1d2009-03-02 05:36:20 +0000744
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000745// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
746//
747static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
748 SkPath path;
749 path.quadTo(157, 366, 286, 208);
750 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000751
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000752 SkMatrix matrix;
753 matrix.setScale(1000*1000, 1000*1000);
754
755 // Be sure that path::transform correctly updates isFinite and the bounds
756 // if the transformation overflows. The previous bug was that isFinite was
757 // set to true in this case, but the bounds were not set to empty (which
758 // they should be).
759 while (path.isFinite()) {
760 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
761 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
762 path.transform(matrix);
763 }
764 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
765
766 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
767 path.transform(matrix);
768 // we need to still be non-finite
769 REPORTER_ASSERT(reporter, !path.isFinite());
770 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
771}
772
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000773static void add_corner_arc(SkPath* path, const SkRect& rect,
774 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000775 int startAngle)
776{
777
778 SkScalar rx = SkMinScalar(rect.width(), xIn);
779 SkScalar ry = SkMinScalar(rect.height(), yIn);
780
781 SkRect arcRect;
782 arcRect.set(-rx, -ry, rx, ry);
783 switch (startAngle) {
784 case 0:
785 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
786 break;
787 case 90:
788 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
789 break;
790 case 180:
791 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
792 break;
793 case 270:
794 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
795 break;
796 default:
797 break;
798 }
799
800 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
801}
802
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000803static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000804 SkScalar xCorner, SkScalar yCorner) {
805 // we are lazy here and use the same x & y for each corner
806 add_corner_arc(path, r, xCorner, yCorner, 270);
807 add_corner_arc(path, r, xCorner, yCorner, 0);
808 add_corner_arc(path, r, xCorner, yCorner, 90);
809 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000810 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000811}
812
813// Chrome creates its own round rects with each corner possibly being different.
814// Performance will suffer if they are not convex.
815// Note: PathBench::ArbRoundRectBench performs almost exactly
816// the same test (but with drawing)
817static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000818 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000819 SkRect r;
820
821 for (int i = 0; i < 5000; ++i) {
822
robertphillips@google.com158618e2012-10-23 16:56:56 +0000823 SkScalar size = rand.nextUScalar1() * 30;
824 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000825 continue;
826 }
827 r.fLeft = rand.nextUScalar1() * 300;
828 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000829 r.fRight = r.fLeft + 2 * size;
830 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000831
832 SkPath temp;
833
834 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
835
836 REPORTER_ASSERT(reporter, temp.isConvex());
837 }
838}
839
robertphillips@google.com158618e2012-10-23 16:56:56 +0000840// Chrome will sometimes create a 0 radius round rect. The degenerate
841// quads prevent the path from being converted to a rect
842// Note: PathBench::ArbRoundRectBench performs almost exactly
843// the same test (but with drawing)
844static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000845 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000846 SkRect r;
847
848 for (int i = 0; i < 5000; ++i) {
849
850 SkScalar size = rand.nextUScalar1() * 30;
851 if (size < SK_Scalar1) {
852 continue;
853 }
854 r.fLeft = rand.nextUScalar1() * 300;
855 r.fTop = rand.nextUScalar1() * 300;
856 r.fRight = r.fLeft + 2 * size;
857 r.fBottom = r.fTop + 2 * size;
858
859 SkPath temp;
860
861 make_arb_round_rect(&temp, r, 0, 0);
862
robertphillips@google.com158618e2012-10-23 16:56:56 +0000863 SkRect result;
864 REPORTER_ASSERT(reporter, temp.isRect(&result));
865 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000866 }
867}
868
reed@google.com0bb18bb2012-07-26 15:20:36 +0000869static void test_rect_isfinite(skiatest::Reporter* reporter) {
870 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000871 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000872 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000873
reed@google.com0bb18bb2012-07-26 15:20:36 +0000874 SkRect r;
875 r.setEmpty();
876 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000877 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000878 REPORTER_ASSERT(reporter, !r.isFinite());
879 r.set(0, 0, nan, 0);
880 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000881
reed@google.com0bb18bb2012-07-26 15:20:36 +0000882 SkPoint pts[] = {
883 { 0, 0 },
884 { SK_Scalar1, 0 },
885 { 0, SK_Scalar1 },
886 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000887
reed@google.com0bb18bb2012-07-26 15:20:36 +0000888 bool isFine = r.setBoundsCheck(pts, 3);
889 REPORTER_ASSERT(reporter, isFine);
890 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891
reed@google.com0bb18bb2012-07-26 15:20:36 +0000892 pts[1].set(inf, 0);
893 isFine = r.setBoundsCheck(pts, 3);
894 REPORTER_ASSERT(reporter, !isFine);
895 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000896
reed@google.com0bb18bb2012-07-26 15:20:36 +0000897 pts[1].set(nan, 0);
898 isFine = r.setBoundsCheck(pts, 3);
899 REPORTER_ASSERT(reporter, !isFine);
900 REPORTER_ASSERT(reporter, r.isEmpty());
901}
902
903static void test_path_isfinite(skiatest::Reporter* reporter) {
904 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000905 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000906 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000907
reed@google.com0bb18bb2012-07-26 15:20:36 +0000908 SkPath path;
909 REPORTER_ASSERT(reporter, path.isFinite());
910
911 path.reset();
912 REPORTER_ASSERT(reporter, path.isFinite());
913
914 path.reset();
915 path.moveTo(SK_Scalar1, 0);
916 REPORTER_ASSERT(reporter, path.isFinite());
917
918 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000919 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000920 REPORTER_ASSERT(reporter, !path.isFinite());
921
922 path.reset();
923 path.moveTo(nan, 0);
924 REPORTER_ASSERT(reporter, !path.isFinite());
925}
926
927static void test_isfinite(skiatest::Reporter* reporter) {
928 test_rect_isfinite(reporter);
929 test_path_isfinite(reporter);
930}
931
fsb1475b02016-01-20 09:51:07 -0800932static void test_islastcontourclosed(skiatest::Reporter* reporter) {
933 SkPath path;
934 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
935 path.moveTo(0, 0);
936 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
937 path.close();
938 REPORTER_ASSERT(reporter, path.isLastContourClosed());
939 path.lineTo(100, 100);
940 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
941 path.moveTo(200, 200);
942 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
943 path.close();
944 REPORTER_ASSERT(reporter, path.isLastContourClosed());
945 path.moveTo(0, 0);
946 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
947}
948
reed@google.com744faba2012-05-29 19:54:52 +0000949// assert that we always
950// start with a moveTo
951// only have 1 moveTo
952// only have Lines after that
953// end with a single close
954// only have (at most) 1 close
955//
956static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000957 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000958 SkPath::RawIter iter(path);
959 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000960
961 bool firstTime = true;
962 bool foundClose = false;
963 for (;;) {
964 switch (iter.next(pts)) {
965 case SkPath::kMove_Verb:
966 REPORTER_ASSERT(reporter, firstTime);
967 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
968 srcPts++;
969 firstTime = false;
970 break;
971 case SkPath::kLine_Verb:
972 REPORTER_ASSERT(reporter, !firstTime);
973 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
974 srcPts++;
975 break;
976 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000977 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000978 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000979 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000980 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000981 break;
reed@google.com744faba2012-05-29 19:54:52 +0000982 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000983 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000984 break;
985 case SkPath::kClose_Verb:
986 REPORTER_ASSERT(reporter, !firstTime);
987 REPORTER_ASSERT(reporter, !foundClose);
988 REPORTER_ASSERT(reporter, expectClose);
989 foundClose = true;
990 break;
991 case SkPath::kDone_Verb:
992 goto DONE;
993 }
994 }
995DONE:
996 REPORTER_ASSERT(reporter, foundClose == expectClose);
997}
998
999static void test_addPoly(skiatest::Reporter* reporter) {
1000 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001001 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001002
reed@google.com744faba2012-05-29 19:54:52 +00001003 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1004 pts[i].fX = rand.nextSScalar1();
1005 pts[i].fY = rand.nextSScalar1();
1006 }
1007
1008 for (int doClose = 0; doClose <= 1; ++doClose) {
1009 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1010 SkPath path;
bsalomon98806072014-12-12 15:11:17 -08001011 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001012 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +00001013 }
1014 }
1015}
1016
reed@google.com8b06f1a2012-05-29 12:03:46 +00001017static void test_strokerec(skiatest::Reporter* reporter) {
1018 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1019 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001020
reed@google.com8b06f1a2012-05-29 12:03:46 +00001021 rec.setHairlineStyle();
1022 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001023
reed@google.com8b06f1a2012-05-29 12:03:46 +00001024 rec.setStrokeStyle(SK_Scalar1, false);
1025 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001026
reed@google.com8b06f1a2012-05-29 12:03:46 +00001027 rec.setStrokeStyle(SK_Scalar1, true);
1028 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001029
reed@google.com8b06f1a2012-05-29 12:03:46 +00001030 rec.setStrokeStyle(0, false);
1031 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001032
reed@google.com8b06f1a2012-05-29 12:03:46 +00001033 rec.setStrokeStyle(0, true);
1034 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1035}
1036
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001037// Set this for paths that don't have a consistent direction such as a bowtie.
1038// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001039const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001040
1041static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001042 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001043 if (expected == kDontCheckDir) {
1044 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001045 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001046 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1047
reed026beb52015-06-10 14:23:15 -07001048 SkPathPriv::FirstDirection dir;
1049 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001050 REPORTER_ASSERT(reporter, dir == expected);
1051 } else {
reed026beb52015-06-10 14:23:15 -07001052 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001053 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001054}
1055
reed@google.com3e71a882012-01-10 18:44:37 +00001056static void test_direction(skiatest::Reporter* reporter) {
1057 size_t i;
1058 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001059 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001060 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1061 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1062 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001063
1064 static const char* gDegen[] = {
1065 "M 10 10",
1066 "M 10 10 M 20 20",
1067 "M 10 10 L 20 20",
1068 "M 10 10 L 10 10 L 10 10",
1069 "M 10 10 Q 10 10 10 10",
1070 "M 10 10 C 10 10 10 10 10 10",
1071 };
1072 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1073 path.reset();
1074 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1075 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001076 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001077 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001078
reed@google.com3e71a882012-01-10 18:44:37 +00001079 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001080 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001081 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001082 "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 +00001083 // rect with top two corners replaced by cubics with identical middle
1084 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001085 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1086 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001087 };
1088 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1089 path.reset();
1090 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1091 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001092 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001093 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001094
reed@google.com3e71a882012-01-10 18:44:37 +00001095 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001096 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001097 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001098 "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 +00001099 // rect with top two corners replaced by cubics with identical middle
1100 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001101 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1102 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001103 };
1104 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1105 path.reset();
1106 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1107 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001108 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001109 }
reed@google.comac8543f2012-01-30 20:51:25 +00001110
1111 // Test two donuts, each wound a different direction. Only the outer contour
1112 // determines the cheap direction
1113 path.reset();
1114 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1115 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001116 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001117
reed@google.comac8543f2012-01-30 20:51:25 +00001118 path.reset();
1119 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1120 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001121 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001122
1123 // triangle with one point really far from the origin.
1124 path.reset();
1125 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001126 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001127 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1128 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001129 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001130
1131 path.reset();
1132 path.conicTo(20, 0, 20, 20, 0.5f);
1133 path.close();
reed026beb52015-06-10 14:23:15 -07001134 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001135
1136 path.reset();
1137 path.lineTo(1, 1e7f);
1138 path.lineTo(1e7f, 2e7f);
1139 path.close();
1140 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001141 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001142}
1143
reed@google.comffdb0182011-11-14 19:29:14 +00001144static void add_rect(SkPath* path, const SkRect& r) {
1145 path->moveTo(r.fLeft, r.fTop);
1146 path->lineTo(r.fRight, r.fTop);
1147 path->lineTo(r.fRight, r.fBottom);
1148 path->lineTo(r.fLeft, r.fBottom);
1149 path->close();
1150}
1151
1152static void test_bounds(skiatest::Reporter* reporter) {
1153 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001154 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1155 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1156 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1157 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001158 };
1159
1160 SkPath path0, path1;
1161 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1162 path0.addRect(rects[i]);
1163 add_rect(&path1, rects[i]);
1164 }
1165
1166 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1167}
1168
reed@google.com55b5f4b2011-09-07 12:23:41 +00001169static void stroke_cubic(const SkPoint pts[4]) {
1170 SkPath path;
1171 path.moveTo(pts[0]);
1172 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001173
reed@google.com55b5f4b2011-09-07 12:23:41 +00001174 SkPaint paint;
1175 paint.setStyle(SkPaint::kStroke_Style);
1176 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001177
reed@google.com55b5f4b2011-09-07 12:23:41 +00001178 SkPath fill;
1179 paint.getFillPath(path, &fill);
1180}
1181
1182// just ensure this can run w/o any SkASSERTS firing in the debug build
1183// we used to assert due to differences in how we determine a degenerate vector
1184// but that was fixed with the introduction of SkPoint::CanNormalize
1185static void stroke_tiny_cubic() {
1186 SkPoint p0[] = {
1187 { 372.0f, 92.0f },
1188 { 372.0f, 92.0f },
1189 { 372.0f, 92.0f },
1190 { 372.0f, 92.0f },
1191 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001192
reed@google.com55b5f4b2011-09-07 12:23:41 +00001193 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001194
reed@google.com55b5f4b2011-09-07 12:23:41 +00001195 SkPoint p1[] = {
1196 { 372.0f, 92.0f },
1197 { 372.0007f, 92.000755f },
1198 { 371.99927f, 92.003922f },
1199 { 371.99826f, 92.003899f },
1200 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001201
reed@google.com55b5f4b2011-09-07 12:23:41 +00001202 stroke_cubic(p1);
1203}
1204
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001205static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1206 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001207 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001208 SkPoint mv;
1209 SkPoint pts[4];
1210 SkPath::Verb v;
1211 int nMT = 0;
1212 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001213 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001214 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1215 switch (v) {
1216 case SkPath::kMove_Verb:
1217 mv = pts[0];
1218 ++nMT;
1219 break;
1220 case SkPath::kClose_Verb:
1221 REPORTER_ASSERT(reporter, mv == pts[0]);
1222 ++nCL;
1223 break;
1224 default:
1225 break;
1226 }
1227 }
1228 // if we force a close on the interator we should have a close
1229 // for every moveTo
1230 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1231 }
1232}
1233
1234static void test_close(skiatest::Reporter* reporter) {
1235 SkPath closePt;
1236 closePt.moveTo(0, 0);
1237 closePt.close();
1238 check_close(reporter, closePt);
1239
1240 SkPath openPt;
1241 openPt.moveTo(0, 0);
1242 check_close(reporter, openPt);
1243
1244 SkPath empty;
1245 check_close(reporter, empty);
1246 empty.close();
1247 check_close(reporter, empty);
1248
1249 SkPath rect;
1250 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1251 check_close(reporter, rect);
1252 rect.close();
1253 check_close(reporter, rect);
1254
1255 SkPath quad;
1256 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1257 check_close(reporter, quad);
1258 quad.close();
1259 check_close(reporter, quad);
1260
1261 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001262 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001263 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1264 check_close(reporter, cubic);
1265 cubic.close();
1266 check_close(reporter, cubic);
1267
1268 SkPath line;
1269 line.moveTo(SK_Scalar1, SK_Scalar1);
1270 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1271 check_close(reporter, line);
1272 line.close();
1273 check_close(reporter, line);
1274
1275 SkPath rect2;
1276 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1277 rect2.close();
1278 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1279 check_close(reporter, rect2);
1280 rect2.close();
1281 check_close(reporter, rect2);
1282
1283 SkPath oval3;
1284 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1285 oval3.close();
1286 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1287 check_close(reporter, oval3);
1288 oval3.close();
1289 check_close(reporter, oval3);
1290
1291 SkPath moves;
1292 moves.moveTo(SK_Scalar1, SK_Scalar1);
1293 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1294 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1295 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1296 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001297
1298 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001299}
1300
reed@google.com7c424812011-05-15 04:38:34 +00001301static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1302 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001303 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1304 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001305 REPORTER_ASSERT(reporter, c == expected);
1306}
1307
caryclarkb4216502015-03-02 13:02:34 -08001308static void test_path_crbug389050(skiatest::Reporter* reporter) {
1309 SkPath tinyConvexPolygon;
1310 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1311 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1312 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1313 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1314 tinyConvexPolygon.close();
1315 tinyConvexPolygon.getConvexity();
1316 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001317 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001318
1319 SkPath platTriangle;
1320 platTriangle.moveTo(0, 0);
1321 platTriangle.lineTo(200, 0);
1322 platTriangle.lineTo(100, 0.04f);
1323 platTriangle.close();
1324 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001325 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001326
1327 platTriangle.reset();
1328 platTriangle.moveTo(0, 0);
1329 platTriangle.lineTo(200, 0);
1330 platTriangle.lineTo(100, 0.03f);
1331 platTriangle.close();
1332 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001333 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001334}
1335
reed@google.com7c424812011-05-15 04:38:34 +00001336static void test_convexity2(skiatest::Reporter* reporter) {
1337 SkPath pt;
1338 pt.moveTo(0, 0);
1339 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001340 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001341 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001342
reed@google.com7c424812011-05-15 04:38:34 +00001343 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001344 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1345 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001346 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001347 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001348 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001349
reed@google.com7c424812011-05-15 04:38:34 +00001350 SkPath triLeft;
1351 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001352 triLeft.lineTo(SK_Scalar1, 0);
1353 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001354 triLeft.close();
1355 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001356 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001357
reed@google.com7c424812011-05-15 04:38:34 +00001358 SkPath triRight;
1359 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001360 triRight.lineTo(-SK_Scalar1, 0);
1361 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001362 triRight.close();
1363 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001364 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001365
reed@google.com7c424812011-05-15 04:38:34 +00001366 SkPath square;
1367 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001368 square.lineTo(SK_Scalar1, 0);
1369 square.lineTo(SK_Scalar1, SK_Scalar1);
1370 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001371 square.close();
1372 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001373 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001374
reed@google.com7c424812011-05-15 04:38:34 +00001375 SkPath redundantSquare;
1376 redundantSquare.moveTo(0, 0);
1377 redundantSquare.lineTo(0, 0);
1378 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001379 redundantSquare.lineTo(SK_Scalar1, 0);
1380 redundantSquare.lineTo(SK_Scalar1, 0);
1381 redundantSquare.lineTo(SK_Scalar1, 0);
1382 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1383 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1384 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1385 redundantSquare.lineTo(0, SK_Scalar1);
1386 redundantSquare.lineTo(0, SK_Scalar1);
1387 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001388 redundantSquare.close();
1389 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001390 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001391
reed@google.com7c424812011-05-15 04:38:34 +00001392 SkPath bowTie;
1393 bowTie.moveTo(0, 0);
1394 bowTie.lineTo(0, 0);
1395 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001396 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1397 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1398 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1399 bowTie.lineTo(SK_Scalar1, 0);
1400 bowTie.lineTo(SK_Scalar1, 0);
1401 bowTie.lineTo(SK_Scalar1, 0);
1402 bowTie.lineTo(0, SK_Scalar1);
1403 bowTie.lineTo(0, SK_Scalar1);
1404 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001405 bowTie.close();
1406 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001407 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001408
reed@google.com7c424812011-05-15 04:38:34 +00001409 SkPath spiral;
1410 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001411 spiral.lineTo(100*SK_Scalar1, 0);
1412 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1413 spiral.lineTo(0, 100*SK_Scalar1);
1414 spiral.lineTo(0, 50*SK_Scalar1);
1415 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1416 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001417 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001418 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001419 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001420
reed@google.com7c424812011-05-15 04:38:34 +00001421 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001422 dent.moveTo(0, 0);
1423 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1424 dent.lineTo(0, 100*SK_Scalar1);
1425 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1426 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001427 dent.close();
1428 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001429 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001430
halcanary6950de62015-11-07 05:29:00 -08001431 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001432 SkPath strokedSin;
1433 for (int i = 0; i < 2000; i++) {
1434 SkScalar x = SkIntToScalar(i) / 2;
1435 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1436 if (0 == i) {
1437 strokedSin.moveTo(x, y);
1438 } else {
1439 strokedSin.lineTo(x, y);
1440 }
1441 }
1442 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1443 stroke.setStrokeStyle(2 * SK_Scalar1);
1444 stroke.applyToPath(&strokedSin, strokedSin);
1445 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1446 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001447
1448 // http://crbug.com/412640
1449 SkPath degenerateConcave;
1450 degenerateConcave.moveTo(148.67912f, 191.875f);
1451 degenerateConcave.lineTo(470.37695f, 7.5f);
1452 degenerateConcave.lineTo(148.67912f, 191.875f);
1453 degenerateConcave.lineTo(41.446522f, 376.25f);
1454 degenerateConcave.lineTo(-55.971577f, 460.0f);
1455 degenerateConcave.lineTo(41.446522f, 376.25f);
1456 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001457 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001458
1459 // http://crbug.com/433683
1460 SkPath badFirstVector;
1461 badFirstVector.moveTo(501.087708f, 319.610352f);
1462 badFirstVector.lineTo(501.087708f, 319.610352f);
1463 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1464 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1465 badFirstVector.lineTo(301.557678f, 98.044601f);
1466 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1467 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1468 badFirstVector.lineTo(504.912292f, 316.389648f);
1469 badFirstVector.lineTo(501.087708f, 319.610352f);
1470 badFirstVector.close();
1471 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001472}
1473
reed@android.com6b82d1a2009-06-03 02:35:01 +00001474static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1475 const SkRect& bounds) {
1476 REPORTER_ASSERT(reporter, p.isConvex());
1477 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001478
reed@android.com6b82d1a2009-06-03 02:35:01 +00001479 SkPath p2(p);
1480 REPORTER_ASSERT(reporter, p2.isConvex());
1481 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1482
1483 SkPath other;
1484 other.swap(p2);
1485 REPORTER_ASSERT(reporter, other.isConvex());
1486 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1487}
1488
reed@google.com04863fa2011-05-15 04:08:24 +00001489static void setFromString(SkPath* path, const char str[]) {
1490 bool first = true;
1491 while (str) {
1492 SkScalar x, y;
1493 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001494 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001495 break;
1496 }
1497 str = SkParse::FindScalar(str, &y);
1498 SkASSERT(str);
1499 if (first) {
1500 path->moveTo(x, y);
1501 first = false;
1502 } else {
1503 path->lineTo(x, y);
1504 }
1505 }
1506}
1507
1508static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001509 SkPath path;
1510
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001511 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001512 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001513 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001514 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001515 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1516
reed@google.com04863fa2011-05-15 04:08:24 +00001517 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001518 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001519 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001520 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001521
reed@google.com04863fa2011-05-15 04:08:24 +00001522 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001523 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001524 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001525 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001526
liyuqianbdabcc42016-11-03 11:40:19 -07001527 path.reset();
1528 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1529 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1530
reed@google.com04863fa2011-05-15 04:08:24 +00001531 static const struct {
reed026beb52015-06-10 14:23:15 -07001532 const char* fPathStr;
1533 SkPath::Convexity fExpectedConvexity;
1534 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001535 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001536 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1537 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1538 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1539 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1540 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1541 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001542 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001543 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001544 };
1545
1546 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1547 SkPath path;
1548 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001549 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1550 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001551 // check after setting the initial convex and direction
1552 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1553 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001554 SkPathPriv::FirstDirection dir;
1555 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1556 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001557 ^ foundDir);
1558 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1559 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1560 }
1561 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1562 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001563 }
caryclarkd3d1a982014-12-08 04:57:38 -08001564
1565 static const SkPoint nonFinitePts[] = {
1566 { SK_ScalarInfinity, 0 },
1567 { 0, SK_ScalarInfinity },
1568 { SK_ScalarInfinity, SK_ScalarInfinity },
1569 { SK_ScalarNegativeInfinity, 0},
1570 { 0, SK_ScalarNegativeInfinity },
1571 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1572 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1573 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1574 { SK_ScalarNaN, 0 },
1575 { 0, SK_ScalarNaN },
1576 { SK_ScalarNaN, SK_ScalarNaN },
1577 };
1578
1579 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1580
1581 static const SkPoint finitePts[] = {
1582 { SK_ScalarMax, 0 },
1583 { 0, SK_ScalarMax },
1584 { SK_ScalarMax, SK_ScalarMax },
1585 { SK_ScalarMin, 0 },
1586 { 0, SK_ScalarMin },
1587 { SK_ScalarMin, SK_ScalarMin },
1588 };
1589
1590 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1591
1592 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1593 int i = (int) (index % nonFinitePtsCount);
1594 int f = (int) (index % finitePtsCount);
1595 int g = (int) ((f + 1) % finitePtsCount);
1596 path.reset();
1597 switch (index % 13) {
1598 case 0: path.lineTo(nonFinitePts[i]); break;
1599 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1600 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1601 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1602 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1603 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1604 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1605 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1606 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1607 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1608 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1609 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1610 case 12: path.moveTo(nonFinitePts[i]); break;
1611 }
1612 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1613 }
1614
1615 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1616 int f = (int) (index % finitePtsCount);
1617 int g = (int) ((f + 1) % finitePtsCount);
1618 path.reset();
1619 int curveSelect = index % 11;
1620 switch (curveSelect) {
1621 case 0: path.moveTo(finitePts[f]); break;
1622 case 1: path.lineTo(finitePts[f]); break;
1623 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1624 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1625 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1626 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1627 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1628 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1629 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1630 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1631 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1632 }
1633 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1634 : SkPath::kUnknown_Convexity);
1635 }
1636
reed@google.com04863fa2011-05-15 04:08:24 +00001637}
1638
reed@google.com7e6c4d12012-05-10 14:05:43 +00001639static void test_isLine(skiatest::Reporter* reporter) {
1640 SkPath path;
1641 SkPoint pts[2];
1642 const SkScalar value = SkIntToScalar(5);
1643
halcanary96fcdcc2015-08-27 07:41:13 -07001644 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001645
reed@google.com7e6c4d12012-05-10 14:05:43 +00001646 // set some non-zero values
1647 pts[0].set(value, value);
1648 pts[1].set(value, value);
1649 REPORTER_ASSERT(reporter, !path.isLine(pts));
1650 // check that pts was untouched
1651 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1652 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1653
1654 const SkScalar moveX = SkIntToScalar(1);
1655 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001656 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001657
1658 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001659 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001660 REPORTER_ASSERT(reporter, !path.isLine(pts));
1661 // check that pts was untouched
1662 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1663 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1664
1665 const SkScalar lineX = SkIntToScalar(2);
1666 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001667 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001668
1669 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001670 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001671
1672 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1673 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1674 REPORTER_ASSERT(reporter, path.isLine(pts));
1675 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1676 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1677
1678 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001679 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001680 REPORTER_ASSERT(reporter, !path.isLine(pts));
1681 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1682 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001683
1684 path.reset();
1685 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001686 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001687}
1688
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001689static void test_conservativelyContains(skiatest::Reporter* reporter) {
1690 SkPath path;
1691
1692 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1693 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1694
1695 // A circle that bounds kBaseRect (with a significant amount of slop)
1696 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001697 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001698 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1699
1700 // round-rect radii
1701 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001702
caryclark@google.com56f233a2012-11-19 13:06:06 +00001703 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001704 SkRect fQueryRect;
1705 bool fInRect;
1706 bool fInCircle;
1707 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001708 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001709 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001710 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001711
1712 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001713 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1714 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1715 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1716 kBaseRect.fBottom - 0.25f*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 // rects with edges off by one from kBaseRect's edges
1720 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1721 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001722 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001723 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1724 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001725 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001726 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1727 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001728 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001729 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1730 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001731 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001732 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1733 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001734 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001735 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1736 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001737 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001738 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1739 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001740 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001741
1742 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001743 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1744 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1745 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1746 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001747
1748 // far away rect
1749 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1750 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001751 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001752
1753 // very large rect containing kBaseRect
1754 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1755 kBaseRect.fTop - 5 * kBaseRect.height(),
1756 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001757 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001758
1759 // skinny rect that spans same y-range as kBaseRect
1760 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1761 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001762 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001763
1764 // short rect that spans same x-range as kBaseRect
1765 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001766 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001767
1768 // skinny rect that spans slightly larger y-range than kBaseRect
1769 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1770 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001771 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001772
1773 // short rect that spans slightly larger x-range than kBaseRect
1774 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1775 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001776 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001777 };
1778
1779 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001780 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001781 SkRect qRect = kQueries[q].fQueryRect;
1782 if (inv & 0x1) {
1783 SkTSwap(qRect.fLeft, qRect.fRight);
1784 }
1785 if (inv & 0x2) {
1786 SkTSwap(qRect.fTop, qRect.fBottom);
1787 }
1788 for (int d = 0; d < 2; ++d) {
1789 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1790 path.reset();
1791 path.addRect(kBaseRect, dir);
1792 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1793 path.conservativelyContainsRect(qRect));
1794
1795 path.reset();
1796 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1797 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1798 path.conservativelyContainsRect(qRect));
1799
1800 path.reset();
1801 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1802 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1803 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001804
1805 path.reset();
1806 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1807 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1808 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1809 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1810 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1811 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1812 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1813 path.close();
1814 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1815 path.conservativelyContainsRect(qRect));
1816
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001817 }
1818 // Slightly non-convex shape, shouldn't contain any rects.
1819 path.reset();
1820 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001821 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001822 path.lineTo(SkIntToScalar(100), 0);
1823 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1824 path.lineTo(0, SkIntToScalar(100));
1825 path.close();
1826 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1827 }
1828 }
1829
1830 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1831 path.reset();
1832 path.moveTo(0, 0);
1833 path.lineTo(SkIntToScalar(100), 0);
1834 path.lineTo(0, SkIntToScalar(100));
1835
1836 // inside, on along top edge
1837 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1838 SkIntToScalar(10),
1839 SkIntToScalar(10))));
1840 // above
1841 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1842 SkRect::MakeXYWH(SkIntToScalar(50),
1843 SkIntToScalar(-10),
1844 SkIntToScalar(10),
1845 SkIntToScalar(10))));
1846 // to the left
1847 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1848 SkIntToScalar(5),
1849 SkIntToScalar(5),
1850 SkIntToScalar(5))));
1851
1852 // outside the diagonal edge
1853 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1854 SkIntToScalar(200),
1855 SkIntToScalar(20),
1856 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001857
bsalomonb17c1292014-08-28 14:04:55 -07001858
1859 // Test that multiple move commands do not cause asserts.
1860
1861 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001862 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001863 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1864#if 0
1865 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1866 SkIntToScalar(10),
1867 SkIntToScalar(10))));
1868#endif
1869
1870 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001871 path.reset();
1872 path.moveTo(100, 100);
1873 path.moveTo(0, 0);
1874 path.lineTo(SkIntToScalar(100), 0);
1875 path.lineTo(0, SkIntToScalar(100));
1876
1877 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1878 SkIntToScalar(10),
1879 SkIntToScalar(10))));
1880
bsalomonb17c1292014-08-28 14:04:55 -07001881 // Test that multiple move commands do not cause asserts and that the function
1882 // is not confused by the multiple moves.
1883 path.reset();
1884 path.moveTo(0, 0);
1885 path.lineTo(SkIntToScalar(100), 0);
1886 path.lineTo(0, SkIntToScalar(100));
1887 path.moveTo(0, SkIntToScalar(200));
1888 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1889 path.lineTo(0, SkIntToScalar(300));
1890
1891 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1892 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1893 SkIntToScalar(10),
1894 SkIntToScalar(10))));
1895
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001896 path.reset();
1897 path.lineTo(100, 100);
1898 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001899}
1900
reed@google.comf32322b2013-10-16 15:14:04 +00001901static void test_isRect_open_close(skiatest::Reporter* reporter) {
1902 SkPath path;
1903 bool isClosed;
1904
1905 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001906 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001907
halcanary96fcdcc2015-08-27 07:41:13 -07001908 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001909 REPORTER_ASSERT(reporter, isClosed);
1910}
1911
caryclark@google.comf1316942011-07-26 19:54:45 +00001912// Simple isRect test is inline TestPath, below.
1913// test_isRect provides more extensive testing.
1914static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001915 test_isRect_open_close(reporter);
1916
caryclark@google.comf1316942011-07-26 19:54:45 +00001917 // passing tests (all moveTo / lineTo...
1918 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1919 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1920 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1921 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1922 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1923 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1924 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1925 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1926 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001927 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1928 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 +00001929 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1930 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1931 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001932 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001933
caryclark@google.comf1316942011-07-26 19:54:45 +00001934 // failing tests
1935 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1936 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1937 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1938 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1939 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1940 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1941 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1942 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001943 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1944 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1945 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001946
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001947 // no close, but we should detect them as fillably the same as a rect
1948 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1949 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1950 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1951
1952 // like c2, but we double-back on ourselves
1953 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1954 // like c2, but we overshoot the start point
1955 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1956 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001957
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001958 struct IsRectTest {
1959 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001960 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001961 bool fClose;
1962 bool fIsRect;
1963 } tests[] = {
1964 { r1, SK_ARRAY_COUNT(r1), true, true },
1965 { r2, SK_ARRAY_COUNT(r2), true, true },
1966 { r3, SK_ARRAY_COUNT(r3), true, true },
1967 { r4, SK_ARRAY_COUNT(r4), true, true },
1968 { r5, SK_ARRAY_COUNT(r5), true, true },
1969 { r6, SK_ARRAY_COUNT(r6), true, true },
1970 { r7, SK_ARRAY_COUNT(r7), true, true },
1971 { r8, SK_ARRAY_COUNT(r8), true, true },
1972 { r9, SK_ARRAY_COUNT(r9), true, true },
1973 { ra, SK_ARRAY_COUNT(ra), true, true },
1974 { rb, SK_ARRAY_COUNT(rb), true, true },
1975 { rc, SK_ARRAY_COUNT(rc), true, true },
1976 { rd, SK_ARRAY_COUNT(rd), true, true },
1977 { re, SK_ARRAY_COUNT(re), true, true },
1978 { rf, SK_ARRAY_COUNT(rf), true, true },
1979
1980 { f1, SK_ARRAY_COUNT(f1), true, false },
1981 { f2, SK_ARRAY_COUNT(f2), true, false },
1982 { f3, SK_ARRAY_COUNT(f3), true, false },
1983 { f4, SK_ARRAY_COUNT(f4), true, false },
1984 { f5, SK_ARRAY_COUNT(f5), true, false },
1985 { f6, SK_ARRAY_COUNT(f6), true, false },
1986 { f7, SK_ARRAY_COUNT(f7), true, false },
1987 { f8, SK_ARRAY_COUNT(f8), true, false },
1988 { f9, SK_ARRAY_COUNT(f9), true, false },
1989 { fa, SK_ARRAY_COUNT(fa), true, false },
1990 { fb, SK_ARRAY_COUNT(fb), true, false },
1991
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001992 { c1, SK_ARRAY_COUNT(c1), false, true },
1993 { c2, SK_ARRAY_COUNT(c2), false, true },
1994 { c3, SK_ARRAY_COUNT(c3), false, true },
1995
1996 { d1, SK_ARRAY_COUNT(d1), false, false },
1997 { d2, SK_ARRAY_COUNT(d2), false, false },
1998 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001999 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002000
2001 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002002 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002003 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2004 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002005 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2006 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2007 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002008 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002009 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002010 path.close();
2011 }
halcanary96fcdcc2015-08-27 07:41:13 -07002012 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002013
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002014 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002015 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002016 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002017 SkPath::Direction direction;
2018 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002019 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002020 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002021 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2022 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002023 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002024 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002025 } else {
2026 SkRect computed;
2027 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002028 bool isClosed = (bool)-1;
2029 SkPath::Direction direction = (SkPath::Direction) - 1;
2030 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002031 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2032 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002033 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2034 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002035 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002036 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002037
caryclark@google.comf1316942011-07-26 19:54:45 +00002038 // fail, close then line
2039 SkPath path1;
2040 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002041 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002042 path1.lineTo(r1[index].fX, r1[index].fY);
2043 }
2044 path1.close();
2045 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002046 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002047
caryclark@google.comf1316942011-07-26 19:54:45 +00002048 // fail, move in the middle
2049 path1.reset();
2050 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002051 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002052 if (index == 2) {
2053 path1.moveTo(1, .5f);
2054 }
2055 path1.lineTo(r1[index].fX, r1[index].fY);
2056 }
2057 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002058 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002059
2060 // fail, move on the edge
2061 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002062 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002063 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2064 path1.lineTo(r1[index].fX, r1[index].fY);
2065 }
2066 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002067 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002068
caryclark@google.comf1316942011-07-26 19:54:45 +00002069 // fail, quad
2070 path1.reset();
2071 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002072 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002073 if (index == 2) {
2074 path1.quadTo(1, .5f, 1, .5f);
2075 }
2076 path1.lineTo(r1[index].fX, r1[index].fY);
2077 }
2078 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002079 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002080
caryclark@google.comf1316942011-07-26 19:54:45 +00002081 // fail, cubic
2082 path1.reset();
2083 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002084 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002085 if (index == 2) {
2086 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2087 }
2088 path1.lineTo(r1[index].fX, r1[index].fY);
2089 }
2090 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002091 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002092}
2093
bsalomonedc743a2016-06-01 09:42:31 -07002094static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2095 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2096 SkRect r = SkRect::MakeEmpty();
2097 SkPath::Direction d = SkPath::kCCW_Direction;
2098 unsigned s = ~0U;
2099
2100 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2101 REPORTER_ASSERT(reporter, r == rect);
2102 REPORTER_ASSERT(reporter, d == dir);
2103 REPORTER_ASSERT(reporter, s == start);
2104}
2105
2106static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2107 SkRect r = SkRect::MakeEmpty();
2108 SkPath::Direction d = SkPath::kCCW_Direction;
2109 unsigned s = ~0U;
2110
2111 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2112 const SkRect emptyRect = SkRect::MakeEmpty();
2113 SkPath path;
2114 for (int start = 0; start < 4; ++start) {
2115 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2116 SkPath path;
2117 path.addRect(testRect, dir, start);
2118 check_simple_closed_rect(reporter, path, testRect, dir, start);
2119 path.close();
2120 check_simple_closed_rect(reporter, path, testRect, dir, start);
2121 SkPath path2 = path;
2122 path2.lineTo(10, 10);
2123 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2124 path2 = path;
2125 path2.moveTo(10, 10);
2126 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2127 path2 = path;
2128 path2.addRect(testRect, dir, start);
2129 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2130 // Make the path by hand, manually closing it.
2131 path2.reset();
2132 SkPath::RawIter iter(path);
2133 SkPath::Verb v;
2134 SkPoint verbPts[4];
2135 SkPoint firstPt = {0.f, 0.f};
2136 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2137 switch(v) {
2138 case SkPath::kMove_Verb:
2139 firstPt = verbPts[0];
2140 path2.moveTo(verbPts[0]);
2141 break;
2142 case SkPath::kLine_Verb:
2143 path2.lineTo(verbPts[1]);
2144 break;
2145 default:
2146 break;
2147 }
2148 }
2149 // We haven't closed it yet...
2150 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2151 // ... now we do and test again.
2152 path2.lineTo(firstPt);
2153 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2154 // A redundant close shouldn't cause a failure.
2155 path2.close();
2156 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2157 // Degenerate point and line rects are not allowed
2158 path2.reset();
2159 path2.addRect(emptyRect, dir, start);
2160 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2161 SkRect degenRect = testRect;
2162 degenRect.fLeft = degenRect.fRight;
2163 path2.reset();
2164 path2.addRect(degenRect, dir, start);
2165 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2166 degenRect = testRect;
2167 degenRect.fTop = degenRect.fBottom;
2168 path2.reset();
2169 path2.addRect(degenRect, dir, start);
2170 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2171 // An inverted rect makes a rect path, but changes the winding dir and start point.
2172 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2173 ? SkPath::kCCW_Direction
2174 : SkPath::kCW_Direction;
2175 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2176 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2177 SkRect swapRect = testRect;
2178 SkTSwap(swapRect.fLeft, swapRect.fRight);
2179 path2.reset();
2180 path2.addRect(swapRect, dir, start);
2181 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2182 swapRect = testRect;
2183 SkTSwap(swapRect.fTop, swapRect.fBottom);
2184 path2.reset();
2185 path2.addRect(swapRect, dir, start);
2186 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2187 }
2188 }
bsalomon057ae8a2016-07-24 05:37:26 -07002189 // down, up, left, close
2190 path.reset();
2191 path.moveTo(1, 1);
2192 path.lineTo(1, 2);
2193 path.lineTo(1, 1);
2194 path.lineTo(0, 1);
2195 SkRect rect;
2196 SkPath::Direction dir;
2197 unsigned start;
2198 path.close();
2199 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2200 // right, left, up, close
2201 path.reset();
2202 path.moveTo(1, 1);
2203 path.lineTo(2, 1);
2204 path.lineTo(1, 1);
2205 path.lineTo(1, 0);
2206 path.close();
2207 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2208 // parallelogram with horizontal edges
2209 path.reset();
2210 path.moveTo(1, 0);
2211 path.lineTo(3, 0);
2212 path.lineTo(2, 1);
2213 path.lineTo(0, 1);
2214 path.close();
2215 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2216 // parallelogram with vertical edges
2217 path.reset();
2218 path.moveTo(0, 1);
2219 path.lineTo(0, 3);
2220 path.lineTo(1, 2);
2221 path.lineTo(1, 0);
2222 path.close();
2223 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2224
bsalomonedc743a2016-06-01 09:42:31 -07002225}
2226
caryclark95bc5f32015-04-08 08:34:15 -07002227static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002228 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002229 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002230 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2231 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2232 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002233 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002234 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2235 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2236 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2237 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002238 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2239 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 +00002240 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2241 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2242 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002243
2244 // failing tests
2245 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2246 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2247 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2248 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2249 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2250 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2251 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2252 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2253
caryclark95bc5f32015-04-08 08:34:15 -07002254 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002255 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2256 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2257
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002258 struct IsNestedRectTest {
2259 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002260 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002261 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002262 bool fClose;
2263 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2264 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002265 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2266 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2267 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2268 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2269 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2270 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2271 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2272 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2273 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2274 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2275 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2276 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2277 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2278 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002279
reed026beb52015-06-10 14:23:15 -07002280 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2281 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2282 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2283 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2284 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2285 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2286 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2287 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002288
reed026beb52015-06-10 14:23:15 -07002289 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2290 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002291 };
2292
2293 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002294 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002295 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002296 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2297 SkPath path;
2298 if (rectFirst) {
2299 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2300 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002301 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2302 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2303 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002304 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002305 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002306 path.close();
2307 }
2308 if (!rectFirst) {
2309 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2310 }
caryclark95bc5f32015-04-08 08:34:15 -07002311 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002312 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002313 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002314 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002315 SkPathPriv::FirstDirection expectedDirs[2];
2316 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002317 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002318 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002319 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2320 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002321 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002322 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002323 } else {
reed026beb52015-06-10 14:23:15 -07002324 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002325 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002326 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002327 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002328 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2329 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002330 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2331 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002332 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002333 }
2334
2335 // fail, close then line
2336 SkPath path1;
2337 if (rectFirst) {
2338 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2339 }
2340 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002341 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002342 path1.lineTo(r1[index].fX, r1[index].fY);
2343 }
2344 path1.close();
2345 path1.lineTo(1, 0);
2346 if (!rectFirst) {
2347 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2348 }
halcanary96fcdcc2015-08-27 07:41:13 -07002349 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002350
2351 // fail, move in the middle
2352 path1.reset();
2353 if (rectFirst) {
2354 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2355 }
2356 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002357 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002358 if (index == 2) {
2359 path1.moveTo(1, .5f);
2360 }
2361 path1.lineTo(r1[index].fX, r1[index].fY);
2362 }
2363 path1.close();
2364 if (!rectFirst) {
2365 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2366 }
halcanary96fcdcc2015-08-27 07:41:13 -07002367 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002368
2369 // fail, move on the edge
2370 path1.reset();
2371 if (rectFirst) {
2372 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2373 }
bsalomon98806072014-12-12 15:11:17 -08002374 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002375 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2376 path1.lineTo(r1[index].fX, r1[index].fY);
2377 }
2378 path1.close();
2379 if (!rectFirst) {
2380 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2381 }
halcanary96fcdcc2015-08-27 07:41:13 -07002382 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002383
2384 // fail, quad
2385 path1.reset();
2386 if (rectFirst) {
2387 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2388 }
2389 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002390 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002391 if (index == 2) {
2392 path1.quadTo(1, .5f, 1, .5f);
2393 }
2394 path1.lineTo(r1[index].fX, r1[index].fY);
2395 }
2396 path1.close();
2397 if (!rectFirst) {
2398 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2399 }
halcanary96fcdcc2015-08-27 07:41:13 -07002400 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002401
2402 // fail, cubic
2403 path1.reset();
2404 if (rectFirst) {
2405 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2406 }
2407 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002408 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002409 if (index == 2) {
2410 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2411 }
2412 path1.lineTo(r1[index].fX, r1[index].fY);
2413 }
2414 path1.close();
2415 if (!rectFirst) {
2416 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2417 }
halcanary96fcdcc2015-08-27 07:41:13 -07002418 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002419
caryclark@google.com56f233a2012-11-19 13:06:06 +00002420 // fail, not nested
2421 path1.reset();
2422 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2423 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002424 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002425 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002426
caryclark95bc5f32015-04-08 08:34:15 -07002427 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2428 SkPath path;
2429 path.moveTo(0, 0);
2430 path.lineTo(10, 0);
2431 path.lineTo(10, 10);
2432 path.lineTo(0, 10);
2433 path.lineTo(0, 0);
2434 path.moveTo(1, 1);
2435 path.lineTo(9, 1);
2436 path.lineTo(9, 9);
2437 path.lineTo(1, 9);
2438 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002439 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002440
caryclark@google.combfe90372012-11-21 13:56:20 +00002441 // pass, stroke rect
2442 SkPath src, dst;
2443 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2444 SkPaint strokePaint;
2445 strokePaint.setStyle(SkPaint::kStroke_Style);
2446 strokePaint.setStrokeWidth(2);
2447 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002448 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002449}
2450
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002451static void write_and_read_back(skiatest::Reporter* reporter,
2452 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002453 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002454 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002455 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002456 SkAutoMalloc storage(size);
2457 writer.flatten(storage.get());
2458 SkReader32 reader(storage.get(), size);
2459
2460 SkPath readBack;
2461 REPORTER_ASSERT(reporter, readBack != p);
2462 reader.readPath(&readBack);
2463 REPORTER_ASSERT(reporter, readBack == p);
2464
rmistry@google.comd6176b02012-08-23 18:14:13 +00002465 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002466 p.getConvexityOrUnknown());
2467
bsalomon78d58d12016-05-27 09:17:04 -07002468 SkRect oval0, oval1;
2469 SkPath::Direction dir0, dir1;
2470 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002471 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002472 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2473 REPORTER_ASSERT(reporter, oval0 == oval1);
2474 REPORTER_ASSERT(reporter, dir0 == dir1);
2475 REPORTER_ASSERT(reporter, start0 == start1);
2476 }
2477 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2478 SkRRect rrect0, rrect1;
2479 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2480 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2481 REPORTER_ASSERT(reporter, dir0 == dir1);
2482 REPORTER_ASSERT(reporter, start0 == start1);
2483 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002484 const SkRect& origBounds = p.getBounds();
2485 const SkRect& readBackBounds = readBack.getBounds();
2486
2487 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2488}
2489
reed@google.com53effc52011-09-21 19:05:12 +00002490static void test_flattening(skiatest::Reporter* reporter) {
2491 SkPath p;
2492
2493 static const SkPoint pts[] = {
2494 { 0, 0 },
2495 { SkIntToScalar(10), SkIntToScalar(10) },
2496 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2497 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2498 };
2499 p.moveTo(pts[0]);
2500 p.lineTo(pts[1]);
2501 p.quadTo(pts[2], pts[3]);
2502 p.cubicTo(pts[4], pts[5], pts[6]);
2503
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002504 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002505
2506 // create a buffer that should be much larger than the path so we don't
2507 // kill our stack if writer goes too far.
2508 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002509 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002510 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002511 REPORTER_ASSERT(reporter, size1 == size2);
2512
2513 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002514 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002515 REPORTER_ASSERT(reporter, size1 == size3);
2516 REPORTER_ASSERT(reporter, p == p2);
2517
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002518 size3 = p2.readFromMemory(buffer, 0);
2519 REPORTER_ASSERT(reporter, !size3);
2520
2521 SkPath tooShort;
2522 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2523 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2524
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002525 char buffer2[1024];
2526 size3 = p2.writeToMemory(buffer2);
2527 REPORTER_ASSERT(reporter, size1 == size3);
2528 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002529
2530 // test persistence of the oval flag & convexity
2531 {
2532 SkPath oval;
2533 SkRect rect = SkRect::MakeWH(10, 10);
2534 oval.addOval(rect);
2535
2536 write_and_read_back(reporter, oval);
2537 }
reed@google.com53effc52011-09-21 19:05:12 +00002538}
2539
2540static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002541 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002542
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002543#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002544 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002545 { 0, 0 }, // move
2546 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2547 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2548 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2549#if CONIC_PERSPECTIVE_BUG_FIXED
2550 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2551#endif
reed@google.com53effc52011-09-21 19:05:12 +00002552 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002553 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002554
reed@google.com53effc52011-09-21 19:05:12 +00002555 p.moveTo(pts[0]);
2556 p.lineTo(pts[1]);
2557 p.quadTo(pts[2], pts[3]);
2558 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002559#if CONIC_PERSPECTIVE_BUG_FIXED
2560 p.conicTo(pts[4], pts[5], 0.5f);
2561#endif
2562 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002563
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002564 {
2565 SkMatrix matrix;
2566 matrix.reset();
2567 SkPath p1;
2568 p.transform(matrix, &p1);
2569 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002570 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002571
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002572
2573 {
2574 SkMatrix matrix;
2575 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2576
2577 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2578
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002579 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002580 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002581 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002582 REPORTER_ASSERT(reporter, kPtCount == count);
2583 for (int i = 0; i < count; ++i) {
2584 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2585 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2586 }
2587 }
2588
2589 {
2590 SkMatrix matrix;
2591 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002592 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002593
2594 SkPath p1;
2595 p1.moveTo(SkPoint::Make(0, 0));
2596
2597 p.transform(matrix, &p1);
2598 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002599 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002600 SkRect pBounds = p.getBounds();
2601 SkRect p1Bounds = p1.getBounds();
2602 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2603 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2604 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2605 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2606 }
2607
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002608 p.reset();
2609 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002610
2611 {
2612 SkMatrix matrix;
2613 matrix.reset();
2614 SkPath p1;
2615 p1.moveTo(SkPoint::Make(0, 0));
2616
2617 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002618 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002619 }
2620
2621
2622 {
2623 SkMatrix matrix;
2624 matrix.reset();
2625 matrix.setScaleX(-1);
2626 SkPath p1;
2627 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2628
2629 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002630 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002631 }
2632
2633 {
2634 SkMatrix matrix;
2635 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2636 SkPath p1;
2637 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2638
2639 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002640 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002641 }
reed@google.com53effc52011-09-21 19:05:12 +00002642}
2643
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002644static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002645 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002646 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002647
caryclark@google.com56f233a2012-11-19 13:06:06 +00002648 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002649 const char* testPath;
2650 const size_t numResultPts;
2651 const SkRect resultBound;
2652 const SkPath::Verb* resultVerbs;
2653 const size_t numResultVerbs;
2654 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002655
schenney@chromium.org7e963602012-06-13 17:05:43 +00002656 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2657 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2658 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2659 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2660 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2661 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2662 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2663 static const SkPath::Verb resultVerbs8[] = {
2664 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2665 };
2666 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2667 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2668 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2669 static const SkPath::Verb resultVerbs12[] = {
2670 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2671 };
2672 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2673 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2674 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2675 static const SkPath::Verb resultVerbs16[] = {
2676 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2677 };
2678 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002679 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002680 { "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 -07002681 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002682 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2683 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2684 { "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) },
2685 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2686 { "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) },
2687 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2688 { "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) },
2689 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2690 { "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) },
2691 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2692 { "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 +00002693 SK_ARRAY_COUNT(resultVerbs14)
2694 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002695 { "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) },
2696 { "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 +00002697 SK_ARRAY_COUNT(resultVerbs16)
2698 }
2699 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002700
schenney@chromium.org7e963602012-06-13 17:05:43 +00002701 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2702 p.reset();
2703 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2704 REPORTER_ASSERT(reporter, valid);
2705 REPORTER_ASSERT(reporter, !p.isEmpty());
2706 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2707 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2708 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2709 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2710 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2711 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002712 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002713}
2714
2715struct SegmentInfo {
2716 SkPath fPath;
2717 int fPointCount;
2718};
2719
reed@google.com10296cc2011-09-21 12:29:05 +00002720#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2721
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002722static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002723 SkPath p, p2;
2724
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002725 p.moveTo(0, 0);
2726 p.quadTo(100, 100, 200, 200);
2727 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2728 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002729 p2 = p;
2730 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002731 p.cubicTo(100, 100, 200, 200, 300, 300);
2732 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2733 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002734 p2 = p;
2735 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2736
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002737 p.reset();
2738 p.moveTo(0, 0);
2739 p.cubicTo(100, 100, 200, 200, 300, 300);
2740 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002741 p2 = p;
2742 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002743
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002744 REPORTER_ASSERT(reporter, !p.isEmpty());
2745}
2746
2747static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002748 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002749 SkPoint pts[4];
2750
2751 // Test an iterator with no path
2752 SkPath::Iter noPathIter;
2753 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002754
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002755 // Test that setting an empty path works
2756 noPathIter.setPath(p, false);
2757 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002758
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002759 // Test that close path makes no difference for an empty path
2760 noPathIter.setPath(p, true);
2761 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002762
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002763 // Test an iterator with an initial empty path
2764 SkPath::Iter iter(p, false);
2765 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2766
2767 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002768 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002769 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2770
rmistry@google.comd6176b02012-08-23 18:14:13 +00002771
schenney@chromium.org7e963602012-06-13 17:05:43 +00002772 struct iterTestData {
2773 const char* testPath;
2774 const bool forceClose;
2775 const bool consumeDegenerates;
2776 const size_t* numResultPtsPerVerb;
2777 const SkPoint* resultPts;
2778 const SkPath::Verb* resultVerbs;
2779 const size_t numResultVerbs;
2780 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002781
schenney@chromium.org7e963602012-06-13 17:05:43 +00002782 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2783 static const SkPath::Verb resultVerbs2[] = {
2784 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2785 };
2786 static const SkPath::Verb resultVerbs3[] = {
2787 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2788 };
2789 static const SkPath::Verb resultVerbs4[] = {
2790 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2791 };
2792 static const SkPath::Verb resultVerbs5[] = {
2793 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2794 };
2795 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002796 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2797 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2798 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2799 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002800 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002801 static const SkPoint resultPts2[] = {
2802 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2803 };
2804 static const SkPoint resultPts3[] = {
2805 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2806 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2807 };
2808 static const SkPoint resultPts4[] = {
2809 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2810 };
2811 static const SkPoint resultPts5[] = {
2812 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2813 };
2814 static const struct iterTestData gIterTests[] = {
2815 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002816 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2817 { "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 +00002818 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2819 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2820 { "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) },
2821 { "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 +00002822 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2823 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2824 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2825 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2826 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2827 { "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 +00002828 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002829
schenney@chromium.org7e963602012-06-13 17:05:43 +00002830 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2831 p.reset();
2832 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2833 REPORTER_ASSERT(reporter, valid);
2834 iter.setPath(p, gIterTests[i].forceClose);
2835 int j = 0, l = 0;
2836 do {
2837 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2838 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2839 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2840 }
2841 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2842 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2843 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002844
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002845 p.reset();
2846 iter.setPath(p, false);
2847 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2848 p.lineTo(1, 1);
2849 p.close();
2850 iter.setPath(p, false);
2851 REPORTER_ASSERT(reporter, iter.isClosedContour());
2852 p.reset();
2853 iter.setPath(p, true);
2854 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2855 p.lineTo(1, 1);
2856 iter.setPath(p, true);
2857 REPORTER_ASSERT(reporter, iter.isClosedContour());
2858 p.moveTo(0, 0);
2859 p.lineTo(2, 2);
2860 iter.setPath(p, false);
2861 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2862
2863 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2864 // check to see if the result is correct.
2865 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2866 p.reset();
2867 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2868 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2869 iter.setPath(p, true);
2870 iter.next(pts, false);
2871 iter.next(pts, false);
2872 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2873 }
2874
2875 p.reset();
2876 p.quadTo(0, 0, 0, 0);
2877 iter.setPath(p, false);
2878 iter.next(pts, false);
2879 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2880 iter.setPath(p, false);
2881 iter.next(pts, false);
2882 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2883
2884 p.reset();
2885 p.conicTo(0, 0, 0, 0, 0.5f);
2886 iter.setPath(p, false);
2887 iter.next(pts, false);
2888 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2889 iter.setPath(p, false);
2890 iter.next(pts, false);
2891 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2892
2893 p.reset();
2894 p.cubicTo(0, 0, 0, 0, 0, 0);
2895 iter.setPath(p, false);
2896 iter.next(pts, false);
2897 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2898 iter.setPath(p, false);
2899 iter.next(pts, false);
2900 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2901
2902 p.moveTo(1, 1); // add a trailing moveto
2903 iter.setPath(p, false);
2904 iter.next(pts, false);
2905 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2906 iter.setPath(p, false);
2907 iter.next(pts, false);
2908 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2909
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002910 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002911
2912 // Test out mixed degenerate and non-degenerate geometry with Conics
2913 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2914 SkRect r = SkRect::MakeWH(100, 100);
2915 SkRRect rr;
2916 rr.setRectRadii(r, radii);
2917 p.reset();
2918 p.addRRect(rr);
2919 iter.setPath(p, false);
2920 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2921 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2922 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2923 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2924 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002925}
2926
2927static void test_raw_iter(skiatest::Reporter* reporter) {
2928 SkPath p;
2929 SkPoint pts[4];
2930
2931 // Test an iterator with no path
2932 SkPath::RawIter noPathIter;
2933 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2934 // Test that setting an empty path works
2935 noPathIter.setPath(p);
2936 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002937
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002938 // Test an iterator with an initial empty path
2939 SkPath::RawIter iter(p);
2940 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2941
2942 // Test that a move-only path returns the move.
2943 p.moveTo(SK_Scalar1, 0);
2944 iter.setPath(p);
2945 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2946 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2947 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2948 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2949
2950 // No matter how many moves we add, we should get them all back
2951 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2952 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2953 iter.setPath(p);
2954 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2955 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2956 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2957 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2958 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2959 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2960 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2961 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2962 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2963 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2964
2965 // Initial close is never ever stored
2966 p.reset();
2967 p.close();
2968 iter.setPath(p);
2969 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2970
2971 // Move/close sequences
2972 p.reset();
2973 p.close(); // Not stored, no purpose
2974 p.moveTo(SK_Scalar1, 0);
2975 p.close();
2976 p.close(); // Not stored, no purpose
2977 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2978 p.close();
2979 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2980 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2981 p.close();
2982 iter.setPath(p);
2983 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2984 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2985 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2986 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002987 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2988 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2989 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2990 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002991 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2992 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2993 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2994 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2995 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2996 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2997 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002998 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2999
3000 // Generate random paths and verify
3001 SkPoint randomPts[25];
3002 for (int i = 0; i < 5; ++i) {
3003 for (int j = 0; j < 5; ++j) {
3004 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3005 }
3006 }
3007
3008 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003009 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003010 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003011 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003012 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003013
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003014 for (int i = 0; i < 500; ++i) {
3015 p.reset();
3016 bool lastWasClose = true;
3017 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003018 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003019 int numPoints = 0;
3020 int numVerbs = (rand.nextU() >> 16) % 10;
3021 int numIterVerbs = 0;
3022 for (int j = 0; j < numVerbs; ++j) {
3023 do {
3024 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3025 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003026 switch (nextVerb) {
3027 case SkPath::kMove_Verb:
3028 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3029 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003030 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003031 numPoints += 1;
3032 lastWasClose = false;
3033 haveMoveTo = true;
3034 break;
3035 case SkPath::kLine_Verb:
3036 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003037 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003038 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3039 haveMoveTo = true;
3040 }
3041 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3042 p.lineTo(expectedPts[numPoints]);
3043 numPoints += 1;
3044 lastWasClose = false;
3045 break;
3046 case SkPath::kQuad_Verb:
3047 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003048 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003049 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3050 haveMoveTo = true;
3051 }
3052 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3053 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3054 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3055 numPoints += 2;
3056 lastWasClose = false;
3057 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003058 case SkPath::kConic_Verb:
3059 if (!haveMoveTo) {
3060 expectedPts[numPoints++] = lastMoveToPt;
3061 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3062 haveMoveTo = true;
3063 }
3064 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3065 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3066 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3067 rand.nextUScalar1() * 4);
3068 numPoints += 2;
3069 lastWasClose = false;
3070 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003071 case SkPath::kCubic_Verb:
3072 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003073 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003074 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3075 haveMoveTo = true;
3076 }
3077 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3078 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3079 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3080 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3081 expectedPts[numPoints + 2]);
3082 numPoints += 3;
3083 lastWasClose = false;
3084 break;
3085 case SkPath::kClose_Verb:
3086 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003087 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003088 lastWasClose = true;
3089 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003090 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003091 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003092 }
3093 expectedVerbs[numIterVerbs++] = nextVerb;
3094 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003095
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003096 iter.setPath(p);
3097 numVerbs = numIterVerbs;
3098 numIterVerbs = 0;
3099 int numIterPts = 0;
3100 SkPoint lastMoveTo;
3101 SkPoint lastPt;
3102 lastMoveTo.set(0, 0);
3103 lastPt.set(0, 0);
3104 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3105 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3106 numIterVerbs++;
3107 switch (nextVerb) {
3108 case SkPath::kMove_Verb:
3109 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3110 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3111 lastPt = lastMoveTo = pts[0];
3112 numIterPts += 1;
3113 break;
3114 case SkPath::kLine_Verb:
3115 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3116 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3117 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3118 lastPt = pts[1];
3119 numIterPts += 1;
3120 break;
3121 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003122 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003123 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3124 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3125 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3126 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3127 lastPt = pts[2];
3128 numIterPts += 2;
3129 break;
3130 case SkPath::kCubic_Verb:
3131 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3132 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3133 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3134 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3135 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3136 lastPt = pts[3];
3137 numIterPts += 3;
3138 break;
3139 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003140 lastPt = lastMoveTo;
3141 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003142 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003143 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003144 }
3145 }
3146 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3147 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3148 }
3149}
3150
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003151static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003152 const SkPath& path,
3153 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003154 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003155 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003156 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003157 SkPath::Direction isOvalDir;
3158 unsigned isOvalStart;
3159 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003160 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003161 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3162 SkPath tmpPath;
3163 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3164 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003165 }
bsalomon78d58d12016-05-27 09:17:04 -07003166 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003167}
3168
3169static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003170 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003171 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003172 SkPath tmp;
3173
3174 SkMatrix m;
3175 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3176 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003177 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003178 if (SkPathPriv::kCCW_FirstDirection == dir) {
3179 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003180 } else {
reed026beb52015-06-10 14:23:15 -07003181 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3182 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003183 }
3184 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003185}
3186
3187static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003188 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003189 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003190 SkPath tmp;
3191
3192 // translate at small offset
3193 SkMatrix m;
3194 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3195 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003196 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003197
3198 tmp.reset();
3199 m.reset();
3200
3201 // translate at a relatively big offset
3202 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3203 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003204 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003205}
3206
3207static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003208 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003209 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003210 for (int angle = 0; angle < 360; ++angle) {
3211 SkPath tmp;
3212 SkMatrix m;
3213 m.setRotate(SkIntToScalar(angle));
3214 path.transform(m, &tmp);
3215
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003216 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003217 // degrees is not an oval anymore, this can be improved. we made this
3218 // for the simplicity of our implementation.
3219 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003220 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003221 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003222 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003223 }
3224 }
3225}
3226
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003227static void test_circle_mirror_x(skiatest::Reporter* reporter,
3228 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003229 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003230 SkPath tmp;
3231 SkMatrix m;
3232 m.reset();
3233 m.setScaleX(-SK_Scalar1);
3234 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003235 if (SkPathPriv::kCW_FirstDirection == dir) {
3236 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003237 } else {
reed026beb52015-06-10 14:23:15 -07003238 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3239 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003240 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003241 check_for_circle(reporter, tmp, true, dir);
3242}
3243
3244static void test_circle_mirror_y(skiatest::Reporter* reporter,
3245 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003246 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003247 SkPath tmp;
3248 SkMatrix m;
3249 m.reset();
3250 m.setScaleY(-SK_Scalar1);
3251 path.transform(m, &tmp);
3252
reed026beb52015-06-10 14:23:15 -07003253 if (SkPathPriv::kCW_FirstDirection == dir) {
3254 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003255 } else {
reed026beb52015-06-10 14:23:15 -07003256 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3257 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003258 }
3259
3260 check_for_circle(reporter, tmp, true, dir);
3261}
3262
3263static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3264 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003265 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003266 SkPath tmp;
3267 SkMatrix m;
3268 m.reset();
3269 m.setScaleX(-SK_Scalar1);
3270 m.setScaleY(-SK_Scalar1);
3271 path.transform(m, &tmp);
3272
3273 check_for_circle(reporter, tmp, true, dir);
3274}
3275
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003276static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003277 SkPath::Direction inDir) {
3278 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003279 SkPath path;
3280
3281 // circle at origin
reed026beb52015-06-10 14:23:15 -07003282 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3283
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003284 check_for_circle(reporter, path, true, dir);
3285 test_circle_rotate(reporter, path, dir);
3286 test_circle_translate(reporter, path, dir);
3287 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003288 test_circle_mirror_x(reporter, path, dir);
3289 test_circle_mirror_y(reporter, path, dir);
3290 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003291
3292 // circle at an offset at (10, 10)
3293 path.reset();
3294 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003295 SkIntToScalar(20), inDir);
3296
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003297 check_for_circle(reporter, path, true, dir);
3298 test_circle_rotate(reporter, path, dir);
3299 test_circle_translate(reporter, path, dir);
3300 test_circle_skew(reporter, path, dir);
3301 test_circle_mirror_x(reporter, path, dir);
3302 test_circle_mirror_y(reporter, path, dir);
3303 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003304
3305 // Try different starting points for the contour.
3306 for (unsigned start = 0; start < 4; ++start) {
3307 path.reset();
3308 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3309 test_circle_rotate(reporter, path, dir);
3310 test_circle_translate(reporter, path, dir);
3311 test_circle_skew(reporter, path, dir);
3312 test_circle_mirror_x(reporter, path, dir);
3313 test_circle_mirror_y(reporter, path, dir);
3314 test_circle_mirror_xy(reporter, path, dir);
3315 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003316}
3317
3318static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3319 SkPath path;
3320 SkPath circle;
3321 SkPath rect;
3322 SkPath empty;
3323
reed026beb52015-06-10 14:23:15 -07003324 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3325 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003326
3327 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003328 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3329 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3330
3331 SkMatrix translate;
3332 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3333
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003334 // Although all the path concatenation related operations leave
3335 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003336
3337 // empty + circle (translate)
3338 path = empty;
3339 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003340 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003341
3342 // circle + empty (translate)
3343 path = circle;
3344 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003345
reed026beb52015-06-10 14:23:15 -07003346 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003347
3348 // test reverseAddPath
3349 path = circle;
3350 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003351 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003352}
3353
3354static void test_circle(skiatest::Reporter* reporter) {
3355 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3356 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3357
3358 // multiple addCircle()
3359 SkPath path;
3360 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3361 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003362 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003363
3364 // some extra lineTo() would make isOval() fail
3365 path.reset();
3366 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3367 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003368 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003369
3370 // not back to the original point
3371 path.reset();
3372 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3373 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003374 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003375
3376 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003377
3378 // test negative radius
3379 path.reset();
3380 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3381 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003382}
3383
3384static void test_oval(skiatest::Reporter* reporter) {
3385 SkRect rect;
3386 SkMatrix m;
3387 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003388 unsigned start = 0;
3389 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003390
3391 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3392 path.addOval(rect);
3393
bsalomon78d58d12016-05-27 09:17:04 -07003394 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003395 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003396
3397 m.setRotate(SkIntToScalar(90));
3398 SkPath tmp;
3399 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003400 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3401 // is unchanged.
3402 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3403 REPORTER_ASSERT(reporter, 2 == start);
3404 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003405
3406 m.reset();
3407 m.setRotate(SkIntToScalar(30));
3408 tmp.reset();
3409 path.transform(m, &tmp);
3410 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003411 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003412
3413 // since empty path being transformed.
3414 path.reset();
3415 tmp.reset();
3416 m.reset();
3417 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003418 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003419
3420 // empty path is not an oval
3421 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003422 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003423
3424 // only has moveTo()s
3425 tmp.reset();
3426 tmp.moveTo(0, 0);
3427 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003428 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003429
3430 // mimic WebKit's calling convention,
3431 // call moveTo() first and then call addOval()
3432 path.reset();
3433 path.moveTo(0, 0);
3434 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003435 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003436
3437 // copy path
3438 path.reset();
3439 tmp.reset();
3440 tmp.addOval(rect);
3441 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003442 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3443 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3444 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003445}
3446
bungeman@google.coma5809a32013-06-21 15:13:34 +00003447static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3448 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003449
reed@android.com3abec1d2009-03-02 05:36:20 +00003450 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003451 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003452 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003453 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003454 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003455 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3456 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003457 REPORTER_ASSERT(reporter, p == empty);
3458 REPORTER_ASSERT(reporter, !(p != empty));
3459}
3460
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003461static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3462 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003463 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003464 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003465 path->setConvexity(SkPath::kUnknown_Convexity);
3466 REPORTER_ASSERT(reporter, path->isConvex());
3467 path->reset();
3468}
3469
caryclarkd3d1a982014-12-08 04:57:38 -08003470static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3471 SkPath::Direction dir) {
3472 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003473 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003474 path->setConvexity(SkPath::kUnknown_Convexity);
3475 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3476 path->reset();
3477}
3478
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003479static void test_rrect(skiatest::Reporter* reporter) {
3480 SkPath p;
3481 SkRRect rr;
3482 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3483 SkRect r = {10, 20, 30, 40};
3484 rr.setRectRadii(r, radii);
3485 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003486 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003487 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003488 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003489 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003490 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003491 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003492 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003493 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003494 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003495 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003496 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3497 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3498 SkVector save = radii[i];
3499 radii[i].set(0, 0);
3500 rr.setRectRadii(r, radii);
3501 p.addRRect(rr);
3502 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3503 radii[i] = save;
3504 }
3505 p.addRoundRect(r, 0, 0);
3506 SkRect returnedRect;
3507 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3508 REPORTER_ASSERT(reporter, returnedRect == r);
3509 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3510 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3511 rr.setRectRadii(r, zeroRadii);
3512 p.addRRect(rr);
3513 bool closed;
3514 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003515 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003516 REPORTER_ASSERT(reporter, closed);
3517 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3518 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3519 p.addRRect(rr, SkPath::kCW_Direction);
3520 p.addRRect(rr, SkPath::kCW_Direction);
3521 REPORTER_ASSERT(reporter, !p.isConvex());
3522 p.reset();
3523 p.addRRect(rr, SkPath::kCCW_Direction);
3524 p.addRRect(rr, SkPath::kCCW_Direction);
3525 REPORTER_ASSERT(reporter, !p.isConvex());
3526 p.reset();
3527 SkRect emptyR = {10, 20, 10, 30};
3528 rr.setRectRadii(emptyR, radii);
3529 p.addRRect(rr);
3530 REPORTER_ASSERT(reporter, p.isEmpty());
3531 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3532 rr.setRectRadii(largeR, radii);
3533 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003534 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003535
3536 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003537 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3538 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003539 REPORTER_ASSERT(reporter, rr.isEmpty());
3540
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003541 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3542 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3543 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003544}
3545
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003546static void test_arc(skiatest::Reporter* reporter) {
3547 SkPath p;
3548 SkRect emptyOval = {10, 20, 30, 20};
3549 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3550 p.addArc(emptyOval, 1, 2);
3551 REPORTER_ASSERT(reporter, p.isEmpty());
3552 p.reset();
3553 SkRect oval = {10, 20, 30, 40};
3554 p.addArc(oval, 1, 0);
3555 REPORTER_ASSERT(reporter, p.isEmpty());
3556 p.reset();
3557 SkPath cwOval;
3558 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003559 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003560 REPORTER_ASSERT(reporter, p == cwOval);
3561 p.reset();
3562 SkPath ccwOval;
3563 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003564 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003565 REPORTER_ASSERT(reporter, p == ccwOval);
3566 p.reset();
3567 p.addArc(oval, 1, 180);
3568 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003569 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003570 p.setConvexity(SkPath::kUnknown_Convexity);
3571 REPORTER_ASSERT(reporter, p.isConvex());
3572}
3573
bsalomon1978ce22016-05-31 10:42:16 -07003574static inline SkScalar oval_start_index_to_angle(unsigned start) {
3575 switch (start) {
3576 case 0:
3577 return 270.f;
3578 case 1:
3579 return 0.f;
3580 case 2:
3581 return 90.f;
3582 case 3:
3583 return 180.f;
3584 default:
3585 return -1.f;
3586 }
3587}
3588
3589static inline SkScalar canonical_start_angle(float angle) {
3590 while (angle < 0.f) {
3591 angle += 360.f;
3592 }
3593 while (angle >= 360.f) {
3594 angle -= 360.f;
3595 }
3596 return angle;
3597}
3598
3599static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3600 const SkPath& path) {
3601 SkRect r = SkRect::MakeEmpty();
3602 SkPath::Direction d = SkPath::kCCW_Direction;
3603 unsigned s = ~0U;
3604 bool isOval = path.isOval(&r, &d, &s);
3605 REPORTER_ASSERT(reporter, isOval);
3606 SkPath recreatedPath;
3607 recreatedPath.addOval(r, d, s);
3608 REPORTER_ASSERT(reporter, path == recreatedPath);
3609 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3610 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3611}
3612
3613static void test_arc_ovals(skiatest::Reporter* reporter) {
3614 SkRect oval = SkRect::MakeWH(10, 20);
3615 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3616 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3617 SkPath path;
3618 path.addArc(oval, start, sweep);
3619 // SkPath's interfaces for inserting and extracting ovals only allow contours
3620 // to start at multiples of 90 degrees.
3621 if (std::fmod(start, 90.f) == 0) {
3622 check_oval_arc(reporter, start, sweep, path);
3623 } else {
3624 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3625 }
3626 }
3627 // Test start angles that are nearly at valid oval start angles.
3628 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3629 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3630 SkPath path;
3631 path.addArc(oval, start + delta, sweep);
3632 check_oval_arc(reporter, start, sweep, path);
3633 }
3634 }
3635 }
3636}
3637
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003638static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3639 SkScalar x0, SkScalar y0) {
3640 SkPoint pts[4];
3641 SkPath::Verb v = iter->next(pts);
3642 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3643 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3644 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3645}
3646
3647static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3648 SkScalar x1, SkScalar y1) {
3649 SkPoint pts[4];
3650 SkPath::Verb v = iter->next(pts);
3651 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3652 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3653 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3654}
3655
3656static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3657 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3658 SkPoint pts[4];
3659 SkPath::Verb v = iter->next(pts);
3660 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3661 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3662 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3663 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3664 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3665}
3666
3667static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3668 SkPoint pts[4];
3669 SkPath::Verb v = iter->next(pts);
3670 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3671}
3672
3673static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3674 check_done(reporter, p, iter);
3675 p->reset();
3676}
3677
3678static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3679 SkScalar x0, SkScalar y0) {
3680 SkPath::RawIter iter(*p);
3681 check_move(reporter, &iter, x0, y0);
3682 check_done_and_reset(reporter, p, &iter);
3683}
3684
3685static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3686 SkScalar x1, SkScalar y1) {
3687 SkPath::RawIter iter(*p);
3688 check_move(reporter, &iter, 0, 0);
3689 check_line(reporter, &iter, x1, y1);
3690 check_done_and_reset(reporter, p, &iter);
3691}
3692
3693static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3694 SkScalar x1, SkScalar y1) {
3695 SkPath::RawIter iter(*p);
3696 check_move(reporter, &iter, 0, 0);
3697 check_line(reporter, &iter, x1, y1);
3698 check_done(reporter, p, &iter);
3699}
3700
3701static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3702 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3703 SkPath::RawIter iter(*p);
3704 check_move(reporter, &iter, 0, 0);
3705 check_line(reporter, &iter, x1, y1);
3706 check_line(reporter, &iter, x2, y2);
3707 check_done_and_reset(reporter, p, &iter);
3708}
3709
3710static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3711 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3712 SkPath::RawIter iter(*p);
3713 check_move(reporter, &iter, 0, 0);
3714 check_quad(reporter, &iter, x1, y1, x2, y2);
3715 check_done_and_reset(reporter, p, &iter);
3716}
3717
reedd5d27d92015-02-09 13:54:43 -08003718static bool nearly_equal(const SkRect& a, const SkRect& b) {
3719 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3720 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3721 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3722 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3723}
3724
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003725static void test_arcTo(skiatest::Reporter* reporter) {
3726 SkPath p;
3727 p.arcTo(0, 0, 1, 2, 1);
3728 check_path_is_line_and_reset(reporter, &p, 0, 0);
3729 p.arcTo(1, 2, 1, 2, 1);
3730 check_path_is_line_and_reset(reporter, &p, 1, 2);
3731 p.arcTo(1, 2, 3, 4, 0);
3732 check_path_is_line_and_reset(reporter, &p, 1, 2);
3733 p.arcTo(1, 2, 0, 0, 1);
3734 check_path_is_line_and_reset(reporter, &p, 1, 2);
3735 p.arcTo(1, 0, 1, 1, 1);
3736 SkPoint pt;
3737 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3738 p.reset();
3739 p.arcTo(1, 0, 1, -1, 1);
3740 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3741 p.reset();
3742 SkRect oval = {1, 2, 3, 4};
3743 p.arcTo(oval, 0, 0, true);
3744 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3745 p.arcTo(oval, 0, 0, false);
3746 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3747 p.arcTo(oval, 360, 0, true);
3748 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3749 p.arcTo(oval, 360, 0, false);
3750 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003751
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003752 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003753 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003754 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003755 sweep += delta;
3756 delta /= 2;
3757 }
3758 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003759 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003760 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003761 sweep -= delta;
3762 delta /= 2;
3763 }
3764 SkRect noOvalWidth = {1, 2, 0, 3};
3765 p.reset();
3766 p.arcTo(noOvalWidth, 0, 360, false);
3767 REPORTER_ASSERT(reporter, p.isEmpty());
3768
3769 SkRect noOvalHeight = {1, 2, 3, 1};
3770 p.reset();
3771 p.arcTo(noOvalHeight, 0, 360, false);
3772 REPORTER_ASSERT(reporter, p.isEmpty());
3773}
3774
3775static void test_addPath(skiatest::Reporter* reporter) {
3776 SkPath p, q;
3777 p.lineTo(1, 2);
3778 q.moveTo(4, 4);
3779 q.lineTo(7, 8);
3780 q.conicTo(8, 7, 6, 5, 0.5f);
3781 q.quadTo(6, 7, 8, 6);
3782 q.cubicTo(5, 6, 7, 8, 7, 5);
3783 q.close();
3784 p.addPath(q, -4, -4);
3785 SkRect expected = {0, 0, 4, 4};
3786 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3787 p.reset();
3788 p.reverseAddPath(q);
3789 SkRect reverseExpected = {4, 4, 8, 8};
3790 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3791}
3792
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003793static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3794 SkPath p, q;
3795 if (explicitMoveTo) {
3796 p.moveTo(1, 1);
3797 }
3798 p.lineTo(1, 2);
3799 if (explicitMoveTo) {
3800 q.moveTo(2, 1);
3801 }
3802 q.lineTo(2, 2);
3803 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3804 uint8_t verbs[4];
3805 int verbcount = p.getVerbs(verbs, 4);
3806 REPORTER_ASSERT(reporter, verbcount == 4);
3807 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3808 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3809 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3810 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3811}
3812
3813static void test_extendClosedPath(skiatest::Reporter* reporter) {
3814 SkPath p, q;
3815 p.moveTo(1, 1);
3816 p.lineTo(1, 2);
3817 p.lineTo(2, 2);
3818 p.close();
3819 q.moveTo(2, 1);
3820 q.lineTo(2, 3);
3821 p.addPath(q, SkPath::kExtend_AddPathMode);
3822 uint8_t verbs[7];
3823 int verbcount = p.getVerbs(verbs, 7);
3824 REPORTER_ASSERT(reporter, verbcount == 7);
3825 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3826 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3827 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3828 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3829 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3830 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3831 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3832
3833 SkPoint pt;
3834 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3835 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3836 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3837}
3838
3839static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3840 SkPath p, q, r;
3841 // case 1: dst is empty
3842 p.moveTo(2, 1);
3843 p.lineTo(2, 3);
3844 q.addPath(p, mode);
3845 REPORTER_ASSERT(reporter, q == p);
3846 // case 2: src is empty
3847 p.addPath(r, mode);
3848 REPORTER_ASSERT(reporter, q == p);
3849 // case 3: src and dst are empty
3850 q.reset();
3851 q.addPath(r, mode);
3852 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003853}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003854
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003855static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3856 SkPath p;
3857 p.conicTo(1, 2, 3, 4, -1);
3858 check_path_is_line_and_reset(reporter, &p, 3, 4);
3859 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3860 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3861 p.conicTo(1, 2, 3, 4, 1);
3862 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3863}
3864
3865static void test_get_point(skiatest::Reporter* reporter) {
3866 SkPath p;
3867 SkPoint pt = p.getPoint(0);
3868 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003869 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003870 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3871 p.setLastPt(10, 10);
3872 pt = p.getPoint(0);
3873 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003874 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003875 p.rMoveTo(10, 10);
3876 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3877}
3878
3879static void test_contains(skiatest::Reporter* reporter) {
3880 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003881 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3882 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
3883 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
3884 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3885 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
3886 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 -08003887 // this may return true or false, depending on the platform's numerics, but it should not crash
3888 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003889
3890 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003891 p.setFillType(SkPath::kInverseWinding_FillType);
3892 REPORTER_ASSERT(reporter, p.contains(0, 0));
3893 p.setFillType(SkPath::kWinding_FillType);
3894 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3895 p.moveTo(4, 4);
3896 p.lineTo(6, 8);
3897 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003898 // test on edge
3899 REPORTER_ASSERT(reporter, p.contains(6, 4));
3900 REPORTER_ASSERT(reporter, p.contains(5, 6));
3901 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003902 // test quick reject
3903 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3904 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3905 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3906 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3907 // test various crossings in x
3908 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3909 REPORTER_ASSERT(reporter, p.contains(6, 7));
3910 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3911 p.reset();
3912 p.moveTo(4, 4);
3913 p.lineTo(8, 6);
3914 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003915 // test on edge
3916 REPORTER_ASSERT(reporter, p.contains(4, 6));
3917 REPORTER_ASSERT(reporter, p.contains(6, 5));
3918 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003919 // test various crossings in y
3920 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3921 REPORTER_ASSERT(reporter, p.contains(7, 6));
3922 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003923 p.reset();
3924 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003925 p.lineTo(8, 4);
3926 p.lineTo(8, 8);
3927 p.lineTo(4, 8);
3928 // test on vertices
3929 REPORTER_ASSERT(reporter, p.contains(4, 4));
3930 REPORTER_ASSERT(reporter, p.contains(8, 4));
3931 REPORTER_ASSERT(reporter, p.contains(8, 8));
3932 REPORTER_ASSERT(reporter, p.contains(4, 8));
3933 p.reset();
3934 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003935 p.lineTo(6, 8);
3936 p.lineTo(2, 8);
3937 // test on edge
3938 REPORTER_ASSERT(reporter, p.contains(5, 6));
3939 REPORTER_ASSERT(reporter, p.contains(4, 8));
3940 REPORTER_ASSERT(reporter, p.contains(3, 6));
3941 p.reset();
3942 p.moveTo(4, 4);
3943 p.lineTo(0, 6);
3944 p.lineTo(4, 8);
3945 // test on edge
3946 REPORTER_ASSERT(reporter, p.contains(2, 5));
3947 REPORTER_ASSERT(reporter, p.contains(2, 7));
3948 REPORTER_ASSERT(reporter, p.contains(4, 6));
3949 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3950 p.reset();
3951 p.moveTo(4, 0);
3952 p.lineTo(6, 4);
3953 p.lineTo(2, 4);
3954 p.moveTo(4, 0);
3955 p.lineTo(0, 8);
3956 p.lineTo(8, 8);
3957 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3958 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3959 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3960 REPORTER_ASSERT(reporter, p.contains(4, 4));
3961
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003962 // test quads
3963 p.reset();
3964 p.moveTo(4, 4);
3965 p.quadTo(6, 6, 8, 8);
3966 p.quadTo(6, 8, 4, 8);
3967 p.quadTo(4, 6, 4, 4);
3968 REPORTER_ASSERT(reporter, p.contains(5, 6));
3969 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003970 // test quad edge
3971 REPORTER_ASSERT(reporter, p.contains(5, 5));
3972 REPORTER_ASSERT(reporter, p.contains(5, 8));
3973 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003974 // test quad endpoints
3975 REPORTER_ASSERT(reporter, p.contains(4, 4));
3976 REPORTER_ASSERT(reporter, p.contains(8, 8));
3977 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003978
3979 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003980 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3981 p.moveTo(qPts[0]);
3982 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3983 p.quadTo(qPts[index], qPts[index + 1]);
3984 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003985 REPORTER_ASSERT(reporter, p.contains(5, 6));
3986 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003987 // test quad edge
3988 SkPoint halfway;
3989 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3990 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3991 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3992 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003993
caryclark9aacd902015-12-14 08:38:09 -08003994 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003995 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003996 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3997 p.moveTo(kPts[0]);
3998 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3999 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4000 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004001 REPORTER_ASSERT(reporter, p.contains(5, 6));
4002 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004003 // test conic edge
4004 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4005 SkConic conic(&kPts[index], 0.5f);
4006 halfway = conic.evalAt(0.5f);
4007 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4008 }
caryclark9cb5d752015-12-18 04:35:24 -08004009 // test conic end points
4010 REPORTER_ASSERT(reporter, p.contains(4, 4));
4011 REPORTER_ASSERT(reporter, p.contains(8, 8));
4012 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004013
4014 // test cubics
4015 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4016 for (int i = 0; i < 3; ++i) {
4017 p.reset();
4018 p.setFillType(SkPath::kEvenOdd_FillType);
4019 p.moveTo(pts[i].fX, pts[i].fY);
4020 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);
4021 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);
4022 p.close();
4023 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4024 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004025 // test cubic edge
4026 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4027 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4028 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4029 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004030 // test cubic end points
4031 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4032 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4033 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004034 }
4035}
4036
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004037class PathRefTest_Private {
4038public:
4039 static void TestPathRef(skiatest::Reporter* reporter) {
4040 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004041
bungeman6bd52842016-10-27 09:30:08 -07004042 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004043
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004044 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004045
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004046 {
4047 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4048 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4049 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4050 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4051 for (int i = 0; i < kRepeatCnt; ++i) {
4052 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4053 }
4054 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004055 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004056
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004057 {
4058 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4059 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4060 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4061 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4062 for (int i = 0; i < kRepeatCnt; ++i) {
4063 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4064 }
4065 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004066 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004067
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004068 {
4069 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4070 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4071 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4072 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4073 for (int i = 0; i < kRepeatCnt; ++i) {
4074 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4075 }
4076 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004077 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004078
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004079 {
halcanary96fcdcc2015-08-27 07:41:13 -07004080 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004081 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4082 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4083 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4084 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4085 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004086 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004087 for (int i = 0; i < kRepeatCnt; ++i) {
4088 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4089 }
4090 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004091 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004092
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004093 {
4094 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4095 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4096 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4097 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4098 for (int i = 0; i < kRepeatCnt; ++i) {
4099 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4100 }
4101 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004102 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004103 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004104};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004105
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004106static void test_operatorEqual(skiatest::Reporter* reporter) {
4107 SkPath a;
4108 SkPath b;
4109 REPORTER_ASSERT(reporter, a == a);
4110 REPORTER_ASSERT(reporter, a == b);
4111 a.setFillType(SkPath::kInverseWinding_FillType);
4112 REPORTER_ASSERT(reporter, a != b);
4113 a.reset();
4114 REPORTER_ASSERT(reporter, a == b);
4115 a.lineTo(1, 1);
4116 REPORTER_ASSERT(reporter, a != b);
4117 a.reset();
4118 REPORTER_ASSERT(reporter, a == b);
4119 a.lineTo(1, 1);
4120 b.lineTo(1, 2);
4121 REPORTER_ASSERT(reporter, a != b);
4122 a.reset();
4123 a.lineTo(1, 2);
4124 REPORTER_ASSERT(reporter, a == b);
4125}
4126
caryclark66a5d8b2014-06-24 08:30:15 -07004127static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004128 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004129 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004130 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004131 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004132 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004133 if (strlen(str) > 0) {
4134 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4135 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004136 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004137 }
caryclark66a5d8b2014-06-24 08:30:15 -07004138}
4139
4140static void test_dump(skiatest::Reporter* reporter) {
4141 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07004142 compare_dump(reporter, p, false, false, "");
4143 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07004144 p.moveTo(1, 2);
4145 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07004146 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4147 "path.lineTo(3, 4);\n");
4148 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
4149 "path.lineTo(3, 4);\n"
4150 "path.lineTo(1, 2);\n"
4151 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004152 p.reset();
4153 p.moveTo(1, 2);
4154 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07004155 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4156 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004157 p.reset();
4158 p.moveTo(1, 2);
4159 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07004160 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4161 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004162 p.reset();
4163 p.moveTo(1, 2);
4164 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07004165 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4166 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4167 p.reset();
4168 p.moveTo(1, 2);
4169 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004170 compare_dump(reporter, p, false, true,
4171 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4172 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004173 p.reset();
4174 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4175 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4176 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4177 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004178}
4179
senorblanco84cd6212015-08-04 10:01:58 -07004180namespace {
4181
4182class ChangeListener : public SkPathRef::GenIDChangeListener {
4183public:
4184 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4185 virtual ~ChangeListener() {}
4186 void onChange() override {
4187 *fChanged = true;
4188 }
4189private:
4190 bool* fChanged;
4191};
4192
4193}
4194
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004195class PathTest_Private {
4196public:
4197 static void TestPathTo(skiatest::Reporter* reporter) {
4198 SkPath p, q;
4199 p.lineTo(4, 4);
4200 p.reversePathTo(q);
4201 check_path_is_line(reporter, &p, 4, 4);
4202 q.moveTo(-4, -4);
4203 p.reversePathTo(q);
4204 check_path_is_line(reporter, &p, 4, 4);
4205 q.lineTo(7, 8);
4206 q.conicTo(8, 7, 6, 5, 0.5f);
4207 q.quadTo(6, 7, 8, 6);
4208 q.cubicTo(5, 6, 7, 8, 7, 5);
4209 q.close();
4210 p.reversePathTo(q);
4211 SkRect reverseExpected = {-4, -4, 8, 8};
4212 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4213 }
senorblanco84cd6212015-08-04 10:01:58 -07004214
4215 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4216 SkPath p;
4217
4218 bool changed = false;
4219 p.moveTo(0, 0);
4220
4221 // Check that listener is notified on moveTo().
4222
halcanary385fe4d2015-08-26 13:07:48 -07004223 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004224 REPORTER_ASSERT(reporter, !changed);
4225 p.moveTo(10, 0);
4226 REPORTER_ASSERT(reporter, changed);
4227
4228 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004229 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004230 REPORTER_ASSERT(reporter, !changed);
4231 p.lineTo(20, 0);
4232 REPORTER_ASSERT(reporter, changed);
4233
4234 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004235 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004236 REPORTER_ASSERT(reporter, !changed);
4237 p.reset();
4238 REPORTER_ASSERT(reporter, changed);
4239
4240 p.moveTo(0, 0);
4241
4242 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004243 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004244 REPORTER_ASSERT(reporter, !changed);
4245 p.rewind();
4246 REPORTER_ASSERT(reporter, changed);
4247
4248 // Check that listener is notified when pathref is deleted.
4249 {
4250 SkPath q;
4251 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004252 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004253 REPORTER_ASSERT(reporter, !changed);
4254 }
4255 // q went out of scope.
4256 REPORTER_ASSERT(reporter, changed);
4257 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004258};
4259
reed158fabb2016-07-20 10:06:59 -07004260static void test_crbug_629455(skiatest::Reporter* reporter) {
4261 SkPath path;
4262 path.moveTo(0, 0);
4263 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4264 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4265 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4266// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4267 path.lineTo(0, 0);
4268
4269 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4270 SkPaint paint;
4271 paint.setAntiAlias(true);
4272 surface->getCanvas()->drawPath(path, paint);
4273}
4274
liyuqianc78eff92016-11-09 11:18:30 -08004275static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4276 SkPath path;
4277 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4278 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4279 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4280 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4281 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4282 path.close();
4283
4284 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4285 SkPaint paint;
4286 paint.setAntiAlias(true);
4287 surface->getCanvas()->clipPath(path, true);
4288 surface->getCanvas()->drawRectCoords(0, 0, 100, 100, paint);
4289}
4290
Yuqian Lic4f66af2016-11-11 09:36:53 -05004291static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4292 SkPath path;
4293 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4294 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4295 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4296 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4297 path.close();
4298
4299 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4300 SkPaint paint;
4301 paint.setAntiAlias(true);
4302 surface->getCanvas()->drawPath(path, paint);
4303}
4304
caryclark8e7b19d2016-02-18 04:11:48 -08004305static void test_interp(skiatest::Reporter* reporter) {
4306 SkPath p1, p2, out;
4307 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4308 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4309 REPORTER_ASSERT(reporter, p1 == out);
4310 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4311 REPORTER_ASSERT(reporter, p1 == out);
4312 p1.moveTo(0, 2);
4313 p1.lineTo(0, 4);
4314 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4315 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4316 p2.moveTo(6, 0);
4317 p2.lineTo(8, 0);
4318 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4319 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4320 REPORTER_ASSERT(reporter, p2 == out);
4321 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4322 REPORTER_ASSERT(reporter, p1 == out);
4323 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4324 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4325 p1.reset();
4326 p1.moveTo(4, 4);
4327 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4328 p2.reset();
4329 p2.moveTo(4, 2);
4330 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4331 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4332 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4333 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4334 p2.reset();
4335 p2.moveTo(4, 2);
4336 p2.conicTo(6, 3, 6, 5, 1);
4337 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4338 p2.reset();
4339 p2.moveTo(4, 4);
4340 p2.conicTo(5, 4, 5, 5, 0.5f);
4341 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4342}
4343
4344DEF_TEST(PathInterp, reporter) {
4345 test_interp(reporter);
4346}
4347
caryclark9aacd902015-12-14 08:38:09 -08004348DEF_TEST(PathContains, reporter) {
4349 test_contains(reporter);
4350}
4351
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004352DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004353 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004354 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004355 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004356 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004357 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004358 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004359 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004360 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004361 test_fuzz_crbug_662780();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004362
bungeman@google.coma5809a32013-06-21 15:13:34 +00004363 SkTSize<SkScalar>::Make(3,4);
4364
4365 SkPath p, empty;
4366 SkRect bounds, bounds2;
4367 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004368
reed@android.comd252db02009-04-01 18:31:44 +00004369 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004370
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004371 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4372 SkPath& self = p;
4373 p = self;
4374
4375 // this triggers a code path in SkPath::swap which is otherwise unexercised
4376 p.swap(self);
4377
reed@android.com3abec1d2009-03-02 05:36:20 +00004378 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004379
reed@android.com6b82d1a2009-06-03 02:35:01 +00004380 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4381 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004382 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004383 REPORTER_ASSERT(reporter,
4384 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004385 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004386
reed@android.com6b82d1a2009-06-03 02:35:01 +00004387 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004388 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004389
reed@android.com6b82d1a2009-06-03 02:35:01 +00004390 p.addOval(bounds);
4391 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004392 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004393
bungeman@google.coma5809a32013-06-21 15:13:34 +00004394 p.rewind();
4395 test_empty(reporter, p);
4396
reed@android.com3abec1d2009-03-02 05:36:20 +00004397 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004398 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004399 // we have only lines
4400 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004401 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004402
bungeman@google.coma5809a32013-06-21 15:13:34 +00004403 REPORTER_ASSERT(reporter, p != empty);
4404 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004405
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004406 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004407 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4408 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004409 SkPoint pts[4];
4410 int count = p.getPoints(pts, 4);
4411 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004412 uint8_t verbs[6];
4413 verbs[5] = 0xff;
4414 p.getVerbs(verbs, 5);
4415 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4416 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4417 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4418 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4419 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4420 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004421 bounds2.set(pts, 4);
4422 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004423
reed@android.com3abec1d2009-03-02 05:36:20 +00004424 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4425 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004426 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004427
halcanary96fcdcc2015-08-27 07:41:13 -07004428 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004429 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004430 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4431 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004432
reed@android.com3abec1d2009-03-02 05:36:20 +00004433 // now force p to not be a rect
4434 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4435 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004436 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004437
robertphillipsfe7c4272014-12-29 11:36:39 -08004438 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4439 // path has a trailing moveTo. Please see crbug.com\445368)
4440 {
4441 SkRect r;
4442 p.reset();
4443 p.addRect(bounds);
4444 REPORTER_ASSERT(reporter, p.isRect(&r));
4445 REPORTER_ASSERT(reporter, r == bounds);
4446 // add a moveTo outside of our bounds
4447 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4448 REPORTER_ASSERT(reporter, p.isRect(&r));
4449 REPORTER_ASSERT(reporter, r == bounds);
4450 }
4451
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004452 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004453 test_isLine(reporter);
4454 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004455 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004456 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004457 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004458 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004459 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004460 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004461 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004462 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004463 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004464 test_flattening(reporter);
4465 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004466 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004467 test_iter(reporter);
4468 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004469 test_circle(reporter);
4470 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004471 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004472 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004473 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004474 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004475 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004476 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004477 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004478 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004479 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004480 test_tricky_cubic();
4481 test_clipped_cubic();
4482 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004483 test_crbug_493450(reporter);
4484 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004485 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004486 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004487 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004488 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004489 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004490 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004491 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004492 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004493 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004494 test_arcTo(reporter);
4495 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004496 test_addPathMode(reporter, false, false);
4497 test_addPathMode(reporter, true, false);
4498 test_addPathMode(reporter, false, true);
4499 test_addPathMode(reporter, true, true);
4500 test_extendClosedPath(reporter);
4501 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4502 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004503 test_conicTo_special_case(reporter);
4504 test_get_point(reporter);
4505 test_contains(reporter);
4506 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004507 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004508 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004509 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004510 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004511 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004512 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004513 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004514 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004515 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004516}