blob: f064a16f77e7a7e5485c12b0995bf8661688c37d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
bsalomon1978ce22016-05-31 10:42:16 -07008#include <cmath>
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -080010#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000011#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070014#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000015#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000017#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000018#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000019#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070020#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070021#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000022#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000023#include "SkTypes.h"
24#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000026
reed5bcbe912014-12-15 12:28:33 -080027static void set_radii(SkVector radii[4], int index, float rad) {
28 sk_bzero(radii, sizeof(SkVector) * 4);
29 radii[index].set(rad, rad);
30}
31
32static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33 const SkVector radii[4]) {
34 SkRRect rrect;
35 rrect.setRectRadii(bounds, radii);
36 REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38 SkPath path;
39 // this line should not assert in the debug build (from validate)
40 path.addRRect(rrect);
41 REPORTER_ASSERT(reporter, bounds == path.getBounds());
42}
43
caryclark5ccef572015-03-02 10:07:56 -080044static void test_skbug_3469(skiatest::Reporter* reporter) {
45 SkPath path;
46 path.moveTo(20, 20);
47 path.quadTo(20, 50, 80, 50);
48 path.quadTo(20, 50, 20, 80);
49 REPORTER_ASSERT(reporter, !path.isConvex());
50}
51
reed5bcbe912014-12-15 12:28:33 -080052static void test_skbug_3239(skiatest::Reporter* reporter) {
53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
56
57 const float rad = 33436320;
58
59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62 SkVector radii[4];
63 for (int i = 0; i < 4; ++i) {
64 set_radii(radii, i, rad);
65 test_add_rrect(reporter, rectx, radii);
66 test_add_rrect(reporter, recty, radii);
67 }
68}
69
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000070static void make_path_crbug364224(SkPath* path) {
71 path->reset();
72 path->moveTo(3.747501373f, 2.724499941f);
73 path->lineTo(3.747501373f, 3.75f);
74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75 path->lineTo(0.7475013733f, 4.0f);
76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77 path->lineTo(0.4975013733f, 1.0f);
78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79 path->lineTo(3.497501373f, 0.75f);
80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81 path->lineTo(3.715001345f, 0.5512499809f);
82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83 path->lineTo(0.7475013733f, 0.4999999702f);
84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85 path->lineTo(0.2475013733f, 3.75f);
86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87 path->lineTo(3.497501373f, 4.25f);
88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89 path->lineTo(3.997501373f, 2.474750042f);
90 path->lineTo(3.747501373f, 2.724499941f);
91 path->close();
92}
93
94static void make_path_crbug364224_simplified(SkPath* path) {
95 path->moveTo(3.747501373f, 2.724499941f);
96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97 path->close();
98}
99
reedff863bc2016-08-17 07:37:57 -0700100static void test_sect_with_horizontal_needs_pinning() {
101 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
102 // intersection.
103 SkPath path;
104 path.reset();
105 path.moveTo(-540000, -720000);
106 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
107 path.lineTo(1, 1);
108
109 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
110 SkPaint paint;
111 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
112}
113
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000114static void test_path_crbug364224() {
115 SkPath path;
116 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700117 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000118 SkCanvas* canvas = surface->getCanvas();
119
120 make_path_crbug364224_simplified(&path);
121 canvas->drawPath(path, paint);
122
123 make_path_crbug364224(&path);
124 canvas->drawPath(path, paint);
125}
126
caryclark3cebe242016-08-23 09:41:00 -0700127// this is a unit test instead of a GM because it doesn't draw anything
128static void test_fuzz_crbug_638223() {
129 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
130 SkCanvas* canvas = surface->getCanvas();
131 SkPath path;
132 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
133 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
134 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
135 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
136 SkPaint paint;
137 paint.setAntiAlias(true);
138 canvas->drawPath(path, paint);
139}
140
caryclarke97fe832016-09-06 08:54:10 -0700141static void test_fuzz_crbug_643933() {
142 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
143 SkCanvas* canvas = surface->getCanvas();
144 SkPaint paint;
145 paint.setAntiAlias(true);
146 SkPath path;
147 path.moveTo(0, 0);
148 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
149 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
150 SkBits2Float(0x721aee0c)); // 3.0687e+30f
151 canvas->drawPath(path, paint);
152 path.reset();
153 path.moveTo(0, 0);
154 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
155 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
156 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
157 canvas->drawPath(path, paint);
158}
159
caryclarkbac10462016-09-22 10:24:59 -0700160static void test_fuzz_crbug_647922() {
161 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
162 SkCanvas* canvas = surface->getCanvas();
163 SkPaint paint;
164 paint.setAntiAlias(true);
165 SkPath path;
166 path.moveTo(0, 0);
167 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
168 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
169 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
170 canvas->drawPath(path, paint);
171}
172
liyuqian041da382016-11-11 09:59:51 -0800173static void test_fuzz_crbug_662780() {
174 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
175 SkCanvas* canvas = surface->getCanvas();
176 SkPaint paint;
177 paint.setAntiAlias(true);
178 SkPath path;
179 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
180 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
181 // 8, 8, 8.00002f, 8, 0.707107f
182 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
183 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
184 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
185 // 308, 8, 308, 8, 0.707107f
186 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
187 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
188 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
189 // 308, 158, 308, 158, 0.707107f
190 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
191 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
192 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
193 // 8, 158, 8, 158, 0.707107f
194 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
195 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
196 path.close();
197 canvas->clipPath(path, true);
198 canvas->drawRectCoords(0, 0, 250, 250, paint);
199}
200
Yuqian Li20079a92016-11-16 13:07:57 -0500201static void test_mask_overflow() {
202 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
203 SkCanvas* canvas = surface->getCanvas();
204 SkPaint paint;
205 paint.setAntiAlias(true);
206 SkPath path;
207 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
208 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
209 // 440.47f, 341, 437, 344.47f, 437, 348.75f
210 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
211 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
212 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
213 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
214 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
215 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
216 canvas->drawPath(path, paint);
217}
218
piotaixrfac4e0e2014-08-26 11:59:04 -0700219/**
220 * In debug mode, this path was causing an assertion to fail in
221 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
222 */
223static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
224 SkPoint orig, p1, p2, p3;
225 orig = SkPoint::Make(1.f, 1.f);
226 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
227 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
228 p3 = SkPoint::Make(2.f, 2.f);
229
230 path->reset();
231 path->moveTo(orig);
232 path->cubicTo(p1, p2, p3);
233 path->close();
234}
235
236static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
237 SkPath path;
238 make_path_crbugskia2820(&path, reporter);
239
240 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
241 stroke.setStrokeStyle(2 * SK_Scalar1);
242 stroke.applyToPath(&path, path);
243}
244
reed@google.comcc8be772013-10-15 15:35:29 +0000245static void make_path0(SkPath* path) {
246 // from * https://code.google.com/p/skia/issues/detail?id=1706
247
248 path->moveTo(146.939f, 1012.84f);
249 path->lineTo(181.747f, 1009.18f);
250 path->lineTo(182.165f, 1013.16f);
251 path->lineTo(147.357f, 1016.82f);
252 path->lineTo(146.939f, 1012.84f);
253 path->close();
254}
255
256static void make_path1(SkPath* path) {
257 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
258}
259
260typedef void (*PathProc)(SkPath*);
261
262/*
263 * Regression test: we used to crash (overwrite internal storage) during
264 * construction of the region when the path was INVERSE. That is now fixed,
265 * so test these regions (which used to assert/crash).
266 *
267 * https://code.google.com/p/skia/issues/detail?id=1706
268 */
269static void test_path_to_region(skiatest::Reporter* reporter) {
270 PathProc procs[] = {
271 make_path0,
272 make_path1,
273 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000274
reed@google.comcc8be772013-10-15 15:35:29 +0000275 SkRegion clip;
276 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000277
reed@google.comcc8be772013-10-15 15:35:29 +0000278 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
279 SkPath path;
280 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000281
reed@google.comcc8be772013-10-15 15:35:29 +0000282 SkRegion rgn;
283 rgn.setPath(path, clip);
284 path.toggleInverseFillType();
285 rgn.setPath(path, clip);
286 }
287}
288
halcanary4dbbd042016-06-07 17:21:10 -0700289#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000290 #define SUPPRESS_VISIBILITY_WARNING
291#else
292 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
293#endif
294
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000295static void test_path_close_issue1474(skiatest::Reporter* reporter) {
296 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
297 // are relative to the point we close to, not relative to the point we close from.
298 SkPath path;
299 SkPoint last;
300
301 // Test rLineTo().
302 path.rLineTo(0, 100);
303 path.rLineTo(100, 0);
304 path.close(); // Returns us back to 0,0.
305 path.rLineTo(50, 50); // This should go to 50,50.
306
307 path.getLastPt(&last);
308 REPORTER_ASSERT(reporter, 50 == last.fX);
309 REPORTER_ASSERT(reporter, 50 == last.fY);
310
311 // Test rQuadTo().
312 path.rewind();
313 path.rLineTo(0, 100);
314 path.rLineTo(100, 0);
315 path.close();
316 path.rQuadTo(50, 50, 75, 75);
317
318 path.getLastPt(&last);
319 REPORTER_ASSERT(reporter, 75 == last.fX);
320 REPORTER_ASSERT(reporter, 75 == last.fY);
321
322 // Test rConicTo().
323 path.rewind();
324 path.rLineTo(0, 100);
325 path.rLineTo(100, 0);
326 path.close();
327 path.rConicTo(50, 50, 85, 85, 2);
328
329 path.getLastPt(&last);
330 REPORTER_ASSERT(reporter, 85 == last.fX);
331 REPORTER_ASSERT(reporter, 85 == last.fY);
332
333 // Test rCubicTo().
334 path.rewind();
335 path.rLineTo(0, 100);
336 path.rLineTo(100, 0);
337 path.close();
338 path.rCubicTo(50, 50, 85, 85, 95, 95);
339
340 path.getLastPt(&last);
341 REPORTER_ASSERT(reporter, 95 == last.fX);
342 REPORTER_ASSERT(reporter, 95 == last.fY);
343}
344
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000345static void test_gen_id(skiatest::Reporter* reporter) {
346 SkPath a, b;
347 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
348
349 a.moveTo(0, 0);
350 const uint32_t z = a.getGenerationID();
351 REPORTER_ASSERT(reporter, z != b.getGenerationID());
352
353 a.reset();
354 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
355
356 a.moveTo(1, 1);
357 const uint32_t y = a.getGenerationID();
358 REPORTER_ASSERT(reporter, z != y);
359
360 b.moveTo(2, 2);
361 const uint32_t x = b.getGenerationID();
362 REPORTER_ASSERT(reporter, x != y && x != z);
363
364 a.swap(b);
365 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
366
367 b = a;
368 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
369
370 SkPath c(a);
371 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
372
373 c.lineTo(3, 3);
374 const uint32_t w = c.getGenerationID();
375 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
376 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
377 REPORTER_ASSERT(reporter, w != x);
378
djsollen50da1d82015-02-17 09:09:53 -0800379#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000380 static bool kExpectGenIDToIgnoreFill = false;
381#else
382 static bool kExpectGenIDToIgnoreFill = true;
383#endif
384
385 c.toggleInverseFillType();
386 const uint32_t v = c.getGenerationID();
387 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
388
389 c.rewind();
390 REPORTER_ASSERT(reporter, v != c.getGenerationID());
391}
392
reed@google.com3eff3592013-05-08 21:08:21 +0000393// This used to assert in the debug build, as the edges did not all line-up.
394static void test_bad_cubic_crbug234190() {
395 SkPath path;
396 path.moveTo(13.8509f, 3.16858f);
397 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
398 -2.38991e+08f, -4.26573e+08f,
399 -2.41016e+08f, -4.30188e+08f);
400
401 SkPaint paint;
402 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700403 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000404 surface->getCanvas()->drawPath(path, paint);
405}
406
reed@google.com7a90daf2013-04-10 18:44:00 +0000407static void test_bad_cubic_crbug229478() {
408 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000409 { 4595.91064f, -11596.9873f },
410 { 4597.2168f, -11595.9414f },
411 { 4598.52344f, -11594.8955f },
412 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000413 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000414
reed@google.com7a90daf2013-04-10 18:44:00 +0000415 SkPath path;
416 path.moveTo(pts[0]);
417 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000418
reed@google.com7a90daf2013-04-10 18:44:00 +0000419 SkPaint paint;
420 paint.setStyle(SkPaint::kStroke_Style);
421 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000422
reed@google.com7a90daf2013-04-10 18:44:00 +0000423 SkPath dst;
424 // Before the fix, this would infinite-recurse, and run out of stack
425 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700426 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000427}
428
reed@google.com64d62952013-01-18 17:49:28 +0000429static void build_path_170666(SkPath& path) {
430 path.moveTo(17.9459f, 21.6344f);
431 path.lineTo(139.545f, -47.8105f);
432 path.lineTo(139.545f, -47.8105f);
433 path.lineTo(131.07f, -47.3888f);
434 path.lineTo(131.07f, -47.3888f);
435 path.lineTo(122.586f, -46.9532f);
436 path.lineTo(122.586f, -46.9532f);
437 path.lineTo(18076.6f, 31390.9f);
438 path.lineTo(18076.6f, 31390.9f);
439 path.lineTo(18085.1f, 31390.5f);
440 path.lineTo(18085.1f, 31390.5f);
441 path.lineTo(18076.6f, 31390.9f);
442 path.lineTo(18076.6f, 31390.9f);
443 path.lineTo(17955, 31460.3f);
444 path.lineTo(17955, 31460.3f);
445 path.lineTo(17963.5f, 31459.9f);
446 path.lineTo(17963.5f, 31459.9f);
447 path.lineTo(17971.9f, 31459.5f);
448 path.lineTo(17971.9f, 31459.5f);
449 path.lineTo(17.9551f, 21.6205f);
450 path.lineTo(17.9551f, 21.6205f);
451 path.lineTo(9.47091f, 22.0561f);
452 path.lineTo(9.47091f, 22.0561f);
453 path.lineTo(17.9459f, 21.6344f);
454 path.lineTo(17.9459f, 21.6344f);
455 path.close();path.moveTo(0.995934f, 22.4779f);
456 path.lineTo(0.986725f, 22.4918f);
457 path.lineTo(0.986725f, 22.4918f);
458 path.lineTo(17955, 31460.4f);
459 path.lineTo(17955, 31460.4f);
460 path.lineTo(17971.9f, 31459.5f);
461 path.lineTo(17971.9f, 31459.5f);
462 path.lineTo(18093.6f, 31390.1f);
463 path.lineTo(18093.6f, 31390.1f);
464 path.lineTo(18093.6f, 31390);
465 path.lineTo(18093.6f, 31390);
466 path.lineTo(139.555f, -47.8244f);
467 path.lineTo(139.555f, -47.8244f);
468 path.lineTo(122.595f, -46.9671f);
469 path.lineTo(122.595f, -46.9671f);
470 path.lineTo(0.995934f, 22.4779f);
471 path.lineTo(0.995934f, 22.4779f);
472 path.close();
473 path.moveTo(5.43941f, 25.5223f);
474 path.lineTo(798267, -28871.1f);
475 path.lineTo(798267, -28871.1f);
476 path.lineTo(3.12512e+06f, -113102);
477 path.lineTo(3.12512e+06f, -113102);
478 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
479 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
480 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
481 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
482 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
483 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
484 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
485 path.lineTo(2.78271e+08f, -1.00733e+07f);
486 path.lineTo(2.78271e+08f, -1.00733e+07f);
487 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
488 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
489 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
490 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
491 path.lineTo(2.77473e+08f, -1.00444e+07f);
492 path.lineTo(2.77473e+08f, -1.00444e+07f);
493 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
494 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
495 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
496 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
497 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
498 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
499 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
500 path.lineTo(798284, -28872);
501 path.lineTo(798284, -28872);
502 path.lineTo(22.4044f, 24.6677f);
503 path.lineTo(22.4044f, 24.6677f);
504 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
505 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
506 path.close();
507}
508
509static void build_path_simple_170666(SkPath& path) {
510 path.moveTo(126.677f, 24.1591f);
511 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
512}
513
514// This used to assert in the SK_DEBUG build, as the clip step would fail with
515// too-few interations in our cubic-line intersection code. That code now runs
516// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000517static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000518 SkPath path;
519 SkPaint paint;
520 paint.setAntiAlias(true);
521
reede8f30622016-03-23 18:59:25 -0700522 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000523
reed@google.com64d62952013-01-18 17:49:28 +0000524 build_path_simple_170666(path);
525 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000526
reed@google.com64d62952013-01-18 17:49:28 +0000527 build_path_170666(path);
528 surface->getCanvas()->drawPath(path, paint);
529}
530
caryclarke8c56662015-07-14 11:19:26 -0700531
532static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
533 SkScalar tx, SkScalar ty, SkScalar scale) {
534 SkPath smallPath;
535 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
536 bool smallConvex = smallPath.isConvex();
537 SkPath largePath;
538 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
539 SkMatrix matrix;
540 matrix.reset();
541 matrix.preTranslate(100, 100);
542 matrix.preScale(scale, scale);
543 largePath.transform(matrix);
544 bool largeConvex = largePath.isConvex();
545 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
546}
547
548static void test_crbug_493450(skiatest::Reporter* reporter) {
549 const char reducedCase[] =
550 "M0,0"
551 "L0.0002, 0"
552 "L0.0002, 0.0002"
553 "L0.0001, 0.0001"
554 "L0,0.0002"
555 "Z";
556 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
557 const char originalFiddleData[] =
558 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
559 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
560 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
561 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
562 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
563 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
564 826357.3384828606f);
565}
566
567static void test_crbug_495894(skiatest::Reporter* reporter) {
568 const char originalFiddleData[] =
569 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
570 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
571 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
572 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
573 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
574 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
575 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
576 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
577 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
578 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
579 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
580 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
581 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
582 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
583 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
584 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
585 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
586 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
587 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
588 "L-0.33993994441916414,-0.11288906492739594Z";
589 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
590 65536);
591}
592
mbarbella99600d02016-06-01 15:39:47 -0700593static void test_crbug_613918() {
594 SkPath path;
595 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
596 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
597 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
598
599 // This call could lead to an assert or uninitialized read due to a failure
600 // to check the return value from SkCubicClipper::ChopMonoAtY.
601 path.contains(-1.84817e-08f, 1.15465e-08f);
602}
603
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000604static void test_addrect(skiatest::Reporter* reporter) {
605 SkPath path;
606 path.lineTo(0, 0);
607 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700608 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000609
610 path.reset();
611 path.lineTo(FLT_EPSILON, FLT_EPSILON);
612 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700613 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000614
615 path.reset();
616 path.quadTo(0, 0, 0, 0);
617 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700618 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000619
620 path.reset();
621 path.conicTo(0, 0, 0, 0, 0.5f);
622 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700623 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000624
625 path.reset();
626 path.cubicTo(0, 0, 0, 0, 0, 0);
627 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700628 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000629}
630
reed@google.coma8790de2012-10-24 21:04:04 +0000631// Make sure we stay non-finite once we get there (unless we reset or rewind).
632static void test_addrect_isfinite(skiatest::Reporter* reporter) {
633 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000634
reed@google.coma8790de2012-10-24 21:04:04 +0000635 path.addRect(SkRect::MakeWH(50, 100));
636 REPORTER_ASSERT(reporter, path.isFinite());
637
638 path.moveTo(0, 0);
639 path.lineTo(SK_ScalarInfinity, 42);
640 REPORTER_ASSERT(reporter, !path.isFinite());
641
642 path.addRect(SkRect::MakeWH(50, 100));
643 REPORTER_ASSERT(reporter, !path.isFinite());
644
645 path.reset();
646 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000647
reed@google.coma8790de2012-10-24 21:04:04 +0000648 path.addRect(SkRect::MakeWH(50, 100));
649 REPORTER_ASSERT(reporter, path.isFinite());
650}
651
reed@google.com848148e2013-01-15 15:51:59 +0000652static void build_big_path(SkPath* path, bool reducedCase) {
653 if (reducedCase) {
654 path->moveTo(577330, 1971.72f);
655 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
656 } else {
657 path->moveTo(60.1631f, 7.70567f);
658 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
659 path->lineTo(577379, 1977.77f);
660 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
661 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
662 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
663 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
664 path->quadTo(577198, 1972, 577238, 1971.31f);
665 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
666 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
667 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
668 path->moveTo(306.718f, -32.912f);
669 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
670 }
671}
672
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000673static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700674 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000675
676 // This path used to assert, because our cubic-chopping code incorrectly
677 // moved control points after the chop. This test should be run in SK_DEBUG
678 // mode to ensure that we no long assert.
679 SkPath path;
680 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
681 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000682
reed@google.com848148e2013-01-15 15:51:59 +0000683 SkPaint paint;
684 for (int doAA = 0; doAA <= 1; ++doAA) {
685 paint.setAntiAlias(SkToBool(doAA));
686 surface->getCanvas()->drawPath(path, paint);
687 }
688 }
689}
690
reed3b2fb982015-07-29 08:37:13 -0700691static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
692 if (expected != bounds) {
693 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
694 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
695 expected.left(), expected.top(), expected.right(), expected.bottom());
696 }
697}
698
reed91f283b2015-07-28 06:00:50 -0700699static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
700 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700701#if 0
702 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
703 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700704 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
705
706 path.moveTo(-5, -8);
707 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
708
709 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
710 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
711
712 path.moveTo(1, 2);
713 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700714#else
715 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
716
717 path.moveTo(-5, -8); // should set the bounds
718 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
719
720 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
721 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
722
723 path.moveTo(1, 2); // don't expect this to have changed the bounds
724 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
725#endif
reed91f283b2015-07-28 06:00:50 -0700726}
727
reedb1b12f82016-07-13 10:56:53 -0700728#include "SkSurface.h"
729static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
730 SkPath path;
731 path.moveTo(0, 0);
732 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
733
734 SkPaint paint;
735 paint.setAntiAlias(true);
736
737 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
738 surf->getCanvas()->drawPath(path, paint);
739}
740
reed@google.com8cae8352012-09-14 15:18:41 +0000741// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
742// which triggered an assert, from a tricky cubic. This test replicates that
743// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
744// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000745static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000746 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000747 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
748 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
749 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
750 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000751 };
752
753 SkPath path;
754 path.moveTo(pts[0]);
755 path.cubicTo(pts[1], pts[2], pts[3]);
756
757 SkPaint paint;
758 paint.setAntiAlias(true);
759
reede8f30622016-03-23 18:59:25 -0700760 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000761}
reed@android.com3abec1d2009-03-02 05:36:20 +0000762
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000763// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
764//
765static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
766 SkPath path;
767 path.quadTo(157, 366, 286, 208);
768 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000769
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000770 SkMatrix matrix;
771 matrix.setScale(1000*1000, 1000*1000);
772
773 // Be sure that path::transform correctly updates isFinite and the bounds
774 // if the transformation overflows. The previous bug was that isFinite was
775 // set to true in this case, but the bounds were not set to empty (which
776 // they should be).
777 while (path.isFinite()) {
778 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
779 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
780 path.transform(matrix);
781 }
782 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
783
784 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
785 path.transform(matrix);
786 // we need to still be non-finite
787 REPORTER_ASSERT(reporter, !path.isFinite());
788 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
789}
790
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000791static void add_corner_arc(SkPath* path, const SkRect& rect,
792 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000793 int startAngle)
794{
795
796 SkScalar rx = SkMinScalar(rect.width(), xIn);
797 SkScalar ry = SkMinScalar(rect.height(), yIn);
798
799 SkRect arcRect;
800 arcRect.set(-rx, -ry, rx, ry);
801 switch (startAngle) {
802 case 0:
803 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
804 break;
805 case 90:
806 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
807 break;
808 case 180:
809 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
810 break;
811 case 270:
812 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
813 break;
814 default:
815 break;
816 }
817
818 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
819}
820
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000821static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000822 SkScalar xCorner, SkScalar yCorner) {
823 // we are lazy here and use the same x & y for each corner
824 add_corner_arc(path, r, xCorner, yCorner, 270);
825 add_corner_arc(path, r, xCorner, yCorner, 0);
826 add_corner_arc(path, r, xCorner, yCorner, 90);
827 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000828 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000829}
830
831// Chrome creates its own round rects with each corner possibly being different.
832// Performance will suffer if they are not convex.
833// Note: PathBench::ArbRoundRectBench performs almost exactly
834// the same test (but with drawing)
835static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000836 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000837 SkRect r;
838
839 for (int i = 0; i < 5000; ++i) {
840
robertphillips@google.com158618e2012-10-23 16:56:56 +0000841 SkScalar size = rand.nextUScalar1() * 30;
842 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000843 continue;
844 }
845 r.fLeft = rand.nextUScalar1() * 300;
846 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000847 r.fRight = r.fLeft + 2 * size;
848 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000849
850 SkPath temp;
851
852 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
853
854 REPORTER_ASSERT(reporter, temp.isConvex());
855 }
856}
857
robertphillips@google.com158618e2012-10-23 16:56:56 +0000858// Chrome will sometimes create a 0 radius round rect. The degenerate
859// quads prevent the path from being converted to a rect
860// Note: PathBench::ArbRoundRectBench performs almost exactly
861// the same test (but with drawing)
862static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000863 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000864 SkRect r;
865
866 for (int i = 0; i < 5000; ++i) {
867
868 SkScalar size = rand.nextUScalar1() * 30;
869 if (size < SK_Scalar1) {
870 continue;
871 }
872 r.fLeft = rand.nextUScalar1() * 300;
873 r.fTop = rand.nextUScalar1() * 300;
874 r.fRight = r.fLeft + 2 * size;
875 r.fBottom = r.fTop + 2 * size;
876
877 SkPath temp;
878
879 make_arb_round_rect(&temp, r, 0, 0);
880
robertphillips@google.com158618e2012-10-23 16:56:56 +0000881 SkRect result;
882 REPORTER_ASSERT(reporter, temp.isRect(&result));
883 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000884 }
885}
886
reed@google.com0bb18bb2012-07-26 15:20:36 +0000887static void test_rect_isfinite(skiatest::Reporter* reporter) {
888 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000889 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000890 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891
reed@google.com0bb18bb2012-07-26 15:20:36 +0000892 SkRect r;
893 r.setEmpty();
894 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000895 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000896 REPORTER_ASSERT(reporter, !r.isFinite());
897 r.set(0, 0, nan, 0);
898 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000899
reed@google.com0bb18bb2012-07-26 15:20:36 +0000900 SkPoint pts[] = {
901 { 0, 0 },
902 { SK_Scalar1, 0 },
903 { 0, SK_Scalar1 },
904 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000905
reed@google.com0bb18bb2012-07-26 15:20:36 +0000906 bool isFine = r.setBoundsCheck(pts, 3);
907 REPORTER_ASSERT(reporter, isFine);
908 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000909
reed@google.com0bb18bb2012-07-26 15:20:36 +0000910 pts[1].set(inf, 0);
911 isFine = r.setBoundsCheck(pts, 3);
912 REPORTER_ASSERT(reporter, !isFine);
913 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000914
reed@google.com0bb18bb2012-07-26 15:20:36 +0000915 pts[1].set(nan, 0);
916 isFine = r.setBoundsCheck(pts, 3);
917 REPORTER_ASSERT(reporter, !isFine);
918 REPORTER_ASSERT(reporter, r.isEmpty());
919}
920
921static void test_path_isfinite(skiatest::Reporter* reporter) {
922 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000923 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000924 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000925
reed@google.com0bb18bb2012-07-26 15:20:36 +0000926 SkPath path;
927 REPORTER_ASSERT(reporter, path.isFinite());
928
929 path.reset();
930 REPORTER_ASSERT(reporter, path.isFinite());
931
932 path.reset();
933 path.moveTo(SK_Scalar1, 0);
934 REPORTER_ASSERT(reporter, path.isFinite());
935
936 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000937 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000938 REPORTER_ASSERT(reporter, !path.isFinite());
939
940 path.reset();
941 path.moveTo(nan, 0);
942 REPORTER_ASSERT(reporter, !path.isFinite());
943}
944
945static void test_isfinite(skiatest::Reporter* reporter) {
946 test_rect_isfinite(reporter);
947 test_path_isfinite(reporter);
948}
949
fsb1475b02016-01-20 09:51:07 -0800950static void test_islastcontourclosed(skiatest::Reporter* reporter) {
951 SkPath path;
952 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
953 path.moveTo(0, 0);
954 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
955 path.close();
956 REPORTER_ASSERT(reporter, path.isLastContourClosed());
957 path.lineTo(100, 100);
958 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
959 path.moveTo(200, 200);
960 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
961 path.close();
962 REPORTER_ASSERT(reporter, path.isLastContourClosed());
963 path.moveTo(0, 0);
964 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
965}
966
reed@google.com744faba2012-05-29 19:54:52 +0000967// assert that we always
968// start with a moveTo
969// only have 1 moveTo
970// only have Lines after that
971// end with a single close
972// only have (at most) 1 close
973//
974static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000975 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000976 SkPath::RawIter iter(path);
977 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000978
979 bool firstTime = true;
980 bool foundClose = false;
981 for (;;) {
982 switch (iter.next(pts)) {
983 case SkPath::kMove_Verb:
984 REPORTER_ASSERT(reporter, firstTime);
985 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
986 srcPts++;
987 firstTime = false;
988 break;
989 case SkPath::kLine_Verb:
990 REPORTER_ASSERT(reporter, !firstTime);
991 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
992 srcPts++;
993 break;
994 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000995 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000996 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000997 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000998 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000999 break;
reed@google.com744faba2012-05-29 19:54:52 +00001000 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001001 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +00001002 break;
1003 case SkPath::kClose_Verb:
1004 REPORTER_ASSERT(reporter, !firstTime);
1005 REPORTER_ASSERT(reporter, !foundClose);
1006 REPORTER_ASSERT(reporter, expectClose);
1007 foundClose = true;
1008 break;
1009 case SkPath::kDone_Verb:
1010 goto DONE;
1011 }
1012 }
1013DONE:
1014 REPORTER_ASSERT(reporter, foundClose == expectClose);
1015}
1016
1017static void test_addPoly(skiatest::Reporter* reporter) {
1018 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001019 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001020
reed@google.com744faba2012-05-29 19:54:52 +00001021 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1022 pts[i].fX = rand.nextSScalar1();
1023 pts[i].fY = rand.nextSScalar1();
1024 }
1025
1026 for (int doClose = 0; doClose <= 1; ++doClose) {
1027 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1028 SkPath path;
bsalomon98806072014-12-12 15:11:17 -08001029 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001030 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +00001031 }
1032 }
1033}
1034
reed@google.com8b06f1a2012-05-29 12:03:46 +00001035static void test_strokerec(skiatest::Reporter* reporter) {
1036 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1037 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001038
reed@google.com8b06f1a2012-05-29 12:03:46 +00001039 rec.setHairlineStyle();
1040 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001041
reed@google.com8b06f1a2012-05-29 12:03:46 +00001042 rec.setStrokeStyle(SK_Scalar1, false);
1043 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001044
reed@google.com8b06f1a2012-05-29 12:03:46 +00001045 rec.setStrokeStyle(SK_Scalar1, true);
1046 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047
reed@google.com8b06f1a2012-05-29 12:03:46 +00001048 rec.setStrokeStyle(0, false);
1049 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001050
reed@google.com8b06f1a2012-05-29 12:03:46 +00001051 rec.setStrokeStyle(0, true);
1052 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1053}
1054
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001055// Set this for paths that don't have a consistent direction such as a bowtie.
1056// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001057const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001058
1059static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001060 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001061 if (expected == kDontCheckDir) {
1062 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001063 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001064 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1065
reed026beb52015-06-10 14:23:15 -07001066 SkPathPriv::FirstDirection dir;
1067 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001068 REPORTER_ASSERT(reporter, dir == expected);
1069 } else {
reed026beb52015-06-10 14:23:15 -07001070 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001071 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001072}
1073
reed@google.com3e71a882012-01-10 18:44:37 +00001074static void test_direction(skiatest::Reporter* reporter) {
1075 size_t i;
1076 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001077 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001078 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1079 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1080 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001081
1082 static const char* gDegen[] = {
1083 "M 10 10",
1084 "M 10 10 M 20 20",
1085 "M 10 10 L 20 20",
1086 "M 10 10 L 10 10 L 10 10",
1087 "M 10 10 Q 10 10 10 10",
1088 "M 10 10 C 10 10 10 10 10 10",
1089 };
1090 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1091 path.reset();
1092 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1093 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001094 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001095 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001096
reed@google.com3e71a882012-01-10 18:44:37 +00001097 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001098 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001099 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001100 "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 +00001101 // rect with top two corners replaced by cubics with identical middle
1102 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001103 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1104 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001105 };
1106 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1107 path.reset();
1108 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1109 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001110 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001111 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001112
reed@google.com3e71a882012-01-10 18:44:37 +00001113 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001114 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001115 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001116 "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 +00001117 // rect with top two corners replaced by cubics with identical middle
1118 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001119 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1120 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001121 };
1122 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1123 path.reset();
1124 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1125 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001126 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001127 }
reed@google.comac8543f2012-01-30 20:51:25 +00001128
1129 // Test two donuts, each wound a different direction. Only the outer contour
1130 // determines the cheap direction
1131 path.reset();
1132 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1133 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001134 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001135
reed@google.comac8543f2012-01-30 20:51:25 +00001136 path.reset();
1137 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1138 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001139 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001140
1141 // triangle with one point really far from the origin.
1142 path.reset();
1143 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001144 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001145 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1146 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001147 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001148
1149 path.reset();
1150 path.conicTo(20, 0, 20, 20, 0.5f);
1151 path.close();
reed026beb52015-06-10 14:23:15 -07001152 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001153
1154 path.reset();
1155 path.lineTo(1, 1e7f);
1156 path.lineTo(1e7f, 2e7f);
1157 path.close();
1158 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001159 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001160}
1161
reed@google.comffdb0182011-11-14 19:29:14 +00001162static void add_rect(SkPath* path, const SkRect& r) {
1163 path->moveTo(r.fLeft, r.fTop);
1164 path->lineTo(r.fRight, r.fTop);
1165 path->lineTo(r.fRight, r.fBottom);
1166 path->lineTo(r.fLeft, r.fBottom);
1167 path->close();
1168}
1169
1170static void test_bounds(skiatest::Reporter* reporter) {
1171 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001172 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1173 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1174 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1175 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001176 };
1177
1178 SkPath path0, path1;
1179 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1180 path0.addRect(rects[i]);
1181 add_rect(&path1, rects[i]);
1182 }
1183
1184 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1185}
1186
reed@google.com55b5f4b2011-09-07 12:23:41 +00001187static void stroke_cubic(const SkPoint pts[4]) {
1188 SkPath path;
1189 path.moveTo(pts[0]);
1190 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001191
reed@google.com55b5f4b2011-09-07 12:23:41 +00001192 SkPaint paint;
1193 paint.setStyle(SkPaint::kStroke_Style);
1194 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001195
reed@google.com55b5f4b2011-09-07 12:23:41 +00001196 SkPath fill;
1197 paint.getFillPath(path, &fill);
1198}
1199
1200// just ensure this can run w/o any SkASSERTS firing in the debug build
1201// we used to assert due to differences in how we determine a degenerate vector
1202// but that was fixed with the introduction of SkPoint::CanNormalize
1203static void stroke_tiny_cubic() {
1204 SkPoint p0[] = {
1205 { 372.0f, 92.0f },
1206 { 372.0f, 92.0f },
1207 { 372.0f, 92.0f },
1208 { 372.0f, 92.0f },
1209 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001210
reed@google.com55b5f4b2011-09-07 12:23:41 +00001211 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001212
reed@google.com55b5f4b2011-09-07 12:23:41 +00001213 SkPoint p1[] = {
1214 { 372.0f, 92.0f },
1215 { 372.0007f, 92.000755f },
1216 { 371.99927f, 92.003922f },
1217 { 371.99826f, 92.003899f },
1218 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001219
reed@google.com55b5f4b2011-09-07 12:23:41 +00001220 stroke_cubic(p1);
1221}
1222
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001223static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1224 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001225 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001226 SkPoint mv;
1227 SkPoint pts[4];
1228 SkPath::Verb v;
1229 int nMT = 0;
1230 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001231 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001232 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1233 switch (v) {
1234 case SkPath::kMove_Verb:
1235 mv = pts[0];
1236 ++nMT;
1237 break;
1238 case SkPath::kClose_Verb:
1239 REPORTER_ASSERT(reporter, mv == pts[0]);
1240 ++nCL;
1241 break;
1242 default:
1243 break;
1244 }
1245 }
1246 // if we force a close on the interator we should have a close
1247 // for every moveTo
1248 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1249 }
1250}
1251
1252static void test_close(skiatest::Reporter* reporter) {
1253 SkPath closePt;
1254 closePt.moveTo(0, 0);
1255 closePt.close();
1256 check_close(reporter, closePt);
1257
1258 SkPath openPt;
1259 openPt.moveTo(0, 0);
1260 check_close(reporter, openPt);
1261
1262 SkPath empty;
1263 check_close(reporter, empty);
1264 empty.close();
1265 check_close(reporter, empty);
1266
1267 SkPath rect;
1268 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1269 check_close(reporter, rect);
1270 rect.close();
1271 check_close(reporter, rect);
1272
1273 SkPath quad;
1274 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1275 check_close(reporter, quad);
1276 quad.close();
1277 check_close(reporter, quad);
1278
1279 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001280 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001281 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1282 check_close(reporter, cubic);
1283 cubic.close();
1284 check_close(reporter, cubic);
1285
1286 SkPath line;
1287 line.moveTo(SK_Scalar1, SK_Scalar1);
1288 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1289 check_close(reporter, line);
1290 line.close();
1291 check_close(reporter, line);
1292
1293 SkPath rect2;
1294 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1295 rect2.close();
1296 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1297 check_close(reporter, rect2);
1298 rect2.close();
1299 check_close(reporter, rect2);
1300
1301 SkPath oval3;
1302 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1303 oval3.close();
1304 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1305 check_close(reporter, oval3);
1306 oval3.close();
1307 check_close(reporter, oval3);
1308
1309 SkPath moves;
1310 moves.moveTo(SK_Scalar1, SK_Scalar1);
1311 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1312 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1313 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1314 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001315
1316 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001317}
1318
reed@google.com7c424812011-05-15 04:38:34 +00001319static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1320 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001321 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1322 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001323 REPORTER_ASSERT(reporter, c == expected);
1324}
1325
caryclarkb4216502015-03-02 13:02:34 -08001326static void test_path_crbug389050(skiatest::Reporter* reporter) {
1327 SkPath tinyConvexPolygon;
1328 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1329 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1330 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1331 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1332 tinyConvexPolygon.close();
1333 tinyConvexPolygon.getConvexity();
1334 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001335 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001336
1337 SkPath platTriangle;
1338 platTriangle.moveTo(0, 0);
1339 platTriangle.lineTo(200, 0);
1340 platTriangle.lineTo(100, 0.04f);
1341 platTriangle.close();
1342 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001343 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001344
1345 platTriangle.reset();
1346 platTriangle.moveTo(0, 0);
1347 platTriangle.lineTo(200, 0);
1348 platTriangle.lineTo(100, 0.03f);
1349 platTriangle.close();
1350 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001351 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001352}
1353
reed@google.com7c424812011-05-15 04:38:34 +00001354static void test_convexity2(skiatest::Reporter* reporter) {
1355 SkPath pt;
1356 pt.moveTo(0, 0);
1357 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001358 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001359 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001360
reed@google.com7c424812011-05-15 04:38:34 +00001361 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001362 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1363 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001364 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001365 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001366 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001367
reed@google.com7c424812011-05-15 04:38:34 +00001368 SkPath triLeft;
1369 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001370 triLeft.lineTo(SK_Scalar1, 0);
1371 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001372 triLeft.close();
1373 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001374 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001375
reed@google.com7c424812011-05-15 04:38:34 +00001376 SkPath triRight;
1377 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001378 triRight.lineTo(-SK_Scalar1, 0);
1379 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001380 triRight.close();
1381 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001382 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001383
reed@google.com7c424812011-05-15 04:38:34 +00001384 SkPath square;
1385 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001386 square.lineTo(SK_Scalar1, 0);
1387 square.lineTo(SK_Scalar1, SK_Scalar1);
1388 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001389 square.close();
1390 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001391 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001392
reed@google.com7c424812011-05-15 04:38:34 +00001393 SkPath redundantSquare;
1394 redundantSquare.moveTo(0, 0);
1395 redundantSquare.lineTo(0, 0);
1396 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001397 redundantSquare.lineTo(SK_Scalar1, 0);
1398 redundantSquare.lineTo(SK_Scalar1, 0);
1399 redundantSquare.lineTo(SK_Scalar1, 0);
1400 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1401 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1402 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1403 redundantSquare.lineTo(0, SK_Scalar1);
1404 redundantSquare.lineTo(0, SK_Scalar1);
1405 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001406 redundantSquare.close();
1407 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001408 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001409
reed@google.com7c424812011-05-15 04:38:34 +00001410 SkPath bowTie;
1411 bowTie.moveTo(0, 0);
1412 bowTie.lineTo(0, 0);
1413 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001414 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1415 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1416 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1417 bowTie.lineTo(SK_Scalar1, 0);
1418 bowTie.lineTo(SK_Scalar1, 0);
1419 bowTie.lineTo(SK_Scalar1, 0);
1420 bowTie.lineTo(0, SK_Scalar1);
1421 bowTie.lineTo(0, SK_Scalar1);
1422 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001423 bowTie.close();
1424 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001425 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001426
reed@google.com7c424812011-05-15 04:38:34 +00001427 SkPath spiral;
1428 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001429 spiral.lineTo(100*SK_Scalar1, 0);
1430 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1431 spiral.lineTo(0, 100*SK_Scalar1);
1432 spiral.lineTo(0, 50*SK_Scalar1);
1433 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1434 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001435 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001436 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001437 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001438
reed@google.com7c424812011-05-15 04:38:34 +00001439 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001440 dent.moveTo(0, 0);
1441 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1442 dent.lineTo(0, 100*SK_Scalar1);
1443 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1444 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001445 dent.close();
1446 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001447 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001448
halcanary6950de62015-11-07 05:29:00 -08001449 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001450 SkPath strokedSin;
1451 for (int i = 0; i < 2000; i++) {
1452 SkScalar x = SkIntToScalar(i) / 2;
1453 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1454 if (0 == i) {
1455 strokedSin.moveTo(x, y);
1456 } else {
1457 strokedSin.lineTo(x, y);
1458 }
1459 }
1460 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1461 stroke.setStrokeStyle(2 * SK_Scalar1);
1462 stroke.applyToPath(&strokedSin, strokedSin);
1463 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1464 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001465
1466 // http://crbug.com/412640
1467 SkPath degenerateConcave;
1468 degenerateConcave.moveTo(148.67912f, 191.875f);
1469 degenerateConcave.lineTo(470.37695f, 7.5f);
1470 degenerateConcave.lineTo(148.67912f, 191.875f);
1471 degenerateConcave.lineTo(41.446522f, 376.25f);
1472 degenerateConcave.lineTo(-55.971577f, 460.0f);
1473 degenerateConcave.lineTo(41.446522f, 376.25f);
1474 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001475 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001476
1477 // http://crbug.com/433683
1478 SkPath badFirstVector;
1479 badFirstVector.moveTo(501.087708f, 319.610352f);
1480 badFirstVector.lineTo(501.087708f, 319.610352f);
1481 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1482 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1483 badFirstVector.lineTo(301.557678f, 98.044601f);
1484 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1485 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1486 badFirstVector.lineTo(504.912292f, 316.389648f);
1487 badFirstVector.lineTo(501.087708f, 319.610352f);
1488 badFirstVector.close();
1489 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001490}
1491
reed@android.com6b82d1a2009-06-03 02:35:01 +00001492static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1493 const SkRect& bounds) {
1494 REPORTER_ASSERT(reporter, p.isConvex());
1495 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001496
reed@android.com6b82d1a2009-06-03 02:35:01 +00001497 SkPath p2(p);
1498 REPORTER_ASSERT(reporter, p2.isConvex());
1499 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1500
1501 SkPath other;
1502 other.swap(p2);
1503 REPORTER_ASSERT(reporter, other.isConvex());
1504 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1505}
1506
reed@google.com04863fa2011-05-15 04:08:24 +00001507static void setFromString(SkPath* path, const char str[]) {
1508 bool first = true;
1509 while (str) {
1510 SkScalar x, y;
1511 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001512 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001513 break;
1514 }
1515 str = SkParse::FindScalar(str, &y);
1516 SkASSERT(str);
1517 if (first) {
1518 path->moveTo(x, y);
1519 first = false;
1520 } else {
1521 path->lineTo(x, y);
1522 }
1523 }
1524}
1525
1526static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001527 SkPath path;
1528
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001529 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001530 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001531 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001532 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001533 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1534
reed@google.com04863fa2011-05-15 04:08:24 +00001535 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001536 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001537 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001538 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001539
reed@google.com04863fa2011-05-15 04:08:24 +00001540 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001541 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001542 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001543 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001544
liyuqianbdabcc42016-11-03 11:40:19 -07001545 path.reset();
1546 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1547 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1548
reed@google.com04863fa2011-05-15 04:08:24 +00001549 static const struct {
reed026beb52015-06-10 14:23:15 -07001550 const char* fPathStr;
1551 SkPath::Convexity fExpectedConvexity;
1552 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001553 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001554 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1555 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1556 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1557 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1558 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1559 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001560 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001561 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001562 };
1563
1564 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1565 SkPath path;
1566 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001567 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1568 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001569 // check after setting the initial convex and direction
1570 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1571 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001572 SkPathPriv::FirstDirection dir;
1573 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1574 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001575 ^ foundDir);
1576 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1577 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1578 }
1579 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1580 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001581 }
caryclarkd3d1a982014-12-08 04:57:38 -08001582
1583 static const SkPoint nonFinitePts[] = {
1584 { SK_ScalarInfinity, 0 },
1585 { 0, SK_ScalarInfinity },
1586 { SK_ScalarInfinity, SK_ScalarInfinity },
1587 { SK_ScalarNegativeInfinity, 0},
1588 { 0, SK_ScalarNegativeInfinity },
1589 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1590 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1591 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1592 { SK_ScalarNaN, 0 },
1593 { 0, SK_ScalarNaN },
1594 { SK_ScalarNaN, SK_ScalarNaN },
1595 };
1596
1597 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1598
1599 static const SkPoint finitePts[] = {
1600 { SK_ScalarMax, 0 },
1601 { 0, SK_ScalarMax },
1602 { SK_ScalarMax, SK_ScalarMax },
1603 { SK_ScalarMin, 0 },
1604 { 0, SK_ScalarMin },
1605 { SK_ScalarMin, SK_ScalarMin },
1606 };
1607
1608 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1609
1610 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1611 int i = (int) (index % nonFinitePtsCount);
1612 int f = (int) (index % finitePtsCount);
1613 int g = (int) ((f + 1) % finitePtsCount);
1614 path.reset();
1615 switch (index % 13) {
1616 case 0: path.lineTo(nonFinitePts[i]); break;
1617 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1618 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1619 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1620 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1621 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1622 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1623 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1624 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1625 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1626 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1627 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1628 case 12: path.moveTo(nonFinitePts[i]); break;
1629 }
1630 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1631 }
1632
1633 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1634 int f = (int) (index % finitePtsCount);
1635 int g = (int) ((f + 1) % finitePtsCount);
1636 path.reset();
1637 int curveSelect = index % 11;
1638 switch (curveSelect) {
1639 case 0: path.moveTo(finitePts[f]); break;
1640 case 1: path.lineTo(finitePts[f]); break;
1641 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1642 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1643 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1644 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1645 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1646 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1647 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1648 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1649 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1650 }
1651 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1652 : SkPath::kUnknown_Convexity);
1653 }
1654
reed@google.com04863fa2011-05-15 04:08:24 +00001655}
1656
reed@google.com7e6c4d12012-05-10 14:05:43 +00001657static void test_isLine(skiatest::Reporter* reporter) {
1658 SkPath path;
1659 SkPoint pts[2];
1660 const SkScalar value = SkIntToScalar(5);
1661
halcanary96fcdcc2015-08-27 07:41:13 -07001662 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001663
reed@google.com7e6c4d12012-05-10 14:05:43 +00001664 // set some non-zero values
1665 pts[0].set(value, value);
1666 pts[1].set(value, value);
1667 REPORTER_ASSERT(reporter, !path.isLine(pts));
1668 // check that pts was untouched
1669 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1670 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1671
1672 const SkScalar moveX = SkIntToScalar(1);
1673 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001674 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001675
1676 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001677 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001678 REPORTER_ASSERT(reporter, !path.isLine(pts));
1679 // check that pts was untouched
1680 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1681 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1682
1683 const SkScalar lineX = SkIntToScalar(2);
1684 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001685 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001686
1687 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001688 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001689
1690 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1691 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1692 REPORTER_ASSERT(reporter, path.isLine(pts));
1693 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1694 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1695
1696 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001697 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001698 REPORTER_ASSERT(reporter, !path.isLine(pts));
1699 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1700 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001701
1702 path.reset();
1703 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001704 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001705}
1706
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001707static void test_conservativelyContains(skiatest::Reporter* reporter) {
1708 SkPath path;
1709
1710 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1711 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1712
1713 // A circle that bounds kBaseRect (with a significant amount of slop)
1714 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001715 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001716 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1717
1718 // round-rect radii
1719 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001720
caryclark@google.com56f233a2012-11-19 13:06:06 +00001721 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001722 SkRect fQueryRect;
1723 bool fInRect;
1724 bool fInCircle;
1725 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001726 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001727 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001728 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001729
1730 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001731 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1732 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1733 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1734 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001735 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001736
1737 // rects with edges off by one from kBaseRect's edges
1738 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1739 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001740 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001741 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1742 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001743 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001744 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1745 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001746 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001747 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1748 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001749 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001750 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1751 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001752 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001753 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1754 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001755 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001756 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1757 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001758 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001759
1760 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001761 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1762 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1763 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1764 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001765
1766 // far away rect
1767 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1768 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001769 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001770
1771 // very large rect containing kBaseRect
1772 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1773 kBaseRect.fTop - 5 * kBaseRect.height(),
1774 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001775 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001776
1777 // skinny rect that spans same y-range as kBaseRect
1778 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1779 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001780 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001781
1782 // short rect that spans same x-range as kBaseRect
1783 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001784 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001785
1786 // skinny rect that spans slightly larger y-range than kBaseRect
1787 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1788 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001789 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001790
1791 // short rect that spans slightly larger x-range than kBaseRect
1792 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1793 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001794 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001795 };
1796
1797 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001798 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001799 SkRect qRect = kQueries[q].fQueryRect;
1800 if (inv & 0x1) {
1801 SkTSwap(qRect.fLeft, qRect.fRight);
1802 }
1803 if (inv & 0x2) {
1804 SkTSwap(qRect.fTop, qRect.fBottom);
1805 }
1806 for (int d = 0; d < 2; ++d) {
1807 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1808 path.reset();
1809 path.addRect(kBaseRect, dir);
1810 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1811 path.conservativelyContainsRect(qRect));
1812
1813 path.reset();
1814 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1815 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1816 path.conservativelyContainsRect(qRect));
1817
1818 path.reset();
1819 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1820 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1821 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001822
1823 path.reset();
1824 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1825 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1826 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1827 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1828 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1829 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1830 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1831 path.close();
1832 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1833 path.conservativelyContainsRect(qRect));
1834
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001835 }
1836 // Slightly non-convex shape, shouldn't contain any rects.
1837 path.reset();
1838 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001839 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001840 path.lineTo(SkIntToScalar(100), 0);
1841 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1842 path.lineTo(0, SkIntToScalar(100));
1843 path.close();
1844 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1845 }
1846 }
1847
1848 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1849 path.reset();
1850 path.moveTo(0, 0);
1851 path.lineTo(SkIntToScalar(100), 0);
1852 path.lineTo(0, SkIntToScalar(100));
1853
1854 // inside, on along top edge
1855 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1856 SkIntToScalar(10),
1857 SkIntToScalar(10))));
1858 // above
1859 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1860 SkRect::MakeXYWH(SkIntToScalar(50),
1861 SkIntToScalar(-10),
1862 SkIntToScalar(10),
1863 SkIntToScalar(10))));
1864 // to the left
1865 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1866 SkIntToScalar(5),
1867 SkIntToScalar(5),
1868 SkIntToScalar(5))));
1869
1870 // outside the diagonal edge
1871 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1872 SkIntToScalar(200),
1873 SkIntToScalar(20),
1874 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001875
bsalomonb17c1292014-08-28 14:04:55 -07001876
1877 // Test that multiple move commands do not cause asserts.
1878
1879 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001880 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001881 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1882#if 0
1883 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1884 SkIntToScalar(10),
1885 SkIntToScalar(10))));
1886#endif
1887
1888 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001889 path.reset();
1890 path.moveTo(100, 100);
1891 path.moveTo(0, 0);
1892 path.lineTo(SkIntToScalar(100), 0);
1893 path.lineTo(0, SkIntToScalar(100));
1894
1895 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1896 SkIntToScalar(10),
1897 SkIntToScalar(10))));
1898
bsalomonb17c1292014-08-28 14:04:55 -07001899 // Test that multiple move commands do not cause asserts and that the function
1900 // is not confused by the multiple moves.
1901 path.reset();
1902 path.moveTo(0, 0);
1903 path.lineTo(SkIntToScalar(100), 0);
1904 path.lineTo(0, SkIntToScalar(100));
1905 path.moveTo(0, SkIntToScalar(200));
1906 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1907 path.lineTo(0, SkIntToScalar(300));
1908
1909 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1910 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1911 SkIntToScalar(10),
1912 SkIntToScalar(10))));
1913
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001914 path.reset();
1915 path.lineTo(100, 100);
1916 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001917}
1918
reed@google.comf32322b2013-10-16 15:14:04 +00001919static void test_isRect_open_close(skiatest::Reporter* reporter) {
1920 SkPath path;
1921 bool isClosed;
1922
1923 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001924 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001925
halcanary96fcdcc2015-08-27 07:41:13 -07001926 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001927 REPORTER_ASSERT(reporter, isClosed);
1928}
1929
caryclark@google.comf1316942011-07-26 19:54:45 +00001930// Simple isRect test is inline TestPath, below.
1931// test_isRect provides more extensive testing.
1932static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001933 test_isRect_open_close(reporter);
1934
caryclark@google.comf1316942011-07-26 19:54:45 +00001935 // passing tests (all moveTo / lineTo...
1936 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1937 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1938 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1939 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1940 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1941 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1942 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1943 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1944 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001945 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1946 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 +00001947 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1948 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1949 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001950 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001951
caryclark@google.comf1316942011-07-26 19:54:45 +00001952 // failing tests
1953 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1954 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1955 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1956 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1957 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1958 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1959 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1960 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001961 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1962 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1963 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001964
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001965 // no close, but we should detect them as fillably the same as a rect
1966 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1967 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1968 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1969
1970 // like c2, but we double-back on ourselves
1971 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1972 // like c2, but we overshoot the start point
1973 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1974 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001975
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001976 struct IsRectTest {
1977 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001978 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001979 bool fClose;
1980 bool fIsRect;
1981 } tests[] = {
1982 { r1, SK_ARRAY_COUNT(r1), true, true },
1983 { r2, SK_ARRAY_COUNT(r2), true, true },
1984 { r3, SK_ARRAY_COUNT(r3), true, true },
1985 { r4, SK_ARRAY_COUNT(r4), true, true },
1986 { r5, SK_ARRAY_COUNT(r5), true, true },
1987 { r6, SK_ARRAY_COUNT(r6), true, true },
1988 { r7, SK_ARRAY_COUNT(r7), true, true },
1989 { r8, SK_ARRAY_COUNT(r8), true, true },
1990 { r9, SK_ARRAY_COUNT(r9), true, true },
1991 { ra, SK_ARRAY_COUNT(ra), true, true },
1992 { rb, SK_ARRAY_COUNT(rb), true, true },
1993 { rc, SK_ARRAY_COUNT(rc), true, true },
1994 { rd, SK_ARRAY_COUNT(rd), true, true },
1995 { re, SK_ARRAY_COUNT(re), true, true },
1996 { rf, SK_ARRAY_COUNT(rf), true, true },
1997
1998 { f1, SK_ARRAY_COUNT(f1), true, false },
1999 { f2, SK_ARRAY_COUNT(f2), true, false },
2000 { f3, SK_ARRAY_COUNT(f3), true, false },
2001 { f4, SK_ARRAY_COUNT(f4), true, false },
2002 { f5, SK_ARRAY_COUNT(f5), true, false },
2003 { f6, SK_ARRAY_COUNT(f6), true, false },
2004 { f7, SK_ARRAY_COUNT(f7), true, false },
2005 { f8, SK_ARRAY_COUNT(f8), true, false },
2006 { f9, SK_ARRAY_COUNT(f9), true, false },
2007 { fa, SK_ARRAY_COUNT(fa), true, false },
2008 { fb, SK_ARRAY_COUNT(fb), true, false },
2009
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002010 { c1, SK_ARRAY_COUNT(c1), false, true },
2011 { c2, SK_ARRAY_COUNT(c2), false, true },
2012 { c3, SK_ARRAY_COUNT(c3), false, true },
2013
2014 { d1, SK_ARRAY_COUNT(d1), false, false },
2015 { d2, SK_ARRAY_COUNT(d2), false, false },
2016 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00002017 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002018
2019 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002020 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002021 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2022 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002023 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2024 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2025 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002026 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002027 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002028 path.close();
2029 }
halcanary96fcdcc2015-08-27 07:41:13 -07002030 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002031
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002032 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002033 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002034 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002035 SkPath::Direction direction;
2036 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002037 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002038 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002039 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2040 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002041 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002042 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002043 } else {
2044 SkRect computed;
2045 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002046 bool isClosed = (bool)-1;
2047 SkPath::Direction direction = (SkPath::Direction) - 1;
2048 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002049 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2050 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002051 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2052 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002053 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002054 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002055
caryclark@google.comf1316942011-07-26 19:54:45 +00002056 // fail, close then line
2057 SkPath path1;
2058 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002059 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002060 path1.lineTo(r1[index].fX, r1[index].fY);
2061 }
2062 path1.close();
2063 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002064 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002065
caryclark@google.comf1316942011-07-26 19:54:45 +00002066 // fail, move in the middle
2067 path1.reset();
2068 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002069 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002070 if (index == 2) {
2071 path1.moveTo(1, .5f);
2072 }
2073 path1.lineTo(r1[index].fX, r1[index].fY);
2074 }
2075 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002076 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002077
2078 // fail, move on the edge
2079 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002080 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002081 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2082 path1.lineTo(r1[index].fX, r1[index].fY);
2083 }
2084 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002085 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002086
caryclark@google.comf1316942011-07-26 19:54:45 +00002087 // fail, quad
2088 path1.reset();
2089 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002090 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002091 if (index == 2) {
2092 path1.quadTo(1, .5f, 1, .5f);
2093 }
2094 path1.lineTo(r1[index].fX, r1[index].fY);
2095 }
2096 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002097 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002098
caryclark@google.comf1316942011-07-26 19:54:45 +00002099 // fail, cubic
2100 path1.reset();
2101 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002102 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002103 if (index == 2) {
2104 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2105 }
2106 path1.lineTo(r1[index].fX, r1[index].fY);
2107 }
2108 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002109 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002110}
2111
bsalomonedc743a2016-06-01 09:42:31 -07002112static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2113 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2114 SkRect r = SkRect::MakeEmpty();
2115 SkPath::Direction d = SkPath::kCCW_Direction;
2116 unsigned s = ~0U;
2117
2118 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2119 REPORTER_ASSERT(reporter, r == rect);
2120 REPORTER_ASSERT(reporter, d == dir);
2121 REPORTER_ASSERT(reporter, s == start);
2122}
2123
2124static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2125 SkRect r = SkRect::MakeEmpty();
2126 SkPath::Direction d = SkPath::kCCW_Direction;
2127 unsigned s = ~0U;
2128
2129 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2130 const SkRect emptyRect = SkRect::MakeEmpty();
2131 SkPath path;
2132 for (int start = 0; start < 4; ++start) {
2133 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2134 SkPath path;
2135 path.addRect(testRect, dir, start);
2136 check_simple_closed_rect(reporter, path, testRect, dir, start);
2137 path.close();
2138 check_simple_closed_rect(reporter, path, testRect, dir, start);
2139 SkPath path2 = path;
2140 path2.lineTo(10, 10);
2141 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2142 path2 = path;
2143 path2.moveTo(10, 10);
2144 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2145 path2 = path;
2146 path2.addRect(testRect, dir, start);
2147 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2148 // Make the path by hand, manually closing it.
2149 path2.reset();
2150 SkPath::RawIter iter(path);
2151 SkPath::Verb v;
2152 SkPoint verbPts[4];
2153 SkPoint firstPt = {0.f, 0.f};
2154 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2155 switch(v) {
2156 case SkPath::kMove_Verb:
2157 firstPt = verbPts[0];
2158 path2.moveTo(verbPts[0]);
2159 break;
2160 case SkPath::kLine_Verb:
2161 path2.lineTo(verbPts[1]);
2162 break;
2163 default:
2164 break;
2165 }
2166 }
2167 // We haven't closed it yet...
2168 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2169 // ... now we do and test again.
2170 path2.lineTo(firstPt);
2171 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2172 // A redundant close shouldn't cause a failure.
2173 path2.close();
2174 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2175 // Degenerate point and line rects are not allowed
2176 path2.reset();
2177 path2.addRect(emptyRect, dir, start);
2178 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2179 SkRect degenRect = testRect;
2180 degenRect.fLeft = degenRect.fRight;
2181 path2.reset();
2182 path2.addRect(degenRect, dir, start);
2183 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2184 degenRect = testRect;
2185 degenRect.fTop = degenRect.fBottom;
2186 path2.reset();
2187 path2.addRect(degenRect, dir, start);
2188 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2189 // An inverted rect makes a rect path, but changes the winding dir and start point.
2190 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2191 ? SkPath::kCCW_Direction
2192 : SkPath::kCW_Direction;
2193 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2194 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2195 SkRect swapRect = testRect;
2196 SkTSwap(swapRect.fLeft, swapRect.fRight);
2197 path2.reset();
2198 path2.addRect(swapRect, dir, start);
2199 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2200 swapRect = testRect;
2201 SkTSwap(swapRect.fTop, swapRect.fBottom);
2202 path2.reset();
2203 path2.addRect(swapRect, dir, start);
2204 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2205 }
2206 }
bsalomon057ae8a2016-07-24 05:37:26 -07002207 // down, up, left, close
2208 path.reset();
2209 path.moveTo(1, 1);
2210 path.lineTo(1, 2);
2211 path.lineTo(1, 1);
2212 path.lineTo(0, 1);
2213 SkRect rect;
2214 SkPath::Direction dir;
2215 unsigned start;
2216 path.close();
2217 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2218 // right, left, up, close
2219 path.reset();
2220 path.moveTo(1, 1);
2221 path.lineTo(2, 1);
2222 path.lineTo(1, 1);
2223 path.lineTo(1, 0);
2224 path.close();
2225 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2226 // parallelogram with horizontal edges
2227 path.reset();
2228 path.moveTo(1, 0);
2229 path.lineTo(3, 0);
2230 path.lineTo(2, 1);
2231 path.lineTo(0, 1);
2232 path.close();
2233 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2234 // parallelogram with vertical edges
2235 path.reset();
2236 path.moveTo(0, 1);
2237 path.lineTo(0, 3);
2238 path.lineTo(1, 2);
2239 path.lineTo(1, 0);
2240 path.close();
2241 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2242
bsalomonedc743a2016-06-01 09:42:31 -07002243}
2244
caryclark95bc5f32015-04-08 08:34:15 -07002245static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002246 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002247 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002248 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2249 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2250 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002251 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002252 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2253 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2254 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2255 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002256 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2257 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 +00002258 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2259 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2260 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002261
2262 // failing tests
2263 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2264 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2265 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2266 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2267 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2268 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2269 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2270 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2271
caryclark95bc5f32015-04-08 08:34:15 -07002272 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002273 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2274 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2275
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002276 struct IsNestedRectTest {
2277 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002278 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002279 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002280 bool fClose;
2281 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2282 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002283 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2284 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2285 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2286 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2287 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2288 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2289 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2290 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2291 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2292 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2293 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2294 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2295 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2296 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002297
reed026beb52015-06-10 14:23:15 -07002298 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2299 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2300 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2301 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2302 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2303 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2304 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2305 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002306
reed026beb52015-06-10 14:23:15 -07002307 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2308 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002309 };
2310
2311 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002312 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002313 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002314 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2315 SkPath path;
2316 if (rectFirst) {
2317 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2318 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002319 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2320 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2321 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002322 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002323 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002324 path.close();
2325 }
2326 if (!rectFirst) {
2327 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2328 }
caryclark95bc5f32015-04-08 08:34:15 -07002329 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002330 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002331 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002332 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002333 SkPathPriv::FirstDirection expectedDirs[2];
2334 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002335 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002336 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002337 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2338 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002339 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002340 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002341 } else {
reed026beb52015-06-10 14:23:15 -07002342 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002343 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002344 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002345 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002346 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2347 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002348 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2349 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002350 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002351 }
2352
2353 // fail, close then line
2354 SkPath path1;
2355 if (rectFirst) {
2356 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2357 }
2358 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002359 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002360 path1.lineTo(r1[index].fX, r1[index].fY);
2361 }
2362 path1.close();
2363 path1.lineTo(1, 0);
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 in the middle
2370 path1.reset();
2371 if (rectFirst) {
2372 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2373 }
2374 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002375 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002376 if (index == 2) {
2377 path1.moveTo(1, .5f);
2378 }
2379 path1.lineTo(r1[index].fX, r1[index].fY);
2380 }
2381 path1.close();
2382 if (!rectFirst) {
2383 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2384 }
halcanary96fcdcc2015-08-27 07:41:13 -07002385 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002386
2387 // fail, move on the edge
2388 path1.reset();
2389 if (rectFirst) {
2390 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2391 }
bsalomon98806072014-12-12 15:11:17 -08002392 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002393 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
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, quad
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.quadTo(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));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002419
2420 // fail, cubic
2421 path1.reset();
2422 if (rectFirst) {
2423 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2424 }
2425 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002426 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002427 if (index == 2) {
2428 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2429 }
2430 path1.lineTo(r1[index].fX, r1[index].fY);
2431 }
2432 path1.close();
2433 if (!rectFirst) {
2434 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2435 }
halcanary96fcdcc2015-08-27 07:41:13 -07002436 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002437
caryclark@google.com56f233a2012-11-19 13:06:06 +00002438 // fail, not nested
2439 path1.reset();
2440 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2441 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002442 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002443 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002444
caryclark95bc5f32015-04-08 08:34:15 -07002445 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2446 SkPath path;
2447 path.moveTo(0, 0);
2448 path.lineTo(10, 0);
2449 path.lineTo(10, 10);
2450 path.lineTo(0, 10);
2451 path.lineTo(0, 0);
2452 path.moveTo(1, 1);
2453 path.lineTo(9, 1);
2454 path.lineTo(9, 9);
2455 path.lineTo(1, 9);
2456 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002457 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002458
caryclark@google.combfe90372012-11-21 13:56:20 +00002459 // pass, stroke rect
2460 SkPath src, dst;
2461 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2462 SkPaint strokePaint;
2463 strokePaint.setStyle(SkPaint::kStroke_Style);
2464 strokePaint.setStrokeWidth(2);
2465 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002466 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002467}
2468
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002469static void write_and_read_back(skiatest::Reporter* reporter,
2470 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002471 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002472 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002473 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002474 SkAutoMalloc storage(size);
2475 writer.flatten(storage.get());
2476 SkReader32 reader(storage.get(), size);
2477
2478 SkPath readBack;
2479 REPORTER_ASSERT(reporter, readBack != p);
2480 reader.readPath(&readBack);
2481 REPORTER_ASSERT(reporter, readBack == p);
2482
rmistry@google.comd6176b02012-08-23 18:14:13 +00002483 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002484 p.getConvexityOrUnknown());
2485
bsalomon78d58d12016-05-27 09:17:04 -07002486 SkRect oval0, oval1;
2487 SkPath::Direction dir0, dir1;
2488 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002489 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002490 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2491 REPORTER_ASSERT(reporter, oval0 == oval1);
2492 REPORTER_ASSERT(reporter, dir0 == dir1);
2493 REPORTER_ASSERT(reporter, start0 == start1);
2494 }
2495 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2496 SkRRect rrect0, rrect1;
2497 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2498 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2499 REPORTER_ASSERT(reporter, dir0 == dir1);
2500 REPORTER_ASSERT(reporter, start0 == start1);
2501 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002502 const SkRect& origBounds = p.getBounds();
2503 const SkRect& readBackBounds = readBack.getBounds();
2504
2505 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2506}
2507
reed@google.com53effc52011-09-21 19:05:12 +00002508static void test_flattening(skiatest::Reporter* reporter) {
2509 SkPath p;
2510
2511 static const SkPoint pts[] = {
2512 { 0, 0 },
2513 { SkIntToScalar(10), SkIntToScalar(10) },
2514 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2515 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2516 };
2517 p.moveTo(pts[0]);
2518 p.lineTo(pts[1]);
2519 p.quadTo(pts[2], pts[3]);
2520 p.cubicTo(pts[4], pts[5], pts[6]);
2521
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002522 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002523
2524 // create a buffer that should be much larger than the path so we don't
2525 // kill our stack if writer goes too far.
2526 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002527 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002528 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002529 REPORTER_ASSERT(reporter, size1 == size2);
2530
2531 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002532 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002533 REPORTER_ASSERT(reporter, size1 == size3);
2534 REPORTER_ASSERT(reporter, p == p2);
2535
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002536 size3 = p2.readFromMemory(buffer, 0);
2537 REPORTER_ASSERT(reporter, !size3);
2538
2539 SkPath tooShort;
2540 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2541 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2542
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002543 char buffer2[1024];
2544 size3 = p2.writeToMemory(buffer2);
2545 REPORTER_ASSERT(reporter, size1 == size3);
2546 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002547
2548 // test persistence of the oval flag & convexity
2549 {
2550 SkPath oval;
2551 SkRect rect = SkRect::MakeWH(10, 10);
2552 oval.addOval(rect);
2553
2554 write_and_read_back(reporter, oval);
2555 }
reed@google.com53effc52011-09-21 19:05:12 +00002556}
2557
2558static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002559 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002560
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002561#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002562 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002563 { 0, 0 }, // move
2564 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2565 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2566 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2567#if CONIC_PERSPECTIVE_BUG_FIXED
2568 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2569#endif
reed@google.com53effc52011-09-21 19:05:12 +00002570 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002571 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002572
reed@google.com53effc52011-09-21 19:05:12 +00002573 p.moveTo(pts[0]);
2574 p.lineTo(pts[1]);
2575 p.quadTo(pts[2], pts[3]);
2576 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002577#if CONIC_PERSPECTIVE_BUG_FIXED
2578 p.conicTo(pts[4], pts[5], 0.5f);
2579#endif
2580 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002581
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002582 {
2583 SkMatrix matrix;
2584 matrix.reset();
2585 SkPath p1;
2586 p.transform(matrix, &p1);
2587 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002588 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002589
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002590
2591 {
2592 SkMatrix matrix;
2593 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2594
2595 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2596
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002597 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002598 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002599 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002600 REPORTER_ASSERT(reporter, kPtCount == count);
2601 for (int i = 0; i < count; ++i) {
2602 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2603 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2604 }
2605 }
2606
2607 {
2608 SkMatrix matrix;
2609 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002610 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002611
2612 SkPath p1;
2613 p1.moveTo(SkPoint::Make(0, 0));
2614
2615 p.transform(matrix, &p1);
2616 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002617 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002618 SkRect pBounds = p.getBounds();
2619 SkRect p1Bounds = p1.getBounds();
2620 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2621 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2622 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2623 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2624 }
2625
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002626 p.reset();
2627 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002628
2629 {
2630 SkMatrix matrix;
2631 matrix.reset();
2632 SkPath p1;
2633 p1.moveTo(SkPoint::Make(0, 0));
2634
2635 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002636 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002637 }
2638
2639
2640 {
2641 SkMatrix matrix;
2642 matrix.reset();
2643 matrix.setScaleX(-1);
2644 SkPath p1;
2645 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2646
2647 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002648 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002649 }
2650
2651 {
2652 SkMatrix matrix;
2653 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2654 SkPath p1;
2655 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2656
2657 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002658 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002659 }
reed@google.com53effc52011-09-21 19:05:12 +00002660}
2661
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002662static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002663 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002664 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002665
caryclark@google.com56f233a2012-11-19 13:06:06 +00002666 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002667 const char* testPath;
2668 const size_t numResultPts;
2669 const SkRect resultBound;
2670 const SkPath::Verb* resultVerbs;
2671 const size_t numResultVerbs;
2672 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002673
schenney@chromium.org7e963602012-06-13 17:05:43 +00002674 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2675 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2676 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2677 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2678 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2679 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2680 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2681 static const SkPath::Verb resultVerbs8[] = {
2682 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2683 };
2684 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2685 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2686 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2687 static const SkPath::Verb resultVerbs12[] = {
2688 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2689 };
2690 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2691 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2692 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2693 static const SkPath::Verb resultVerbs16[] = {
2694 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2695 };
2696 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002697 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002698 { "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 -07002699 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002700 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2701 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2702 { "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) },
2703 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2704 { "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) },
2705 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2706 { "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) },
2707 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2708 { "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) },
2709 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2710 { "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 +00002711 SK_ARRAY_COUNT(resultVerbs14)
2712 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002713 { "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) },
2714 { "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 +00002715 SK_ARRAY_COUNT(resultVerbs16)
2716 }
2717 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002718
schenney@chromium.org7e963602012-06-13 17:05:43 +00002719 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2720 p.reset();
2721 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2722 REPORTER_ASSERT(reporter, valid);
2723 REPORTER_ASSERT(reporter, !p.isEmpty());
2724 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2725 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2726 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2727 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2728 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2729 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002730 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002731}
2732
2733struct SegmentInfo {
2734 SkPath fPath;
2735 int fPointCount;
2736};
2737
reed@google.com10296cc2011-09-21 12:29:05 +00002738#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2739
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002740static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002741 SkPath p, p2;
2742
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002743 p.moveTo(0, 0);
2744 p.quadTo(100, 100, 200, 200);
2745 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2746 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002747 p2 = p;
2748 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002749 p.cubicTo(100, 100, 200, 200, 300, 300);
2750 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2751 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002752 p2 = p;
2753 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2754
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002755 p.reset();
2756 p.moveTo(0, 0);
2757 p.cubicTo(100, 100, 200, 200, 300, 300);
2758 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002759 p2 = p;
2760 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002761
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002762 REPORTER_ASSERT(reporter, !p.isEmpty());
2763}
2764
2765static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002766 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002767 SkPoint pts[4];
2768
2769 // Test an iterator with no path
2770 SkPath::Iter noPathIter;
2771 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002772
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002773 // Test that setting an empty path works
2774 noPathIter.setPath(p, false);
2775 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002776
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002777 // Test that close path makes no difference for an empty path
2778 noPathIter.setPath(p, true);
2779 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002780
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002781 // Test an iterator with an initial empty path
2782 SkPath::Iter iter(p, false);
2783 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2784
2785 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002786 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002787 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2788
rmistry@google.comd6176b02012-08-23 18:14:13 +00002789
schenney@chromium.org7e963602012-06-13 17:05:43 +00002790 struct iterTestData {
2791 const char* testPath;
2792 const bool forceClose;
2793 const bool consumeDegenerates;
2794 const size_t* numResultPtsPerVerb;
2795 const SkPoint* resultPts;
2796 const SkPath::Verb* resultVerbs;
2797 const size_t numResultVerbs;
2798 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002799
schenney@chromium.org7e963602012-06-13 17:05:43 +00002800 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2801 static const SkPath::Verb resultVerbs2[] = {
2802 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2803 };
2804 static const SkPath::Verb resultVerbs3[] = {
2805 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2806 };
2807 static const SkPath::Verb resultVerbs4[] = {
2808 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2809 };
2810 static const SkPath::Verb resultVerbs5[] = {
2811 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2812 };
2813 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002814 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2815 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2816 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2817 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002818 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002819 static const SkPoint resultPts2[] = {
2820 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2821 };
2822 static const SkPoint resultPts3[] = {
2823 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2824 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2825 };
2826 static const SkPoint resultPts4[] = {
2827 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2828 };
2829 static const SkPoint resultPts5[] = {
2830 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2831 };
2832 static const struct iterTestData gIterTests[] = {
2833 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002834 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2835 { "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 +00002836 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2837 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2838 { "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) },
2839 { "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 +00002840 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2841 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2842 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2843 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2844 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2845 { "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 +00002846 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002847
schenney@chromium.org7e963602012-06-13 17:05:43 +00002848 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2849 p.reset();
2850 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2851 REPORTER_ASSERT(reporter, valid);
2852 iter.setPath(p, gIterTests[i].forceClose);
2853 int j = 0, l = 0;
2854 do {
2855 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2856 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2857 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2858 }
2859 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2860 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2861 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002862
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002863 p.reset();
2864 iter.setPath(p, false);
2865 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2866 p.lineTo(1, 1);
2867 p.close();
2868 iter.setPath(p, false);
2869 REPORTER_ASSERT(reporter, iter.isClosedContour());
2870 p.reset();
2871 iter.setPath(p, true);
2872 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2873 p.lineTo(1, 1);
2874 iter.setPath(p, true);
2875 REPORTER_ASSERT(reporter, iter.isClosedContour());
2876 p.moveTo(0, 0);
2877 p.lineTo(2, 2);
2878 iter.setPath(p, false);
2879 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2880
2881 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2882 // check to see if the result is correct.
2883 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2884 p.reset();
2885 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2886 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2887 iter.setPath(p, true);
2888 iter.next(pts, false);
2889 iter.next(pts, false);
2890 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2891 }
2892
2893 p.reset();
2894 p.quadTo(0, 0, 0, 0);
2895 iter.setPath(p, false);
2896 iter.next(pts, false);
2897 REPORTER_ASSERT(reporter, SkPath::kQuad_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.reset();
2903 p.conicTo(0, 0, 0, 0, 0.5f);
2904 iter.setPath(p, false);
2905 iter.next(pts, false);
2906 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2907 iter.setPath(p, false);
2908 iter.next(pts, false);
2909 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2910
2911 p.reset();
2912 p.cubicTo(0, 0, 0, 0, 0, 0);
2913 iter.setPath(p, false);
2914 iter.next(pts, false);
2915 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2916 iter.setPath(p, false);
2917 iter.next(pts, false);
2918 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2919
2920 p.moveTo(1, 1); // add a trailing moveto
2921 iter.setPath(p, false);
2922 iter.next(pts, false);
2923 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2924 iter.setPath(p, false);
2925 iter.next(pts, false);
2926 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2927
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002928 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002929
2930 // Test out mixed degenerate and non-degenerate geometry with Conics
2931 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2932 SkRect r = SkRect::MakeWH(100, 100);
2933 SkRRect rr;
2934 rr.setRectRadii(r, radii);
2935 p.reset();
2936 p.addRRect(rr);
2937 iter.setPath(p, false);
2938 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2939 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2940 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2941 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2942 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002943}
2944
2945static void test_raw_iter(skiatest::Reporter* reporter) {
2946 SkPath p;
2947 SkPoint pts[4];
2948
2949 // Test an iterator with no path
2950 SkPath::RawIter noPathIter;
2951 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2952 // Test that setting an empty path works
2953 noPathIter.setPath(p);
2954 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002955
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002956 // Test an iterator with an initial empty path
2957 SkPath::RawIter iter(p);
2958 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2959
2960 // Test that a move-only path returns the move.
2961 p.moveTo(SK_Scalar1, 0);
2962 iter.setPath(p);
2963 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2964 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2965 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2966 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2967
2968 // No matter how many moves we add, we should get them all back
2969 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2970 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2971 iter.setPath(p);
2972 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2973 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2974 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2975 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2976 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2977 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2978 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2979 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2980 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2981 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2982
2983 // Initial close is never ever stored
2984 p.reset();
2985 p.close();
2986 iter.setPath(p);
2987 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2988
2989 // Move/close sequences
2990 p.reset();
2991 p.close(); // Not stored, no purpose
2992 p.moveTo(SK_Scalar1, 0);
2993 p.close();
2994 p.close(); // Not stored, no purpose
2995 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2996 p.close();
2997 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2998 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2999 p.close();
3000 iter.setPath(p);
3001 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3002 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3003 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3004 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003005 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3006 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3007 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3008 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003009 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3010 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3011 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3012 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3013 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3014 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3015 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003016 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3017
3018 // Generate random paths and verify
3019 SkPoint randomPts[25];
3020 for (int i = 0; i < 5; ++i) {
3021 for (int j = 0; j < 5; ++j) {
3022 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3023 }
3024 }
3025
3026 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003027 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003028 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003029 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003030 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003031
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003032 for (int i = 0; i < 500; ++i) {
3033 p.reset();
3034 bool lastWasClose = true;
3035 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003036 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003037 int numPoints = 0;
3038 int numVerbs = (rand.nextU() >> 16) % 10;
3039 int numIterVerbs = 0;
3040 for (int j = 0; j < numVerbs; ++j) {
3041 do {
3042 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3043 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003044 switch (nextVerb) {
3045 case SkPath::kMove_Verb:
3046 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3047 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003048 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003049 numPoints += 1;
3050 lastWasClose = false;
3051 haveMoveTo = true;
3052 break;
3053 case SkPath::kLine_Verb:
3054 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003055 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003056 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3057 haveMoveTo = true;
3058 }
3059 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3060 p.lineTo(expectedPts[numPoints]);
3061 numPoints += 1;
3062 lastWasClose = false;
3063 break;
3064 case SkPath::kQuad_Verb:
3065 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003066 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003067 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3068 haveMoveTo = true;
3069 }
3070 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3071 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3072 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3073 numPoints += 2;
3074 lastWasClose = false;
3075 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003076 case SkPath::kConic_Verb:
3077 if (!haveMoveTo) {
3078 expectedPts[numPoints++] = lastMoveToPt;
3079 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3080 haveMoveTo = true;
3081 }
3082 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3083 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3084 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3085 rand.nextUScalar1() * 4);
3086 numPoints += 2;
3087 lastWasClose = false;
3088 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003089 case SkPath::kCubic_Verb:
3090 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003091 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003092 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3093 haveMoveTo = true;
3094 }
3095 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3096 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3097 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3098 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3099 expectedPts[numPoints + 2]);
3100 numPoints += 3;
3101 lastWasClose = false;
3102 break;
3103 case SkPath::kClose_Verb:
3104 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003105 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003106 lastWasClose = true;
3107 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003108 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003109 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003110 }
3111 expectedVerbs[numIterVerbs++] = nextVerb;
3112 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003113
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003114 iter.setPath(p);
3115 numVerbs = numIterVerbs;
3116 numIterVerbs = 0;
3117 int numIterPts = 0;
3118 SkPoint lastMoveTo;
3119 SkPoint lastPt;
3120 lastMoveTo.set(0, 0);
3121 lastPt.set(0, 0);
3122 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3123 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3124 numIterVerbs++;
3125 switch (nextVerb) {
3126 case SkPath::kMove_Verb:
3127 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3128 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3129 lastPt = lastMoveTo = pts[0];
3130 numIterPts += 1;
3131 break;
3132 case SkPath::kLine_Verb:
3133 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3134 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3135 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3136 lastPt = pts[1];
3137 numIterPts += 1;
3138 break;
3139 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003140 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003141 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3142 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3143 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3144 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3145 lastPt = pts[2];
3146 numIterPts += 2;
3147 break;
3148 case SkPath::kCubic_Verb:
3149 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3150 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3151 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3152 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3153 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3154 lastPt = pts[3];
3155 numIterPts += 3;
3156 break;
3157 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003158 lastPt = lastMoveTo;
3159 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003160 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003161 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003162 }
3163 }
3164 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3165 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3166 }
3167}
3168
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003169static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003170 const SkPath& path,
3171 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003172 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003173 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003174 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003175 SkPath::Direction isOvalDir;
3176 unsigned isOvalStart;
3177 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003178 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003179 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3180 SkPath tmpPath;
3181 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3182 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003183 }
bsalomon78d58d12016-05-27 09:17:04 -07003184 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003185}
3186
3187static void test_circle_skew(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 SkMatrix m;
3193 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3194 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003195 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003196 if (SkPathPriv::kCCW_FirstDirection == dir) {
3197 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003198 } else {
reed026beb52015-06-10 14:23:15 -07003199 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3200 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003201 }
3202 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003203}
3204
3205static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003206 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003207 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003208 SkPath tmp;
3209
3210 // translate at small offset
3211 SkMatrix m;
3212 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3213 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003214 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003215
3216 tmp.reset();
3217 m.reset();
3218
3219 // translate at a relatively big offset
3220 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3221 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003222 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003223}
3224
3225static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003226 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003227 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003228 for (int angle = 0; angle < 360; ++angle) {
3229 SkPath tmp;
3230 SkMatrix m;
3231 m.setRotate(SkIntToScalar(angle));
3232 path.transform(m, &tmp);
3233
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003234 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003235 // degrees is not an oval anymore, this can be improved. we made this
3236 // for the simplicity of our implementation.
3237 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003238 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003239 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003240 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003241 }
3242 }
3243}
3244
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003245static void test_circle_mirror_x(skiatest::Reporter* reporter,
3246 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003247 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003248 SkPath tmp;
3249 SkMatrix m;
3250 m.reset();
3251 m.setScaleX(-SK_Scalar1);
3252 path.transform(m, &tmp);
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 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003259 check_for_circle(reporter, tmp, true, dir);
3260}
3261
3262static void test_circle_mirror_y(skiatest::Reporter* reporter,
3263 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003264 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003265 SkPath tmp;
3266 SkMatrix m;
3267 m.reset();
3268 m.setScaleY(-SK_Scalar1);
3269 path.transform(m, &tmp);
3270
reed026beb52015-06-10 14:23:15 -07003271 if (SkPathPriv::kCW_FirstDirection == dir) {
3272 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003273 } else {
reed026beb52015-06-10 14:23:15 -07003274 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3275 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003276 }
3277
3278 check_for_circle(reporter, tmp, true, dir);
3279}
3280
3281static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3282 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003283 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003284 SkPath tmp;
3285 SkMatrix m;
3286 m.reset();
3287 m.setScaleX(-SK_Scalar1);
3288 m.setScaleY(-SK_Scalar1);
3289 path.transform(m, &tmp);
3290
3291 check_for_circle(reporter, tmp, true, dir);
3292}
3293
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003294static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003295 SkPath::Direction inDir) {
3296 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003297 SkPath path;
3298
3299 // circle at origin
reed026beb52015-06-10 14:23:15 -07003300 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3301
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003302 check_for_circle(reporter, path, true, dir);
3303 test_circle_rotate(reporter, path, dir);
3304 test_circle_translate(reporter, path, dir);
3305 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003306 test_circle_mirror_x(reporter, path, dir);
3307 test_circle_mirror_y(reporter, path, dir);
3308 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003309
3310 // circle at an offset at (10, 10)
3311 path.reset();
3312 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003313 SkIntToScalar(20), inDir);
3314
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003315 check_for_circle(reporter, path, true, dir);
3316 test_circle_rotate(reporter, path, dir);
3317 test_circle_translate(reporter, path, dir);
3318 test_circle_skew(reporter, path, dir);
3319 test_circle_mirror_x(reporter, path, dir);
3320 test_circle_mirror_y(reporter, path, dir);
3321 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003322
3323 // Try different starting points for the contour.
3324 for (unsigned start = 0; start < 4; ++start) {
3325 path.reset();
3326 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3327 test_circle_rotate(reporter, path, dir);
3328 test_circle_translate(reporter, path, dir);
3329 test_circle_skew(reporter, path, dir);
3330 test_circle_mirror_x(reporter, path, dir);
3331 test_circle_mirror_y(reporter, path, dir);
3332 test_circle_mirror_xy(reporter, path, dir);
3333 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003334}
3335
3336static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3337 SkPath path;
3338 SkPath circle;
3339 SkPath rect;
3340 SkPath empty;
3341
reed026beb52015-06-10 14:23:15 -07003342 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3343 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003344
3345 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003346 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3347 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3348
3349 SkMatrix translate;
3350 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3351
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003352 // Although all the path concatenation related operations leave
3353 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003354
3355 // empty + circle (translate)
3356 path = empty;
3357 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003358 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003359
3360 // circle + empty (translate)
3361 path = circle;
3362 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003363
reed026beb52015-06-10 14:23:15 -07003364 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003365
3366 // test reverseAddPath
3367 path = circle;
3368 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003369 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003370}
3371
3372static void test_circle(skiatest::Reporter* reporter) {
3373 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3374 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3375
3376 // multiple addCircle()
3377 SkPath path;
3378 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3379 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003380 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003381
3382 // some extra lineTo() would make isOval() fail
3383 path.reset();
3384 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3385 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003386 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003387
3388 // not back to the original point
3389 path.reset();
3390 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3391 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003392 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003393
3394 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003395
3396 // test negative radius
3397 path.reset();
3398 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3399 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003400}
3401
3402static void test_oval(skiatest::Reporter* reporter) {
3403 SkRect rect;
3404 SkMatrix m;
3405 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003406 unsigned start = 0;
3407 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003408
3409 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3410 path.addOval(rect);
3411
bsalomon78d58d12016-05-27 09:17:04 -07003412 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003413 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003414
3415 m.setRotate(SkIntToScalar(90));
3416 SkPath tmp;
3417 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003418 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3419 // is unchanged.
3420 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3421 REPORTER_ASSERT(reporter, 2 == start);
3422 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003423
3424 m.reset();
3425 m.setRotate(SkIntToScalar(30));
3426 tmp.reset();
3427 path.transform(m, &tmp);
3428 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003429 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003430
3431 // since empty path being transformed.
3432 path.reset();
3433 tmp.reset();
3434 m.reset();
3435 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003436 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003437
3438 // empty path is not an oval
3439 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003440 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003441
3442 // only has moveTo()s
3443 tmp.reset();
3444 tmp.moveTo(0, 0);
3445 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003446 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003447
3448 // mimic WebKit's calling convention,
3449 // call moveTo() first and then call addOval()
3450 path.reset();
3451 path.moveTo(0, 0);
3452 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003453 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003454
3455 // copy path
3456 path.reset();
3457 tmp.reset();
3458 tmp.addOval(rect);
3459 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003460 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3461 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3462 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003463}
3464
bungeman@google.coma5809a32013-06-21 15:13:34 +00003465static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3466 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003467
reed@android.com3abec1d2009-03-02 05:36:20 +00003468 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003469 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003470 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003471 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003472 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003473 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3474 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003475 REPORTER_ASSERT(reporter, p == empty);
3476 REPORTER_ASSERT(reporter, !(p != empty));
3477}
3478
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003479static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3480 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003481 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003482 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003483 path->setConvexity(SkPath::kUnknown_Convexity);
3484 REPORTER_ASSERT(reporter, path->isConvex());
3485 path->reset();
3486}
3487
caryclarkd3d1a982014-12-08 04:57:38 -08003488static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3489 SkPath::Direction dir) {
3490 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003491 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003492 path->setConvexity(SkPath::kUnknown_Convexity);
3493 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3494 path->reset();
3495}
3496
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003497static void test_rrect(skiatest::Reporter* reporter) {
3498 SkPath p;
3499 SkRRect rr;
3500 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3501 SkRect r = {10, 20, 30, 40};
3502 rr.setRectRadii(r, radii);
3503 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003504 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003505 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003506 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003507 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003508 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003509 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003510 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003511 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003512 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003513 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003514 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3515 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3516 SkVector save = radii[i];
3517 radii[i].set(0, 0);
3518 rr.setRectRadii(r, radii);
3519 p.addRRect(rr);
3520 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3521 radii[i] = save;
3522 }
3523 p.addRoundRect(r, 0, 0);
3524 SkRect returnedRect;
3525 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3526 REPORTER_ASSERT(reporter, returnedRect == r);
3527 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3528 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3529 rr.setRectRadii(r, zeroRadii);
3530 p.addRRect(rr);
3531 bool closed;
3532 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003533 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003534 REPORTER_ASSERT(reporter, closed);
3535 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3536 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3537 p.addRRect(rr, SkPath::kCW_Direction);
3538 p.addRRect(rr, SkPath::kCW_Direction);
3539 REPORTER_ASSERT(reporter, !p.isConvex());
3540 p.reset();
3541 p.addRRect(rr, SkPath::kCCW_Direction);
3542 p.addRRect(rr, SkPath::kCCW_Direction);
3543 REPORTER_ASSERT(reporter, !p.isConvex());
3544 p.reset();
3545 SkRect emptyR = {10, 20, 10, 30};
3546 rr.setRectRadii(emptyR, radii);
3547 p.addRRect(rr);
3548 REPORTER_ASSERT(reporter, p.isEmpty());
3549 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3550 rr.setRectRadii(largeR, radii);
3551 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003552 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003553
3554 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003555 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3556 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003557 REPORTER_ASSERT(reporter, rr.isEmpty());
3558
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003559 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3560 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3561 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003562}
3563
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003564static void test_arc(skiatest::Reporter* reporter) {
3565 SkPath p;
3566 SkRect emptyOval = {10, 20, 30, 20};
3567 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3568 p.addArc(emptyOval, 1, 2);
3569 REPORTER_ASSERT(reporter, p.isEmpty());
3570 p.reset();
3571 SkRect oval = {10, 20, 30, 40};
3572 p.addArc(oval, 1, 0);
3573 REPORTER_ASSERT(reporter, p.isEmpty());
3574 p.reset();
3575 SkPath cwOval;
3576 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003577 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003578 REPORTER_ASSERT(reporter, p == cwOval);
3579 p.reset();
3580 SkPath ccwOval;
3581 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003582 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003583 REPORTER_ASSERT(reporter, p == ccwOval);
3584 p.reset();
3585 p.addArc(oval, 1, 180);
3586 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003587 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003588 p.setConvexity(SkPath::kUnknown_Convexity);
3589 REPORTER_ASSERT(reporter, p.isConvex());
3590}
3591
bsalomon1978ce22016-05-31 10:42:16 -07003592static inline SkScalar oval_start_index_to_angle(unsigned start) {
3593 switch (start) {
3594 case 0:
3595 return 270.f;
3596 case 1:
3597 return 0.f;
3598 case 2:
3599 return 90.f;
3600 case 3:
3601 return 180.f;
3602 default:
3603 return -1.f;
3604 }
3605}
3606
3607static inline SkScalar canonical_start_angle(float angle) {
3608 while (angle < 0.f) {
3609 angle += 360.f;
3610 }
3611 while (angle >= 360.f) {
3612 angle -= 360.f;
3613 }
3614 return angle;
3615}
3616
3617static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3618 const SkPath& path) {
3619 SkRect r = SkRect::MakeEmpty();
3620 SkPath::Direction d = SkPath::kCCW_Direction;
3621 unsigned s = ~0U;
3622 bool isOval = path.isOval(&r, &d, &s);
3623 REPORTER_ASSERT(reporter, isOval);
3624 SkPath recreatedPath;
3625 recreatedPath.addOval(r, d, s);
3626 REPORTER_ASSERT(reporter, path == recreatedPath);
3627 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3628 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3629}
3630
3631static void test_arc_ovals(skiatest::Reporter* reporter) {
3632 SkRect oval = SkRect::MakeWH(10, 20);
3633 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3634 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3635 SkPath path;
3636 path.addArc(oval, start, sweep);
3637 // SkPath's interfaces for inserting and extracting ovals only allow contours
3638 // to start at multiples of 90 degrees.
3639 if (std::fmod(start, 90.f) == 0) {
3640 check_oval_arc(reporter, start, sweep, path);
3641 } else {
3642 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3643 }
3644 }
3645 // Test start angles that are nearly at valid oval start angles.
3646 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3647 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3648 SkPath path;
3649 path.addArc(oval, start + delta, sweep);
3650 check_oval_arc(reporter, start, sweep, path);
3651 }
3652 }
3653 }
3654}
3655
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003656static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3657 SkScalar x0, SkScalar y0) {
3658 SkPoint pts[4];
3659 SkPath::Verb v = iter->next(pts);
3660 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3661 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3662 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3663}
3664
3665static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3666 SkScalar x1, SkScalar y1) {
3667 SkPoint pts[4];
3668 SkPath::Verb v = iter->next(pts);
3669 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3670 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3671 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3672}
3673
3674static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3675 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3676 SkPoint pts[4];
3677 SkPath::Verb v = iter->next(pts);
3678 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3679 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3680 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3681 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3682 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3683}
3684
3685static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3686 SkPoint pts[4];
3687 SkPath::Verb v = iter->next(pts);
3688 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3689}
3690
3691static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3692 check_done(reporter, p, iter);
3693 p->reset();
3694}
3695
3696static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3697 SkScalar x0, SkScalar y0) {
3698 SkPath::RawIter iter(*p);
3699 check_move(reporter, &iter, x0, y0);
3700 check_done_and_reset(reporter, p, &iter);
3701}
3702
3703static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3704 SkScalar x1, SkScalar y1) {
3705 SkPath::RawIter iter(*p);
3706 check_move(reporter, &iter, 0, 0);
3707 check_line(reporter, &iter, x1, y1);
3708 check_done_and_reset(reporter, p, &iter);
3709}
3710
3711static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3712 SkScalar x1, SkScalar y1) {
3713 SkPath::RawIter iter(*p);
3714 check_move(reporter, &iter, 0, 0);
3715 check_line(reporter, &iter, x1, y1);
3716 check_done(reporter, p, &iter);
3717}
3718
3719static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3720 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3721 SkPath::RawIter iter(*p);
3722 check_move(reporter, &iter, 0, 0);
3723 check_line(reporter, &iter, x1, y1);
3724 check_line(reporter, &iter, x2, y2);
3725 check_done_and_reset(reporter, p, &iter);
3726}
3727
3728static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3729 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3730 SkPath::RawIter iter(*p);
3731 check_move(reporter, &iter, 0, 0);
3732 check_quad(reporter, &iter, x1, y1, x2, y2);
3733 check_done_and_reset(reporter, p, &iter);
3734}
3735
reedd5d27d92015-02-09 13:54:43 -08003736static bool nearly_equal(const SkRect& a, const SkRect& b) {
3737 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3738 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3739 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3740 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3741}
3742
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003743static void test_arcTo(skiatest::Reporter* reporter) {
3744 SkPath p;
3745 p.arcTo(0, 0, 1, 2, 1);
3746 check_path_is_line_and_reset(reporter, &p, 0, 0);
3747 p.arcTo(1, 2, 1, 2, 1);
3748 check_path_is_line_and_reset(reporter, &p, 1, 2);
3749 p.arcTo(1, 2, 3, 4, 0);
3750 check_path_is_line_and_reset(reporter, &p, 1, 2);
3751 p.arcTo(1, 2, 0, 0, 1);
3752 check_path_is_line_and_reset(reporter, &p, 1, 2);
3753 p.arcTo(1, 0, 1, 1, 1);
3754 SkPoint pt;
3755 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3756 p.reset();
3757 p.arcTo(1, 0, 1, -1, 1);
3758 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3759 p.reset();
3760 SkRect oval = {1, 2, 3, 4};
3761 p.arcTo(oval, 0, 0, true);
3762 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3763 p.arcTo(oval, 0, 0, false);
3764 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3765 p.arcTo(oval, 360, 0, true);
3766 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3767 p.arcTo(oval, 360, 0, false);
3768 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003769
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003770 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003771 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003772 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003773 sweep += delta;
3774 delta /= 2;
3775 }
3776 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003777 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003778 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003779 sweep -= delta;
3780 delta /= 2;
3781 }
3782 SkRect noOvalWidth = {1, 2, 0, 3};
3783 p.reset();
3784 p.arcTo(noOvalWidth, 0, 360, false);
3785 REPORTER_ASSERT(reporter, p.isEmpty());
3786
3787 SkRect noOvalHeight = {1, 2, 3, 1};
3788 p.reset();
3789 p.arcTo(noOvalHeight, 0, 360, false);
3790 REPORTER_ASSERT(reporter, p.isEmpty());
3791}
3792
3793static void test_addPath(skiatest::Reporter* reporter) {
3794 SkPath p, q;
3795 p.lineTo(1, 2);
3796 q.moveTo(4, 4);
3797 q.lineTo(7, 8);
3798 q.conicTo(8, 7, 6, 5, 0.5f);
3799 q.quadTo(6, 7, 8, 6);
3800 q.cubicTo(5, 6, 7, 8, 7, 5);
3801 q.close();
3802 p.addPath(q, -4, -4);
3803 SkRect expected = {0, 0, 4, 4};
3804 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3805 p.reset();
3806 p.reverseAddPath(q);
3807 SkRect reverseExpected = {4, 4, 8, 8};
3808 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3809}
3810
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003811static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3812 SkPath p, q;
3813 if (explicitMoveTo) {
3814 p.moveTo(1, 1);
3815 }
3816 p.lineTo(1, 2);
3817 if (explicitMoveTo) {
3818 q.moveTo(2, 1);
3819 }
3820 q.lineTo(2, 2);
3821 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3822 uint8_t verbs[4];
3823 int verbcount = p.getVerbs(verbs, 4);
3824 REPORTER_ASSERT(reporter, verbcount == 4);
3825 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3826 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3827 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3828 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3829}
3830
3831static void test_extendClosedPath(skiatest::Reporter* reporter) {
3832 SkPath p, q;
3833 p.moveTo(1, 1);
3834 p.lineTo(1, 2);
3835 p.lineTo(2, 2);
3836 p.close();
3837 q.moveTo(2, 1);
3838 q.lineTo(2, 3);
3839 p.addPath(q, SkPath::kExtend_AddPathMode);
3840 uint8_t verbs[7];
3841 int verbcount = p.getVerbs(verbs, 7);
3842 REPORTER_ASSERT(reporter, verbcount == 7);
3843 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3844 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3845 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3846 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3847 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3848 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3849 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3850
3851 SkPoint pt;
3852 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3853 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3854 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3855}
3856
3857static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3858 SkPath p, q, r;
3859 // case 1: dst is empty
3860 p.moveTo(2, 1);
3861 p.lineTo(2, 3);
3862 q.addPath(p, mode);
3863 REPORTER_ASSERT(reporter, q == p);
3864 // case 2: src is empty
3865 p.addPath(r, mode);
3866 REPORTER_ASSERT(reporter, q == p);
3867 // case 3: src and dst are empty
3868 q.reset();
3869 q.addPath(r, mode);
3870 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003871}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003872
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003873static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3874 SkPath p;
3875 p.conicTo(1, 2, 3, 4, -1);
3876 check_path_is_line_and_reset(reporter, &p, 3, 4);
3877 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3878 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3879 p.conicTo(1, 2, 3, 4, 1);
3880 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3881}
3882
3883static void test_get_point(skiatest::Reporter* reporter) {
3884 SkPath p;
3885 SkPoint pt = p.getPoint(0);
3886 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003887 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003888 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3889 p.setLastPt(10, 10);
3890 pt = p.getPoint(0);
3891 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003892 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003893 p.rMoveTo(10, 10);
3894 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3895}
3896
3897static void test_contains(skiatest::Reporter* reporter) {
3898 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003899 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3900 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
3901 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
3902 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3903 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
3904 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 -08003905 // this may return true or false, depending on the platform's numerics, but it should not crash
3906 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003907
3908 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003909 p.setFillType(SkPath::kInverseWinding_FillType);
3910 REPORTER_ASSERT(reporter, p.contains(0, 0));
3911 p.setFillType(SkPath::kWinding_FillType);
3912 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3913 p.moveTo(4, 4);
3914 p.lineTo(6, 8);
3915 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003916 // test on edge
3917 REPORTER_ASSERT(reporter, p.contains(6, 4));
3918 REPORTER_ASSERT(reporter, p.contains(5, 6));
3919 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003920 // test quick reject
3921 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3922 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3923 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3924 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3925 // test various crossings in x
3926 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3927 REPORTER_ASSERT(reporter, p.contains(6, 7));
3928 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3929 p.reset();
3930 p.moveTo(4, 4);
3931 p.lineTo(8, 6);
3932 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003933 // test on edge
3934 REPORTER_ASSERT(reporter, p.contains(4, 6));
3935 REPORTER_ASSERT(reporter, p.contains(6, 5));
3936 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003937 // test various crossings in y
3938 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3939 REPORTER_ASSERT(reporter, p.contains(7, 6));
3940 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003941 p.reset();
3942 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003943 p.lineTo(8, 4);
3944 p.lineTo(8, 8);
3945 p.lineTo(4, 8);
3946 // test on vertices
3947 REPORTER_ASSERT(reporter, p.contains(4, 4));
3948 REPORTER_ASSERT(reporter, p.contains(8, 4));
3949 REPORTER_ASSERT(reporter, p.contains(8, 8));
3950 REPORTER_ASSERT(reporter, p.contains(4, 8));
3951 p.reset();
3952 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003953 p.lineTo(6, 8);
3954 p.lineTo(2, 8);
3955 // test on edge
3956 REPORTER_ASSERT(reporter, p.contains(5, 6));
3957 REPORTER_ASSERT(reporter, p.contains(4, 8));
3958 REPORTER_ASSERT(reporter, p.contains(3, 6));
3959 p.reset();
3960 p.moveTo(4, 4);
3961 p.lineTo(0, 6);
3962 p.lineTo(4, 8);
3963 // test on edge
3964 REPORTER_ASSERT(reporter, p.contains(2, 5));
3965 REPORTER_ASSERT(reporter, p.contains(2, 7));
3966 REPORTER_ASSERT(reporter, p.contains(4, 6));
3967 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3968 p.reset();
3969 p.moveTo(4, 0);
3970 p.lineTo(6, 4);
3971 p.lineTo(2, 4);
3972 p.moveTo(4, 0);
3973 p.lineTo(0, 8);
3974 p.lineTo(8, 8);
3975 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3976 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3977 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3978 REPORTER_ASSERT(reporter, p.contains(4, 4));
3979
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003980 // test quads
3981 p.reset();
3982 p.moveTo(4, 4);
3983 p.quadTo(6, 6, 8, 8);
3984 p.quadTo(6, 8, 4, 8);
3985 p.quadTo(4, 6, 4, 4);
3986 REPORTER_ASSERT(reporter, p.contains(5, 6));
3987 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003988 // test quad edge
3989 REPORTER_ASSERT(reporter, p.contains(5, 5));
3990 REPORTER_ASSERT(reporter, p.contains(5, 8));
3991 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003992 // test quad endpoints
3993 REPORTER_ASSERT(reporter, p.contains(4, 4));
3994 REPORTER_ASSERT(reporter, p.contains(8, 8));
3995 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003996
3997 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003998 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3999 p.moveTo(qPts[0]);
4000 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4001 p.quadTo(qPts[index], qPts[index + 1]);
4002 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004003 REPORTER_ASSERT(reporter, p.contains(5, 6));
4004 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004005 // test quad edge
4006 SkPoint halfway;
4007 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4008 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4009 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4010 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004011
caryclark9aacd902015-12-14 08:38:09 -08004012 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004013 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004014 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4015 p.moveTo(kPts[0]);
4016 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4017 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4018 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004019 REPORTER_ASSERT(reporter, p.contains(5, 6));
4020 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004021 // test conic edge
4022 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4023 SkConic conic(&kPts[index], 0.5f);
4024 halfway = conic.evalAt(0.5f);
4025 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4026 }
caryclark9cb5d752015-12-18 04:35:24 -08004027 // test conic end points
4028 REPORTER_ASSERT(reporter, p.contains(4, 4));
4029 REPORTER_ASSERT(reporter, p.contains(8, 8));
4030 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004031
4032 // test cubics
4033 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4034 for (int i = 0; i < 3; ++i) {
4035 p.reset();
4036 p.setFillType(SkPath::kEvenOdd_FillType);
4037 p.moveTo(pts[i].fX, pts[i].fY);
4038 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);
4039 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);
4040 p.close();
4041 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4042 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004043 // test cubic edge
4044 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4045 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4046 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4047 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004048 // test cubic end points
4049 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4050 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4051 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004052 }
4053}
4054
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004055class PathRefTest_Private {
4056public:
4057 static void TestPathRef(skiatest::Reporter* reporter) {
4058 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004059
bungeman6bd52842016-10-27 09:30:08 -07004060 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004061
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004062 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004063
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004064 {
4065 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4066 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4067 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4068 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4069 for (int i = 0; i < kRepeatCnt; ++i) {
4070 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4071 }
4072 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004073 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004074
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004075 {
4076 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4077 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4078 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4079 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4080 for (int i = 0; i < kRepeatCnt; ++i) {
4081 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4082 }
4083 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004084 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004085
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004086 {
4087 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4088 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4089 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4090 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4091 for (int i = 0; i < kRepeatCnt; ++i) {
4092 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4093 }
4094 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004095 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004096
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004097 {
halcanary96fcdcc2015-08-27 07:41:13 -07004098 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004099 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4100 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4101 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4102 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4103 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004104 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004105 for (int i = 0; i < kRepeatCnt; ++i) {
4106 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4107 }
4108 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004109 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004110
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004111 {
4112 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4113 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4114 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4115 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4116 for (int i = 0; i < kRepeatCnt; ++i) {
4117 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4118 }
4119 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004120 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004121 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004122};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004123
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004124static void test_operatorEqual(skiatest::Reporter* reporter) {
4125 SkPath a;
4126 SkPath b;
4127 REPORTER_ASSERT(reporter, a == a);
4128 REPORTER_ASSERT(reporter, a == b);
4129 a.setFillType(SkPath::kInverseWinding_FillType);
4130 REPORTER_ASSERT(reporter, a != b);
4131 a.reset();
4132 REPORTER_ASSERT(reporter, a == b);
4133 a.lineTo(1, 1);
4134 REPORTER_ASSERT(reporter, a != b);
4135 a.reset();
4136 REPORTER_ASSERT(reporter, a == b);
4137 a.lineTo(1, 1);
4138 b.lineTo(1, 2);
4139 REPORTER_ASSERT(reporter, a != b);
4140 a.reset();
4141 a.lineTo(1, 2);
4142 REPORTER_ASSERT(reporter, a == b);
4143}
4144
caryclark66a5d8b2014-06-24 08:30:15 -07004145static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004146 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004147 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004148 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004149 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004150 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004151 if (strlen(str) > 0) {
4152 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4153 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004154 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004155 }
caryclark66a5d8b2014-06-24 08:30:15 -07004156}
4157
4158static void test_dump(skiatest::Reporter* reporter) {
4159 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07004160 compare_dump(reporter, p, false, false, "");
4161 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07004162 p.moveTo(1, 2);
4163 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07004164 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4165 "path.lineTo(3, 4);\n");
4166 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
4167 "path.lineTo(3, 4);\n"
4168 "path.lineTo(1, 2);\n"
4169 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004170 p.reset();
4171 p.moveTo(1, 2);
4172 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07004173 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4174 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004175 p.reset();
4176 p.moveTo(1, 2);
4177 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07004178 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4179 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004180 p.reset();
4181 p.moveTo(1, 2);
4182 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07004183 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4184 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4185 p.reset();
4186 p.moveTo(1, 2);
4187 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004188 compare_dump(reporter, p, false, true,
4189 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4190 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004191 p.reset();
4192 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4193 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4194 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4195 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004196}
4197
senorblanco84cd6212015-08-04 10:01:58 -07004198namespace {
4199
4200class ChangeListener : public SkPathRef::GenIDChangeListener {
4201public:
4202 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4203 virtual ~ChangeListener() {}
4204 void onChange() override {
4205 *fChanged = true;
4206 }
4207private:
4208 bool* fChanged;
4209};
4210
4211}
4212
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004213class PathTest_Private {
4214public:
4215 static void TestPathTo(skiatest::Reporter* reporter) {
4216 SkPath p, q;
4217 p.lineTo(4, 4);
4218 p.reversePathTo(q);
4219 check_path_is_line(reporter, &p, 4, 4);
4220 q.moveTo(-4, -4);
4221 p.reversePathTo(q);
4222 check_path_is_line(reporter, &p, 4, 4);
4223 q.lineTo(7, 8);
4224 q.conicTo(8, 7, 6, 5, 0.5f);
4225 q.quadTo(6, 7, 8, 6);
4226 q.cubicTo(5, 6, 7, 8, 7, 5);
4227 q.close();
4228 p.reversePathTo(q);
4229 SkRect reverseExpected = {-4, -4, 8, 8};
4230 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4231 }
senorblanco84cd6212015-08-04 10:01:58 -07004232
4233 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4234 SkPath p;
4235
4236 bool changed = false;
4237 p.moveTo(0, 0);
4238
4239 // Check that listener is notified on moveTo().
4240
halcanary385fe4d2015-08-26 13:07:48 -07004241 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004242 REPORTER_ASSERT(reporter, !changed);
4243 p.moveTo(10, 0);
4244 REPORTER_ASSERT(reporter, changed);
4245
4246 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004247 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004248 REPORTER_ASSERT(reporter, !changed);
4249 p.lineTo(20, 0);
4250 REPORTER_ASSERT(reporter, changed);
4251
4252 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004253 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004254 REPORTER_ASSERT(reporter, !changed);
4255 p.reset();
4256 REPORTER_ASSERT(reporter, changed);
4257
4258 p.moveTo(0, 0);
4259
4260 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004261 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004262 REPORTER_ASSERT(reporter, !changed);
4263 p.rewind();
4264 REPORTER_ASSERT(reporter, changed);
4265
4266 // Check that listener is notified when pathref is deleted.
4267 {
4268 SkPath q;
4269 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004270 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004271 REPORTER_ASSERT(reporter, !changed);
4272 }
4273 // q went out of scope.
4274 REPORTER_ASSERT(reporter, changed);
4275 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004276};
4277
reed158fabb2016-07-20 10:06:59 -07004278static void test_crbug_629455(skiatest::Reporter* reporter) {
4279 SkPath path;
4280 path.moveTo(0, 0);
4281 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4282 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4283 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4284// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4285 path.lineTo(0, 0);
4286
4287 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4288 SkPaint paint;
4289 paint.setAntiAlias(true);
4290 surface->getCanvas()->drawPath(path, paint);
4291}
4292
liyuqianc78eff92016-11-09 11:18:30 -08004293static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4294 SkPath path;
4295 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4296 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4297 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4298 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4299 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4300 path.close();
4301
4302 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4303 SkPaint paint;
4304 paint.setAntiAlias(true);
4305 surface->getCanvas()->clipPath(path, true);
4306 surface->getCanvas()->drawRectCoords(0, 0, 100, 100, paint);
4307}
4308
Yuqian Lic4f66af2016-11-11 09:36:53 -05004309static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4310 SkPath path;
4311 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4312 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4313 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4314 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4315 path.close();
4316
4317 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4318 SkPaint paint;
4319 paint.setAntiAlias(true);
4320 surface->getCanvas()->drawPath(path, paint);
4321}
4322
caryclark8e7b19d2016-02-18 04:11:48 -08004323static void test_interp(skiatest::Reporter* reporter) {
4324 SkPath p1, p2, out;
4325 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4326 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4327 REPORTER_ASSERT(reporter, p1 == out);
4328 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4329 REPORTER_ASSERT(reporter, p1 == out);
4330 p1.moveTo(0, 2);
4331 p1.lineTo(0, 4);
4332 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4333 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4334 p2.moveTo(6, 0);
4335 p2.lineTo(8, 0);
4336 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4337 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4338 REPORTER_ASSERT(reporter, p2 == out);
4339 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4340 REPORTER_ASSERT(reporter, p1 == out);
4341 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4342 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4343 p1.reset();
4344 p1.moveTo(4, 4);
4345 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4346 p2.reset();
4347 p2.moveTo(4, 2);
4348 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4349 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4350 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4351 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4352 p2.reset();
4353 p2.moveTo(4, 2);
4354 p2.conicTo(6, 3, 6, 5, 1);
4355 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4356 p2.reset();
4357 p2.moveTo(4, 4);
4358 p2.conicTo(5, 4, 5, 5, 0.5f);
4359 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4360}
4361
4362DEF_TEST(PathInterp, reporter) {
4363 test_interp(reporter);
4364}
4365
caryclark9aacd902015-12-14 08:38:09 -08004366DEF_TEST(PathContains, reporter) {
4367 test_contains(reporter);
4368}
4369
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004370DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004371 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004372 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004373 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004374 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004375 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004376 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004377 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004378 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004379 test_fuzz_crbug_662780();
Yuqian Li20079a92016-11-16 13:07:57 -05004380 test_mask_overflow();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004381
bungeman@google.coma5809a32013-06-21 15:13:34 +00004382 SkTSize<SkScalar>::Make(3,4);
4383
4384 SkPath p, empty;
4385 SkRect bounds, bounds2;
4386 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004387
reed@android.comd252db02009-04-01 18:31:44 +00004388 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004389
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004390 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4391 SkPath& self = p;
4392 p = self;
4393
4394 // this triggers a code path in SkPath::swap which is otherwise unexercised
4395 p.swap(self);
4396
reed@android.com3abec1d2009-03-02 05:36:20 +00004397 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004398
reed@android.com6b82d1a2009-06-03 02:35:01 +00004399 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4400 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004401 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004402 REPORTER_ASSERT(reporter,
4403 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004404 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004405
reed@android.com6b82d1a2009-06-03 02:35:01 +00004406 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004407 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004408
reed@android.com6b82d1a2009-06-03 02:35:01 +00004409 p.addOval(bounds);
4410 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004411 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004412
bungeman@google.coma5809a32013-06-21 15:13:34 +00004413 p.rewind();
4414 test_empty(reporter, p);
4415
reed@android.com3abec1d2009-03-02 05:36:20 +00004416 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004417 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004418 // we have only lines
4419 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004420 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004421
bungeman@google.coma5809a32013-06-21 15:13:34 +00004422 REPORTER_ASSERT(reporter, p != empty);
4423 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004424
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004425 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004426 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4427 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004428 SkPoint pts[4];
4429 int count = p.getPoints(pts, 4);
4430 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004431 uint8_t verbs[6];
4432 verbs[5] = 0xff;
4433 p.getVerbs(verbs, 5);
4434 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4435 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4436 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4437 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4438 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4439 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004440 bounds2.set(pts, 4);
4441 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004442
reed@android.com3abec1d2009-03-02 05:36:20 +00004443 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4444 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004445 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004446
halcanary96fcdcc2015-08-27 07:41:13 -07004447 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004448 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004449 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4450 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004451
reed@android.com3abec1d2009-03-02 05:36:20 +00004452 // now force p to not be a rect
4453 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4454 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004455 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004456
robertphillipsfe7c4272014-12-29 11:36:39 -08004457 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4458 // path has a trailing moveTo. Please see crbug.com\445368)
4459 {
4460 SkRect r;
4461 p.reset();
4462 p.addRect(bounds);
4463 REPORTER_ASSERT(reporter, p.isRect(&r));
4464 REPORTER_ASSERT(reporter, r == bounds);
4465 // add a moveTo outside of our bounds
4466 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4467 REPORTER_ASSERT(reporter, p.isRect(&r));
4468 REPORTER_ASSERT(reporter, r == bounds);
4469 }
4470
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004471 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004472 test_isLine(reporter);
4473 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004474 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004475 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004476 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004477 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004478 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004479 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004480 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004481 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004482 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004483 test_flattening(reporter);
4484 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004485 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004486 test_iter(reporter);
4487 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004488 test_circle(reporter);
4489 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004490 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004491 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004492 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004493 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004494 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004495 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004496 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004497 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004498 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004499 test_tricky_cubic();
4500 test_clipped_cubic();
4501 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004502 test_crbug_493450(reporter);
4503 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004504 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004505 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004506 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004507 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004508 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004509 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004510 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004511 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004512 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004513 test_arcTo(reporter);
4514 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004515 test_addPathMode(reporter, false, false);
4516 test_addPathMode(reporter, true, false);
4517 test_addPathMode(reporter, false, true);
4518 test_addPathMode(reporter, true, true);
4519 test_extendClosedPath(reporter);
4520 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4521 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004522 test_conicTo_special_case(reporter);
4523 test_get_point(reporter);
4524 test_contains(reporter);
4525 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004526 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004527 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004528 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004529 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004530 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004531 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004532 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004533 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004534 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004535}