blob: 117da5bdeb7279dba08c432ed74bdb311715f6ff [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
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -080010#include "SkGeometry.h"
Cary Clarke0b72872017-04-12 12:03:15 -040011#include "SkNullCanvas.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000012#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000013#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000014#include "SkParsePath.h"
Kevin Lubickc456b732017-01-11 17:21:57 +000015#include "SkPathEffect.h"
Hal Canary95e3c052017-01-11 12:44:43 -050016#include "SkPathPriv.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000018#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000019#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000020#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070021#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070022#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000023#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000024#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
Hal Canary95e3c052017-01-11 12:44:43 -050026#include <cmath>
reed@google.com8cae8352012-09-14 15:18:41 +000027
Cary Clarke0b72872017-04-12 12:03:15 -040028
reed5bcbe912014-12-15 12:28:33 -080029static void set_radii(SkVector radii[4], int index, float rad) {
30 sk_bzero(radii, sizeof(SkVector) * 4);
31 radii[index].set(rad, rad);
32}
33
34static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
35 const SkVector radii[4]) {
36 SkRRect rrect;
37 rrect.setRectRadii(bounds, radii);
38 REPORTER_ASSERT(reporter, bounds == rrect.rect());
39
40 SkPath path;
41 // this line should not assert in the debug build (from validate)
42 path.addRRect(rrect);
43 REPORTER_ASSERT(reporter, bounds == path.getBounds());
44}
45
caryclark5ccef572015-03-02 10:07:56 -080046static void test_skbug_3469(skiatest::Reporter* reporter) {
47 SkPath path;
48 path.moveTo(20, 20);
49 path.quadTo(20, 50, 80, 50);
50 path.quadTo(20, 50, 20, 80);
51 REPORTER_ASSERT(reporter, !path.isConvex());
52}
53
reed5bcbe912014-12-15 12:28:33 -080054static void test_skbug_3239(skiatest::Reporter* reporter) {
55 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
56 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
57 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
58
59 const float rad = 33436320;
60
61 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
62 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
63
64 SkVector radii[4];
65 for (int i = 0; i < 4; ++i) {
66 set_radii(radii, i, rad);
67 test_add_rrect(reporter, rectx, radii);
68 test_add_rrect(reporter, recty, radii);
69 }
70}
71
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000072static void make_path_crbug364224(SkPath* path) {
73 path->reset();
74 path->moveTo(3.747501373f, 2.724499941f);
75 path->lineTo(3.747501373f, 3.75f);
76 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
77 path->lineTo(0.7475013733f, 4.0f);
78 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
79 path->lineTo(0.4975013733f, 1.0f);
80 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
81 path->lineTo(3.497501373f, 0.75f);
82 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
83 path->lineTo(3.715001345f, 0.5512499809f);
84 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
85 path->lineTo(0.7475013733f, 0.4999999702f);
86 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
87 path->lineTo(0.2475013733f, 3.75f);
88 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
89 path->lineTo(3.497501373f, 4.25f);
90 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
91 path->lineTo(3.997501373f, 2.474750042f);
92 path->lineTo(3.747501373f, 2.724499941f);
93 path->close();
94}
95
96static void make_path_crbug364224_simplified(SkPath* path) {
97 path->moveTo(3.747501373f, 2.724499941f);
98 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
99 path->close();
100}
101
reedff863bc2016-08-17 07:37:57 -0700102static void test_sect_with_horizontal_needs_pinning() {
103 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the
104 // intersection.
105 SkPath path;
106 path.reset();
107 path.moveTo(-540000, -720000);
108 path.lineTo(-9.10000017e-05f, 9.99999996e-13f);
109 path.lineTo(1, 1);
110
111 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper
112 SkPaint paint;
113 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint);
114}
115
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000116static void test_path_crbug364224() {
117 SkPath path;
118 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700119 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000120 SkCanvas* canvas = surface->getCanvas();
121
122 make_path_crbug364224_simplified(&path);
123 canvas->drawPath(path, paint);
124
125 make_path_crbug364224(&path);
126 canvas->drawPath(path, paint);
127}
128
caryclark3cebe242016-08-23 09:41:00 -0700129// this is a unit test instead of a GM because it doesn't draw anything
130static void test_fuzz_crbug_638223() {
131 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
132 SkCanvas* canvas = surface->getCanvas();
133 SkPath path;
134 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f
135 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000),
136 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0),
137 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f
138 SkPaint paint;
139 paint.setAntiAlias(true);
140 canvas->drawPath(path, paint);
141}
142
caryclarke97fe832016-09-06 08:54:10 -0700143static void test_fuzz_crbug_643933() {
144 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
145 SkCanvas* canvas = surface->getCanvas();
146 SkPaint paint;
147 paint.setAntiAlias(true);
148 SkPath path;
149 path.moveTo(0, 0);
150 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f
151 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f
152 SkBits2Float(0x721aee0c)); // 3.0687e+30f
153 canvas->drawPath(path, paint);
154 path.reset();
155 path.moveTo(0, 0);
156 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f
157 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f
158 SkBits2Float(0x5d06eff8)); // 6.07704e+17f
159 canvas->drawPath(path, paint);
160}
161
caryclarkbac10462016-09-22 10:24:59 -0700162static void test_fuzz_crbug_647922() {
163 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
164 SkCanvas* canvas = surface->getCanvas();
165 SkPaint paint;
166 paint.setAntiAlias(true);
167 SkPath path;
168 path.moveTo(0, 0);
169 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f
170 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f
171 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f
172 canvas->drawPath(path, paint);
173}
174
liyuqian041da382016-11-11 09:59:51 -0800175static void test_fuzz_crbug_662780() {
176 auto surface(SkSurface::MakeRasterN32Premul(250, 250));
177 SkCanvas* canvas = surface->getCanvas();
178 SkPaint paint;
179 paint.setAntiAlias(true);
180 SkPath path;
181 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
182 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120
183 // 8, 8, 8.00002f, 8, 0.707107f
184 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000),
185 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
186 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8
187 // 308, 8, 308, 8, 0.707107f
188 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000),
189 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3));
190 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158
191 // 308, 158, 308, 158, 0.707107f
192 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000),
193 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
194 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158
195 // 8, 158, 8, 158, 0.707107f
196 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000),
197 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3));
198 path.close();
199 canvas->clipPath(path, true);
Mike Reed3661bc92017-02-22 13:21:42 -0500200 canvas->drawRect(SkRect::MakeWH(250, 250), paint);
liyuqian041da382016-11-11 09:59:51 -0800201}
202
Yuqian Li20079a92016-11-16 13:07:57 -0500203static void test_mask_overflow() {
204 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
205 SkCanvas* canvas = surface->getCanvas();
206 SkPaint paint;
207 paint.setAntiAlias(true);
208 SkPath path;
209 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
210 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341
211 // 440.47f, 341, 437, 344.47f, 437, 348.75f
212 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000),
213 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29),
214 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000));
215 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355
216 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355
217 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341
218 canvas->drawPath(path, paint);
219}
220
Yuqian Li79252f72016-11-29 15:02:49 -0500221static void test_fuzz_crbug_668907() {
222 auto surface(SkSurface::MakeRasterN32Premul(400, 500));
223 SkCanvas* canvas = surface->getCanvas();
224 SkPaint paint;
225 paint.setAntiAlias(true);
226 SkPath path;
227 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
228 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141),
229 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f
230 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f
231 path.close();
232 canvas->drawPath(path, paint);
233}
234
piotaixrfac4e0e2014-08-26 11:59:04 -0700235/**
236 * In debug mode, this path was causing an assertion to fail in
237 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
238 */
239static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
240 SkPoint orig, p1, p2, p3;
241 orig = SkPoint::Make(1.f, 1.f);
242 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
243 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
244 p3 = SkPoint::Make(2.f, 2.f);
245
246 path->reset();
247 path->moveTo(orig);
248 path->cubicTo(p1, p2, p3);
249 path->close();
250}
251
252static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
253 SkPath path;
254 make_path_crbugskia2820(&path, reporter);
255
256 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
257 stroke.setStrokeStyle(2 * SK_Scalar1);
258 stroke.applyToPath(&path, path);
259}
260
Yuqian Li9939bd62016-11-29 10:27:16 -0500261static void test_path_crbugskia5995() {
262 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
263 SkCanvas* canvas = surface->getCanvas();
264 SkPaint paint;
265 paint.setAntiAlias(true);
266 SkPath path;
267 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f
268 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030),
269 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0
270 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
271 canvas->drawPath(path, paint);
272}
273
reed@google.comcc8be772013-10-15 15:35:29 +0000274static void make_path0(SkPath* path) {
275 // from * https://code.google.com/p/skia/issues/detail?id=1706
276
277 path->moveTo(146.939f, 1012.84f);
278 path->lineTo(181.747f, 1009.18f);
279 path->lineTo(182.165f, 1013.16f);
280 path->lineTo(147.357f, 1016.82f);
281 path->lineTo(146.939f, 1012.84f);
282 path->close();
283}
284
285static void make_path1(SkPath* path) {
286 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
287}
288
289typedef void (*PathProc)(SkPath*);
290
291/*
292 * Regression test: we used to crash (overwrite internal storage) during
293 * construction of the region when the path was INVERSE. That is now fixed,
294 * so test these regions (which used to assert/crash).
295 *
296 * https://code.google.com/p/skia/issues/detail?id=1706
297 */
298static void test_path_to_region(skiatest::Reporter* reporter) {
299 PathProc procs[] = {
300 make_path0,
301 make_path1,
302 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000303
reed@google.comcc8be772013-10-15 15:35:29 +0000304 SkRegion clip;
305 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000306
reed@google.comcc8be772013-10-15 15:35:29 +0000307 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
308 SkPath path;
309 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000310
reed@google.comcc8be772013-10-15 15:35:29 +0000311 SkRegion rgn;
312 rgn.setPath(path, clip);
313 path.toggleInverseFillType();
314 rgn.setPath(path, clip);
315 }
316}
317
halcanary4dbbd042016-06-07 17:21:10 -0700318#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000319 #define SUPPRESS_VISIBILITY_WARNING
320#else
321 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
322#endif
323
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000324static void test_path_close_issue1474(skiatest::Reporter* reporter) {
325 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
326 // are relative to the point we close to, not relative to the point we close from.
327 SkPath path;
328 SkPoint last;
329
330 // Test rLineTo().
331 path.rLineTo(0, 100);
332 path.rLineTo(100, 0);
333 path.close(); // Returns us back to 0,0.
334 path.rLineTo(50, 50); // This should go to 50,50.
335
336 path.getLastPt(&last);
337 REPORTER_ASSERT(reporter, 50 == last.fX);
338 REPORTER_ASSERT(reporter, 50 == last.fY);
339
340 // Test rQuadTo().
341 path.rewind();
342 path.rLineTo(0, 100);
343 path.rLineTo(100, 0);
344 path.close();
345 path.rQuadTo(50, 50, 75, 75);
346
347 path.getLastPt(&last);
348 REPORTER_ASSERT(reporter, 75 == last.fX);
349 REPORTER_ASSERT(reporter, 75 == last.fY);
350
351 // Test rConicTo().
352 path.rewind();
353 path.rLineTo(0, 100);
354 path.rLineTo(100, 0);
355 path.close();
356 path.rConicTo(50, 50, 85, 85, 2);
357
358 path.getLastPt(&last);
359 REPORTER_ASSERT(reporter, 85 == last.fX);
360 REPORTER_ASSERT(reporter, 85 == last.fY);
361
362 // Test rCubicTo().
363 path.rewind();
364 path.rLineTo(0, 100);
365 path.rLineTo(100, 0);
366 path.close();
367 path.rCubicTo(50, 50, 85, 85, 95, 95);
368
369 path.getLastPt(&last);
370 REPORTER_ASSERT(reporter, 95 == last.fX);
371 REPORTER_ASSERT(reporter, 95 == last.fY);
372}
373
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000374static void test_gen_id(skiatest::Reporter* reporter) {
375 SkPath a, b;
376 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
377
378 a.moveTo(0, 0);
379 const uint32_t z = a.getGenerationID();
380 REPORTER_ASSERT(reporter, z != b.getGenerationID());
381
382 a.reset();
383 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
384
385 a.moveTo(1, 1);
386 const uint32_t y = a.getGenerationID();
387 REPORTER_ASSERT(reporter, z != y);
388
389 b.moveTo(2, 2);
390 const uint32_t x = b.getGenerationID();
391 REPORTER_ASSERT(reporter, x != y && x != z);
392
393 a.swap(b);
394 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
395
396 b = a;
397 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
398
399 SkPath c(a);
400 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
401
402 c.lineTo(3, 3);
403 const uint32_t w = c.getGenerationID();
404 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
405 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
406 REPORTER_ASSERT(reporter, w != x);
407
djsollen50da1d82015-02-17 09:09:53 -0800408#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000409 static bool kExpectGenIDToIgnoreFill = false;
410#else
411 static bool kExpectGenIDToIgnoreFill = true;
412#endif
413
414 c.toggleInverseFillType();
415 const uint32_t v = c.getGenerationID();
416 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
417
418 c.rewind();
419 REPORTER_ASSERT(reporter, v != c.getGenerationID());
420}
421
reed@google.com3eff3592013-05-08 21:08:21 +0000422// This used to assert in the debug build, as the edges did not all line-up.
423static void test_bad_cubic_crbug234190() {
424 SkPath path;
425 path.moveTo(13.8509f, 3.16858f);
426 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
427 -2.38991e+08f, -4.26573e+08f,
428 -2.41016e+08f, -4.30188e+08f);
429
430 SkPaint paint;
431 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700432 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000433 surface->getCanvas()->drawPath(path, paint);
434}
435
reed@google.com7a90daf2013-04-10 18:44:00 +0000436static void test_bad_cubic_crbug229478() {
437 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000438 { 4595.91064f, -11596.9873f },
439 { 4597.2168f, -11595.9414f },
440 { 4598.52344f, -11594.8955f },
441 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000442 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000443
reed@google.com7a90daf2013-04-10 18:44:00 +0000444 SkPath path;
445 path.moveTo(pts[0]);
446 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000447
reed@google.com7a90daf2013-04-10 18:44:00 +0000448 SkPaint paint;
449 paint.setStyle(SkPaint::kStroke_Style);
450 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000451
reed@google.com7a90daf2013-04-10 18:44:00 +0000452 SkPath dst;
453 // Before the fix, this would infinite-recurse, and run out of stack
454 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700455 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000456}
457
reed@google.com64d62952013-01-18 17:49:28 +0000458static void build_path_170666(SkPath& path) {
459 path.moveTo(17.9459f, 21.6344f);
460 path.lineTo(139.545f, -47.8105f);
461 path.lineTo(139.545f, -47.8105f);
462 path.lineTo(131.07f, -47.3888f);
463 path.lineTo(131.07f, -47.3888f);
464 path.lineTo(122.586f, -46.9532f);
465 path.lineTo(122.586f, -46.9532f);
466 path.lineTo(18076.6f, 31390.9f);
467 path.lineTo(18076.6f, 31390.9f);
468 path.lineTo(18085.1f, 31390.5f);
469 path.lineTo(18085.1f, 31390.5f);
470 path.lineTo(18076.6f, 31390.9f);
471 path.lineTo(18076.6f, 31390.9f);
472 path.lineTo(17955, 31460.3f);
473 path.lineTo(17955, 31460.3f);
474 path.lineTo(17963.5f, 31459.9f);
475 path.lineTo(17963.5f, 31459.9f);
476 path.lineTo(17971.9f, 31459.5f);
477 path.lineTo(17971.9f, 31459.5f);
478 path.lineTo(17.9551f, 21.6205f);
479 path.lineTo(17.9551f, 21.6205f);
480 path.lineTo(9.47091f, 22.0561f);
481 path.lineTo(9.47091f, 22.0561f);
482 path.lineTo(17.9459f, 21.6344f);
483 path.lineTo(17.9459f, 21.6344f);
484 path.close();path.moveTo(0.995934f, 22.4779f);
485 path.lineTo(0.986725f, 22.4918f);
486 path.lineTo(0.986725f, 22.4918f);
487 path.lineTo(17955, 31460.4f);
488 path.lineTo(17955, 31460.4f);
489 path.lineTo(17971.9f, 31459.5f);
490 path.lineTo(17971.9f, 31459.5f);
491 path.lineTo(18093.6f, 31390.1f);
492 path.lineTo(18093.6f, 31390.1f);
493 path.lineTo(18093.6f, 31390);
494 path.lineTo(18093.6f, 31390);
495 path.lineTo(139.555f, -47.8244f);
496 path.lineTo(139.555f, -47.8244f);
497 path.lineTo(122.595f, -46.9671f);
498 path.lineTo(122.595f, -46.9671f);
499 path.lineTo(0.995934f, 22.4779f);
500 path.lineTo(0.995934f, 22.4779f);
501 path.close();
502 path.moveTo(5.43941f, 25.5223f);
503 path.lineTo(798267, -28871.1f);
504 path.lineTo(798267, -28871.1f);
505 path.lineTo(3.12512e+06f, -113102);
506 path.lineTo(3.12512e+06f, -113102);
507 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
508 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
509 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
510 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
511 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
512 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
513 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
514 path.lineTo(2.78271e+08f, -1.00733e+07f);
515 path.lineTo(2.78271e+08f, -1.00733e+07f);
516 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
517 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
518 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
519 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
520 path.lineTo(2.77473e+08f, -1.00444e+07f);
521 path.lineTo(2.77473e+08f, -1.00444e+07f);
522 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
523 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
524 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
525 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
526 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
527 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
528 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
529 path.lineTo(798284, -28872);
530 path.lineTo(798284, -28872);
531 path.lineTo(22.4044f, 24.6677f);
532 path.lineTo(22.4044f, 24.6677f);
533 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
534 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
535 path.close();
536}
537
538static void build_path_simple_170666(SkPath& path) {
539 path.moveTo(126.677f, 24.1591f);
540 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
541}
542
543// This used to assert in the SK_DEBUG build, as the clip step would fail with
544// too-few interations in our cubic-line intersection code. That code now runs
545// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000546static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000547 SkPath path;
548 SkPaint paint;
549 paint.setAntiAlias(true);
550
reede8f30622016-03-23 18:59:25 -0700551 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000552
reed@google.com64d62952013-01-18 17:49:28 +0000553 build_path_simple_170666(path);
554 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000555
reed@google.com64d62952013-01-18 17:49:28 +0000556 build_path_170666(path);
557 surface->getCanvas()->drawPath(path, paint);
558}
559
caryclarke8c56662015-07-14 11:19:26 -0700560
561static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
562 SkScalar tx, SkScalar ty, SkScalar scale) {
563 SkPath smallPath;
564 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
565 bool smallConvex = smallPath.isConvex();
566 SkPath largePath;
567 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
568 SkMatrix matrix;
569 matrix.reset();
570 matrix.preTranslate(100, 100);
571 matrix.preScale(scale, scale);
572 largePath.transform(matrix);
573 bool largeConvex = largePath.isConvex();
574 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
575}
576
577static void test_crbug_493450(skiatest::Reporter* reporter) {
578 const char reducedCase[] =
579 "M0,0"
580 "L0.0002, 0"
581 "L0.0002, 0.0002"
582 "L0.0001, 0.0001"
583 "L0,0.0002"
584 "Z";
585 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
586 const char originalFiddleData[] =
587 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
588 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
589 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
590 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
591 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
592 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
593 826357.3384828606f);
594}
595
596static void test_crbug_495894(skiatest::Reporter* reporter) {
597 const char originalFiddleData[] =
598 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
599 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
600 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
601 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
602 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
603 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
604 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
605 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
606 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
607 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
608 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
609 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
610 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
611 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
612 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
613 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
614 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
615 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
616 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
617 "L-0.33993994441916414,-0.11288906492739594Z";
618 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
619 65536);
620}
621
mbarbella99600d02016-06-01 15:39:47 -0700622static void test_crbug_613918() {
623 SkPath path;
624 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
625 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
626 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
627
628 // This call could lead to an assert or uninitialized read due to a failure
629 // to check the return value from SkCubicClipper::ChopMonoAtY.
630 path.contains(-1.84817e-08f, 1.15465e-08f);
631}
632
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000633static void test_addrect(skiatest::Reporter* reporter) {
634 SkPath path;
635 path.lineTo(0, 0);
636 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700637 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000638
639 path.reset();
640 path.lineTo(FLT_EPSILON, FLT_EPSILON);
641 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700642 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000643
644 path.reset();
645 path.quadTo(0, 0, 0, 0);
646 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700647 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000648
649 path.reset();
650 path.conicTo(0, 0, 0, 0, 0.5f);
651 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700652 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000653
654 path.reset();
655 path.cubicTo(0, 0, 0, 0, 0, 0);
656 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700657 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000658}
659
reed@google.coma8790de2012-10-24 21:04:04 +0000660// Make sure we stay non-finite once we get there (unless we reset or rewind).
661static void test_addrect_isfinite(skiatest::Reporter* reporter) {
662 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000663
reed@google.coma8790de2012-10-24 21:04:04 +0000664 path.addRect(SkRect::MakeWH(50, 100));
665 REPORTER_ASSERT(reporter, path.isFinite());
666
667 path.moveTo(0, 0);
668 path.lineTo(SK_ScalarInfinity, 42);
669 REPORTER_ASSERT(reporter, !path.isFinite());
670
671 path.addRect(SkRect::MakeWH(50, 100));
672 REPORTER_ASSERT(reporter, !path.isFinite());
673
674 path.reset();
675 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000676
reed@google.coma8790de2012-10-24 21:04:04 +0000677 path.addRect(SkRect::MakeWH(50, 100));
678 REPORTER_ASSERT(reporter, path.isFinite());
679}
680
reed@google.com848148e2013-01-15 15:51:59 +0000681static void build_big_path(SkPath* path, bool reducedCase) {
682 if (reducedCase) {
683 path->moveTo(577330, 1971.72f);
684 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
685 } else {
686 path->moveTo(60.1631f, 7.70567f);
687 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
688 path->lineTo(577379, 1977.77f);
689 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
690 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
691 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
692 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
693 path->quadTo(577198, 1972, 577238, 1971.31f);
694 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
695 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
696 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
697 path->moveTo(306.718f, -32.912f);
698 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
699 }
700}
701
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000702static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700703 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000704
705 // This path used to assert, because our cubic-chopping code incorrectly
706 // moved control points after the chop. This test should be run in SK_DEBUG
707 // mode to ensure that we no long assert.
708 SkPath path;
709 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
710 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000711
reed@google.com848148e2013-01-15 15:51:59 +0000712 SkPaint paint;
713 for (int doAA = 0; doAA <= 1; ++doAA) {
714 paint.setAntiAlias(SkToBool(doAA));
715 surface->getCanvas()->drawPath(path, paint);
716 }
717 }
718}
719
reed3b2fb982015-07-29 08:37:13 -0700720static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
721 if (expected != bounds) {
722 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
723 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
724 expected.left(), expected.top(), expected.right(), expected.bottom());
725 }
726}
727
reed91f283b2015-07-28 06:00:50 -0700728static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
729 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700730#if 0
731 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
732 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700733 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
734
735 path.moveTo(-5, -8);
736 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
737
738 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
739 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
740
741 path.moveTo(1, 2);
742 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700743#else
744 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
745
746 path.moveTo(-5, -8); // should set the bounds
747 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
748
749 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
750 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
751
752 path.moveTo(1, 2); // don't expect this to have changed the bounds
753 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
754#endif
reed91f283b2015-07-28 06:00:50 -0700755}
756
reedb1b12f82016-07-13 10:56:53 -0700757#include "SkSurface.h"
758static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) {
759 SkPath path;
760 path.moveTo(0, 0);
761 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f);
762
763 SkPaint paint;
764 paint.setAntiAlias(true);
765
766 auto surf = SkSurface::MakeRasterN32Premul(100, 100);
767 surf->getCanvas()->drawPath(path, paint);
768}
769
reed@google.com8cae8352012-09-14 15:18:41 +0000770// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
771// which triggered an assert, from a tricky cubic. This test replicates that
772// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
773// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000774static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000775 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000776 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
777 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
778 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
779 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000780 };
781
782 SkPath path;
783 path.moveTo(pts[0]);
784 path.cubicTo(pts[1], pts[2], pts[3]);
785
786 SkPaint paint;
787 paint.setAntiAlias(true);
788
reede8f30622016-03-23 18:59:25 -0700789 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000790}
reed@android.com3abec1d2009-03-02 05:36:20 +0000791
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000792// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
793//
794static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
795 SkPath path;
796 path.quadTo(157, 366, 286, 208);
797 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000798
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000799 SkMatrix matrix;
800 matrix.setScale(1000*1000, 1000*1000);
801
802 // Be sure that path::transform correctly updates isFinite and the bounds
803 // if the transformation overflows. The previous bug was that isFinite was
804 // set to true in this case, but the bounds were not set to empty (which
805 // they should be).
806 while (path.isFinite()) {
807 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
808 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
809 path.transform(matrix);
810 }
811 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
812
813 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
814 path.transform(matrix);
815 // we need to still be non-finite
816 REPORTER_ASSERT(reporter, !path.isFinite());
817 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
818}
819
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000820static void add_corner_arc(SkPath* path, const SkRect& rect,
821 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000822 int startAngle)
823{
824
825 SkScalar rx = SkMinScalar(rect.width(), xIn);
826 SkScalar ry = SkMinScalar(rect.height(), yIn);
827
828 SkRect arcRect;
829 arcRect.set(-rx, -ry, rx, ry);
830 switch (startAngle) {
831 case 0:
832 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
833 break;
834 case 90:
835 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
836 break;
837 case 180:
838 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
839 break;
840 case 270:
841 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
842 break;
843 default:
844 break;
845 }
846
847 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
848}
849
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000850static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000851 SkScalar xCorner, SkScalar yCorner) {
852 // we are lazy here and use the same x & y for each corner
853 add_corner_arc(path, r, xCorner, yCorner, 270);
854 add_corner_arc(path, r, xCorner, yCorner, 0);
855 add_corner_arc(path, r, xCorner, yCorner, 90);
856 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000857 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000858}
859
860// Chrome creates its own round rects with each corner possibly being different.
861// Performance will suffer if they are not convex.
862// Note: PathBench::ArbRoundRectBench performs almost exactly
863// the same test (but with drawing)
864static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000865 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000866 SkRect r;
867
868 for (int i = 0; i < 5000; ++i) {
869
robertphillips@google.com158618e2012-10-23 16:56:56 +0000870 SkScalar size = rand.nextUScalar1() * 30;
871 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000872 continue;
873 }
874 r.fLeft = rand.nextUScalar1() * 300;
875 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000876 r.fRight = r.fLeft + 2 * size;
877 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000878
879 SkPath temp;
880
881 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
882
883 REPORTER_ASSERT(reporter, temp.isConvex());
884 }
885}
886
robertphillips@google.com158618e2012-10-23 16:56:56 +0000887// Chrome will sometimes create a 0 radius round rect. The degenerate
888// quads prevent the path from being converted to a rect
889// Note: PathBench::ArbRoundRectBench performs almost exactly
890// the same test (but with drawing)
891static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000892 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000893 SkRect r;
894
895 for (int i = 0; i < 5000; ++i) {
896
897 SkScalar size = rand.nextUScalar1() * 30;
898 if (size < SK_Scalar1) {
899 continue;
900 }
901 r.fLeft = rand.nextUScalar1() * 300;
902 r.fTop = rand.nextUScalar1() * 300;
903 r.fRight = r.fLeft + 2 * size;
904 r.fBottom = r.fTop + 2 * size;
905
906 SkPath temp;
907
908 make_arb_round_rect(&temp, r, 0, 0);
909
robertphillips@google.com158618e2012-10-23 16:56:56 +0000910 SkRect result;
911 REPORTER_ASSERT(reporter, temp.isRect(&result));
912 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000913 }
914}
915
reed@google.com0bb18bb2012-07-26 15:20:36 +0000916static void test_rect_isfinite(skiatest::Reporter* reporter) {
917 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000918 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000919 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000920
reed@google.com0bb18bb2012-07-26 15:20:36 +0000921 SkRect r;
922 r.setEmpty();
923 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000924 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000925 REPORTER_ASSERT(reporter, !r.isFinite());
926 r.set(0, 0, nan, 0);
927 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000928
reed@google.com0bb18bb2012-07-26 15:20:36 +0000929 SkPoint pts[] = {
930 { 0, 0 },
931 { SK_Scalar1, 0 },
932 { 0, SK_Scalar1 },
933 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000934
reed@google.com0bb18bb2012-07-26 15:20:36 +0000935 bool isFine = r.setBoundsCheck(pts, 3);
936 REPORTER_ASSERT(reporter, isFine);
937 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000938
reed@google.com0bb18bb2012-07-26 15:20:36 +0000939 pts[1].set(inf, 0);
940 isFine = r.setBoundsCheck(pts, 3);
941 REPORTER_ASSERT(reporter, !isFine);
942 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000943
reed@google.com0bb18bb2012-07-26 15:20:36 +0000944 pts[1].set(nan, 0);
945 isFine = r.setBoundsCheck(pts, 3);
946 REPORTER_ASSERT(reporter, !isFine);
947 REPORTER_ASSERT(reporter, r.isEmpty());
948}
949
950static void test_path_isfinite(skiatest::Reporter* reporter) {
951 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000952 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000953 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000954
reed@google.com0bb18bb2012-07-26 15:20:36 +0000955 SkPath path;
956 REPORTER_ASSERT(reporter, path.isFinite());
957
958 path.reset();
959 REPORTER_ASSERT(reporter, path.isFinite());
960
961 path.reset();
962 path.moveTo(SK_Scalar1, 0);
963 REPORTER_ASSERT(reporter, path.isFinite());
964
965 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000966 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000967 REPORTER_ASSERT(reporter, !path.isFinite());
968
969 path.reset();
970 path.moveTo(nan, 0);
971 REPORTER_ASSERT(reporter, !path.isFinite());
972}
973
974static void test_isfinite(skiatest::Reporter* reporter) {
975 test_rect_isfinite(reporter);
976 test_path_isfinite(reporter);
977}
978
fsb1475b02016-01-20 09:51:07 -0800979static void test_islastcontourclosed(skiatest::Reporter* reporter) {
980 SkPath path;
981 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
982 path.moveTo(0, 0);
983 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
984 path.close();
985 REPORTER_ASSERT(reporter, path.isLastContourClosed());
986 path.lineTo(100, 100);
987 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
988 path.moveTo(200, 200);
989 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
990 path.close();
991 REPORTER_ASSERT(reporter, path.isLastContourClosed());
992 path.moveTo(0, 0);
993 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
994}
995
reed@google.com744faba2012-05-29 19:54:52 +0000996// assert that we always
997// start with a moveTo
998// only have 1 moveTo
999// only have Lines after that
1000// end with a single close
1001// only have (at most) 1 close
1002//
1003static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001004 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +00001005 SkPath::RawIter iter(path);
1006 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +00001007
1008 bool firstTime = true;
1009 bool foundClose = false;
1010 for (;;) {
1011 switch (iter.next(pts)) {
1012 case SkPath::kMove_Verb:
1013 REPORTER_ASSERT(reporter, firstTime);
1014 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
1015 srcPts++;
1016 firstTime = false;
1017 break;
1018 case SkPath::kLine_Verb:
1019 REPORTER_ASSERT(reporter, !firstTime);
1020 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
1021 srcPts++;
1022 break;
1023 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001024 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +00001025 break;
reed@google.com277c3f82013-05-31 15:17:50 +00001026 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001027 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +00001028 break;
reed@google.com744faba2012-05-29 19:54:52 +00001029 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +00001030 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +00001031 break;
1032 case SkPath::kClose_Verb:
1033 REPORTER_ASSERT(reporter, !firstTime);
1034 REPORTER_ASSERT(reporter, !foundClose);
1035 REPORTER_ASSERT(reporter, expectClose);
1036 foundClose = true;
1037 break;
1038 case SkPath::kDone_Verb:
1039 goto DONE;
1040 }
1041 }
1042DONE:
1043 REPORTER_ASSERT(reporter, foundClose == expectClose);
1044}
1045
1046static void test_addPoly(skiatest::Reporter* reporter) {
1047 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00001048 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001049
reed@google.com744faba2012-05-29 19:54:52 +00001050 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
1051 pts[i].fX = rand.nextSScalar1();
1052 pts[i].fY = rand.nextSScalar1();
1053 }
1054
1055 for (int doClose = 0; doClose <= 1; ++doClose) {
1056 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
1057 SkPath path;
bsalomon98806072014-12-12 15:11:17 -08001058 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00001059 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +00001060 }
1061 }
1062}
1063
reed@google.com8b06f1a2012-05-29 12:03:46 +00001064static void test_strokerec(skiatest::Reporter* reporter) {
1065 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
1066 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001067
reed@google.com8b06f1a2012-05-29 12:03:46 +00001068 rec.setHairlineStyle();
1069 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001070
reed@google.com8b06f1a2012-05-29 12:03:46 +00001071 rec.setStrokeStyle(SK_Scalar1, false);
1072 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001073
reed@google.com8b06f1a2012-05-29 12:03:46 +00001074 rec.setStrokeStyle(SK_Scalar1, true);
1075 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001076
reed@google.com8b06f1a2012-05-29 12:03:46 +00001077 rec.setStrokeStyle(0, false);
1078 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001079
reed@google.com8b06f1a2012-05-29 12:03:46 +00001080 rec.setStrokeStyle(0, true);
1081 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
1082}
1083
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001084// Set this for paths that don't have a consistent direction such as a bowtie.
1085// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -07001086const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001087
1088static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -07001089 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001090 if (expected == kDontCheckDir) {
1091 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001092 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001093 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1094
reed026beb52015-06-10 14:23:15 -07001095 SkPathPriv::FirstDirection dir;
1096 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001097 REPORTER_ASSERT(reporter, dir == expected);
1098 } else {
reed026beb52015-06-10 14:23:15 -07001099 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001100 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001101}
1102
reed@google.com3e71a882012-01-10 18:44:37 +00001103static void test_direction(skiatest::Reporter* reporter) {
1104 size_t i;
1105 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -07001106 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -07001107 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
1108 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
1109 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +00001110
1111 static const char* gDegen[] = {
1112 "M 10 10",
1113 "M 10 10 M 20 20",
1114 "M 10 10 L 20 20",
1115 "M 10 10 L 10 10 L 10 10",
1116 "M 10 10 Q 10 10 10 10",
1117 "M 10 10 C 10 10 10 10 10 10",
1118 };
1119 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
1120 path.reset();
1121 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
1122 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -07001123 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +00001124 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001125
reed@google.com3e71a882012-01-10 18:44:37 +00001126 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001127 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +00001128 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +00001129 "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 +00001130 // rect with top two corners replaced by cubics with identical middle
1131 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001132 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
1133 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001134 };
1135 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
1136 path.reset();
1137 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
1138 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001139 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001140 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001141
reed@google.com3e71a882012-01-10 18:44:37 +00001142 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +00001143 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +00001144 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +00001145 "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 +00001146 // rect with top two corners replaced by cubics with identical middle
1147 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001148 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1149 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001150 };
1151 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1152 path.reset();
1153 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1154 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001155 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001156 }
reed@google.comac8543f2012-01-30 20:51:25 +00001157
1158 // Test two donuts, each wound a different direction. Only the outer contour
1159 // determines the cheap direction
1160 path.reset();
1161 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1162 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001163 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001164
reed@google.comac8543f2012-01-30 20:51:25 +00001165 path.reset();
1166 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1167 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001168 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001169
1170 // triangle with one point really far from the origin.
1171 path.reset();
1172 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001173 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001174 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1175 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001176 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001177
1178 path.reset();
1179 path.conicTo(20, 0, 20, 20, 0.5f);
1180 path.close();
reed026beb52015-06-10 14:23:15 -07001181 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001182
1183 path.reset();
1184 path.lineTo(1, 1e7f);
1185 path.lineTo(1e7f, 2e7f);
1186 path.close();
1187 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001188 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001189}
1190
reed@google.comffdb0182011-11-14 19:29:14 +00001191static void add_rect(SkPath* path, const SkRect& r) {
1192 path->moveTo(r.fLeft, r.fTop);
1193 path->lineTo(r.fRight, r.fTop);
1194 path->lineTo(r.fRight, r.fBottom);
1195 path->lineTo(r.fLeft, r.fBottom);
1196 path->close();
1197}
1198
1199static void test_bounds(skiatest::Reporter* reporter) {
1200 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001201 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1202 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1203 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1204 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001205 };
1206
1207 SkPath path0, path1;
1208 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1209 path0.addRect(rects[i]);
1210 add_rect(&path1, rects[i]);
1211 }
1212
1213 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1214}
1215
reed@google.com55b5f4b2011-09-07 12:23:41 +00001216static void stroke_cubic(const SkPoint pts[4]) {
1217 SkPath path;
1218 path.moveTo(pts[0]);
1219 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001220
reed@google.com55b5f4b2011-09-07 12:23:41 +00001221 SkPaint paint;
1222 paint.setStyle(SkPaint::kStroke_Style);
1223 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001224
reed@google.com55b5f4b2011-09-07 12:23:41 +00001225 SkPath fill;
1226 paint.getFillPath(path, &fill);
1227}
1228
1229// just ensure this can run w/o any SkASSERTS firing in the debug build
1230// we used to assert due to differences in how we determine a degenerate vector
1231// but that was fixed with the introduction of SkPoint::CanNormalize
1232static void stroke_tiny_cubic() {
1233 SkPoint p0[] = {
1234 { 372.0f, 92.0f },
1235 { 372.0f, 92.0f },
1236 { 372.0f, 92.0f },
1237 { 372.0f, 92.0f },
1238 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001239
reed@google.com55b5f4b2011-09-07 12:23:41 +00001240 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001241
reed@google.com55b5f4b2011-09-07 12:23:41 +00001242 SkPoint p1[] = {
1243 { 372.0f, 92.0f },
1244 { 372.0007f, 92.000755f },
1245 { 371.99927f, 92.003922f },
1246 { 371.99826f, 92.003899f },
1247 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001248
reed@google.com55b5f4b2011-09-07 12:23:41 +00001249 stroke_cubic(p1);
1250}
1251
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001252static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1253 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001254 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001255 SkPoint mv;
1256 SkPoint pts[4];
1257 SkPath::Verb v;
1258 int nMT = 0;
1259 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001260 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001261 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1262 switch (v) {
1263 case SkPath::kMove_Verb:
1264 mv = pts[0];
1265 ++nMT;
1266 break;
1267 case SkPath::kClose_Verb:
1268 REPORTER_ASSERT(reporter, mv == pts[0]);
1269 ++nCL;
1270 break;
1271 default:
1272 break;
1273 }
1274 }
1275 // if we force a close on the interator we should have a close
1276 // for every moveTo
1277 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1278 }
1279}
1280
1281static void test_close(skiatest::Reporter* reporter) {
1282 SkPath closePt;
1283 closePt.moveTo(0, 0);
1284 closePt.close();
1285 check_close(reporter, closePt);
1286
1287 SkPath openPt;
1288 openPt.moveTo(0, 0);
1289 check_close(reporter, openPt);
1290
1291 SkPath empty;
1292 check_close(reporter, empty);
1293 empty.close();
1294 check_close(reporter, empty);
1295
1296 SkPath rect;
1297 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1298 check_close(reporter, rect);
1299 rect.close();
1300 check_close(reporter, rect);
1301
1302 SkPath quad;
1303 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1304 check_close(reporter, quad);
1305 quad.close();
1306 check_close(reporter, quad);
1307
1308 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001309 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001310 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1311 check_close(reporter, cubic);
1312 cubic.close();
1313 check_close(reporter, cubic);
1314
1315 SkPath line;
1316 line.moveTo(SK_Scalar1, SK_Scalar1);
1317 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1318 check_close(reporter, line);
1319 line.close();
1320 check_close(reporter, line);
1321
1322 SkPath rect2;
1323 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1324 rect2.close();
1325 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1326 check_close(reporter, rect2);
1327 rect2.close();
1328 check_close(reporter, rect2);
1329
1330 SkPath oval3;
1331 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1332 oval3.close();
1333 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1334 check_close(reporter, oval3);
1335 oval3.close();
1336 check_close(reporter, oval3);
1337
1338 SkPath moves;
1339 moves.moveTo(SK_Scalar1, SK_Scalar1);
1340 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1341 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1342 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1343 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001344
1345 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001346}
1347
reed@google.com7c424812011-05-15 04:38:34 +00001348static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1349 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001350 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1351 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001352 REPORTER_ASSERT(reporter, c == expected);
1353}
1354
caryclarkb4216502015-03-02 13:02:34 -08001355static void test_path_crbug389050(skiatest::Reporter* reporter) {
1356 SkPath tinyConvexPolygon;
1357 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1358 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1359 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1360 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1361 tinyConvexPolygon.close();
1362 tinyConvexPolygon.getConvexity();
1363 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001364 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001365
1366 SkPath platTriangle;
1367 platTriangle.moveTo(0, 0);
1368 platTriangle.lineTo(200, 0);
1369 platTriangle.lineTo(100, 0.04f);
1370 platTriangle.close();
1371 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001372 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001373
1374 platTriangle.reset();
1375 platTriangle.moveTo(0, 0);
1376 platTriangle.lineTo(200, 0);
1377 platTriangle.lineTo(100, 0.03f);
1378 platTriangle.close();
1379 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001380 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001381}
1382
reed@google.com7c424812011-05-15 04:38:34 +00001383static void test_convexity2(skiatest::Reporter* reporter) {
1384 SkPath pt;
1385 pt.moveTo(0, 0);
1386 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001387 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001388 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001389
reed@google.com7c424812011-05-15 04:38:34 +00001390 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001391 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1392 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001393 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001394 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001395 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001396
reed@google.com7c424812011-05-15 04:38:34 +00001397 SkPath triLeft;
1398 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001399 triLeft.lineTo(SK_Scalar1, 0);
1400 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001401 triLeft.close();
1402 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001403 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001404
reed@google.com7c424812011-05-15 04:38:34 +00001405 SkPath triRight;
1406 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001407 triRight.lineTo(-SK_Scalar1, 0);
1408 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001409 triRight.close();
1410 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001411 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001412
reed@google.com7c424812011-05-15 04:38:34 +00001413 SkPath square;
1414 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001415 square.lineTo(SK_Scalar1, 0);
1416 square.lineTo(SK_Scalar1, SK_Scalar1);
1417 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001418 square.close();
1419 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001420 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001421
reed@google.com7c424812011-05-15 04:38:34 +00001422 SkPath redundantSquare;
1423 redundantSquare.moveTo(0, 0);
1424 redundantSquare.lineTo(0, 0);
1425 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001426 redundantSquare.lineTo(SK_Scalar1, 0);
1427 redundantSquare.lineTo(SK_Scalar1, 0);
1428 redundantSquare.lineTo(SK_Scalar1, 0);
1429 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1430 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1431 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1432 redundantSquare.lineTo(0, SK_Scalar1);
1433 redundantSquare.lineTo(0, SK_Scalar1);
1434 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001435 redundantSquare.close();
1436 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001437 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001438
reed@google.com7c424812011-05-15 04:38:34 +00001439 SkPath bowTie;
1440 bowTie.moveTo(0, 0);
1441 bowTie.lineTo(0, 0);
1442 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001443 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1444 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1445 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1446 bowTie.lineTo(SK_Scalar1, 0);
1447 bowTie.lineTo(SK_Scalar1, 0);
1448 bowTie.lineTo(SK_Scalar1, 0);
1449 bowTie.lineTo(0, SK_Scalar1);
1450 bowTie.lineTo(0, SK_Scalar1);
1451 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001452 bowTie.close();
1453 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001454 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001455
reed@google.com7c424812011-05-15 04:38:34 +00001456 SkPath spiral;
1457 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001458 spiral.lineTo(100*SK_Scalar1, 0);
1459 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1460 spiral.lineTo(0, 100*SK_Scalar1);
1461 spiral.lineTo(0, 50*SK_Scalar1);
1462 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1463 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001464 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001465 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001466 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001467
reed@google.com7c424812011-05-15 04:38:34 +00001468 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001469 dent.moveTo(0, 0);
1470 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1471 dent.lineTo(0, 100*SK_Scalar1);
1472 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1473 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001474 dent.close();
1475 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001476 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001477
halcanary6950de62015-11-07 05:29:00 -08001478 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001479 SkPath strokedSin;
1480 for (int i = 0; i < 2000; i++) {
1481 SkScalar x = SkIntToScalar(i) / 2;
1482 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1483 if (0 == i) {
1484 strokedSin.moveTo(x, y);
1485 } else {
1486 strokedSin.lineTo(x, y);
1487 }
1488 }
1489 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1490 stroke.setStrokeStyle(2 * SK_Scalar1);
1491 stroke.applyToPath(&strokedSin, strokedSin);
1492 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1493 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001494
1495 // http://crbug.com/412640
1496 SkPath degenerateConcave;
1497 degenerateConcave.moveTo(148.67912f, 191.875f);
1498 degenerateConcave.lineTo(470.37695f, 7.5f);
1499 degenerateConcave.lineTo(148.67912f, 191.875f);
1500 degenerateConcave.lineTo(41.446522f, 376.25f);
1501 degenerateConcave.lineTo(-55.971577f, 460.0f);
1502 degenerateConcave.lineTo(41.446522f, 376.25f);
1503 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001504 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001505
1506 // http://crbug.com/433683
1507 SkPath badFirstVector;
1508 badFirstVector.moveTo(501.087708f, 319.610352f);
1509 badFirstVector.lineTo(501.087708f, 319.610352f);
1510 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1511 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1512 badFirstVector.lineTo(301.557678f, 98.044601f);
1513 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1514 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1515 badFirstVector.lineTo(504.912292f, 316.389648f);
1516 badFirstVector.lineTo(501.087708f, 319.610352f);
1517 badFirstVector.close();
1518 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001519}
1520
reed@android.com6b82d1a2009-06-03 02:35:01 +00001521static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1522 const SkRect& bounds) {
1523 REPORTER_ASSERT(reporter, p.isConvex());
1524 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001525
reed@android.com6b82d1a2009-06-03 02:35:01 +00001526 SkPath p2(p);
1527 REPORTER_ASSERT(reporter, p2.isConvex());
1528 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1529
1530 SkPath other;
1531 other.swap(p2);
1532 REPORTER_ASSERT(reporter, other.isConvex());
1533 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1534}
1535
reed@google.com04863fa2011-05-15 04:08:24 +00001536static void setFromString(SkPath* path, const char str[]) {
1537 bool first = true;
1538 while (str) {
1539 SkScalar x, y;
1540 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001541 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001542 break;
1543 }
1544 str = SkParse::FindScalar(str, &y);
1545 SkASSERT(str);
1546 if (first) {
1547 path->moveTo(x, y);
1548 first = false;
1549 } else {
1550 path->lineTo(x, y);
1551 }
1552 }
1553}
1554
1555static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001556 SkPath path;
1557
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001558 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001559 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001560 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001561 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001562 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1563
reed@google.com04863fa2011-05-15 04:08:24 +00001564 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001565 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001566 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001567 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001568
reed@google.com04863fa2011-05-15 04:08:24 +00001569 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001570 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001571 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001572 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001573
liyuqianbdabcc42016-11-03 11:40:19 -07001574 path.reset();
1575 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths
1576 check_convexity(reporter, path, SkPath::kConvex_Convexity);
1577
reed@google.com04863fa2011-05-15 04:08:24 +00001578 static const struct {
reed026beb52015-06-10 14:23:15 -07001579 const char* fPathStr;
1580 SkPath::Convexity fExpectedConvexity;
1581 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001582 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001583 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1584 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1585 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1586 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1587 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1588 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001589 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001590 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001591 };
1592
1593 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1594 SkPath path;
1595 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001596 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1597 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001598 // check after setting the initial convex and direction
1599 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1600 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001601 SkPathPriv::FirstDirection dir;
1602 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1603 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001604 ^ foundDir);
1605 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1606 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1607 }
1608 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1609 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001610 }
caryclarkd3d1a982014-12-08 04:57:38 -08001611
1612 static const SkPoint nonFinitePts[] = {
1613 { SK_ScalarInfinity, 0 },
1614 { 0, SK_ScalarInfinity },
1615 { SK_ScalarInfinity, SK_ScalarInfinity },
1616 { SK_ScalarNegativeInfinity, 0},
1617 { 0, SK_ScalarNegativeInfinity },
1618 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1619 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1620 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1621 { SK_ScalarNaN, 0 },
1622 { 0, SK_ScalarNaN },
1623 { SK_ScalarNaN, SK_ScalarNaN },
1624 };
1625
1626 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1627
1628 static const SkPoint finitePts[] = {
1629 { SK_ScalarMax, 0 },
1630 { 0, SK_ScalarMax },
1631 { SK_ScalarMax, SK_ScalarMax },
1632 { SK_ScalarMin, 0 },
1633 { 0, SK_ScalarMin },
1634 { SK_ScalarMin, SK_ScalarMin },
1635 };
1636
1637 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1638
1639 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1640 int i = (int) (index % nonFinitePtsCount);
1641 int f = (int) (index % finitePtsCount);
1642 int g = (int) ((f + 1) % finitePtsCount);
1643 path.reset();
1644 switch (index % 13) {
1645 case 0: path.lineTo(nonFinitePts[i]); break;
1646 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1647 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1648 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1649 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1650 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1651 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1652 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1653 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1654 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1655 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1656 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1657 case 12: path.moveTo(nonFinitePts[i]); break;
1658 }
1659 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1660 }
1661
1662 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1663 int f = (int) (index % finitePtsCount);
1664 int g = (int) ((f + 1) % finitePtsCount);
1665 path.reset();
1666 int curveSelect = index % 11;
1667 switch (curveSelect) {
1668 case 0: path.moveTo(finitePts[f]); break;
1669 case 1: path.lineTo(finitePts[f]); break;
1670 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1671 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1672 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1673 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1674 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1675 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1676 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1677 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1678 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1679 }
1680 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1681 : SkPath::kUnknown_Convexity);
1682 }
1683
reed@google.com04863fa2011-05-15 04:08:24 +00001684}
1685
reed@google.com7e6c4d12012-05-10 14:05:43 +00001686static void test_isLine(skiatest::Reporter* reporter) {
1687 SkPath path;
1688 SkPoint pts[2];
1689 const SkScalar value = SkIntToScalar(5);
1690
halcanary96fcdcc2015-08-27 07:41:13 -07001691 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001692
reed@google.com7e6c4d12012-05-10 14:05:43 +00001693 // set some non-zero values
1694 pts[0].set(value, value);
1695 pts[1].set(value, value);
1696 REPORTER_ASSERT(reporter, !path.isLine(pts));
1697 // check that pts was untouched
1698 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1699 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1700
1701 const SkScalar moveX = SkIntToScalar(1);
1702 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001703 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001704
1705 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001706 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001707 REPORTER_ASSERT(reporter, !path.isLine(pts));
1708 // check that pts was untouched
1709 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1710 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1711
1712 const SkScalar lineX = SkIntToScalar(2);
1713 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001714 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001715
1716 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001717 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001718
1719 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1720 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1721 REPORTER_ASSERT(reporter, path.isLine(pts));
1722 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1723 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1724
1725 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001726 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001727 REPORTER_ASSERT(reporter, !path.isLine(pts));
1728 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1729 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001730
1731 path.reset();
1732 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001733 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001734}
1735
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001736static void test_conservativelyContains(skiatest::Reporter* reporter) {
1737 SkPath path;
1738
1739 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1740 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1741
1742 // A circle that bounds kBaseRect (with a significant amount of slop)
1743 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
Mike Reeddf85c382017-02-14 10:59:19 -05001744 circleR *= 1.75f / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001745 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1746
1747 // round-rect radii
1748 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001749
caryclark@google.com56f233a2012-11-19 13:06:06 +00001750 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001751 SkRect fQueryRect;
1752 bool fInRect;
1753 bool fInCircle;
1754 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001755 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001756 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001757 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001758
1759 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001760 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1761 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1762 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1763 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001764 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001765
1766 // rects with edges off by one from kBaseRect's edges
1767 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1768 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001769 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001770 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1771 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001772 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001773 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1774 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001775 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001776 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1777 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001778 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001779 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1780 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001781 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001782 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1783 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001784 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001785 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1786 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001787 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001788
1789 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001790 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1791 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1792 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1793 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001794
1795 // far away rect
1796 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1797 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001798 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001799
1800 // very large rect containing kBaseRect
1801 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1802 kBaseRect.fTop - 5 * kBaseRect.height(),
1803 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001804 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001805
1806 // skinny rect that spans same y-range as kBaseRect
1807 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1808 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001809 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001810
1811 // short rect that spans same x-range as kBaseRect
1812 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001813 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001814
1815 // skinny rect that spans slightly larger y-range than kBaseRect
1816 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1817 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001818 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001819
1820 // short rect that spans slightly larger x-range than kBaseRect
1821 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1822 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001823 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001824 };
1825
1826 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001827 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001828 SkRect qRect = kQueries[q].fQueryRect;
1829 if (inv & 0x1) {
1830 SkTSwap(qRect.fLeft, qRect.fRight);
1831 }
1832 if (inv & 0x2) {
1833 SkTSwap(qRect.fTop, qRect.fBottom);
1834 }
1835 for (int d = 0; d < 2; ++d) {
1836 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1837 path.reset();
1838 path.addRect(kBaseRect, dir);
1839 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1840 path.conservativelyContainsRect(qRect));
1841
1842 path.reset();
1843 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1844 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1845 path.conservativelyContainsRect(qRect));
1846
1847 path.reset();
1848 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1849 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1850 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001851
1852 path.reset();
1853 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1854 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1855 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1856 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1857 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1858 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1859 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1860 path.close();
1861 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1862 path.conservativelyContainsRect(qRect));
1863
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001864 }
1865 // Slightly non-convex shape, shouldn't contain any rects.
1866 path.reset();
1867 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001868 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001869 path.lineTo(SkIntToScalar(100), 0);
1870 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1871 path.lineTo(0, SkIntToScalar(100));
1872 path.close();
1873 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1874 }
1875 }
1876
1877 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1878 path.reset();
1879 path.moveTo(0, 0);
1880 path.lineTo(SkIntToScalar(100), 0);
1881 path.lineTo(0, SkIntToScalar(100));
1882
1883 // inside, on along top edge
1884 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1885 SkIntToScalar(10),
1886 SkIntToScalar(10))));
1887 // above
1888 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1889 SkRect::MakeXYWH(SkIntToScalar(50),
1890 SkIntToScalar(-10),
1891 SkIntToScalar(10),
1892 SkIntToScalar(10))));
1893 // to the left
1894 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1895 SkIntToScalar(5),
1896 SkIntToScalar(5),
1897 SkIntToScalar(5))));
1898
1899 // outside the diagonal edge
1900 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1901 SkIntToScalar(200),
1902 SkIntToScalar(20),
1903 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001904
bsalomonb17c1292014-08-28 14:04:55 -07001905
1906 // Test that multiple move commands do not cause asserts.
1907
1908 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001909 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001910 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1911#if 0
1912 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1913 SkIntToScalar(10),
1914 SkIntToScalar(10))));
1915#endif
1916
1917 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001918 path.reset();
1919 path.moveTo(100, 100);
1920 path.moveTo(0, 0);
1921 path.lineTo(SkIntToScalar(100), 0);
1922 path.lineTo(0, SkIntToScalar(100));
1923
1924 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1925 SkIntToScalar(10),
1926 SkIntToScalar(10))));
1927
Lee Salzmana19f0242017-01-12 13:06:21 -05001928 // Same as above path and first test but with the extra moveTo making a degenerate sub-path
1929 // following the non-empty sub-path. Verifies that this does not trigger assertions.
1930 path.reset();
1931 path.moveTo(0, 0);
1932 path.lineTo(SkIntToScalar(100), 0);
1933 path.lineTo(0, SkIntToScalar(100));
1934 path.moveTo(100, 100);
1935
1936 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1937 SkIntToScalar(10),
1938 SkIntToScalar(10))));
1939
bsalomonb17c1292014-08-28 14:04:55 -07001940 // Test that multiple move commands do not cause asserts and that the function
1941 // is not confused by the multiple moves.
1942 path.reset();
1943 path.moveTo(0, 0);
1944 path.lineTo(SkIntToScalar(100), 0);
1945 path.lineTo(0, SkIntToScalar(100));
1946 path.moveTo(0, SkIntToScalar(200));
1947 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1948 path.lineTo(0, SkIntToScalar(300));
1949
1950 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1951 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1952 SkIntToScalar(10),
1953 SkIntToScalar(10))));
1954
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001955 path.reset();
1956 path.lineTo(100, 100);
1957 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001958}
1959
reed@google.comf32322b2013-10-16 15:14:04 +00001960static void test_isRect_open_close(skiatest::Reporter* reporter) {
1961 SkPath path;
1962 bool isClosed;
1963
1964 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001965 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001966
halcanary96fcdcc2015-08-27 07:41:13 -07001967 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001968 REPORTER_ASSERT(reporter, isClosed);
1969}
1970
caryclark@google.comf1316942011-07-26 19:54:45 +00001971// Simple isRect test is inline TestPath, below.
1972// test_isRect provides more extensive testing.
1973static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001974 test_isRect_open_close(reporter);
1975
caryclark@google.comf1316942011-07-26 19:54:45 +00001976 // passing tests (all moveTo / lineTo...
1977 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1978 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1979 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1980 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1981 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1982 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1983 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1984 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1985 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001986 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1987 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 +00001988 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1989 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1990 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001991 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001992
caryclark@google.comf1316942011-07-26 19:54:45 +00001993 // failing tests
1994 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1995 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1996 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1997 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1998 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1999 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2000 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2001 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00002002 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
2003 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
2004 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00002005
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002006 // no close, but we should detect them as fillably the same as a rect
2007 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
2008 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
2009 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
2010
2011 // like c2, but we double-back on ourselves
2012 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
2013 // like c2, but we overshoot the start point
2014 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
2015 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00002016
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002017 struct IsRectTest {
2018 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002019 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002020 bool fClose;
2021 bool fIsRect;
2022 } tests[] = {
2023 { r1, SK_ARRAY_COUNT(r1), true, true },
2024 { r2, SK_ARRAY_COUNT(r2), true, true },
2025 { r3, SK_ARRAY_COUNT(r3), true, true },
2026 { r4, SK_ARRAY_COUNT(r4), true, true },
2027 { r5, SK_ARRAY_COUNT(r5), true, true },
2028 { r6, SK_ARRAY_COUNT(r6), true, true },
2029 { r7, SK_ARRAY_COUNT(r7), true, true },
2030 { r8, SK_ARRAY_COUNT(r8), true, true },
2031 { r9, SK_ARRAY_COUNT(r9), true, true },
2032 { ra, SK_ARRAY_COUNT(ra), true, true },
2033 { rb, SK_ARRAY_COUNT(rb), true, true },
2034 { rc, SK_ARRAY_COUNT(rc), true, true },
2035 { rd, SK_ARRAY_COUNT(rd), true, true },
2036 { re, SK_ARRAY_COUNT(re), true, true },
2037 { rf, SK_ARRAY_COUNT(rf), true, true },
2038
2039 { f1, SK_ARRAY_COUNT(f1), true, false },
2040 { f2, SK_ARRAY_COUNT(f2), true, false },
2041 { f3, SK_ARRAY_COUNT(f3), true, false },
2042 { f4, SK_ARRAY_COUNT(f4), true, false },
2043 { f5, SK_ARRAY_COUNT(f5), true, false },
2044 { f6, SK_ARRAY_COUNT(f6), true, false },
2045 { f7, SK_ARRAY_COUNT(f7), true, false },
2046 { f8, SK_ARRAY_COUNT(f8), true, false },
2047 { f9, SK_ARRAY_COUNT(f9), true, false },
2048 { fa, SK_ARRAY_COUNT(fa), true, false },
2049 { fb, SK_ARRAY_COUNT(fb), true, false },
2050
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00002051 { c1, SK_ARRAY_COUNT(c1), false, true },
2052 { c2, SK_ARRAY_COUNT(c2), false, true },
2053 { c3, SK_ARRAY_COUNT(c3), false, true },
2054
2055 { d1, SK_ARRAY_COUNT(d1), false, false },
2056 { d2, SK_ARRAY_COUNT(d2), false, false },
2057 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00002058 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002059
2060 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002061 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00002062 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2063 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002064 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2065 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2066 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00002067 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002068 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002069 path.close();
2070 }
halcanary96fcdcc2015-08-27 07:41:13 -07002071 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002072
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002073 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002074 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00002075 bool isClosed;
reed026beb52015-06-10 14:23:15 -07002076 SkPath::Direction direction;
2077 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08002078 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07002079 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08002080 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
2081 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002082 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07002083 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002084 } else {
2085 SkRect computed;
2086 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08002087 bool isClosed = (bool)-1;
2088 SkPath::Direction direction = (SkPath::Direction) - 1;
2089 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00002090 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
2091 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00002092 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
2093 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002094 }
caryclark@google.comf1316942011-07-26 19:54:45 +00002095 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002096
caryclark@google.comf1316942011-07-26 19:54:45 +00002097 // fail, close then line
2098 SkPath path1;
2099 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002100 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002101 path1.lineTo(r1[index].fX, r1[index].fY);
2102 }
2103 path1.close();
2104 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07002105 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002106
caryclark@google.comf1316942011-07-26 19:54:45 +00002107 // fail, move in the middle
2108 path1.reset();
2109 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002110 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002111 if (index == 2) {
2112 path1.moveTo(1, .5f);
2113 }
2114 path1.lineTo(r1[index].fX, r1[index].fY);
2115 }
2116 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002117 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002118
2119 // fail, move on the edge
2120 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08002121 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002122 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2123 path1.lineTo(r1[index].fX, r1[index].fY);
2124 }
2125 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002126 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002127
caryclark@google.comf1316942011-07-26 19:54:45 +00002128 // fail, quad
2129 path1.reset();
2130 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002131 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002132 if (index == 2) {
2133 path1.quadTo(1, .5f, 1, .5f);
2134 }
2135 path1.lineTo(r1[index].fX, r1[index].fY);
2136 }
2137 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002138 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00002139
caryclark@google.comf1316942011-07-26 19:54:45 +00002140 // fail, cubic
2141 path1.reset();
2142 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002143 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00002144 if (index == 2) {
2145 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2146 }
2147 path1.lineTo(r1[index].fX, r1[index].fY);
2148 }
2149 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07002150 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00002151}
2152
bsalomonedc743a2016-06-01 09:42:31 -07002153static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
2154 const SkRect& rect, SkPath::Direction dir, unsigned start) {
2155 SkRect r = SkRect::MakeEmpty();
2156 SkPath::Direction d = SkPath::kCCW_Direction;
2157 unsigned s = ~0U;
2158
2159 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
2160 REPORTER_ASSERT(reporter, r == rect);
2161 REPORTER_ASSERT(reporter, d == dir);
2162 REPORTER_ASSERT(reporter, s == start);
2163}
2164
2165static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2166 SkRect r = SkRect::MakeEmpty();
2167 SkPath::Direction d = SkPath::kCCW_Direction;
2168 unsigned s = ~0U;
2169
2170 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2171 const SkRect emptyRect = SkRect::MakeEmpty();
2172 SkPath path;
2173 for (int start = 0; start < 4; ++start) {
2174 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2175 SkPath path;
2176 path.addRect(testRect, dir, start);
2177 check_simple_closed_rect(reporter, path, testRect, dir, start);
2178 path.close();
2179 check_simple_closed_rect(reporter, path, testRect, dir, start);
2180 SkPath path2 = path;
2181 path2.lineTo(10, 10);
2182 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2183 path2 = path;
2184 path2.moveTo(10, 10);
2185 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2186 path2 = path;
2187 path2.addRect(testRect, dir, start);
2188 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2189 // Make the path by hand, manually closing it.
2190 path2.reset();
2191 SkPath::RawIter iter(path);
2192 SkPath::Verb v;
2193 SkPoint verbPts[4];
2194 SkPoint firstPt = {0.f, 0.f};
2195 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2196 switch(v) {
2197 case SkPath::kMove_Verb:
2198 firstPt = verbPts[0];
2199 path2.moveTo(verbPts[0]);
2200 break;
2201 case SkPath::kLine_Verb:
2202 path2.lineTo(verbPts[1]);
2203 break;
2204 default:
2205 break;
2206 }
2207 }
2208 // We haven't closed it yet...
2209 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2210 // ... now we do and test again.
2211 path2.lineTo(firstPt);
2212 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2213 // A redundant close shouldn't cause a failure.
2214 path2.close();
2215 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2216 // Degenerate point and line rects are not allowed
2217 path2.reset();
2218 path2.addRect(emptyRect, dir, start);
2219 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2220 SkRect degenRect = testRect;
2221 degenRect.fLeft = degenRect.fRight;
2222 path2.reset();
2223 path2.addRect(degenRect, dir, start);
2224 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2225 degenRect = testRect;
2226 degenRect.fTop = degenRect.fBottom;
2227 path2.reset();
2228 path2.addRect(degenRect, dir, start);
2229 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2230 // An inverted rect makes a rect path, but changes the winding dir and start point.
2231 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2232 ? SkPath::kCCW_Direction
2233 : SkPath::kCW_Direction;
2234 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2235 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2236 SkRect swapRect = testRect;
2237 SkTSwap(swapRect.fLeft, swapRect.fRight);
2238 path2.reset();
2239 path2.addRect(swapRect, dir, start);
2240 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2241 swapRect = testRect;
2242 SkTSwap(swapRect.fTop, swapRect.fBottom);
2243 path2.reset();
2244 path2.addRect(swapRect, dir, start);
2245 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2246 }
2247 }
bsalomon057ae8a2016-07-24 05:37:26 -07002248 // down, up, left, close
2249 path.reset();
2250 path.moveTo(1, 1);
2251 path.lineTo(1, 2);
2252 path.lineTo(1, 1);
2253 path.lineTo(0, 1);
2254 SkRect rect;
2255 SkPath::Direction dir;
2256 unsigned start;
2257 path.close();
2258 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2259 // right, left, up, close
2260 path.reset();
2261 path.moveTo(1, 1);
2262 path.lineTo(2, 1);
2263 path.lineTo(1, 1);
2264 path.lineTo(1, 0);
2265 path.close();
2266 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2267 // parallelogram with horizontal edges
2268 path.reset();
2269 path.moveTo(1, 0);
2270 path.lineTo(3, 0);
2271 path.lineTo(2, 1);
2272 path.lineTo(0, 1);
2273 path.close();
2274 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2275 // parallelogram with vertical edges
2276 path.reset();
2277 path.moveTo(0, 1);
2278 path.lineTo(0, 3);
2279 path.lineTo(1, 2);
2280 path.lineTo(1, 0);
2281 path.close();
2282 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start));
2283
bsalomonedc743a2016-06-01 09:42:31 -07002284}
2285
caryclark95bc5f32015-04-08 08:34:15 -07002286static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002287 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002288 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002289 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2290 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2291 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002292 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002293 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2294 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2295 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2296 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002297 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2298 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 +00002299 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2300 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2301 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002302
2303 // failing tests
2304 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2305 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2306 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2307 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2308 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2309 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2310 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2311 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2312
caryclark95bc5f32015-04-08 08:34:15 -07002313 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002314 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2315 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2316
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002317 struct IsNestedRectTest {
2318 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002319 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002320 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002321 bool fClose;
2322 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2323 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002324 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2325 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2326 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2327 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2328 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2329 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2330 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2331 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2332 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2333 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2334 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2335 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2336 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2337 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002338
reed026beb52015-06-10 14:23:15 -07002339 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2340 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2341 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2342 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2343 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2344 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2345 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2346 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002347
reed026beb52015-06-10 14:23:15 -07002348 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2349 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002350 };
2351
2352 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002353 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002354 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002355 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2356 SkPath path;
2357 if (rectFirst) {
2358 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2359 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002360 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2361 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2362 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002363 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002364 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002365 path.close();
2366 }
2367 if (!rectFirst) {
2368 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2369 }
caryclark95bc5f32015-04-08 08:34:15 -07002370 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002371 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002372 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002373 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002374 SkPathPriv::FirstDirection expectedDirs[2];
2375 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002376 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002377 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002378 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2379 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002380 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002381 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002382 } else {
reed026beb52015-06-10 14:23:15 -07002383 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002384 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002385 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002386 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002387 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2388 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002389 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2390 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002391 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002392 }
2393
2394 // fail, close then line
2395 SkPath path1;
2396 if (rectFirst) {
2397 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2398 }
2399 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002400 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002401 path1.lineTo(r1[index].fX, r1[index].fY);
2402 }
2403 path1.close();
2404 path1.lineTo(1, 0);
2405 if (!rectFirst) {
2406 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2407 }
halcanary96fcdcc2015-08-27 07:41:13 -07002408 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002409
2410 // fail, move in the middle
2411 path1.reset();
2412 if (rectFirst) {
2413 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2414 }
2415 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002416 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002417 if (index == 2) {
2418 path1.moveTo(1, .5f);
2419 }
2420 path1.lineTo(r1[index].fX, r1[index].fY);
2421 }
2422 path1.close();
2423 if (!rectFirst) {
2424 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2425 }
halcanary96fcdcc2015-08-27 07:41:13 -07002426 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002427
2428 // fail, move on the edge
2429 path1.reset();
2430 if (rectFirst) {
2431 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2432 }
bsalomon98806072014-12-12 15:11:17 -08002433 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002434 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2435 path1.lineTo(r1[index].fX, r1[index].fY);
2436 }
2437 path1.close();
2438 if (!rectFirst) {
2439 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2440 }
halcanary96fcdcc2015-08-27 07:41:13 -07002441 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002442
2443 // fail, quad
2444 path1.reset();
2445 if (rectFirst) {
2446 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2447 }
2448 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002449 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002450 if (index == 2) {
2451 path1.quadTo(1, .5f, 1, .5f);
2452 }
2453 path1.lineTo(r1[index].fX, r1[index].fY);
2454 }
2455 path1.close();
2456 if (!rectFirst) {
2457 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2458 }
halcanary96fcdcc2015-08-27 07:41:13 -07002459 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002460
2461 // fail, cubic
2462 path1.reset();
2463 if (rectFirst) {
2464 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2465 }
2466 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002467 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002468 if (index == 2) {
2469 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2470 }
2471 path1.lineTo(r1[index].fX, r1[index].fY);
2472 }
2473 path1.close();
2474 if (!rectFirst) {
2475 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2476 }
halcanary96fcdcc2015-08-27 07:41:13 -07002477 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002478
caryclark@google.com56f233a2012-11-19 13:06:06 +00002479 // fail, not nested
2480 path1.reset();
2481 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2482 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002483 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002484 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002485
caryclark95bc5f32015-04-08 08:34:15 -07002486 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2487 SkPath path;
2488 path.moveTo(0, 0);
2489 path.lineTo(10, 0);
2490 path.lineTo(10, 10);
2491 path.lineTo(0, 10);
2492 path.lineTo(0, 0);
2493 path.moveTo(1, 1);
2494 path.lineTo(9, 1);
2495 path.lineTo(9, 9);
2496 path.lineTo(1, 9);
2497 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002498 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002499
caryclark@google.combfe90372012-11-21 13:56:20 +00002500 // pass, stroke rect
2501 SkPath src, dst;
2502 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2503 SkPaint strokePaint;
2504 strokePaint.setStyle(SkPaint::kStroke_Style);
2505 strokePaint.setStrokeWidth(2);
2506 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002507 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002508}
2509
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002510static void write_and_read_back(skiatest::Reporter* reporter,
2511 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002512 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002513 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002514 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002515 SkAutoMalloc storage(size);
2516 writer.flatten(storage.get());
2517 SkReader32 reader(storage.get(), size);
2518
2519 SkPath readBack;
2520 REPORTER_ASSERT(reporter, readBack != p);
2521 reader.readPath(&readBack);
2522 REPORTER_ASSERT(reporter, readBack == p);
2523
rmistry@google.comd6176b02012-08-23 18:14:13 +00002524 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002525 p.getConvexityOrUnknown());
2526
bsalomon78d58d12016-05-27 09:17:04 -07002527 SkRect oval0, oval1;
2528 SkPath::Direction dir0, dir1;
2529 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002530 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002531 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2532 REPORTER_ASSERT(reporter, oval0 == oval1);
2533 REPORTER_ASSERT(reporter, dir0 == dir1);
2534 REPORTER_ASSERT(reporter, start0 == start1);
2535 }
2536 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2537 SkRRect rrect0, rrect1;
2538 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2539 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2540 REPORTER_ASSERT(reporter, dir0 == dir1);
2541 REPORTER_ASSERT(reporter, start0 == start1);
2542 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002543 const SkRect& origBounds = p.getBounds();
2544 const SkRect& readBackBounds = readBack.getBounds();
2545
2546 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2547}
2548
Mike Reede3374d62017-01-03 13:58:21 -05002549static void test_corrupt_flattening(skiatest::Reporter* reporter) {
2550 SkPath path;
2551 path.moveTo(1, 2);
2552 path.lineTo(1, 2);
2553 path.quadTo(1, 2, 3, 4);
2554 path.conicTo(1, 2, 3, 4, 0.5f);
2555 path.cubicTo(1, 2, 3, 4, 5, 6);
2556 uint8_t buffer[1024];
2557 SkDEBUGCODE(size_t size =) path.writeToMemory(buffer);
2558 SkASSERT(size <= sizeof(buffer));
2559
2560 // find where the counts and verbs are stored : from the impl in SkPathRef.cpp
2561 int32_t* vCount = (int32_t*)&buffer[16];
2562 SkASSERT(*vCount == 5);
2563 int32_t* pCount = (int32_t*)&buffer[20];
2564 SkASSERT(*pCount == 9);
2565 int32_t* cCount = (int32_t*)&buffer[24];
2566 SkASSERT(*cCount == 1);
2567 uint8_t* verbs = &buffer[28];
2568
2569 REPORTER_ASSERT(reporter, path.readFromMemory(buffer, sizeof(buffer)));
2570
2571 // check that we detect under/over-flow of counts
2572
2573 *vCount += 1;
2574 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2575 *vCount -= 1; // restore
2576
2577 *pCount += 1;
2578 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2579 *pCount -= 2;
2580 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2581 *pCount += 1; // restore
2582
2583 *cCount += 1;
2584 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2585 *cCount -= 2;
2586 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2587 *cCount += 1; // restore
2588
2589 // Check that we detect when the verbs indicate more or fewer pts/conics
2590
2591 uint8_t save = verbs[0];
2592 SkASSERT(save == SkPath::kCubic_Verb);
2593 verbs[0] = SkPath::kQuad_Verb;
2594 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2595 verbs[0] = save;
2596
2597 save = verbs[1];
2598 SkASSERT(save == SkPath::kConic_Verb);
2599 verbs[1] = SkPath::kQuad_Verb;
2600 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2601 verbs[1] = SkPath::kCubic_Verb;
2602 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2603 verbs[1] = save;
2604
2605 // Check that we detect invalid verbs
2606 save = verbs[1];
2607 verbs[1] = 17;
2608 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer)));
2609 verbs[1] = save;
2610}
2611
reed@google.com53effc52011-09-21 19:05:12 +00002612static void test_flattening(skiatest::Reporter* reporter) {
2613 SkPath p;
2614
2615 static const SkPoint pts[] = {
2616 { 0, 0 },
2617 { SkIntToScalar(10), SkIntToScalar(10) },
2618 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2619 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2620 };
2621 p.moveTo(pts[0]);
2622 p.lineTo(pts[1]);
2623 p.quadTo(pts[2], pts[3]);
2624 p.cubicTo(pts[4], pts[5], pts[6]);
2625
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002626 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002627
2628 // create a buffer that should be much larger than the path so we don't
2629 // kill our stack if writer goes too far.
2630 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002631 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002632 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002633 REPORTER_ASSERT(reporter, size1 == size2);
2634
2635 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002636 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002637 REPORTER_ASSERT(reporter, size1 == size3);
2638 REPORTER_ASSERT(reporter, p == p2);
2639
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002640 size3 = p2.readFromMemory(buffer, 0);
2641 REPORTER_ASSERT(reporter, !size3);
2642
2643 SkPath tooShort;
2644 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2645 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2646
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002647 char buffer2[1024];
2648 size3 = p2.writeToMemory(buffer2);
2649 REPORTER_ASSERT(reporter, size1 == size3);
2650 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002651
2652 // test persistence of the oval flag & convexity
2653 {
2654 SkPath oval;
2655 SkRect rect = SkRect::MakeWH(10, 10);
2656 oval.addOval(rect);
2657
2658 write_and_read_back(reporter, oval);
2659 }
Mike Reede3374d62017-01-03 13:58:21 -05002660
2661 test_corrupt_flattening(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00002662}
2663
2664static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002665 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002666
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002667#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002668 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002669 { 0, 0 }, // move
2670 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2671 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2672 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2673#if CONIC_PERSPECTIVE_BUG_FIXED
2674 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2675#endif
reed@google.com53effc52011-09-21 19:05:12 +00002676 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002677 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002678
reed@google.com53effc52011-09-21 19:05:12 +00002679 p.moveTo(pts[0]);
2680 p.lineTo(pts[1]);
2681 p.quadTo(pts[2], pts[3]);
2682 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002683#if CONIC_PERSPECTIVE_BUG_FIXED
2684 p.conicTo(pts[4], pts[5], 0.5f);
2685#endif
2686 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002687
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002688 {
2689 SkMatrix matrix;
2690 matrix.reset();
2691 SkPath p1;
2692 p.transform(matrix, &p1);
2693 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002694 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002695
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002696
2697 {
2698 SkMatrix matrix;
2699 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2700
2701 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2702
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002703 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002704 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002705 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002706 REPORTER_ASSERT(reporter, kPtCount == count);
2707 for (int i = 0; i < count; ++i) {
2708 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2709 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2710 }
2711 }
2712
2713 {
2714 SkMatrix matrix;
2715 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002716 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002717
2718 SkPath p1;
2719 p1.moveTo(SkPoint::Make(0, 0));
2720
2721 p.transform(matrix, &p1);
2722 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002723 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002724 SkRect pBounds = p.getBounds();
2725 SkRect p1Bounds = p1.getBounds();
2726 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2727 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2728 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2729 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2730 }
2731
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002732 p.reset();
2733 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002734
2735 {
2736 SkMatrix matrix;
2737 matrix.reset();
2738 SkPath p1;
2739 p1.moveTo(SkPoint::Make(0, 0));
2740
2741 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002742 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002743 }
2744
2745
2746 {
2747 SkMatrix matrix;
2748 matrix.reset();
2749 matrix.setScaleX(-1);
2750 SkPath p1;
2751 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2752
2753 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002754 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002755 }
2756
2757 {
2758 SkMatrix matrix;
2759 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2760 SkPath p1;
2761 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2762
2763 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002764 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002765 }
reed@google.com53effc52011-09-21 19:05:12 +00002766}
2767
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002768static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002769 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002770 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002771
caryclark@google.com56f233a2012-11-19 13:06:06 +00002772 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002773 const char* testPath;
2774 const size_t numResultPts;
2775 const SkRect resultBound;
2776 const SkPath::Verb* resultVerbs;
2777 const size_t numResultVerbs;
2778 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002779
schenney@chromium.org7e963602012-06-13 17:05:43 +00002780 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2781 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2782 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2783 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2784 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2785 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2786 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2787 static const SkPath::Verb resultVerbs8[] = {
2788 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2789 };
2790 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2791 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2792 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2793 static const SkPath::Verb resultVerbs12[] = {
2794 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2795 };
2796 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2797 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2798 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2799 static const SkPath::Verb resultVerbs16[] = {
2800 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2801 };
2802 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002803 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002804 { "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 -07002805 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002806 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2807 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2808 { "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) },
2809 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2810 { "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) },
2811 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2812 { "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) },
2813 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2814 { "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) },
2815 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2816 { "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 +00002817 SK_ARRAY_COUNT(resultVerbs14)
2818 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002819 { "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) },
2820 { "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 +00002821 SK_ARRAY_COUNT(resultVerbs16)
2822 }
2823 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002824
schenney@chromium.org7e963602012-06-13 17:05:43 +00002825 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2826 p.reset();
2827 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2828 REPORTER_ASSERT(reporter, valid);
2829 REPORTER_ASSERT(reporter, !p.isEmpty());
2830 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2831 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2832 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2833 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2834 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2835 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002836 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002837}
2838
2839struct SegmentInfo {
2840 SkPath fPath;
2841 int fPointCount;
2842};
2843
reed@google.com10296cc2011-09-21 12:29:05 +00002844#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2845
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002846static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002847 SkPath p, p2;
2848
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002849 p.moveTo(0, 0);
2850 p.quadTo(100, 100, 200, 200);
2851 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2852 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002853 p2 = p;
2854 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002855 p.cubicTo(100, 100, 200, 200, 300, 300);
2856 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2857 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002858 p2 = p;
2859 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2860
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002861 p.reset();
2862 p.moveTo(0, 0);
2863 p.cubicTo(100, 100, 200, 200, 300, 300);
2864 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002865 p2 = p;
2866 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002867
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002868 REPORTER_ASSERT(reporter, !p.isEmpty());
2869}
2870
2871static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002872 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002873 SkPoint pts[4];
2874
2875 // Test an iterator with no path
2876 SkPath::Iter noPathIter;
2877 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002878
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002879 // Test that setting an empty path works
2880 noPathIter.setPath(p, false);
2881 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002882
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002883 // Test that close path makes no difference for an empty path
2884 noPathIter.setPath(p, true);
2885 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002886
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002887 // Test an iterator with an initial empty path
2888 SkPath::Iter iter(p, false);
2889 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2890
2891 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002892 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002893 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2894
rmistry@google.comd6176b02012-08-23 18:14:13 +00002895
schenney@chromium.org7e963602012-06-13 17:05:43 +00002896 struct iterTestData {
2897 const char* testPath;
2898 const bool forceClose;
2899 const bool consumeDegenerates;
2900 const size_t* numResultPtsPerVerb;
2901 const SkPoint* resultPts;
2902 const SkPath::Verb* resultVerbs;
2903 const size_t numResultVerbs;
2904 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002905
schenney@chromium.org7e963602012-06-13 17:05:43 +00002906 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2907 static const SkPath::Verb resultVerbs2[] = {
2908 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2909 };
2910 static const SkPath::Verb resultVerbs3[] = {
2911 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2912 };
2913 static const SkPath::Verb resultVerbs4[] = {
2914 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2915 };
2916 static const SkPath::Verb resultVerbs5[] = {
2917 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2918 };
2919 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002920 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2921 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2922 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2923 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002924 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002925 static const SkPoint resultPts2[] = {
2926 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2927 };
2928 static const SkPoint resultPts3[] = {
2929 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2930 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2931 };
2932 static const SkPoint resultPts4[] = {
2933 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2934 };
2935 static const SkPoint resultPts5[] = {
2936 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2937 };
2938 static const struct iterTestData gIterTests[] = {
2939 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002940 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2941 { "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 +00002942 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2943 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2944 { "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) },
2945 { "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 +00002946 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2947 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2948 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2949 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2950 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2951 { "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 +00002952 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002953
schenney@chromium.org7e963602012-06-13 17:05:43 +00002954 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2955 p.reset();
2956 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2957 REPORTER_ASSERT(reporter, valid);
2958 iter.setPath(p, gIterTests[i].forceClose);
2959 int j = 0, l = 0;
2960 do {
2961 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2962 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2963 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2964 }
2965 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2966 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2967 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002968
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002969 p.reset();
2970 iter.setPath(p, false);
2971 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2972 p.lineTo(1, 1);
2973 p.close();
2974 iter.setPath(p, false);
2975 REPORTER_ASSERT(reporter, iter.isClosedContour());
2976 p.reset();
2977 iter.setPath(p, true);
2978 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2979 p.lineTo(1, 1);
2980 iter.setPath(p, true);
2981 REPORTER_ASSERT(reporter, iter.isClosedContour());
2982 p.moveTo(0, 0);
2983 p.lineTo(2, 2);
2984 iter.setPath(p, false);
2985 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2986
2987 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2988 // check to see if the result is correct.
2989 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2990 p.reset();
2991 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2992 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2993 iter.setPath(p, true);
2994 iter.next(pts, false);
2995 iter.next(pts, false);
2996 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2997 }
2998
2999 p.reset();
3000 p.quadTo(0, 0, 0, 0);
3001 iter.setPath(p, false);
3002 iter.next(pts, false);
3003 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
3004 iter.setPath(p, false);
3005 iter.next(pts, false);
3006 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3007
3008 p.reset();
3009 p.conicTo(0, 0, 0, 0, 0.5f);
3010 iter.setPath(p, false);
3011 iter.next(pts, false);
3012 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
3013 iter.setPath(p, false);
3014 iter.next(pts, false);
3015 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3016
3017 p.reset();
3018 p.cubicTo(0, 0, 0, 0, 0, 0);
3019 iter.setPath(p, false);
3020 iter.next(pts, false);
3021 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3022 iter.setPath(p, false);
3023 iter.next(pts, false);
3024 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3025
3026 p.moveTo(1, 1); // add a trailing moveto
3027 iter.setPath(p, false);
3028 iter.next(pts, false);
3029 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
3030 iter.setPath(p, false);
3031 iter.next(pts, false);
3032 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
3033
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003034 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08003035
3036 // Test out mixed degenerate and non-degenerate geometry with Conics
3037 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
3038 SkRect r = SkRect::MakeWH(100, 100);
3039 SkRRect rr;
3040 rr.setRectRadii(r, radii);
3041 p.reset();
3042 p.addRRect(rr);
3043 iter.setPath(p, false);
3044 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
3045 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3046 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
3047 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
3048 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003049}
3050
3051static void test_raw_iter(skiatest::Reporter* reporter) {
3052 SkPath p;
3053 SkPoint pts[4];
3054
3055 // Test an iterator with no path
3056 SkPath::RawIter noPathIter;
3057 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
3058 // Test that setting an empty path works
3059 noPathIter.setPath(p);
3060 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00003061
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003062 // Test an iterator with an initial empty path
3063 SkPath::RawIter iter(p);
3064 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3065
3066 // Test that a move-only path returns the move.
3067 p.moveTo(SK_Scalar1, 0);
3068 iter.setPath(p);
3069 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3070 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3071 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3072 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3073
3074 // No matter how many moves we add, we should get them all back
3075 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3076 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3077 iter.setPath(p);
3078 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3079 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3080 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3081 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3082 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3083 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3084 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3085 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3086 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3087 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3088
3089 // Initial close is never ever stored
3090 p.reset();
3091 p.close();
3092 iter.setPath(p);
3093 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3094
3095 // Move/close sequences
3096 p.reset();
3097 p.close(); // Not stored, no purpose
3098 p.moveTo(SK_Scalar1, 0);
3099 p.close();
3100 p.close(); // Not stored, no purpose
3101 p.moveTo(SK_Scalar1*2, SK_Scalar1);
3102 p.close();
3103 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
3104 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
3105 p.close();
3106 iter.setPath(p);
3107 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3108 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
3109 REPORTER_ASSERT(reporter, pts[0].fY == 0);
3110 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003111 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3112 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
3113 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
3114 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003115 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3116 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
3117 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
3118 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
3119 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
3120 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
3121 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003122 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
3123
3124 // Generate random paths and verify
3125 SkPoint randomPts[25];
3126 for (int i = 0; i < 5; ++i) {
3127 for (int j = 0; j < 5; ++j) {
3128 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
3129 }
3130 }
3131
3132 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00003133 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003134 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00003135 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003136 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00003137
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003138 for (int i = 0; i < 500; ++i) {
3139 p.reset();
3140 bool lastWasClose = true;
3141 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00003142 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003143 int numPoints = 0;
3144 int numVerbs = (rand.nextU() >> 16) % 10;
3145 int numIterVerbs = 0;
3146 for (int j = 0; j < numVerbs; ++j) {
3147 do {
3148 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
3149 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003150 switch (nextVerb) {
3151 case SkPath::kMove_Verb:
3152 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3153 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00003154 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003155 numPoints += 1;
3156 lastWasClose = false;
3157 haveMoveTo = true;
3158 break;
3159 case SkPath::kLine_Verb:
3160 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003161 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003162 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3163 haveMoveTo = true;
3164 }
3165 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3166 p.lineTo(expectedPts[numPoints]);
3167 numPoints += 1;
3168 lastWasClose = false;
3169 break;
3170 case SkPath::kQuad_Verb:
3171 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003172 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003173 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3174 haveMoveTo = true;
3175 }
3176 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3177 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3178 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
3179 numPoints += 2;
3180 lastWasClose = false;
3181 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003182 case SkPath::kConic_Verb:
3183 if (!haveMoveTo) {
3184 expectedPts[numPoints++] = lastMoveToPt;
3185 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3186 haveMoveTo = true;
3187 }
3188 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3189 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3190 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3191 rand.nextUScalar1() * 4);
3192 numPoints += 2;
3193 lastWasClose = false;
3194 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003195 case SkPath::kCubic_Verb:
3196 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00003197 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003198 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
3199 haveMoveTo = true;
3200 }
3201 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
3202 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
3203 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
3204 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
3205 expectedPts[numPoints + 2]);
3206 numPoints += 3;
3207 lastWasClose = false;
3208 break;
3209 case SkPath::kClose_Verb:
3210 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00003211 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003212 lastWasClose = true;
3213 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003214 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003215 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003216 }
3217 expectedVerbs[numIterVerbs++] = nextVerb;
3218 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00003219
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003220 iter.setPath(p);
3221 numVerbs = numIterVerbs;
3222 numIterVerbs = 0;
3223 int numIterPts = 0;
3224 SkPoint lastMoveTo;
3225 SkPoint lastPt;
3226 lastMoveTo.set(0, 0);
3227 lastPt.set(0, 0);
3228 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
3229 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
3230 numIterVerbs++;
3231 switch (nextVerb) {
3232 case SkPath::kMove_Verb:
3233 REPORTER_ASSERT(reporter, numIterPts < numPoints);
3234 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
3235 lastPt = lastMoveTo = pts[0];
3236 numIterPts += 1;
3237 break;
3238 case SkPath::kLine_Verb:
3239 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
3240 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3241 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3242 lastPt = pts[1];
3243 numIterPts += 1;
3244 break;
3245 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00003246 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003247 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
3248 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3249 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3250 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3251 lastPt = pts[2];
3252 numIterPts += 2;
3253 break;
3254 case SkPath::kCubic_Verb:
3255 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
3256 REPORTER_ASSERT(reporter, pts[0] == lastPt);
3257 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
3258 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
3259 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
3260 lastPt = pts[3];
3261 numIterPts += 3;
3262 break;
3263 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003264 lastPt = lastMoveTo;
3265 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003266 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003267 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003268 }
3269 }
3270 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3271 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3272 }
3273}
3274
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003275static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003276 const SkPath& path,
3277 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003278 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003279 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003280 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003281 SkPath::Direction isOvalDir;
3282 unsigned isOvalStart;
3283 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003284 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003285 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3286 SkPath tmpPath;
3287 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3288 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003289 }
bsalomon78d58d12016-05-27 09:17:04 -07003290 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003291}
3292
3293static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003294 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003295 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003296 SkPath tmp;
3297
3298 SkMatrix m;
3299 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3300 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003301 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003302 if (SkPathPriv::kCCW_FirstDirection == dir) {
3303 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003304 } else {
reed026beb52015-06-10 14:23:15 -07003305 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3306 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003307 }
3308 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003309}
3310
3311static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003312 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003313 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003314 SkPath tmp;
3315
3316 // translate at small offset
3317 SkMatrix m;
3318 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3319 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003320 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003321
3322 tmp.reset();
3323 m.reset();
3324
3325 // translate at a relatively big offset
3326 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3327 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003328 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003329}
3330
3331static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003332 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003333 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003334 for (int angle = 0; angle < 360; ++angle) {
3335 SkPath tmp;
3336 SkMatrix m;
3337 m.setRotate(SkIntToScalar(angle));
3338 path.transform(m, &tmp);
3339
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003340 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003341 // degrees is not an oval anymore, this can be improved. we made this
3342 // for the simplicity of our implementation.
3343 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003344 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003345 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003346 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003347 }
3348 }
3349}
3350
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003351static void test_circle_mirror_x(skiatest::Reporter* reporter,
3352 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003353 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003354 SkPath tmp;
3355 SkMatrix m;
3356 m.reset();
3357 m.setScaleX(-SK_Scalar1);
3358 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003359 if (SkPathPriv::kCW_FirstDirection == dir) {
3360 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003361 } else {
reed026beb52015-06-10 14:23:15 -07003362 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3363 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003364 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003365 check_for_circle(reporter, tmp, true, dir);
3366}
3367
3368static void test_circle_mirror_y(skiatest::Reporter* reporter,
3369 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003370 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003371 SkPath tmp;
3372 SkMatrix m;
3373 m.reset();
3374 m.setScaleY(-SK_Scalar1);
3375 path.transform(m, &tmp);
3376
reed026beb52015-06-10 14:23:15 -07003377 if (SkPathPriv::kCW_FirstDirection == dir) {
3378 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003379 } else {
reed026beb52015-06-10 14:23:15 -07003380 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3381 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003382 }
3383
3384 check_for_circle(reporter, tmp, true, dir);
3385}
3386
3387static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3388 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003389 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003390 SkPath tmp;
3391 SkMatrix m;
3392 m.reset();
3393 m.setScaleX(-SK_Scalar1);
3394 m.setScaleY(-SK_Scalar1);
3395 path.transform(m, &tmp);
3396
3397 check_for_circle(reporter, tmp, true, dir);
3398}
3399
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003400static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003401 SkPath::Direction inDir) {
3402 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003403 SkPath path;
3404
3405 // circle at origin
reed026beb52015-06-10 14:23:15 -07003406 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3407
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003408 check_for_circle(reporter, path, true, dir);
3409 test_circle_rotate(reporter, path, dir);
3410 test_circle_translate(reporter, path, dir);
3411 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003412 test_circle_mirror_x(reporter, path, dir);
3413 test_circle_mirror_y(reporter, path, dir);
3414 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003415
3416 // circle at an offset at (10, 10)
3417 path.reset();
3418 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003419 SkIntToScalar(20), inDir);
3420
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003421 check_for_circle(reporter, path, true, dir);
3422 test_circle_rotate(reporter, path, dir);
3423 test_circle_translate(reporter, path, dir);
3424 test_circle_skew(reporter, path, dir);
3425 test_circle_mirror_x(reporter, path, dir);
3426 test_circle_mirror_y(reporter, path, dir);
3427 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003428
3429 // Try different starting points for the contour.
3430 for (unsigned start = 0; start < 4; ++start) {
3431 path.reset();
3432 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3433 test_circle_rotate(reporter, path, dir);
3434 test_circle_translate(reporter, path, dir);
3435 test_circle_skew(reporter, path, dir);
3436 test_circle_mirror_x(reporter, path, dir);
3437 test_circle_mirror_y(reporter, path, dir);
3438 test_circle_mirror_xy(reporter, path, dir);
3439 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003440}
3441
3442static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3443 SkPath path;
3444 SkPath circle;
3445 SkPath rect;
3446 SkPath empty;
3447
reed026beb52015-06-10 14:23:15 -07003448 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3449 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003450
3451 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003452 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3453 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3454
3455 SkMatrix translate;
3456 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3457
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003458 // Although all the path concatenation related operations leave
3459 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003460
3461 // empty + circle (translate)
3462 path = empty;
3463 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003464 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003465
3466 // circle + empty (translate)
3467 path = circle;
3468 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003469
reed026beb52015-06-10 14:23:15 -07003470 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003471
3472 // test reverseAddPath
3473 path = circle;
3474 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003475 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003476}
3477
3478static void test_circle(skiatest::Reporter* reporter) {
3479 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3480 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3481
3482 // multiple addCircle()
3483 SkPath path;
3484 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3485 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003486 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003487
3488 // some extra lineTo() would make isOval() fail
3489 path.reset();
3490 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3491 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003492 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003493
3494 // not back to the original point
3495 path.reset();
3496 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3497 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003498 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003499
3500 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003501
3502 // test negative radius
3503 path.reset();
3504 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3505 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003506}
3507
3508static void test_oval(skiatest::Reporter* reporter) {
3509 SkRect rect;
3510 SkMatrix m;
3511 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003512 unsigned start = 0;
3513 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003514
3515 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3516 path.addOval(rect);
3517
bsalomon78d58d12016-05-27 09:17:04 -07003518 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003519 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003520
3521 m.setRotate(SkIntToScalar(90));
3522 SkPath tmp;
3523 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003524 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3525 // is unchanged.
3526 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3527 REPORTER_ASSERT(reporter, 2 == start);
3528 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003529
3530 m.reset();
3531 m.setRotate(SkIntToScalar(30));
3532 tmp.reset();
3533 path.transform(m, &tmp);
3534 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003535 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003536
3537 // since empty path being transformed.
3538 path.reset();
3539 tmp.reset();
3540 m.reset();
3541 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003542 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003543
3544 // empty path is not an oval
3545 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003546 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003547
3548 // only has moveTo()s
3549 tmp.reset();
3550 tmp.moveTo(0, 0);
3551 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003552 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003553
3554 // mimic WebKit's calling convention,
3555 // call moveTo() first and then call addOval()
3556 path.reset();
3557 path.moveTo(0, 0);
3558 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003559 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003560
3561 // copy path
3562 path.reset();
3563 tmp.reset();
3564 tmp.addOval(rect);
3565 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003566 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3567 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3568 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003569}
3570
bungeman@google.coma5809a32013-06-21 15:13:34 +00003571static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3572 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003573
reed@android.com3abec1d2009-03-02 05:36:20 +00003574 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003575 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003576 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003577 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003578 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003579 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3580 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003581 REPORTER_ASSERT(reporter, p == empty);
3582 REPORTER_ASSERT(reporter, !(p != empty));
3583}
3584
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003585static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3586 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003587 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003588 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003589 path->setConvexity(SkPath::kUnknown_Convexity);
3590 REPORTER_ASSERT(reporter, path->isConvex());
3591 path->reset();
3592}
3593
caryclarkd3d1a982014-12-08 04:57:38 -08003594static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3595 SkPath::Direction dir) {
3596 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003597 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003598 path->setConvexity(SkPath::kUnknown_Convexity);
3599 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3600 path->reset();
3601}
3602
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003603static void test_rrect(skiatest::Reporter* reporter) {
3604 SkPath p;
3605 SkRRect rr;
3606 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3607 SkRect r = {10, 20, 30, 40};
3608 rr.setRectRadii(r, radii);
3609 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003610 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003611 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003612 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003613 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003614 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003615 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003616 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003617 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003618 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003619 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003620 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3621 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3622 SkVector save = radii[i];
3623 radii[i].set(0, 0);
3624 rr.setRectRadii(r, radii);
3625 p.addRRect(rr);
3626 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3627 radii[i] = save;
3628 }
3629 p.addRoundRect(r, 0, 0);
3630 SkRect returnedRect;
3631 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3632 REPORTER_ASSERT(reporter, returnedRect == r);
3633 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3634 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3635 rr.setRectRadii(r, zeroRadii);
3636 p.addRRect(rr);
3637 bool closed;
3638 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003639 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003640 REPORTER_ASSERT(reporter, closed);
3641 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3642 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3643 p.addRRect(rr, SkPath::kCW_Direction);
3644 p.addRRect(rr, SkPath::kCW_Direction);
3645 REPORTER_ASSERT(reporter, !p.isConvex());
3646 p.reset();
3647 p.addRRect(rr, SkPath::kCCW_Direction);
3648 p.addRRect(rr, SkPath::kCCW_Direction);
3649 REPORTER_ASSERT(reporter, !p.isConvex());
3650 p.reset();
3651 SkRect emptyR = {10, 20, 10, 30};
3652 rr.setRectRadii(emptyR, radii);
3653 p.addRRect(rr);
3654 REPORTER_ASSERT(reporter, p.isEmpty());
3655 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3656 rr.setRectRadii(largeR, radii);
3657 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003658 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003659
3660 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003661 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3662 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003663 REPORTER_ASSERT(reporter, rr.isEmpty());
3664
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003665 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3666 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3667 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003668}
3669
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003670static void test_arc(skiatest::Reporter* reporter) {
3671 SkPath p;
3672 SkRect emptyOval = {10, 20, 30, 20};
3673 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3674 p.addArc(emptyOval, 1, 2);
3675 REPORTER_ASSERT(reporter, p.isEmpty());
3676 p.reset();
3677 SkRect oval = {10, 20, 30, 40};
3678 p.addArc(oval, 1, 0);
3679 REPORTER_ASSERT(reporter, p.isEmpty());
3680 p.reset();
3681 SkPath cwOval;
3682 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003683 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003684 REPORTER_ASSERT(reporter, p == cwOval);
3685 p.reset();
3686 SkPath ccwOval;
3687 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003688 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003689 REPORTER_ASSERT(reporter, p == ccwOval);
3690 p.reset();
3691 p.addArc(oval, 1, 180);
3692 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003693 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003694 p.setConvexity(SkPath::kUnknown_Convexity);
3695 REPORTER_ASSERT(reporter, p.isConvex());
3696}
3697
bsalomon1978ce22016-05-31 10:42:16 -07003698static inline SkScalar oval_start_index_to_angle(unsigned start) {
3699 switch (start) {
3700 case 0:
3701 return 270.f;
3702 case 1:
3703 return 0.f;
3704 case 2:
3705 return 90.f;
3706 case 3:
3707 return 180.f;
3708 default:
3709 return -1.f;
3710 }
3711}
3712
3713static inline SkScalar canonical_start_angle(float angle) {
3714 while (angle < 0.f) {
3715 angle += 360.f;
3716 }
3717 while (angle >= 360.f) {
3718 angle -= 360.f;
3719 }
3720 return angle;
3721}
3722
3723static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3724 const SkPath& path) {
3725 SkRect r = SkRect::MakeEmpty();
3726 SkPath::Direction d = SkPath::kCCW_Direction;
3727 unsigned s = ~0U;
3728 bool isOval = path.isOval(&r, &d, &s);
3729 REPORTER_ASSERT(reporter, isOval);
3730 SkPath recreatedPath;
3731 recreatedPath.addOval(r, d, s);
3732 REPORTER_ASSERT(reporter, path == recreatedPath);
3733 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3734 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3735}
3736
3737static void test_arc_ovals(skiatest::Reporter* reporter) {
3738 SkRect oval = SkRect::MakeWH(10, 20);
3739 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3740 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3741 SkPath path;
3742 path.addArc(oval, start, sweep);
3743 // SkPath's interfaces for inserting and extracting ovals only allow contours
3744 // to start at multiples of 90 degrees.
3745 if (std::fmod(start, 90.f) == 0) {
3746 check_oval_arc(reporter, start, sweep, path);
3747 } else {
3748 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3749 }
3750 }
3751 // Test start angles that are nearly at valid oval start angles.
3752 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3753 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3754 SkPath path;
3755 path.addArc(oval, start + delta, sweep);
3756 check_oval_arc(reporter, start, sweep, path);
3757 }
3758 }
3759 }
3760}
3761
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003762static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3763 SkScalar x0, SkScalar y0) {
3764 SkPoint pts[4];
3765 SkPath::Verb v = iter->next(pts);
3766 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3767 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3768 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3769}
3770
3771static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3772 SkScalar x1, SkScalar y1) {
3773 SkPoint pts[4];
3774 SkPath::Verb v = iter->next(pts);
3775 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3776 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3777 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3778}
3779
3780static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3781 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3782 SkPoint pts[4];
3783 SkPath::Verb v = iter->next(pts);
3784 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3785 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3786 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3787 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3788 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3789}
3790
3791static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3792 SkPoint pts[4];
3793 SkPath::Verb v = iter->next(pts);
3794 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3795}
3796
3797static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3798 check_done(reporter, p, iter);
3799 p->reset();
3800}
3801
3802static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3803 SkScalar x0, SkScalar y0) {
3804 SkPath::RawIter iter(*p);
3805 check_move(reporter, &iter, x0, y0);
3806 check_done_and_reset(reporter, p, &iter);
3807}
3808
3809static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3810 SkScalar x1, SkScalar y1) {
3811 SkPath::RawIter iter(*p);
3812 check_move(reporter, &iter, 0, 0);
3813 check_line(reporter, &iter, x1, y1);
3814 check_done_and_reset(reporter, p, &iter);
3815}
3816
3817static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3818 SkScalar x1, SkScalar y1) {
3819 SkPath::RawIter iter(*p);
3820 check_move(reporter, &iter, 0, 0);
3821 check_line(reporter, &iter, x1, y1);
3822 check_done(reporter, p, &iter);
3823}
3824
3825static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3826 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3827 SkPath::RawIter iter(*p);
3828 check_move(reporter, &iter, 0, 0);
3829 check_line(reporter, &iter, x1, y1);
3830 check_line(reporter, &iter, x2, y2);
3831 check_done_and_reset(reporter, p, &iter);
3832}
3833
3834static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3835 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3836 SkPath::RawIter iter(*p);
3837 check_move(reporter, &iter, 0, 0);
3838 check_quad(reporter, &iter, x1, y1, x2, y2);
3839 check_done_and_reset(reporter, p, &iter);
3840}
3841
reedd5d27d92015-02-09 13:54:43 -08003842static bool nearly_equal(const SkRect& a, const SkRect& b) {
3843 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3844 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3845 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3846 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3847}
3848
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003849static void test_arcTo(skiatest::Reporter* reporter) {
3850 SkPath p;
3851 p.arcTo(0, 0, 1, 2, 1);
3852 check_path_is_line_and_reset(reporter, &p, 0, 0);
3853 p.arcTo(1, 2, 1, 2, 1);
3854 check_path_is_line_and_reset(reporter, &p, 1, 2);
3855 p.arcTo(1, 2, 3, 4, 0);
3856 check_path_is_line_and_reset(reporter, &p, 1, 2);
3857 p.arcTo(1, 2, 0, 0, 1);
3858 check_path_is_line_and_reset(reporter, &p, 1, 2);
3859 p.arcTo(1, 0, 1, 1, 1);
3860 SkPoint pt;
3861 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3862 p.reset();
3863 p.arcTo(1, 0, 1, -1, 1);
3864 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3865 p.reset();
3866 SkRect oval = {1, 2, 3, 4};
3867 p.arcTo(oval, 0, 0, true);
3868 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3869 p.arcTo(oval, 0, 0, false);
3870 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3871 p.arcTo(oval, 360, 0, true);
3872 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3873 p.arcTo(oval, 360, 0, false);
3874 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003875
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003876 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003877 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003878 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003879 sweep += delta;
3880 delta /= 2;
3881 }
3882 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003883 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003884 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003885 sweep -= delta;
3886 delta /= 2;
3887 }
3888 SkRect noOvalWidth = {1, 2, 0, 3};
3889 p.reset();
3890 p.arcTo(noOvalWidth, 0, 360, false);
3891 REPORTER_ASSERT(reporter, p.isEmpty());
3892
3893 SkRect noOvalHeight = {1, 2, 3, 1};
3894 p.reset();
3895 p.arcTo(noOvalHeight, 0, 360, false);
3896 REPORTER_ASSERT(reporter, p.isEmpty());
3897}
3898
3899static void test_addPath(skiatest::Reporter* reporter) {
3900 SkPath p, q;
3901 p.lineTo(1, 2);
3902 q.moveTo(4, 4);
3903 q.lineTo(7, 8);
3904 q.conicTo(8, 7, 6, 5, 0.5f);
3905 q.quadTo(6, 7, 8, 6);
3906 q.cubicTo(5, 6, 7, 8, 7, 5);
3907 q.close();
3908 p.addPath(q, -4, -4);
3909 SkRect expected = {0, 0, 4, 4};
3910 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3911 p.reset();
3912 p.reverseAddPath(q);
3913 SkRect reverseExpected = {4, 4, 8, 8};
3914 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3915}
3916
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003917static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3918 SkPath p, q;
3919 if (explicitMoveTo) {
3920 p.moveTo(1, 1);
3921 }
3922 p.lineTo(1, 2);
3923 if (explicitMoveTo) {
3924 q.moveTo(2, 1);
3925 }
3926 q.lineTo(2, 2);
3927 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3928 uint8_t verbs[4];
3929 int verbcount = p.getVerbs(verbs, 4);
3930 REPORTER_ASSERT(reporter, verbcount == 4);
3931 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3932 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3933 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3934 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3935}
3936
3937static void test_extendClosedPath(skiatest::Reporter* reporter) {
3938 SkPath p, q;
3939 p.moveTo(1, 1);
3940 p.lineTo(1, 2);
3941 p.lineTo(2, 2);
3942 p.close();
3943 q.moveTo(2, 1);
3944 q.lineTo(2, 3);
3945 p.addPath(q, SkPath::kExtend_AddPathMode);
3946 uint8_t verbs[7];
3947 int verbcount = p.getVerbs(verbs, 7);
3948 REPORTER_ASSERT(reporter, verbcount == 7);
3949 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3950 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3951 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3952 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3953 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3954 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3955 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3956
3957 SkPoint pt;
3958 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3959 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3960 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3961}
3962
3963static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3964 SkPath p, q, r;
3965 // case 1: dst is empty
3966 p.moveTo(2, 1);
3967 p.lineTo(2, 3);
3968 q.addPath(p, mode);
3969 REPORTER_ASSERT(reporter, q == p);
3970 // case 2: src is empty
3971 p.addPath(r, mode);
3972 REPORTER_ASSERT(reporter, q == p);
3973 // case 3: src and dst are empty
3974 q.reset();
3975 q.addPath(r, mode);
3976 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003977}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003978
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003979static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3980 SkPath p;
3981 p.conicTo(1, 2, 3, 4, -1);
3982 check_path_is_line_and_reset(reporter, &p, 3, 4);
3983 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3984 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3985 p.conicTo(1, 2, 3, 4, 1);
3986 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3987}
3988
3989static void test_get_point(skiatest::Reporter* reporter) {
3990 SkPath p;
3991 SkPoint pt = p.getPoint(0);
3992 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003993 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003994 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3995 p.setLastPt(10, 10);
3996 pt = p.getPoint(0);
3997 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003998 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003999 p.rMoveTo(10, 10);
4000 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
4001}
4002
4003static void test_contains(skiatest::Reporter* reporter) {
4004 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08004005 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
4006 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
4007 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
4008 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
4009 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
4010 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 -08004011 // this may return true or false, depending on the platform's numerics, but it should not crash
4012 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08004013
4014 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004015 p.setFillType(SkPath::kInverseWinding_FillType);
4016 REPORTER_ASSERT(reporter, p.contains(0, 0));
4017 p.setFillType(SkPath::kWinding_FillType);
4018 REPORTER_ASSERT(reporter, !p.contains(0, 0));
4019 p.moveTo(4, 4);
4020 p.lineTo(6, 8);
4021 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08004022 // test on edge
4023 REPORTER_ASSERT(reporter, p.contains(6, 4));
4024 REPORTER_ASSERT(reporter, p.contains(5, 6));
4025 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004026 // test quick reject
4027 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4028 REPORTER_ASSERT(reporter, !p.contains(0, 4));
4029 REPORTER_ASSERT(reporter, !p.contains(4, 10));
4030 REPORTER_ASSERT(reporter, !p.contains(10, 4));
4031 // test various crossings in x
4032 REPORTER_ASSERT(reporter, !p.contains(5, 7));
4033 REPORTER_ASSERT(reporter, p.contains(6, 7));
4034 REPORTER_ASSERT(reporter, !p.contains(7, 7));
4035 p.reset();
4036 p.moveTo(4, 4);
4037 p.lineTo(8, 6);
4038 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08004039 // test on edge
4040 REPORTER_ASSERT(reporter, p.contains(4, 6));
4041 REPORTER_ASSERT(reporter, p.contains(6, 5));
4042 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004043 // test various crossings in y
4044 REPORTER_ASSERT(reporter, !p.contains(7, 5));
4045 REPORTER_ASSERT(reporter, p.contains(7, 6));
4046 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08004047 p.reset();
4048 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08004049 p.lineTo(8, 4);
4050 p.lineTo(8, 8);
4051 p.lineTo(4, 8);
4052 // test on vertices
4053 REPORTER_ASSERT(reporter, p.contains(4, 4));
4054 REPORTER_ASSERT(reporter, p.contains(8, 4));
4055 REPORTER_ASSERT(reporter, p.contains(8, 8));
4056 REPORTER_ASSERT(reporter, p.contains(4, 8));
4057 p.reset();
4058 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08004059 p.lineTo(6, 8);
4060 p.lineTo(2, 8);
4061 // test on edge
4062 REPORTER_ASSERT(reporter, p.contains(5, 6));
4063 REPORTER_ASSERT(reporter, p.contains(4, 8));
4064 REPORTER_ASSERT(reporter, p.contains(3, 6));
4065 p.reset();
4066 p.moveTo(4, 4);
4067 p.lineTo(0, 6);
4068 p.lineTo(4, 8);
4069 // test on edge
4070 REPORTER_ASSERT(reporter, p.contains(2, 5));
4071 REPORTER_ASSERT(reporter, p.contains(2, 7));
4072 REPORTER_ASSERT(reporter, p.contains(4, 6));
4073 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
4074 p.reset();
4075 p.moveTo(4, 0);
4076 p.lineTo(6, 4);
4077 p.lineTo(2, 4);
4078 p.moveTo(4, 0);
4079 p.lineTo(0, 8);
4080 p.lineTo(8, 8);
4081 REPORTER_ASSERT(reporter, !p.contains(1, 2));
4082 REPORTER_ASSERT(reporter, !p.contains(3, 2));
4083 REPORTER_ASSERT(reporter, !p.contains(4, 0));
4084 REPORTER_ASSERT(reporter, p.contains(4, 4));
4085
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004086 // test quads
4087 p.reset();
4088 p.moveTo(4, 4);
4089 p.quadTo(6, 6, 8, 8);
4090 p.quadTo(6, 8, 4, 8);
4091 p.quadTo(4, 6, 4, 4);
4092 REPORTER_ASSERT(reporter, p.contains(5, 6));
4093 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004094 // test quad edge
4095 REPORTER_ASSERT(reporter, p.contains(5, 5));
4096 REPORTER_ASSERT(reporter, p.contains(5, 8));
4097 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08004098 // test quad endpoints
4099 REPORTER_ASSERT(reporter, p.contains(4, 4));
4100 REPORTER_ASSERT(reporter, p.contains(8, 8));
4101 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004102
4103 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004104 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
4105 p.moveTo(qPts[0]);
4106 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
4107 p.quadTo(qPts[index], qPts[index + 1]);
4108 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004109 REPORTER_ASSERT(reporter, p.contains(5, 6));
4110 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004111 // test quad edge
4112 SkPoint halfway;
4113 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
4114 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
4115 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4116 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004117
caryclark9aacd902015-12-14 08:38:09 -08004118 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004119 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08004120 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
4121 p.moveTo(kPts[0]);
4122 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
4123 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
4124 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004125 REPORTER_ASSERT(reporter, p.contains(5, 6));
4126 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08004127 // test conic edge
4128 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
4129 SkConic conic(&kPts[index], 0.5f);
4130 halfway = conic.evalAt(0.5f);
4131 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4132 }
caryclark9cb5d752015-12-18 04:35:24 -08004133 // test conic end points
4134 REPORTER_ASSERT(reporter, p.contains(4, 4));
4135 REPORTER_ASSERT(reporter, p.contains(8, 8));
4136 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004137
4138 // test cubics
4139 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
4140 for (int i = 0; i < 3; ++i) {
4141 p.reset();
4142 p.setFillType(SkPath::kEvenOdd_FillType);
4143 p.moveTo(pts[i].fX, pts[i].fY);
4144 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);
4145 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);
4146 p.close();
4147 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
4148 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08004149 // test cubic edge
4150 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
4151 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
4152 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
4153 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08004154 // test cubic end points
4155 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
4156 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
4157 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004158 }
4159}
4160
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004161class PathRefTest_Private {
4162public:
4163 static void TestPathRef(skiatest::Reporter* reporter) {
4164 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004165
bungeman6bd52842016-10-27 09:30:08 -07004166 sk_sp<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004167
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004168 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004169
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004170 {
4171 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
4172 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4173 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4174 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
4175 for (int i = 0; i < kRepeatCnt; ++i) {
4176 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
4177 }
4178 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004179 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004180
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004181 {
4182 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
4183 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4184 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
4185 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
4186 for (int i = 0; i < kRepeatCnt; ++i) {
4187 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
4188 }
4189 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004190 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004191
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004192 {
4193 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
4194 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4195 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4196 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
4197 for (int i = 0; i < kRepeatCnt; ++i) {
4198 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
4199 }
4200 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004201 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004202
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004203 {
halcanary96fcdcc2015-08-27 07:41:13 -07004204 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004205 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
4206 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4207 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
4208 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
4209 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07004210 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004211 for (int i = 0; i < kRepeatCnt; ++i) {
4212 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
4213 }
4214 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004215 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004216
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004217 {
4218 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
4219 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
4220 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
4221 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
4222 for (int i = 0; i < kRepeatCnt; ++i) {
4223 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
4224 }
4225 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004226 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004227 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004228};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00004229
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004230static void test_operatorEqual(skiatest::Reporter* reporter) {
4231 SkPath a;
4232 SkPath b;
4233 REPORTER_ASSERT(reporter, a == a);
4234 REPORTER_ASSERT(reporter, a == b);
4235 a.setFillType(SkPath::kInverseWinding_FillType);
4236 REPORTER_ASSERT(reporter, a != b);
4237 a.reset();
4238 REPORTER_ASSERT(reporter, a == b);
4239 a.lineTo(1, 1);
4240 REPORTER_ASSERT(reporter, a != b);
4241 a.reset();
4242 REPORTER_ASSERT(reporter, a == b);
4243 a.lineTo(1, 1);
4244 b.lineTo(1, 2);
4245 REPORTER_ASSERT(reporter, a != b);
4246 a.reset();
4247 a.lineTo(1, 2);
4248 REPORTER_ASSERT(reporter, a == b);
4249}
4250
caryclark66a5d8b2014-06-24 08:30:15 -07004251static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07004252 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07004253 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07004254 path.dump(&wStream, force, dumpAsHex);
reed42943c82016-09-12 12:01:44 -07004255 sk_sp<SkData> data = wStream.detachAsData();
caryclark66a5d8b2014-06-24 08:30:15 -07004256 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08004257 if (strlen(str) > 0) {
4258 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
4259 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07004260 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08004261 }
caryclark66a5d8b2014-06-24 08:30:15 -07004262}
4263
4264static void test_dump(skiatest::Reporter* reporter) {
4265 SkPath p;
Cary Clark9f67f042016-12-16 11:32:54 -05004266 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n");
4267 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004268 p.moveTo(1, 2);
4269 p.lineTo(3, 4);
Cary Clark9f67f042016-12-16 11:32:54 -05004270 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4271 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004272 "path.lineTo(3, 4);\n");
Cary Clark9f67f042016-12-16 11:32:54 -05004273 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4274 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004275 "path.lineTo(3, 4);\n"
4276 "path.lineTo(1, 2);\n"
4277 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004278 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004279 p.setFillType(SkPath::kEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004280 p.moveTo(1, 2);
4281 p.quadTo(3, 4, 5, 6);
Cary Clark9f67f042016-12-16 11:32:54 -05004282 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n"
4283 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004284 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004285 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004286 p.setFillType(SkPath::kInverseWinding_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004287 p.moveTo(1, 2);
4288 p.conicTo(3, 4, 5, 6, 0.5f);
Cary Clark9f67f042016-12-16 11:32:54 -05004289 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n"
4290 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004291 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004292 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004293 p.setFillType(SkPath::kInverseEvenOdd_FillType);
caryclark66a5d8b2014-06-24 08:30:15 -07004294 p.moveTo(1, 2);
4295 p.cubicTo(3, 4, 5, 6, 7, 8);
Cary Clark9f67f042016-12-16 11:32:54 -05004296 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n"
4297 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004298 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4299 p.reset();
Cary Clark9f67f042016-12-16 11:32:54 -05004300 p.setFillType(SkPath::kWinding_FillType);
caryclarke9562592014-09-15 09:26:09 -07004301 p.moveTo(1, 2);
4302 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004303 compare_dump(reporter, p, false, true,
Cary Clark9f67f042016-12-16 11:32:54 -05004304 "path.setFillType(SkPath::kWinding_FillType);\n"
caryclark08fa28c2014-10-23 13:08:56 -07004305 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4306 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004307 p.reset();
4308 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4309 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
Cary Clark9f67f042016-12-16 11:32:54 -05004310 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"
4311 "path.moveTo(1, 2);\n"
caryclarke9562592014-09-15 09:26:09 -07004312 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004313}
4314
senorblanco84cd6212015-08-04 10:01:58 -07004315namespace {
4316
4317class ChangeListener : public SkPathRef::GenIDChangeListener {
4318public:
4319 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
Brian Salomond3b65972017-03-22 12:05:03 -04004320 ~ChangeListener() override {}
senorblanco84cd6212015-08-04 10:01:58 -07004321 void onChange() override {
4322 *fChanged = true;
4323 }
4324private:
4325 bool* fChanged;
4326};
4327
4328}
4329
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004330class PathTest_Private {
4331public:
4332 static void TestPathTo(skiatest::Reporter* reporter) {
4333 SkPath p, q;
4334 p.lineTo(4, 4);
4335 p.reversePathTo(q);
4336 check_path_is_line(reporter, &p, 4, 4);
4337 q.moveTo(-4, -4);
4338 p.reversePathTo(q);
4339 check_path_is_line(reporter, &p, 4, 4);
4340 q.lineTo(7, 8);
4341 q.conicTo(8, 7, 6, 5, 0.5f);
4342 q.quadTo(6, 7, 8, 6);
4343 q.cubicTo(5, 6, 7, 8, 7, 5);
4344 q.close();
4345 p.reversePathTo(q);
4346 SkRect reverseExpected = {-4, -4, 8, 8};
4347 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4348 }
senorblanco84cd6212015-08-04 10:01:58 -07004349
4350 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4351 SkPath p;
4352
4353 bool changed = false;
4354 p.moveTo(0, 0);
4355
4356 // Check that listener is notified on moveTo().
4357
halcanary385fe4d2015-08-26 13:07:48 -07004358 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004359 REPORTER_ASSERT(reporter, !changed);
4360 p.moveTo(10, 0);
4361 REPORTER_ASSERT(reporter, changed);
4362
4363 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004364 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004365 REPORTER_ASSERT(reporter, !changed);
4366 p.lineTo(20, 0);
4367 REPORTER_ASSERT(reporter, changed);
4368
4369 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004370 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004371 REPORTER_ASSERT(reporter, !changed);
4372 p.reset();
4373 REPORTER_ASSERT(reporter, changed);
4374
4375 p.moveTo(0, 0);
4376
4377 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004378 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004379 REPORTER_ASSERT(reporter, !changed);
4380 p.rewind();
4381 REPORTER_ASSERT(reporter, changed);
4382
4383 // Check that listener is notified when pathref is deleted.
4384 {
4385 SkPath q;
4386 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004387 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004388 REPORTER_ASSERT(reporter, !changed);
4389 }
4390 // q went out of scope.
4391 REPORTER_ASSERT(reporter, changed);
4392 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004393};
4394
reed158fabb2016-07-20 10:06:59 -07004395static void test_crbug_629455(skiatest::Reporter* reporter) {
4396 SkPath path;
4397 path.moveTo(0, 0);
4398 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd),
4399 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd),
4400 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341));
4401// AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f);
4402 path.lineTo(0, 0);
4403
4404 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4405 SkPaint paint;
4406 paint.setAntiAlias(true);
4407 surface->getCanvas()->drawPath(path, paint);
4408}
4409
liyuqianc78eff92016-11-09 11:18:30 -08004410static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) {
4411 SkPath path;
4412 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4413 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f
4414 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f
4415 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f
4416 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f
4417 path.close();
4418
4419 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4420 SkPaint paint;
4421 paint.setAntiAlias(true);
4422 surface->getCanvas()->clipPath(path, true);
Mike Reed3661bc92017-02-22 13:21:42 -05004423 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint);
liyuqianc78eff92016-11-09 11:18:30 -08004424}
4425
Yuqian Lidb13a092016-11-29 10:29:22 -05004426static void test_path_crbugskia6003() {
4427 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
4428 SkCanvas* canvas = surface->getCanvas();
4429 SkPaint paint;
4430 paint.setAntiAlias(true);
4431 SkPath path;
4432 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4433 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f
4434 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f
4435 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f
4436 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f
4437 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f
4438 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4439 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666));
4440 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f
4441 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc),
4442 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666));
4443 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f
4444 path.close();
4445 canvas->clipPath(path, true);
Mike Reed3661bc92017-02-22 13:21:42 -05004446 canvas->drawRect(SkRect::MakeWH(500, 500), paint);
Yuqian Lidb13a092016-11-29 10:29:22 -05004447}
4448
Yuqian Lic4f66af2016-11-11 09:36:53 -05004449static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) {
4450 SkPath path;
4451 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4452 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f
4453 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f
4454 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f
4455 path.close();
4456
4457 auto surface = SkSurface::MakeRasterN32Premul(100, 100);
4458 SkPaint paint;
4459 paint.setAntiAlias(true);
4460 surface->getCanvas()->drawPath(path, paint);
4461}
4462
caryclark8e7b19d2016-02-18 04:11:48 -08004463static void test_interp(skiatest::Reporter* reporter) {
4464 SkPath p1, p2, out;
4465 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4466 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4467 REPORTER_ASSERT(reporter, p1 == out);
4468 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4469 REPORTER_ASSERT(reporter, p1 == out);
4470 p1.moveTo(0, 2);
4471 p1.lineTo(0, 4);
4472 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4473 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4474 p2.moveTo(6, 0);
4475 p2.lineTo(8, 0);
4476 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4477 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4478 REPORTER_ASSERT(reporter, p2 == out);
4479 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4480 REPORTER_ASSERT(reporter, p1 == out);
4481 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4482 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4483 p1.reset();
4484 p1.moveTo(4, 4);
4485 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4486 p2.reset();
4487 p2.moveTo(4, 2);
4488 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4489 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4490 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4491 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4492 p2.reset();
4493 p2.moveTo(4, 2);
4494 p2.conicTo(6, 3, 6, 5, 1);
4495 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4496 p2.reset();
4497 p2.moveTo(4, 4);
4498 p2.conicTo(5, 4, 5, 5, 0.5f);
4499 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4500}
4501
4502DEF_TEST(PathInterp, reporter) {
4503 test_interp(reporter);
4504}
4505
Mike Reed0c0da2b2017-01-26 11:03:50 -05004506#include "SkSurface.h"
4507DEF_TEST(PathBigCubic, reporter) {
4508 SkPath path;
4509 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0
4510 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4511 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), SkBits2Float(0x00000100), SkBits2Float(0x00000000), SkBits2Float(0x00000100), SkBits2Float(0x00000000)); // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4512 path.moveTo(0, 512);
4513
4514 // this call should not assert
Mike Reedc121a882017-01-27 11:59:07 -05004515 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint());
Mike Reed0c0da2b2017-01-26 11:03:50 -05004516}
4517
caryclark9aacd902015-12-14 08:38:09 -08004518DEF_TEST(PathContains, reporter) {
4519 test_contains(reporter);
4520}
4521
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004522DEF_TEST(Paths, reporter) {
caryclarkbac10462016-09-22 10:24:59 -07004523 test_fuzz_crbug_647922();
caryclarke97fe832016-09-06 08:54:10 -07004524 test_fuzz_crbug_643933();
reedff863bc2016-08-17 07:37:57 -07004525 test_sect_with_horizontal_needs_pinning();
reed158fabb2016-07-20 10:06:59 -07004526 test_crbug_629455(reporter);
reedb1b12f82016-07-13 10:56:53 -07004527 test_fuzz_crbug_627414(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004528 test_path_crbug364224();
liyuqianc78eff92016-11-09 11:18:30 -08004529 test_fuzz_crbug_662952(reporter);
Yuqian Lic4f66af2016-11-11 09:36:53 -05004530 test_fuzz_crbug_662730(reporter);
liyuqian041da382016-11-11 09:59:51 -08004531 test_fuzz_crbug_662780();
Yuqian Li20079a92016-11-16 13:07:57 -05004532 test_mask_overflow();
Yuqian Lidb13a092016-11-29 10:29:22 -05004533 test_path_crbugskia6003();
Yuqian Li79252f72016-11-29 15:02:49 -05004534 test_fuzz_crbug_668907();
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004535
Hal Canaryfafe1352017-04-11 12:12:02 -04004536 SkSize::Make(3, 4);
bungeman@google.coma5809a32013-06-21 15:13:34 +00004537
4538 SkPath p, empty;
4539 SkRect bounds, bounds2;
4540 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004541
reed@android.comd252db02009-04-01 18:31:44 +00004542 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004543
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004544 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4545 SkPath& self = p;
4546 p = self;
4547
4548 // this triggers a code path in SkPath::swap which is otherwise unexercised
4549 p.swap(self);
4550
reed@android.com3abec1d2009-03-02 05:36:20 +00004551 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004552
reed@android.com6b82d1a2009-06-03 02:35:01 +00004553 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4554 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004555 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004556 REPORTER_ASSERT(reporter,
4557 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004558 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004559
reed@android.com6b82d1a2009-06-03 02:35:01 +00004560 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004561 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004562
reed@android.com6b82d1a2009-06-03 02:35:01 +00004563 p.addOval(bounds);
4564 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004565 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004566
bungeman@google.coma5809a32013-06-21 15:13:34 +00004567 p.rewind();
4568 test_empty(reporter, p);
4569
reed@android.com3abec1d2009-03-02 05:36:20 +00004570 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004571 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004572 // we have only lines
4573 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004574 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004575
bungeman@google.coma5809a32013-06-21 15:13:34 +00004576 REPORTER_ASSERT(reporter, p != empty);
4577 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004578
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004579 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004580 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4581 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004582 SkPoint pts[4];
4583 int count = p.getPoints(pts, 4);
4584 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004585 uint8_t verbs[6];
4586 verbs[5] = 0xff;
4587 p.getVerbs(verbs, 5);
4588 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4589 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4590 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4591 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4592 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4593 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004594 bounds2.set(pts, 4);
4595 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004596
reed@android.com3abec1d2009-03-02 05:36:20 +00004597 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4598 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004599 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004600
halcanary96fcdcc2015-08-27 07:41:13 -07004601 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004602 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004603 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4604 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004605
reed@android.com3abec1d2009-03-02 05:36:20 +00004606 // now force p to not be a rect
4607 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4608 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004609 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004610
robertphillipsfe7c4272014-12-29 11:36:39 -08004611 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4612 // path has a trailing moveTo. Please see crbug.com\445368)
4613 {
4614 SkRect r;
4615 p.reset();
4616 p.addRect(bounds);
4617 REPORTER_ASSERT(reporter, p.isRect(&r));
4618 REPORTER_ASSERT(reporter, r == bounds);
4619 // add a moveTo outside of our bounds
4620 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4621 REPORTER_ASSERT(reporter, p.isRect(&r));
4622 REPORTER_ASSERT(reporter, r == bounds);
4623 }
4624
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004625 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004626 test_isLine(reporter);
4627 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004628 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004629 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004630 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004631 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004632 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004633 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004634 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004635 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004636 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004637 test_flattening(reporter);
4638 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004639 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004640 test_iter(reporter);
4641 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004642 test_circle(reporter);
4643 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004644 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004645 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004646 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004647 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004648 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004649 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004650 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004651 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004652 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004653 test_tricky_cubic();
4654 test_clipped_cubic();
4655 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004656 test_crbug_493450(reporter);
4657 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004658 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004659 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004660 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004661 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004662 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004663 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004664 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004665 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004666 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004667 test_arcTo(reporter);
4668 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004669 test_addPathMode(reporter, false, false);
4670 test_addPathMode(reporter, true, false);
4671 test_addPathMode(reporter, false, true);
4672 test_addPathMode(reporter, true, true);
4673 test_extendClosedPath(reporter);
4674 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4675 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004676 test_conicTo_special_case(reporter);
4677 test_get_point(reporter);
4678 test_contains(reporter);
4679 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004680 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004681 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004682 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004683 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004684 test_path_crbugskia2820(reporter);
Yuqian Li9939bd62016-11-29 10:27:16 -05004685 test_path_crbugskia5995();
caryclark5ccef572015-03-02 10:07:56 -08004686 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004687 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004688 test_bounds_crbug_513799(reporter);
caryclark3cebe242016-08-23 09:41:00 -07004689 test_fuzz_crbug_638223();
reed@android.com3abec1d2009-03-02 05:36:20 +00004690}
Mike Reed7717d4a2017-01-24 10:12:53 -05004691
4692DEF_TEST(conservatively_contains_rect, reporter) {
4693 SkPath path;
4694
4695 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f
4696 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0
4697 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052),
4698 SkBits2Float(0x00000100), SkBits2Float(0x00000000),
4699 SkBits2Float(0x00000100), SkBits2Float(0x00000000));
4700 path.moveTo(0, 0);
4701
4702 // this guy should not assert
4703 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f });
4704}
Mike Reed0d7dac82017-02-02 17:45:56 -08004705
4706///////////////////////////////////////////////////////////////////////////////////////////////////
4707
4708static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) {
4709 for (int i = 0; i < n; ++i) {
4710 switch (verb) {
4711 case SkPath::kLine_Verb:
4712 path->lineTo(rand.nextF()*100, rand.nextF()*100);
4713 break;
4714 case SkPath::kQuad_Verb:
4715 path->quadTo(rand.nextF()*100, rand.nextF()*100,
4716 rand.nextF()*100, rand.nextF()*100);
4717 break;
4718 case SkPath::kConic_Verb:
4719 path->conicTo(rand.nextF()*100, rand.nextF()*100,
4720 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10);
4721 break;
4722 case SkPath::kCubic_Verb:
4723 path->cubicTo(rand.nextF()*100, rand.nextF()*100,
4724 rand.nextF()*100, rand.nextF()*100,
4725 rand.nextF()*100, rand.nextF()*100);
4726 break;
4727 default:
4728 SkASSERT(false);
4729 }
4730 }
4731}
4732
4733#include "SkPathOps.h"
4734DEF_TEST(path_tight_bounds, reporter) {
4735 SkRandom rand;
4736
4737 const SkPath::Verb verbs[] = {
4738 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb,
4739 };
4740 for (int i = 0; i < 1000; ++i) {
4741 for (int n = 1; n <= 10; n += 9) {
4742 for (SkPath::Verb verb : verbs) {
4743 SkPath path;
4744 rand_path(&path, rand, verb, n);
4745 SkRect bounds = path.getBounds();
Mike Reed8d3196b2017-02-03 11:34:13 -05004746 SkRect tight = path.computeTightBounds();
Mike Reed0d7dac82017-02-02 17:45:56 -08004747 REPORTER_ASSERT(reporter, bounds.contains(tight));
4748
4749 SkRect tight2;
4750 TightBounds(path, &tight2);
4751 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2));
4752 }
4753 }
4754 }
4755}
Cary Clarke0b72872017-04-12 12:03:15 -04004756
4757DEF_TEST(skbug_6450, r) {
4758 SkRect ri = { 0.18554693f, 195.26283f, 0.185784385f, 752.644409f };
4759 SkVector rdi[4] = {
4760 { 1.81159976e-09f, 7.58768801e-05f },
4761 { 0.000118725002f, 0.000118725002f },
4762 { 0.000118725002f, 0.000118725002f },
4763 { 0.000118725002f, 0.486297607f }
4764 };
4765 SkRRect irr;
4766 irr.setRectRadii(ri, rdi);
4767 SkRect ro = { 9.18354821e-39f, 2.1710848e+9f, 2.16945843e+9f, 3.47808128e+9f };
4768 SkVector rdo[4] = {
4769 { 0, 0 },
4770 { 0.0103298295f, 0.185887396f },
4771 { 2.52999727e-29f, 169.001938f },
4772 { 195.262741f, 195.161255f }
4773 };
4774 SkRRect orr;
4775 orr.setRectRadii(ro, rdo);
4776 SkMakeNullCanvas()->drawDRRect(orr, irr, SkPaint());
4777}