blob: dd6f41cbfae83371ff2e399053fd752128988da1 [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
reed@google.com8cae8352012-09-14 15:18:41 +00008#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -08009#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000010#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000011#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000012#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070013#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000014#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000016#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000017#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000018#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070019#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070020#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000021#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000022#include "SkTypes.h"
23#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000025
reed5bcbe912014-12-15 12:28:33 -080026static void set_radii(SkVector radii[4], int index, float rad) {
27 sk_bzero(radii, sizeof(SkVector) * 4);
28 radii[index].set(rad, rad);
29}
30
31static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
32 const SkVector radii[4]) {
33 SkRRect rrect;
34 rrect.setRectRadii(bounds, radii);
35 REPORTER_ASSERT(reporter, bounds == rrect.rect());
36
37 SkPath path;
38 // this line should not assert in the debug build (from validate)
39 path.addRRect(rrect);
40 REPORTER_ASSERT(reporter, bounds == path.getBounds());
41}
42
caryclark5ccef572015-03-02 10:07:56 -080043static void test_skbug_3469(skiatest::Reporter* reporter) {
44 SkPath path;
45 path.moveTo(20, 20);
46 path.quadTo(20, 50, 80, 50);
47 path.quadTo(20, 50, 20, 80);
48 REPORTER_ASSERT(reporter, !path.isConvex());
49}
50
reed5bcbe912014-12-15 12:28:33 -080051static void test_skbug_3239(skiatest::Reporter* reporter) {
52 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
53 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
54 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
55
56 const float rad = 33436320;
57
58 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
59 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
60
61 SkVector radii[4];
62 for (int i = 0; i < 4; ++i) {
63 set_radii(radii, i, rad);
64 test_add_rrect(reporter, rectx, radii);
65 test_add_rrect(reporter, recty, radii);
66 }
67}
68
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000069static void make_path_crbug364224(SkPath* path) {
70 path->reset();
71 path->moveTo(3.747501373f, 2.724499941f);
72 path->lineTo(3.747501373f, 3.75f);
73 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
74 path->lineTo(0.7475013733f, 4.0f);
75 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
76 path->lineTo(0.4975013733f, 1.0f);
77 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
78 path->lineTo(3.497501373f, 0.75f);
79 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
80 path->lineTo(3.715001345f, 0.5512499809f);
81 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
82 path->lineTo(0.7475013733f, 0.4999999702f);
83 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
84 path->lineTo(0.2475013733f, 3.75f);
85 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
86 path->lineTo(3.497501373f, 4.25f);
87 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
88 path->lineTo(3.997501373f, 2.474750042f);
89 path->lineTo(3.747501373f, 2.724499941f);
90 path->close();
91}
92
93static void make_path_crbug364224_simplified(SkPath* path) {
94 path->moveTo(3.747501373f, 2.724499941f);
95 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
96 path->close();
97}
98
99static void test_path_crbug364224() {
100 SkPath path;
101 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700102 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000103 SkCanvas* canvas = surface->getCanvas();
104
105 make_path_crbug364224_simplified(&path);
106 canvas->drawPath(path, paint);
107
108 make_path_crbug364224(&path);
109 canvas->drawPath(path, paint);
110}
111
piotaixrfac4e0e2014-08-26 11:59:04 -0700112/**
113 * In debug mode, this path was causing an assertion to fail in
114 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
115 */
116static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
117 SkPoint orig, p1, p2, p3;
118 orig = SkPoint::Make(1.f, 1.f);
119 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
120 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
121 p3 = SkPoint::Make(2.f, 2.f);
122
123 path->reset();
124 path->moveTo(orig);
125 path->cubicTo(p1, p2, p3);
126 path->close();
127}
128
129static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
130 SkPath path;
131 make_path_crbugskia2820(&path, reporter);
132
133 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
134 stroke.setStrokeStyle(2 * SK_Scalar1);
135 stroke.applyToPath(&path, path);
136}
137
reed@google.comcc8be772013-10-15 15:35:29 +0000138static void make_path0(SkPath* path) {
139 // from * https://code.google.com/p/skia/issues/detail?id=1706
140
141 path->moveTo(146.939f, 1012.84f);
142 path->lineTo(181.747f, 1009.18f);
143 path->lineTo(182.165f, 1013.16f);
144 path->lineTo(147.357f, 1016.82f);
145 path->lineTo(146.939f, 1012.84f);
146 path->close();
147}
148
149static void make_path1(SkPath* path) {
150 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
151}
152
153typedef void (*PathProc)(SkPath*);
154
155/*
156 * Regression test: we used to crash (overwrite internal storage) during
157 * construction of the region when the path was INVERSE. That is now fixed,
158 * so test these regions (which used to assert/crash).
159 *
160 * https://code.google.com/p/skia/issues/detail?id=1706
161 */
162static void test_path_to_region(skiatest::Reporter* reporter) {
163 PathProc procs[] = {
164 make_path0,
165 make_path1,
166 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000167
reed@google.comcc8be772013-10-15 15:35:29 +0000168 SkRegion clip;
169 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000170
reed@google.comcc8be772013-10-15 15:35:29 +0000171 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
172 SkPath path;
173 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000174
reed@google.comcc8be772013-10-15 15:35:29 +0000175 SkRegion rgn;
176 rgn.setPath(path, clip);
177 path.toggleInverseFillType();
178 rgn.setPath(path, clip);
179 }
180}
181
caryclark@google.com56f233a2012-11-19 13:06:06 +0000182#if defined(WIN32)
183 #define SUPPRESS_VISIBILITY_WARNING
184#else
185 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
186#endif
187
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000188static void test_path_close_issue1474(skiatest::Reporter* reporter) {
189 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
190 // are relative to the point we close to, not relative to the point we close from.
191 SkPath path;
192 SkPoint last;
193
194 // Test rLineTo().
195 path.rLineTo(0, 100);
196 path.rLineTo(100, 0);
197 path.close(); // Returns us back to 0,0.
198 path.rLineTo(50, 50); // This should go to 50,50.
199
200 path.getLastPt(&last);
201 REPORTER_ASSERT(reporter, 50 == last.fX);
202 REPORTER_ASSERT(reporter, 50 == last.fY);
203
204 // Test rQuadTo().
205 path.rewind();
206 path.rLineTo(0, 100);
207 path.rLineTo(100, 0);
208 path.close();
209 path.rQuadTo(50, 50, 75, 75);
210
211 path.getLastPt(&last);
212 REPORTER_ASSERT(reporter, 75 == last.fX);
213 REPORTER_ASSERT(reporter, 75 == last.fY);
214
215 // Test rConicTo().
216 path.rewind();
217 path.rLineTo(0, 100);
218 path.rLineTo(100, 0);
219 path.close();
220 path.rConicTo(50, 50, 85, 85, 2);
221
222 path.getLastPt(&last);
223 REPORTER_ASSERT(reporter, 85 == last.fX);
224 REPORTER_ASSERT(reporter, 85 == last.fY);
225
226 // Test rCubicTo().
227 path.rewind();
228 path.rLineTo(0, 100);
229 path.rLineTo(100, 0);
230 path.close();
231 path.rCubicTo(50, 50, 85, 85, 95, 95);
232
233 path.getLastPt(&last);
234 REPORTER_ASSERT(reporter, 95 == last.fX);
235 REPORTER_ASSERT(reporter, 95 == last.fY);
236}
237
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000238static void test_gen_id(skiatest::Reporter* reporter) {
239 SkPath a, b;
240 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
241
242 a.moveTo(0, 0);
243 const uint32_t z = a.getGenerationID();
244 REPORTER_ASSERT(reporter, z != b.getGenerationID());
245
246 a.reset();
247 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
248
249 a.moveTo(1, 1);
250 const uint32_t y = a.getGenerationID();
251 REPORTER_ASSERT(reporter, z != y);
252
253 b.moveTo(2, 2);
254 const uint32_t x = b.getGenerationID();
255 REPORTER_ASSERT(reporter, x != y && x != z);
256
257 a.swap(b);
258 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
259
260 b = a;
261 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
262
263 SkPath c(a);
264 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
265
266 c.lineTo(3, 3);
267 const uint32_t w = c.getGenerationID();
268 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
269 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
270 REPORTER_ASSERT(reporter, w != x);
271
djsollen50da1d82015-02-17 09:09:53 -0800272#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000273 static bool kExpectGenIDToIgnoreFill = false;
274#else
275 static bool kExpectGenIDToIgnoreFill = true;
276#endif
277
278 c.toggleInverseFillType();
279 const uint32_t v = c.getGenerationID();
280 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
281
282 c.rewind();
283 REPORTER_ASSERT(reporter, v != c.getGenerationID());
284}
285
reed@google.com3eff3592013-05-08 21:08:21 +0000286// This used to assert in the debug build, as the edges did not all line-up.
287static void test_bad_cubic_crbug234190() {
288 SkPath path;
289 path.moveTo(13.8509f, 3.16858f);
290 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
291 -2.38991e+08f, -4.26573e+08f,
292 -2.41016e+08f, -4.30188e+08f);
293
294 SkPaint paint;
295 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700296 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000297 surface->getCanvas()->drawPath(path, paint);
298}
299
reed@google.com7a90daf2013-04-10 18:44:00 +0000300static void test_bad_cubic_crbug229478() {
301 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000302 { 4595.91064f, -11596.9873f },
303 { 4597.2168f, -11595.9414f },
304 { 4598.52344f, -11594.8955f },
305 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000306 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000307
reed@google.com7a90daf2013-04-10 18:44:00 +0000308 SkPath path;
309 path.moveTo(pts[0]);
310 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000311
reed@google.com7a90daf2013-04-10 18:44:00 +0000312 SkPaint paint;
313 paint.setStyle(SkPaint::kStroke_Style);
314 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000315
reed@google.com7a90daf2013-04-10 18:44:00 +0000316 SkPath dst;
317 // Before the fix, this would infinite-recurse, and run out of stack
318 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700319 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000320}
321
reed@google.com64d62952013-01-18 17:49:28 +0000322static void build_path_170666(SkPath& path) {
323 path.moveTo(17.9459f, 21.6344f);
324 path.lineTo(139.545f, -47.8105f);
325 path.lineTo(139.545f, -47.8105f);
326 path.lineTo(131.07f, -47.3888f);
327 path.lineTo(131.07f, -47.3888f);
328 path.lineTo(122.586f, -46.9532f);
329 path.lineTo(122.586f, -46.9532f);
330 path.lineTo(18076.6f, 31390.9f);
331 path.lineTo(18076.6f, 31390.9f);
332 path.lineTo(18085.1f, 31390.5f);
333 path.lineTo(18085.1f, 31390.5f);
334 path.lineTo(18076.6f, 31390.9f);
335 path.lineTo(18076.6f, 31390.9f);
336 path.lineTo(17955, 31460.3f);
337 path.lineTo(17955, 31460.3f);
338 path.lineTo(17963.5f, 31459.9f);
339 path.lineTo(17963.5f, 31459.9f);
340 path.lineTo(17971.9f, 31459.5f);
341 path.lineTo(17971.9f, 31459.5f);
342 path.lineTo(17.9551f, 21.6205f);
343 path.lineTo(17.9551f, 21.6205f);
344 path.lineTo(9.47091f, 22.0561f);
345 path.lineTo(9.47091f, 22.0561f);
346 path.lineTo(17.9459f, 21.6344f);
347 path.lineTo(17.9459f, 21.6344f);
348 path.close();path.moveTo(0.995934f, 22.4779f);
349 path.lineTo(0.986725f, 22.4918f);
350 path.lineTo(0.986725f, 22.4918f);
351 path.lineTo(17955, 31460.4f);
352 path.lineTo(17955, 31460.4f);
353 path.lineTo(17971.9f, 31459.5f);
354 path.lineTo(17971.9f, 31459.5f);
355 path.lineTo(18093.6f, 31390.1f);
356 path.lineTo(18093.6f, 31390.1f);
357 path.lineTo(18093.6f, 31390);
358 path.lineTo(18093.6f, 31390);
359 path.lineTo(139.555f, -47.8244f);
360 path.lineTo(139.555f, -47.8244f);
361 path.lineTo(122.595f, -46.9671f);
362 path.lineTo(122.595f, -46.9671f);
363 path.lineTo(0.995934f, 22.4779f);
364 path.lineTo(0.995934f, 22.4779f);
365 path.close();
366 path.moveTo(5.43941f, 25.5223f);
367 path.lineTo(798267, -28871.1f);
368 path.lineTo(798267, -28871.1f);
369 path.lineTo(3.12512e+06f, -113102);
370 path.lineTo(3.12512e+06f, -113102);
371 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
372 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
373 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
374 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
375 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
376 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
377 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
378 path.lineTo(2.78271e+08f, -1.00733e+07f);
379 path.lineTo(2.78271e+08f, -1.00733e+07f);
380 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
381 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
382 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
383 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
384 path.lineTo(2.77473e+08f, -1.00444e+07f);
385 path.lineTo(2.77473e+08f, -1.00444e+07f);
386 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
387 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
388 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
389 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
390 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
391 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
392 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
393 path.lineTo(798284, -28872);
394 path.lineTo(798284, -28872);
395 path.lineTo(22.4044f, 24.6677f);
396 path.lineTo(22.4044f, 24.6677f);
397 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
398 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
399 path.close();
400}
401
402static void build_path_simple_170666(SkPath& path) {
403 path.moveTo(126.677f, 24.1591f);
404 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
405}
406
407// This used to assert in the SK_DEBUG build, as the clip step would fail with
408// too-few interations in our cubic-line intersection code. That code now runs
409// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000410static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000411 SkPath path;
412 SkPaint paint;
413 paint.setAntiAlias(true);
414
reede8f30622016-03-23 18:59:25 -0700415 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000416
reed@google.com64d62952013-01-18 17:49:28 +0000417 build_path_simple_170666(path);
418 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000419
reed@google.com64d62952013-01-18 17:49:28 +0000420 build_path_170666(path);
421 surface->getCanvas()->drawPath(path, paint);
422}
423
caryclarke8c56662015-07-14 11:19:26 -0700424
425static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
426 SkScalar tx, SkScalar ty, SkScalar scale) {
427 SkPath smallPath;
428 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
429 bool smallConvex = smallPath.isConvex();
430 SkPath largePath;
431 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
432 SkMatrix matrix;
433 matrix.reset();
434 matrix.preTranslate(100, 100);
435 matrix.preScale(scale, scale);
436 largePath.transform(matrix);
437 bool largeConvex = largePath.isConvex();
438 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
439}
440
441static void test_crbug_493450(skiatest::Reporter* reporter) {
442 const char reducedCase[] =
443 "M0,0"
444 "L0.0002, 0"
445 "L0.0002, 0.0002"
446 "L0.0001, 0.0001"
447 "L0,0.0002"
448 "Z";
449 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
450 const char originalFiddleData[] =
451 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
452 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
453 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
454 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
455 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
456 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
457 826357.3384828606f);
458}
459
460static void test_crbug_495894(skiatest::Reporter* reporter) {
461 const char originalFiddleData[] =
462 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
463 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
464 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
465 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
466 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
467 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
468 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
469 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
470 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
471 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
472 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
473 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
474 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
475 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
476 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
477 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
478 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
479 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
480 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
481 "L-0.33993994441916414,-0.11288906492739594Z";
482 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
483 65536);
484}
485
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000486static void test_addrect(skiatest::Reporter* reporter) {
487 SkPath path;
488 path.lineTo(0, 0);
489 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700490 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000491
492 path.reset();
493 path.lineTo(FLT_EPSILON, FLT_EPSILON);
494 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700495 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000496
497 path.reset();
498 path.quadTo(0, 0, 0, 0);
499 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700500 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000501
502 path.reset();
503 path.conicTo(0, 0, 0, 0, 0.5f);
504 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700505 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000506
507 path.reset();
508 path.cubicTo(0, 0, 0, 0, 0, 0);
509 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700510 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000511}
512
reed@google.coma8790de2012-10-24 21:04:04 +0000513// Make sure we stay non-finite once we get there (unless we reset or rewind).
514static void test_addrect_isfinite(skiatest::Reporter* reporter) {
515 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000516
reed@google.coma8790de2012-10-24 21:04:04 +0000517 path.addRect(SkRect::MakeWH(50, 100));
518 REPORTER_ASSERT(reporter, path.isFinite());
519
520 path.moveTo(0, 0);
521 path.lineTo(SK_ScalarInfinity, 42);
522 REPORTER_ASSERT(reporter, !path.isFinite());
523
524 path.addRect(SkRect::MakeWH(50, 100));
525 REPORTER_ASSERT(reporter, !path.isFinite());
526
527 path.reset();
528 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000529
reed@google.coma8790de2012-10-24 21:04:04 +0000530 path.addRect(SkRect::MakeWH(50, 100));
531 REPORTER_ASSERT(reporter, path.isFinite());
532}
533
reed@google.com848148e2013-01-15 15:51:59 +0000534static void build_big_path(SkPath* path, bool reducedCase) {
535 if (reducedCase) {
536 path->moveTo(577330, 1971.72f);
537 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
538 } else {
539 path->moveTo(60.1631f, 7.70567f);
540 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
541 path->lineTo(577379, 1977.77f);
542 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
543 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
544 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
545 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
546 path->quadTo(577198, 1972, 577238, 1971.31f);
547 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
548 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
549 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
550 path->moveTo(306.718f, -32.912f);
551 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
552 }
553}
554
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000555static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700556 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000557
558 // This path used to assert, because our cubic-chopping code incorrectly
559 // moved control points after the chop. This test should be run in SK_DEBUG
560 // mode to ensure that we no long assert.
561 SkPath path;
562 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
563 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000564
reed@google.com848148e2013-01-15 15:51:59 +0000565 SkPaint paint;
566 for (int doAA = 0; doAA <= 1; ++doAA) {
567 paint.setAntiAlias(SkToBool(doAA));
568 surface->getCanvas()->drawPath(path, paint);
569 }
570 }
571}
572
reed3b2fb982015-07-29 08:37:13 -0700573static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
574 if (expected != bounds) {
575 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
576 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
577 expected.left(), expected.top(), expected.right(), expected.bottom());
578 }
579}
580
reed91f283b2015-07-28 06:00:50 -0700581static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
582 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700583#if 0
584 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
585 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700586 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
587
588 path.moveTo(-5, -8);
589 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
590
591 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
592 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
593
594 path.moveTo(1, 2);
595 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700596#else
597 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
598
599 path.moveTo(-5, -8); // should set the bounds
600 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
601
602 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
603 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
604
605 path.moveTo(1, 2); // don't expect this to have changed the bounds
606 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
607#endif
reed91f283b2015-07-28 06:00:50 -0700608}
609
reed@google.com8cae8352012-09-14 15:18:41 +0000610// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
611// which triggered an assert, from a tricky cubic. This test replicates that
612// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
613// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000614static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000615 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000616 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
617 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
618 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
619 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000620 };
621
622 SkPath path;
623 path.moveTo(pts[0]);
624 path.cubicTo(pts[1], pts[2], pts[3]);
625
626 SkPaint paint;
627 paint.setAntiAlias(true);
628
reede8f30622016-03-23 18:59:25 -0700629 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000630}
reed@android.com3abec1d2009-03-02 05:36:20 +0000631
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000632// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
633//
634static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
635 SkPath path;
636 path.quadTo(157, 366, 286, 208);
637 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000638
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000639 SkMatrix matrix;
640 matrix.setScale(1000*1000, 1000*1000);
641
642 // Be sure that path::transform correctly updates isFinite and the bounds
643 // if the transformation overflows. The previous bug was that isFinite was
644 // set to true in this case, but the bounds were not set to empty (which
645 // they should be).
646 while (path.isFinite()) {
647 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
648 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
649 path.transform(matrix);
650 }
651 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
652
653 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
654 path.transform(matrix);
655 // we need to still be non-finite
656 REPORTER_ASSERT(reporter, !path.isFinite());
657 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
658}
659
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000660static void add_corner_arc(SkPath* path, const SkRect& rect,
661 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000662 int startAngle)
663{
664
665 SkScalar rx = SkMinScalar(rect.width(), xIn);
666 SkScalar ry = SkMinScalar(rect.height(), yIn);
667
668 SkRect arcRect;
669 arcRect.set(-rx, -ry, rx, ry);
670 switch (startAngle) {
671 case 0:
672 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
673 break;
674 case 90:
675 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
676 break;
677 case 180:
678 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
679 break;
680 case 270:
681 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
682 break;
683 default:
684 break;
685 }
686
687 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
688}
689
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000690static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000691 SkScalar xCorner, SkScalar yCorner) {
692 // we are lazy here and use the same x & y for each corner
693 add_corner_arc(path, r, xCorner, yCorner, 270);
694 add_corner_arc(path, r, xCorner, yCorner, 0);
695 add_corner_arc(path, r, xCorner, yCorner, 90);
696 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000697 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000698}
699
700// Chrome creates its own round rects with each corner possibly being different.
701// Performance will suffer if they are not convex.
702// Note: PathBench::ArbRoundRectBench performs almost exactly
703// the same test (but with drawing)
704static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000705 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000706 SkRect r;
707
708 for (int i = 0; i < 5000; ++i) {
709
robertphillips@google.com158618e2012-10-23 16:56:56 +0000710 SkScalar size = rand.nextUScalar1() * 30;
711 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000712 continue;
713 }
714 r.fLeft = rand.nextUScalar1() * 300;
715 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000716 r.fRight = r.fLeft + 2 * size;
717 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000718
719 SkPath temp;
720
721 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
722
723 REPORTER_ASSERT(reporter, temp.isConvex());
724 }
725}
726
robertphillips@google.com158618e2012-10-23 16:56:56 +0000727// Chrome will sometimes create a 0 radius round rect. The degenerate
728// quads prevent the path from being converted to a rect
729// Note: PathBench::ArbRoundRectBench performs almost exactly
730// the same test (but with drawing)
731static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000732 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000733 SkRect r;
734
735 for (int i = 0; i < 5000; ++i) {
736
737 SkScalar size = rand.nextUScalar1() * 30;
738 if (size < SK_Scalar1) {
739 continue;
740 }
741 r.fLeft = rand.nextUScalar1() * 300;
742 r.fTop = rand.nextUScalar1() * 300;
743 r.fRight = r.fLeft + 2 * size;
744 r.fBottom = r.fTop + 2 * size;
745
746 SkPath temp;
747
748 make_arb_round_rect(&temp, r, 0, 0);
749
robertphillips@google.com158618e2012-10-23 16:56:56 +0000750 SkRect result;
751 REPORTER_ASSERT(reporter, temp.isRect(&result));
752 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000753 }
754}
755
reed@google.com0bb18bb2012-07-26 15:20:36 +0000756static void test_rect_isfinite(skiatest::Reporter* reporter) {
757 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000758 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000759 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000760
reed@google.com0bb18bb2012-07-26 15:20:36 +0000761 SkRect r;
762 r.setEmpty();
763 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000764 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000765 REPORTER_ASSERT(reporter, !r.isFinite());
766 r.set(0, 0, nan, 0);
767 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000768
reed@google.com0bb18bb2012-07-26 15:20:36 +0000769 SkPoint pts[] = {
770 { 0, 0 },
771 { SK_Scalar1, 0 },
772 { 0, SK_Scalar1 },
773 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000774
reed@google.com0bb18bb2012-07-26 15:20:36 +0000775 bool isFine = r.setBoundsCheck(pts, 3);
776 REPORTER_ASSERT(reporter, isFine);
777 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000778
reed@google.com0bb18bb2012-07-26 15:20:36 +0000779 pts[1].set(inf, 0);
780 isFine = r.setBoundsCheck(pts, 3);
781 REPORTER_ASSERT(reporter, !isFine);
782 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000783
reed@google.com0bb18bb2012-07-26 15:20:36 +0000784 pts[1].set(nan, 0);
785 isFine = r.setBoundsCheck(pts, 3);
786 REPORTER_ASSERT(reporter, !isFine);
787 REPORTER_ASSERT(reporter, r.isEmpty());
788}
789
790static void test_path_isfinite(skiatest::Reporter* reporter) {
791 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000792 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000793 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000794
reed@google.com0bb18bb2012-07-26 15:20:36 +0000795 SkPath path;
796 REPORTER_ASSERT(reporter, path.isFinite());
797
798 path.reset();
799 REPORTER_ASSERT(reporter, path.isFinite());
800
801 path.reset();
802 path.moveTo(SK_Scalar1, 0);
803 REPORTER_ASSERT(reporter, path.isFinite());
804
805 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000806 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000807 REPORTER_ASSERT(reporter, !path.isFinite());
808
809 path.reset();
810 path.moveTo(nan, 0);
811 REPORTER_ASSERT(reporter, !path.isFinite());
812}
813
814static void test_isfinite(skiatest::Reporter* reporter) {
815 test_rect_isfinite(reporter);
816 test_path_isfinite(reporter);
817}
818
fsb1475b02016-01-20 09:51:07 -0800819static void test_islastcontourclosed(skiatest::Reporter* reporter) {
820 SkPath path;
821 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
822 path.moveTo(0, 0);
823 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
824 path.close();
825 REPORTER_ASSERT(reporter, path.isLastContourClosed());
826 path.lineTo(100, 100);
827 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
828 path.moveTo(200, 200);
829 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
830 path.close();
831 REPORTER_ASSERT(reporter, path.isLastContourClosed());
832 path.moveTo(0, 0);
833 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
834}
835
reed@google.com744faba2012-05-29 19:54:52 +0000836// assert that we always
837// start with a moveTo
838// only have 1 moveTo
839// only have Lines after that
840// end with a single close
841// only have (at most) 1 close
842//
843static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000844 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000845 SkPath::RawIter iter(path);
846 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000847
848 bool firstTime = true;
849 bool foundClose = false;
850 for (;;) {
851 switch (iter.next(pts)) {
852 case SkPath::kMove_Verb:
853 REPORTER_ASSERT(reporter, firstTime);
854 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
855 srcPts++;
856 firstTime = false;
857 break;
858 case SkPath::kLine_Verb:
859 REPORTER_ASSERT(reporter, !firstTime);
860 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
861 srcPts++;
862 break;
863 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000864 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000865 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000866 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000867 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000868 break;
reed@google.com744faba2012-05-29 19:54:52 +0000869 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000870 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000871 break;
872 case SkPath::kClose_Verb:
873 REPORTER_ASSERT(reporter, !firstTime);
874 REPORTER_ASSERT(reporter, !foundClose);
875 REPORTER_ASSERT(reporter, expectClose);
876 foundClose = true;
877 break;
878 case SkPath::kDone_Verb:
879 goto DONE;
880 }
881 }
882DONE:
883 REPORTER_ASSERT(reporter, foundClose == expectClose);
884}
885
886static void test_addPoly(skiatest::Reporter* reporter) {
887 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000888 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000889
reed@google.com744faba2012-05-29 19:54:52 +0000890 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
891 pts[i].fX = rand.nextSScalar1();
892 pts[i].fY = rand.nextSScalar1();
893 }
894
895 for (int doClose = 0; doClose <= 1; ++doClose) {
896 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
897 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800898 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000899 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000900 }
901 }
902}
903
reed@google.com8b06f1a2012-05-29 12:03:46 +0000904static void test_strokerec(skiatest::Reporter* reporter) {
905 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
906 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000907
reed@google.com8b06f1a2012-05-29 12:03:46 +0000908 rec.setHairlineStyle();
909 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000910
reed@google.com8b06f1a2012-05-29 12:03:46 +0000911 rec.setStrokeStyle(SK_Scalar1, false);
912 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000913
reed@google.com8b06f1a2012-05-29 12:03:46 +0000914 rec.setStrokeStyle(SK_Scalar1, true);
915 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000916
reed@google.com8b06f1a2012-05-29 12:03:46 +0000917 rec.setStrokeStyle(0, false);
918 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000919
reed@google.com8b06f1a2012-05-29 12:03:46 +0000920 rec.setStrokeStyle(0, true);
921 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
922}
923
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000924// Set this for paths that don't have a consistent direction such as a bowtie.
925// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700926const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000927
928static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -0700929 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000930 if (expected == kDontCheckDir) {
931 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000932 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000933 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
934
reed026beb52015-06-10 14:23:15 -0700935 SkPathPriv::FirstDirection dir;
936 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000937 REPORTER_ASSERT(reporter, dir == expected);
938 } else {
reed026beb52015-06-10 14:23:15 -0700939 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000940 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000941}
942
reed@google.com3e71a882012-01-10 18:44:37 +0000943static void test_direction(skiatest::Reporter* reporter) {
944 size_t i;
945 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -0700946 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -0700947 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
948 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
949 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +0000950
951 static const char* gDegen[] = {
952 "M 10 10",
953 "M 10 10 M 20 20",
954 "M 10 10 L 20 20",
955 "M 10 10 L 10 10 L 10 10",
956 "M 10 10 Q 10 10 10 10",
957 "M 10 10 C 10 10 10 10 10 10",
958 };
959 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
960 path.reset();
961 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
962 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -0700963 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +0000964 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000965
reed@google.com3e71a882012-01-10 18:44:37 +0000966 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000967 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000968 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000969 "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 +0000970 // rect with top two corners replaced by cubics with identical middle
971 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000972 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
973 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000974 };
975 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
976 path.reset();
977 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
978 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700979 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000980 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000981
reed@google.com3e71a882012-01-10 18:44:37 +0000982 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000983 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000984 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000985 "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 +0000986 // rect with top two corners replaced by cubics with identical middle
987 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000988 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
989 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000990 };
991 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
992 path.reset();
993 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
994 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700995 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000996 }
reed@google.comac8543f2012-01-30 20:51:25 +0000997
998 // Test two donuts, each wound a different direction. Only the outer contour
999 // determines the cheap direction
1000 path.reset();
1001 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1002 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001003 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001004
reed@google.comac8543f2012-01-30 20:51:25 +00001005 path.reset();
1006 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1007 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001008 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001009
1010 // triangle with one point really far from the origin.
1011 path.reset();
1012 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001013 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001014 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1015 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001016 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001017
1018 path.reset();
1019 path.conicTo(20, 0, 20, 20, 0.5f);
1020 path.close();
reed026beb52015-06-10 14:23:15 -07001021 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001022
1023 path.reset();
1024 path.lineTo(1, 1e7f);
1025 path.lineTo(1e7f, 2e7f);
1026 path.close();
1027 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001028 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001029}
1030
reed@google.comffdb0182011-11-14 19:29:14 +00001031static void add_rect(SkPath* path, const SkRect& r) {
1032 path->moveTo(r.fLeft, r.fTop);
1033 path->lineTo(r.fRight, r.fTop);
1034 path->lineTo(r.fRight, r.fBottom);
1035 path->lineTo(r.fLeft, r.fBottom);
1036 path->close();
1037}
1038
1039static void test_bounds(skiatest::Reporter* reporter) {
1040 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001041 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1042 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1043 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1044 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001045 };
1046
1047 SkPath path0, path1;
1048 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1049 path0.addRect(rects[i]);
1050 add_rect(&path1, rects[i]);
1051 }
1052
1053 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1054}
1055
reed@google.com55b5f4b2011-09-07 12:23:41 +00001056static void stroke_cubic(const SkPoint pts[4]) {
1057 SkPath path;
1058 path.moveTo(pts[0]);
1059 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001060
reed@google.com55b5f4b2011-09-07 12:23:41 +00001061 SkPaint paint;
1062 paint.setStyle(SkPaint::kStroke_Style);
1063 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001064
reed@google.com55b5f4b2011-09-07 12:23:41 +00001065 SkPath fill;
1066 paint.getFillPath(path, &fill);
1067}
1068
1069// just ensure this can run w/o any SkASSERTS firing in the debug build
1070// we used to assert due to differences in how we determine a degenerate vector
1071// but that was fixed with the introduction of SkPoint::CanNormalize
1072static void stroke_tiny_cubic() {
1073 SkPoint p0[] = {
1074 { 372.0f, 92.0f },
1075 { 372.0f, 92.0f },
1076 { 372.0f, 92.0f },
1077 { 372.0f, 92.0f },
1078 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001079
reed@google.com55b5f4b2011-09-07 12:23:41 +00001080 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001081
reed@google.com55b5f4b2011-09-07 12:23:41 +00001082 SkPoint p1[] = {
1083 { 372.0f, 92.0f },
1084 { 372.0007f, 92.000755f },
1085 { 371.99927f, 92.003922f },
1086 { 371.99826f, 92.003899f },
1087 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001088
reed@google.com55b5f4b2011-09-07 12:23:41 +00001089 stroke_cubic(p1);
1090}
1091
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001092static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1093 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001094 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001095 SkPoint mv;
1096 SkPoint pts[4];
1097 SkPath::Verb v;
1098 int nMT = 0;
1099 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001100 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001101 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1102 switch (v) {
1103 case SkPath::kMove_Verb:
1104 mv = pts[0];
1105 ++nMT;
1106 break;
1107 case SkPath::kClose_Verb:
1108 REPORTER_ASSERT(reporter, mv == pts[0]);
1109 ++nCL;
1110 break;
1111 default:
1112 break;
1113 }
1114 }
1115 // if we force a close on the interator we should have a close
1116 // for every moveTo
1117 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1118 }
1119}
1120
1121static void test_close(skiatest::Reporter* reporter) {
1122 SkPath closePt;
1123 closePt.moveTo(0, 0);
1124 closePt.close();
1125 check_close(reporter, closePt);
1126
1127 SkPath openPt;
1128 openPt.moveTo(0, 0);
1129 check_close(reporter, openPt);
1130
1131 SkPath empty;
1132 check_close(reporter, empty);
1133 empty.close();
1134 check_close(reporter, empty);
1135
1136 SkPath rect;
1137 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1138 check_close(reporter, rect);
1139 rect.close();
1140 check_close(reporter, rect);
1141
1142 SkPath quad;
1143 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1144 check_close(reporter, quad);
1145 quad.close();
1146 check_close(reporter, quad);
1147
1148 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001149 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001150 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1151 check_close(reporter, cubic);
1152 cubic.close();
1153 check_close(reporter, cubic);
1154
1155 SkPath line;
1156 line.moveTo(SK_Scalar1, SK_Scalar1);
1157 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1158 check_close(reporter, line);
1159 line.close();
1160 check_close(reporter, line);
1161
1162 SkPath rect2;
1163 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1164 rect2.close();
1165 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1166 check_close(reporter, rect2);
1167 rect2.close();
1168 check_close(reporter, rect2);
1169
1170 SkPath oval3;
1171 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1172 oval3.close();
1173 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1174 check_close(reporter, oval3);
1175 oval3.close();
1176 check_close(reporter, oval3);
1177
1178 SkPath moves;
1179 moves.moveTo(SK_Scalar1, SK_Scalar1);
1180 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1181 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1182 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1183 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001184
1185 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001186}
1187
reed@google.com7c424812011-05-15 04:38:34 +00001188static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1189 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001190 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1191 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001192 REPORTER_ASSERT(reporter, c == expected);
1193}
1194
caryclarkb4216502015-03-02 13:02:34 -08001195static void test_path_crbug389050(skiatest::Reporter* reporter) {
1196 SkPath tinyConvexPolygon;
1197 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1198 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1199 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1200 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1201 tinyConvexPolygon.close();
1202 tinyConvexPolygon.getConvexity();
1203 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001204 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001205
1206 SkPath platTriangle;
1207 platTriangle.moveTo(0, 0);
1208 platTriangle.lineTo(200, 0);
1209 platTriangle.lineTo(100, 0.04f);
1210 platTriangle.close();
1211 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001212 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001213
1214 platTriangle.reset();
1215 platTriangle.moveTo(0, 0);
1216 platTriangle.lineTo(200, 0);
1217 platTriangle.lineTo(100, 0.03f);
1218 platTriangle.close();
1219 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001220 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001221}
1222
reed@google.com7c424812011-05-15 04:38:34 +00001223static void test_convexity2(skiatest::Reporter* reporter) {
1224 SkPath pt;
1225 pt.moveTo(0, 0);
1226 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001227 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001228 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001229
reed@google.com7c424812011-05-15 04:38:34 +00001230 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001231 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1232 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001233 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001234 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001235 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001236
reed@google.com7c424812011-05-15 04:38:34 +00001237 SkPath triLeft;
1238 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001239 triLeft.lineTo(SK_Scalar1, 0);
1240 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001241 triLeft.close();
1242 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001243 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001244
reed@google.com7c424812011-05-15 04:38:34 +00001245 SkPath triRight;
1246 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001247 triRight.lineTo(-SK_Scalar1, 0);
1248 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001249 triRight.close();
1250 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001251 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001252
reed@google.com7c424812011-05-15 04:38:34 +00001253 SkPath square;
1254 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001255 square.lineTo(SK_Scalar1, 0);
1256 square.lineTo(SK_Scalar1, SK_Scalar1);
1257 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001258 square.close();
1259 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001260 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001261
reed@google.com7c424812011-05-15 04:38:34 +00001262 SkPath redundantSquare;
1263 redundantSquare.moveTo(0, 0);
1264 redundantSquare.lineTo(0, 0);
1265 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001266 redundantSquare.lineTo(SK_Scalar1, 0);
1267 redundantSquare.lineTo(SK_Scalar1, 0);
1268 redundantSquare.lineTo(SK_Scalar1, 0);
1269 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1270 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1271 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1272 redundantSquare.lineTo(0, SK_Scalar1);
1273 redundantSquare.lineTo(0, SK_Scalar1);
1274 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001275 redundantSquare.close();
1276 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001277 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001278
reed@google.com7c424812011-05-15 04:38:34 +00001279 SkPath bowTie;
1280 bowTie.moveTo(0, 0);
1281 bowTie.lineTo(0, 0);
1282 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001283 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1284 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1285 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1286 bowTie.lineTo(SK_Scalar1, 0);
1287 bowTie.lineTo(SK_Scalar1, 0);
1288 bowTie.lineTo(SK_Scalar1, 0);
1289 bowTie.lineTo(0, SK_Scalar1);
1290 bowTie.lineTo(0, SK_Scalar1);
1291 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001292 bowTie.close();
1293 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001294 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001295
reed@google.com7c424812011-05-15 04:38:34 +00001296 SkPath spiral;
1297 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001298 spiral.lineTo(100*SK_Scalar1, 0);
1299 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1300 spiral.lineTo(0, 100*SK_Scalar1);
1301 spiral.lineTo(0, 50*SK_Scalar1);
1302 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1303 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001304 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001305 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001306 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001307
reed@google.com7c424812011-05-15 04:38:34 +00001308 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001309 dent.moveTo(0, 0);
1310 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1311 dent.lineTo(0, 100*SK_Scalar1);
1312 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1313 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001314 dent.close();
1315 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001316 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001317
halcanary6950de62015-11-07 05:29:00 -08001318 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001319 SkPath strokedSin;
1320 for (int i = 0; i < 2000; i++) {
1321 SkScalar x = SkIntToScalar(i) / 2;
1322 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1323 if (0 == i) {
1324 strokedSin.moveTo(x, y);
1325 } else {
1326 strokedSin.lineTo(x, y);
1327 }
1328 }
1329 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1330 stroke.setStrokeStyle(2 * SK_Scalar1);
1331 stroke.applyToPath(&strokedSin, strokedSin);
1332 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1333 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001334
1335 // http://crbug.com/412640
1336 SkPath degenerateConcave;
1337 degenerateConcave.moveTo(148.67912f, 191.875f);
1338 degenerateConcave.lineTo(470.37695f, 7.5f);
1339 degenerateConcave.lineTo(148.67912f, 191.875f);
1340 degenerateConcave.lineTo(41.446522f, 376.25f);
1341 degenerateConcave.lineTo(-55.971577f, 460.0f);
1342 degenerateConcave.lineTo(41.446522f, 376.25f);
1343 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001344 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001345
1346 // http://crbug.com/433683
1347 SkPath badFirstVector;
1348 badFirstVector.moveTo(501.087708f, 319.610352f);
1349 badFirstVector.lineTo(501.087708f, 319.610352f);
1350 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1351 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1352 badFirstVector.lineTo(301.557678f, 98.044601f);
1353 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1354 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1355 badFirstVector.lineTo(504.912292f, 316.389648f);
1356 badFirstVector.lineTo(501.087708f, 319.610352f);
1357 badFirstVector.close();
1358 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001359}
1360
reed@android.com6b82d1a2009-06-03 02:35:01 +00001361static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1362 const SkRect& bounds) {
1363 REPORTER_ASSERT(reporter, p.isConvex());
1364 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001365
reed@android.com6b82d1a2009-06-03 02:35:01 +00001366 SkPath p2(p);
1367 REPORTER_ASSERT(reporter, p2.isConvex());
1368 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1369
1370 SkPath other;
1371 other.swap(p2);
1372 REPORTER_ASSERT(reporter, other.isConvex());
1373 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1374}
1375
reed@google.com04863fa2011-05-15 04:08:24 +00001376static void setFromString(SkPath* path, const char str[]) {
1377 bool first = true;
1378 while (str) {
1379 SkScalar x, y;
1380 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001381 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001382 break;
1383 }
1384 str = SkParse::FindScalar(str, &y);
1385 SkASSERT(str);
1386 if (first) {
1387 path->moveTo(x, y);
1388 first = false;
1389 } else {
1390 path->lineTo(x, y);
1391 }
1392 }
1393}
1394
1395static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001396 SkPath path;
1397
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001398 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001399 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001400 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001401 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001402 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1403
reed@google.com04863fa2011-05-15 04:08:24 +00001404 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001405 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001406 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001407 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001408
reed@google.com04863fa2011-05-15 04:08:24 +00001409 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001410 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001411 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001412 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001413
reed@google.com04863fa2011-05-15 04:08:24 +00001414 static const struct {
reed026beb52015-06-10 14:23:15 -07001415 const char* fPathStr;
1416 SkPath::Convexity fExpectedConvexity;
1417 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001418 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001419 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1420 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1421 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1422 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1423 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1424 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001425 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001426 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001427 };
1428
1429 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1430 SkPath path;
1431 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001432 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1433 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001434 // check after setting the initial convex and direction
1435 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1436 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001437 SkPathPriv::FirstDirection dir;
1438 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1439 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001440 ^ foundDir);
1441 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1442 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1443 }
1444 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1445 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001446 }
caryclarkd3d1a982014-12-08 04:57:38 -08001447
1448 static const SkPoint nonFinitePts[] = {
1449 { SK_ScalarInfinity, 0 },
1450 { 0, SK_ScalarInfinity },
1451 { SK_ScalarInfinity, SK_ScalarInfinity },
1452 { SK_ScalarNegativeInfinity, 0},
1453 { 0, SK_ScalarNegativeInfinity },
1454 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1455 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1456 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1457 { SK_ScalarNaN, 0 },
1458 { 0, SK_ScalarNaN },
1459 { SK_ScalarNaN, SK_ScalarNaN },
1460 };
1461
1462 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1463
1464 static const SkPoint finitePts[] = {
1465 { SK_ScalarMax, 0 },
1466 { 0, SK_ScalarMax },
1467 { SK_ScalarMax, SK_ScalarMax },
1468 { SK_ScalarMin, 0 },
1469 { 0, SK_ScalarMin },
1470 { SK_ScalarMin, SK_ScalarMin },
1471 };
1472
1473 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1474
1475 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1476 int i = (int) (index % nonFinitePtsCount);
1477 int f = (int) (index % finitePtsCount);
1478 int g = (int) ((f + 1) % finitePtsCount);
1479 path.reset();
1480 switch (index % 13) {
1481 case 0: path.lineTo(nonFinitePts[i]); break;
1482 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1483 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1484 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1485 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1486 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1487 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1488 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1489 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1490 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1491 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1492 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1493 case 12: path.moveTo(nonFinitePts[i]); break;
1494 }
1495 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1496 }
1497
1498 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1499 int f = (int) (index % finitePtsCount);
1500 int g = (int) ((f + 1) % finitePtsCount);
1501 path.reset();
1502 int curveSelect = index % 11;
1503 switch (curveSelect) {
1504 case 0: path.moveTo(finitePts[f]); break;
1505 case 1: path.lineTo(finitePts[f]); break;
1506 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1507 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1508 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1509 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1510 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1511 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1512 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1513 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1514 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1515 }
1516 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1517 : SkPath::kUnknown_Convexity);
1518 }
1519
reed@google.com04863fa2011-05-15 04:08:24 +00001520}
1521
reed@google.com7e6c4d12012-05-10 14:05:43 +00001522static void test_isLine(skiatest::Reporter* reporter) {
1523 SkPath path;
1524 SkPoint pts[2];
1525 const SkScalar value = SkIntToScalar(5);
1526
halcanary96fcdcc2015-08-27 07:41:13 -07001527 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001528
reed@google.com7e6c4d12012-05-10 14:05:43 +00001529 // set some non-zero values
1530 pts[0].set(value, value);
1531 pts[1].set(value, value);
1532 REPORTER_ASSERT(reporter, !path.isLine(pts));
1533 // check that pts was untouched
1534 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1535 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1536
1537 const SkScalar moveX = SkIntToScalar(1);
1538 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001539 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001540
1541 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001542 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001543 REPORTER_ASSERT(reporter, !path.isLine(pts));
1544 // check that pts was untouched
1545 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1546 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1547
1548 const SkScalar lineX = SkIntToScalar(2);
1549 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001550 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001551
1552 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001553 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001554
1555 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1556 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1557 REPORTER_ASSERT(reporter, path.isLine(pts));
1558 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1559 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1560
1561 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001562 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001563 REPORTER_ASSERT(reporter, !path.isLine(pts));
1564 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1565 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001566
1567 path.reset();
1568 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001569 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001570}
1571
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001572static void test_conservativelyContains(skiatest::Reporter* reporter) {
1573 SkPath path;
1574
1575 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1576 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1577
1578 // A circle that bounds kBaseRect (with a significant amount of slop)
1579 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001580 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001581 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1582
1583 // round-rect radii
1584 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001585
caryclark@google.com56f233a2012-11-19 13:06:06 +00001586 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001587 SkRect fQueryRect;
1588 bool fInRect;
1589 bool fInCircle;
1590 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001591 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001592 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001593 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001594
1595 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001596 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1597 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1598 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1599 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001600 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001601
1602 // rects with edges off by one from kBaseRect's edges
1603 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1604 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001605 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001606 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1607 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001608 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001609 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1610 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001611 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001612 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1613 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001614 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001615 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1616 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001617 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001618 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1619 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001620 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001621 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1622 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001623 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001624
1625 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001626 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1627 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1628 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1629 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001630
1631 // far away rect
1632 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1633 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001634 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001635
1636 // very large rect containing kBaseRect
1637 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1638 kBaseRect.fTop - 5 * kBaseRect.height(),
1639 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001640 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001641
1642 // skinny rect that spans same y-range as kBaseRect
1643 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1644 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001645 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001646
1647 // short rect that spans same x-range as kBaseRect
1648 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001649 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001650
1651 // skinny rect that spans slightly larger y-range than kBaseRect
1652 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1653 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001654 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001655
1656 // short rect that spans slightly larger x-range than kBaseRect
1657 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1658 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001659 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001660 };
1661
1662 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001663 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001664 SkRect qRect = kQueries[q].fQueryRect;
1665 if (inv & 0x1) {
1666 SkTSwap(qRect.fLeft, qRect.fRight);
1667 }
1668 if (inv & 0x2) {
1669 SkTSwap(qRect.fTop, qRect.fBottom);
1670 }
1671 for (int d = 0; d < 2; ++d) {
1672 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1673 path.reset();
1674 path.addRect(kBaseRect, dir);
1675 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1676 path.conservativelyContainsRect(qRect));
1677
1678 path.reset();
1679 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1680 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1681 path.conservativelyContainsRect(qRect));
1682
1683 path.reset();
1684 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1685 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1686 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001687
1688 path.reset();
1689 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1690 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1691 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1692 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1693 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1694 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1695 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1696 path.close();
1697 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1698 path.conservativelyContainsRect(qRect));
1699
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001700 }
1701 // Slightly non-convex shape, shouldn't contain any rects.
1702 path.reset();
1703 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001704 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001705 path.lineTo(SkIntToScalar(100), 0);
1706 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1707 path.lineTo(0, SkIntToScalar(100));
1708 path.close();
1709 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1710 }
1711 }
1712
1713 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1714 path.reset();
1715 path.moveTo(0, 0);
1716 path.lineTo(SkIntToScalar(100), 0);
1717 path.lineTo(0, SkIntToScalar(100));
1718
1719 // inside, on along top edge
1720 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1721 SkIntToScalar(10),
1722 SkIntToScalar(10))));
1723 // above
1724 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1725 SkRect::MakeXYWH(SkIntToScalar(50),
1726 SkIntToScalar(-10),
1727 SkIntToScalar(10),
1728 SkIntToScalar(10))));
1729 // to the left
1730 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1731 SkIntToScalar(5),
1732 SkIntToScalar(5),
1733 SkIntToScalar(5))));
1734
1735 // outside the diagonal edge
1736 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1737 SkIntToScalar(200),
1738 SkIntToScalar(20),
1739 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001740
bsalomonb17c1292014-08-28 14:04:55 -07001741
1742 // Test that multiple move commands do not cause asserts.
1743
1744 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001745 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001746 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1747#if 0
1748 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1749 SkIntToScalar(10),
1750 SkIntToScalar(10))));
1751#endif
1752
1753 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001754 path.reset();
1755 path.moveTo(100, 100);
1756 path.moveTo(0, 0);
1757 path.lineTo(SkIntToScalar(100), 0);
1758 path.lineTo(0, SkIntToScalar(100));
1759
1760 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1761 SkIntToScalar(10),
1762 SkIntToScalar(10))));
1763
bsalomonb17c1292014-08-28 14:04:55 -07001764 // Test that multiple move commands do not cause asserts and that the function
1765 // is not confused by the multiple moves.
1766 path.reset();
1767 path.moveTo(0, 0);
1768 path.lineTo(SkIntToScalar(100), 0);
1769 path.lineTo(0, SkIntToScalar(100));
1770 path.moveTo(0, SkIntToScalar(200));
1771 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1772 path.lineTo(0, SkIntToScalar(300));
1773
1774 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1775 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1776 SkIntToScalar(10),
1777 SkIntToScalar(10))));
1778
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001779 path.reset();
1780 path.lineTo(100, 100);
1781 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001782}
1783
reed@google.comf32322b2013-10-16 15:14:04 +00001784static void test_isRect_open_close(skiatest::Reporter* reporter) {
1785 SkPath path;
1786 bool isClosed;
1787
1788 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001789 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001790
halcanary96fcdcc2015-08-27 07:41:13 -07001791 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001792 REPORTER_ASSERT(reporter, isClosed);
1793}
1794
caryclark@google.comf1316942011-07-26 19:54:45 +00001795// Simple isRect test is inline TestPath, below.
1796// test_isRect provides more extensive testing.
1797static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001798 test_isRect_open_close(reporter);
1799
caryclark@google.comf1316942011-07-26 19:54:45 +00001800 // passing tests (all moveTo / lineTo...
1801 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1802 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1803 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1804 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1805 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1806 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1807 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1808 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1809 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001810 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1811 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 +00001812 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1813 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1814 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001815 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001816
caryclark@google.comf1316942011-07-26 19:54:45 +00001817 // failing tests
1818 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1819 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1820 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1821 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1822 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1823 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1824 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1825 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001826 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1827 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1828 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001829
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001830 // no close, but we should detect them as fillably the same as a rect
1831 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1832 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1833 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1834
1835 // like c2, but we double-back on ourselves
1836 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1837 // like c2, but we overshoot the start point
1838 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1839 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001840
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001841 struct IsRectTest {
1842 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001843 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001844 bool fClose;
1845 bool fIsRect;
1846 } tests[] = {
1847 { r1, SK_ARRAY_COUNT(r1), true, true },
1848 { r2, SK_ARRAY_COUNT(r2), true, true },
1849 { r3, SK_ARRAY_COUNT(r3), true, true },
1850 { r4, SK_ARRAY_COUNT(r4), true, true },
1851 { r5, SK_ARRAY_COUNT(r5), true, true },
1852 { r6, SK_ARRAY_COUNT(r6), true, true },
1853 { r7, SK_ARRAY_COUNT(r7), true, true },
1854 { r8, SK_ARRAY_COUNT(r8), true, true },
1855 { r9, SK_ARRAY_COUNT(r9), true, true },
1856 { ra, SK_ARRAY_COUNT(ra), true, true },
1857 { rb, SK_ARRAY_COUNT(rb), true, true },
1858 { rc, SK_ARRAY_COUNT(rc), true, true },
1859 { rd, SK_ARRAY_COUNT(rd), true, true },
1860 { re, SK_ARRAY_COUNT(re), true, true },
1861 { rf, SK_ARRAY_COUNT(rf), true, true },
1862
1863 { f1, SK_ARRAY_COUNT(f1), true, false },
1864 { f2, SK_ARRAY_COUNT(f2), true, false },
1865 { f3, SK_ARRAY_COUNT(f3), true, false },
1866 { f4, SK_ARRAY_COUNT(f4), true, false },
1867 { f5, SK_ARRAY_COUNT(f5), true, false },
1868 { f6, SK_ARRAY_COUNT(f6), true, false },
1869 { f7, SK_ARRAY_COUNT(f7), true, false },
1870 { f8, SK_ARRAY_COUNT(f8), true, false },
1871 { f9, SK_ARRAY_COUNT(f9), true, false },
1872 { fa, SK_ARRAY_COUNT(fa), true, false },
1873 { fb, SK_ARRAY_COUNT(fb), true, false },
1874
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001875 { c1, SK_ARRAY_COUNT(c1), false, true },
1876 { c2, SK_ARRAY_COUNT(c2), false, true },
1877 { c3, SK_ARRAY_COUNT(c3), false, true },
1878
1879 { d1, SK_ARRAY_COUNT(d1), false, false },
1880 { d2, SK_ARRAY_COUNT(d2), false, false },
1881 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001882 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001883
1884 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001885 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001886 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1887 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001888 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1889 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1890 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001891 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001892 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001893 path.close();
1894 }
halcanary96fcdcc2015-08-27 07:41:13 -07001895 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001896
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001897 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001898 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001899 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001900 SkPath::Direction direction;
1901 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001902 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001903 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001904 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1905 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001906 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001907 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001908 } else {
1909 SkRect computed;
1910 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001911 bool isClosed = (bool)-1;
1912 SkPath::Direction direction = (SkPath::Direction) - 1;
1913 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001914 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1915 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001916 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1917 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001918 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001919 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001920
caryclark@google.comf1316942011-07-26 19:54:45 +00001921 // fail, close then line
1922 SkPath path1;
1923 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001924 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001925 path1.lineTo(r1[index].fX, r1[index].fY);
1926 }
1927 path1.close();
1928 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001929 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001930
caryclark@google.comf1316942011-07-26 19:54:45 +00001931 // fail, move in the middle
1932 path1.reset();
1933 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001934 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001935 if (index == 2) {
1936 path1.moveTo(1, .5f);
1937 }
1938 path1.lineTo(r1[index].fX, r1[index].fY);
1939 }
1940 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001941 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001942
1943 // fail, move on the edge
1944 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08001945 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001946 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1947 path1.lineTo(r1[index].fX, r1[index].fY);
1948 }
1949 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001950 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001951
caryclark@google.comf1316942011-07-26 19:54:45 +00001952 // fail, quad
1953 path1.reset();
1954 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001955 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001956 if (index == 2) {
1957 path1.quadTo(1, .5f, 1, .5f);
1958 }
1959 path1.lineTo(r1[index].fX, r1[index].fY);
1960 }
1961 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001962 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001963
caryclark@google.comf1316942011-07-26 19:54:45 +00001964 // fail, cubic
1965 path1.reset();
1966 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001967 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001968 if (index == 2) {
1969 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1970 }
1971 path1.lineTo(r1[index].fX, r1[index].fY);
1972 }
1973 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001974 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001975}
1976
caryclark95bc5f32015-04-08 08:34:15 -07001977static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001978 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001979 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001980 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1981 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1982 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001983 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001984 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1985 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1986 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1987 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001988 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1989 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 +00001990 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1991 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1992 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001993
1994 // failing tests
1995 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1996 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1997 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1998 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1999 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2000 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2001 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2002 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2003
caryclark95bc5f32015-04-08 08:34:15 -07002004 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002005 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2006 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2007
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002008 struct IsNestedRectTest {
2009 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002010 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002011 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002012 bool fClose;
2013 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2014 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002015 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2016 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2017 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2018 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2019 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2020 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2021 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2022 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2023 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2024 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2025 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2026 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2027 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2028 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002029
reed026beb52015-06-10 14:23:15 -07002030 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2031 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2032 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2033 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2034 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2035 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2036 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2037 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002038
reed026beb52015-06-10 14:23:15 -07002039 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2040 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002041 };
2042
2043 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002044 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002045 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002046 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2047 SkPath path;
2048 if (rectFirst) {
2049 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2050 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002051 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2052 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2053 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002054 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002055 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002056 path.close();
2057 }
2058 if (!rectFirst) {
2059 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2060 }
caryclark95bc5f32015-04-08 08:34:15 -07002061 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002062 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002063 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002064 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002065 SkPathPriv::FirstDirection expectedDirs[2];
2066 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002067 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002068 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002069 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2070 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002071 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002072 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002073 } else {
reed026beb52015-06-10 14:23:15 -07002074 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002075 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002076 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002077 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002078 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2079 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002080 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2081 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002082 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002083 }
2084
2085 // fail, close then line
2086 SkPath path1;
2087 if (rectFirst) {
2088 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2089 }
2090 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002091 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002092 path1.lineTo(r1[index].fX, r1[index].fY);
2093 }
2094 path1.close();
2095 path1.lineTo(1, 0);
2096 if (!rectFirst) {
2097 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2098 }
halcanary96fcdcc2015-08-27 07:41:13 -07002099 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002100
2101 // fail, move in the middle
2102 path1.reset();
2103 if (rectFirst) {
2104 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2105 }
2106 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002107 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002108 if (index == 2) {
2109 path1.moveTo(1, .5f);
2110 }
2111 path1.lineTo(r1[index].fX, r1[index].fY);
2112 }
2113 path1.close();
2114 if (!rectFirst) {
2115 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2116 }
halcanary96fcdcc2015-08-27 07:41:13 -07002117 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002118
2119 // fail, move on the edge
2120 path1.reset();
2121 if (rectFirst) {
2122 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2123 }
bsalomon98806072014-12-12 15:11:17 -08002124 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002125 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2126 path1.lineTo(r1[index].fX, r1[index].fY);
2127 }
2128 path1.close();
2129 if (!rectFirst) {
2130 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2131 }
halcanary96fcdcc2015-08-27 07:41:13 -07002132 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002133
2134 // fail, quad
2135 path1.reset();
2136 if (rectFirst) {
2137 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2138 }
2139 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002140 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002141 if (index == 2) {
2142 path1.quadTo(1, .5f, 1, .5f);
2143 }
2144 path1.lineTo(r1[index].fX, r1[index].fY);
2145 }
2146 path1.close();
2147 if (!rectFirst) {
2148 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2149 }
halcanary96fcdcc2015-08-27 07:41:13 -07002150 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002151
2152 // fail, cubic
2153 path1.reset();
2154 if (rectFirst) {
2155 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2156 }
2157 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002158 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002159 if (index == 2) {
2160 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2161 }
2162 path1.lineTo(r1[index].fX, r1[index].fY);
2163 }
2164 path1.close();
2165 if (!rectFirst) {
2166 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2167 }
halcanary96fcdcc2015-08-27 07:41:13 -07002168 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002169
caryclark@google.com56f233a2012-11-19 13:06:06 +00002170 // fail, not nested
2171 path1.reset();
2172 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2173 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002174 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002175 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002176
caryclark95bc5f32015-04-08 08:34:15 -07002177 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2178 SkPath path;
2179 path.moveTo(0, 0);
2180 path.lineTo(10, 0);
2181 path.lineTo(10, 10);
2182 path.lineTo(0, 10);
2183 path.lineTo(0, 0);
2184 path.moveTo(1, 1);
2185 path.lineTo(9, 1);
2186 path.lineTo(9, 9);
2187 path.lineTo(1, 9);
2188 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002189 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002190
caryclark@google.combfe90372012-11-21 13:56:20 +00002191 // pass, stroke rect
2192 SkPath src, dst;
2193 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2194 SkPaint strokePaint;
2195 strokePaint.setStyle(SkPaint::kStroke_Style);
2196 strokePaint.setStrokeWidth(2);
2197 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002198 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002199}
2200
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002201static void write_and_read_back(skiatest::Reporter* reporter,
2202 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002203 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002204 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002205 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002206 SkAutoMalloc storage(size);
2207 writer.flatten(storage.get());
2208 SkReader32 reader(storage.get(), size);
2209
2210 SkPath readBack;
2211 REPORTER_ASSERT(reporter, readBack != p);
2212 reader.readPath(&readBack);
2213 REPORTER_ASSERT(reporter, readBack == p);
2214
rmistry@google.comd6176b02012-08-23 18:14:13 +00002215 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002216 p.getConvexityOrUnknown());
2217
halcanary96fcdcc2015-08-27 07:41:13 -07002218 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002219
2220 const SkRect& origBounds = p.getBounds();
2221 const SkRect& readBackBounds = readBack.getBounds();
2222
2223 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2224}
2225
reed@google.com53effc52011-09-21 19:05:12 +00002226static void test_flattening(skiatest::Reporter* reporter) {
2227 SkPath p;
2228
2229 static const SkPoint pts[] = {
2230 { 0, 0 },
2231 { SkIntToScalar(10), SkIntToScalar(10) },
2232 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2233 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2234 };
2235 p.moveTo(pts[0]);
2236 p.lineTo(pts[1]);
2237 p.quadTo(pts[2], pts[3]);
2238 p.cubicTo(pts[4], pts[5], pts[6]);
2239
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002240 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002241
2242 // create a buffer that should be much larger than the path so we don't
2243 // kill our stack if writer goes too far.
2244 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002245 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002246 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002247 REPORTER_ASSERT(reporter, size1 == size2);
2248
2249 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002250 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002251 REPORTER_ASSERT(reporter, size1 == size3);
2252 REPORTER_ASSERT(reporter, p == p2);
2253
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002254 size3 = p2.readFromMemory(buffer, 0);
2255 REPORTER_ASSERT(reporter, !size3);
2256
2257 SkPath tooShort;
2258 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2259 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2260
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002261 char buffer2[1024];
2262 size3 = p2.writeToMemory(buffer2);
2263 REPORTER_ASSERT(reporter, size1 == size3);
2264 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002265
2266 // test persistence of the oval flag & convexity
2267 {
2268 SkPath oval;
2269 SkRect rect = SkRect::MakeWH(10, 10);
2270 oval.addOval(rect);
2271
2272 write_and_read_back(reporter, oval);
2273 }
reed@google.com53effc52011-09-21 19:05:12 +00002274}
2275
2276static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002277 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002278
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002279#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002280 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002281 { 0, 0 }, // move
2282 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2283 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2284 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2285#if CONIC_PERSPECTIVE_BUG_FIXED
2286 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2287#endif
reed@google.com53effc52011-09-21 19:05:12 +00002288 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002289 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002290
reed@google.com53effc52011-09-21 19:05:12 +00002291 p.moveTo(pts[0]);
2292 p.lineTo(pts[1]);
2293 p.quadTo(pts[2], pts[3]);
2294 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002295#if CONIC_PERSPECTIVE_BUG_FIXED
2296 p.conicTo(pts[4], pts[5], 0.5f);
2297#endif
2298 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002299
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002300 {
2301 SkMatrix matrix;
2302 matrix.reset();
2303 SkPath p1;
2304 p.transform(matrix, &p1);
2305 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002306 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002307
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002308
2309 {
2310 SkMatrix matrix;
2311 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2312
2313 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2314
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002315 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002316 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002317 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002318 REPORTER_ASSERT(reporter, kPtCount == count);
2319 for (int i = 0; i < count; ++i) {
2320 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2321 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2322 }
2323 }
2324
2325 {
2326 SkMatrix matrix;
2327 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002328 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002329
2330 SkPath p1;
2331 p1.moveTo(SkPoint::Make(0, 0));
2332
2333 p.transform(matrix, &p1);
2334 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002335 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002336 SkRect pBounds = p.getBounds();
2337 SkRect p1Bounds = p1.getBounds();
2338 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2339 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2340 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2341 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2342 }
2343
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002344 p.reset();
2345 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002346
2347 {
2348 SkMatrix matrix;
2349 matrix.reset();
2350 SkPath p1;
2351 p1.moveTo(SkPoint::Make(0, 0));
2352
2353 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002354 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002355 }
2356
2357
2358 {
2359 SkMatrix matrix;
2360 matrix.reset();
2361 matrix.setScaleX(-1);
2362 SkPath p1;
2363 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2364
2365 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002366 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002367 }
2368
2369 {
2370 SkMatrix matrix;
2371 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2372 SkPath p1;
2373 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2374
2375 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002376 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002377 }
reed@google.com53effc52011-09-21 19:05:12 +00002378}
2379
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002380static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002381 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002382 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002383
caryclark@google.com56f233a2012-11-19 13:06:06 +00002384 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002385 const char* testPath;
2386 const size_t numResultPts;
2387 const SkRect resultBound;
2388 const SkPath::Verb* resultVerbs;
2389 const size_t numResultVerbs;
2390 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002391
schenney@chromium.org7e963602012-06-13 17:05:43 +00002392 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2393 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2394 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2395 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2396 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2397 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2398 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2399 static const SkPath::Verb resultVerbs8[] = {
2400 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2401 };
2402 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2403 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2404 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2405 static const SkPath::Verb resultVerbs12[] = {
2406 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2407 };
2408 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2409 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2410 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2411 static const SkPath::Verb resultVerbs16[] = {
2412 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2413 };
2414 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002415 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002416 { "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 -07002417 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002418 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2419 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2420 { "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) },
2421 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2422 { "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) },
2423 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2424 { "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) },
2425 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2426 { "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) },
2427 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2428 { "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 +00002429 SK_ARRAY_COUNT(resultVerbs14)
2430 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002431 { "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) },
2432 { "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 +00002433 SK_ARRAY_COUNT(resultVerbs16)
2434 }
2435 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002436
schenney@chromium.org7e963602012-06-13 17:05:43 +00002437 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2438 p.reset();
2439 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2440 REPORTER_ASSERT(reporter, valid);
2441 REPORTER_ASSERT(reporter, !p.isEmpty());
2442 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2443 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2444 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2445 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2446 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2447 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002448 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002449}
2450
2451struct SegmentInfo {
2452 SkPath fPath;
2453 int fPointCount;
2454};
2455
reed@google.com10296cc2011-09-21 12:29:05 +00002456#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2457
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002458static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002459 SkPath p, p2;
2460
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002461 p.moveTo(0, 0);
2462 p.quadTo(100, 100, 200, 200);
2463 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2464 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002465 p2 = p;
2466 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002467 p.cubicTo(100, 100, 200, 200, 300, 300);
2468 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2469 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002470 p2 = p;
2471 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2472
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002473 p.reset();
2474 p.moveTo(0, 0);
2475 p.cubicTo(100, 100, 200, 200, 300, 300);
2476 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002477 p2 = p;
2478 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002479
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002480 REPORTER_ASSERT(reporter, !p.isEmpty());
2481}
2482
2483static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002484 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002485 SkPoint pts[4];
2486
2487 // Test an iterator with no path
2488 SkPath::Iter noPathIter;
2489 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002490
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002491 // Test that setting an empty path works
2492 noPathIter.setPath(p, false);
2493 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002494
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002495 // Test that close path makes no difference for an empty path
2496 noPathIter.setPath(p, true);
2497 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002498
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002499 // Test an iterator with an initial empty path
2500 SkPath::Iter iter(p, false);
2501 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2502
2503 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002504 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002505 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2506
rmistry@google.comd6176b02012-08-23 18:14:13 +00002507
schenney@chromium.org7e963602012-06-13 17:05:43 +00002508 struct iterTestData {
2509 const char* testPath;
2510 const bool forceClose;
2511 const bool consumeDegenerates;
2512 const size_t* numResultPtsPerVerb;
2513 const SkPoint* resultPts;
2514 const SkPath::Verb* resultVerbs;
2515 const size_t numResultVerbs;
2516 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002517
schenney@chromium.org7e963602012-06-13 17:05:43 +00002518 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2519 static const SkPath::Verb resultVerbs2[] = {
2520 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2521 };
2522 static const SkPath::Verb resultVerbs3[] = {
2523 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2524 };
2525 static const SkPath::Verb resultVerbs4[] = {
2526 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2527 };
2528 static const SkPath::Verb resultVerbs5[] = {
2529 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2530 };
2531 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002532 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2533 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2534 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2535 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002536 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002537 static const SkPoint resultPts2[] = {
2538 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2539 };
2540 static const SkPoint resultPts3[] = {
2541 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2542 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2543 };
2544 static const SkPoint resultPts4[] = {
2545 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2546 };
2547 static const SkPoint resultPts5[] = {
2548 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2549 };
2550 static const struct iterTestData gIterTests[] = {
2551 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002552 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2553 { "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 +00002554 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2555 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2556 { "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) },
2557 { "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 +00002558 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2559 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2560 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2561 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2562 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2563 { "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 +00002564 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002565
schenney@chromium.org7e963602012-06-13 17:05:43 +00002566 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2567 p.reset();
2568 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2569 REPORTER_ASSERT(reporter, valid);
2570 iter.setPath(p, gIterTests[i].forceClose);
2571 int j = 0, l = 0;
2572 do {
2573 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2574 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2575 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2576 }
2577 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2578 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2579 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002580
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002581 p.reset();
2582 iter.setPath(p, false);
2583 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2584 p.lineTo(1, 1);
2585 p.close();
2586 iter.setPath(p, false);
2587 REPORTER_ASSERT(reporter, iter.isClosedContour());
2588 p.reset();
2589 iter.setPath(p, true);
2590 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2591 p.lineTo(1, 1);
2592 iter.setPath(p, true);
2593 REPORTER_ASSERT(reporter, iter.isClosedContour());
2594 p.moveTo(0, 0);
2595 p.lineTo(2, 2);
2596 iter.setPath(p, false);
2597 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2598
2599 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2600 // check to see if the result is correct.
2601 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2602 p.reset();
2603 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2604 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2605 iter.setPath(p, true);
2606 iter.next(pts, false);
2607 iter.next(pts, false);
2608 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2609 }
2610
2611 p.reset();
2612 p.quadTo(0, 0, 0, 0);
2613 iter.setPath(p, false);
2614 iter.next(pts, false);
2615 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2616 iter.setPath(p, false);
2617 iter.next(pts, false);
2618 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2619
2620 p.reset();
2621 p.conicTo(0, 0, 0, 0, 0.5f);
2622 iter.setPath(p, false);
2623 iter.next(pts, false);
2624 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2625 iter.setPath(p, false);
2626 iter.next(pts, false);
2627 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2628
2629 p.reset();
2630 p.cubicTo(0, 0, 0, 0, 0, 0);
2631 iter.setPath(p, false);
2632 iter.next(pts, false);
2633 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2634 iter.setPath(p, false);
2635 iter.next(pts, false);
2636 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2637
2638 p.moveTo(1, 1); // add a trailing moveto
2639 iter.setPath(p, false);
2640 iter.next(pts, false);
2641 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2642 iter.setPath(p, false);
2643 iter.next(pts, false);
2644 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2645
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002646 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002647
2648 // Test out mixed degenerate and non-degenerate geometry with Conics
2649 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2650 SkRect r = SkRect::MakeWH(100, 100);
2651 SkRRect rr;
2652 rr.setRectRadii(r, radii);
2653 p.reset();
2654 p.addRRect(rr);
2655 iter.setPath(p, false);
2656 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2657 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2658 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2659 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2660 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002661}
2662
2663static void test_raw_iter(skiatest::Reporter* reporter) {
2664 SkPath p;
2665 SkPoint pts[4];
2666
2667 // Test an iterator with no path
2668 SkPath::RawIter noPathIter;
2669 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2670 // Test that setting an empty path works
2671 noPathIter.setPath(p);
2672 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002673
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002674 // Test an iterator with an initial empty path
2675 SkPath::RawIter iter(p);
2676 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2677
2678 // Test that a move-only path returns the move.
2679 p.moveTo(SK_Scalar1, 0);
2680 iter.setPath(p);
2681 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2682 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2683 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2684 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2685
2686 // No matter how many moves we add, we should get them all back
2687 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2688 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2689 iter.setPath(p);
2690 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2691 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2692 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2693 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2694 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2695 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2696 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2697 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2698 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2699 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2700
2701 // Initial close is never ever stored
2702 p.reset();
2703 p.close();
2704 iter.setPath(p);
2705 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2706
2707 // Move/close sequences
2708 p.reset();
2709 p.close(); // Not stored, no purpose
2710 p.moveTo(SK_Scalar1, 0);
2711 p.close();
2712 p.close(); // Not stored, no purpose
2713 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2714 p.close();
2715 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2716 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2717 p.close();
2718 iter.setPath(p);
2719 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2720 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2721 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2722 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002723 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2724 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2725 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2726 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002727 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2728 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2729 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2730 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2731 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2732 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2733 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002734 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2735
2736 // Generate random paths and verify
2737 SkPoint randomPts[25];
2738 for (int i = 0; i < 5; ++i) {
2739 for (int j = 0; j < 5; ++j) {
2740 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2741 }
2742 }
2743
2744 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002745 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002746 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002747 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002748 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002749
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002750 for (int i = 0; i < 500; ++i) {
2751 p.reset();
2752 bool lastWasClose = true;
2753 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002754 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002755 int numPoints = 0;
2756 int numVerbs = (rand.nextU() >> 16) % 10;
2757 int numIterVerbs = 0;
2758 for (int j = 0; j < numVerbs; ++j) {
2759 do {
2760 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2761 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002762 switch (nextVerb) {
2763 case SkPath::kMove_Verb:
2764 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2765 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002766 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002767 numPoints += 1;
2768 lastWasClose = false;
2769 haveMoveTo = true;
2770 break;
2771 case SkPath::kLine_Verb:
2772 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002773 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002774 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2775 haveMoveTo = true;
2776 }
2777 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2778 p.lineTo(expectedPts[numPoints]);
2779 numPoints += 1;
2780 lastWasClose = false;
2781 break;
2782 case SkPath::kQuad_Verb:
2783 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002784 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002785 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2786 haveMoveTo = true;
2787 }
2788 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2789 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2790 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2791 numPoints += 2;
2792 lastWasClose = false;
2793 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002794 case SkPath::kConic_Verb:
2795 if (!haveMoveTo) {
2796 expectedPts[numPoints++] = lastMoveToPt;
2797 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2798 haveMoveTo = true;
2799 }
2800 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2801 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2802 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2803 rand.nextUScalar1() * 4);
2804 numPoints += 2;
2805 lastWasClose = false;
2806 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002807 case SkPath::kCubic_Verb:
2808 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002809 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002810 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2811 haveMoveTo = true;
2812 }
2813 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2814 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2815 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2816 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2817 expectedPts[numPoints + 2]);
2818 numPoints += 3;
2819 lastWasClose = false;
2820 break;
2821 case SkPath::kClose_Verb:
2822 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002823 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002824 lastWasClose = true;
2825 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002826 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002827 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002828 }
2829 expectedVerbs[numIterVerbs++] = nextVerb;
2830 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002831
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002832 iter.setPath(p);
2833 numVerbs = numIterVerbs;
2834 numIterVerbs = 0;
2835 int numIterPts = 0;
2836 SkPoint lastMoveTo;
2837 SkPoint lastPt;
2838 lastMoveTo.set(0, 0);
2839 lastPt.set(0, 0);
2840 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2841 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2842 numIterVerbs++;
2843 switch (nextVerb) {
2844 case SkPath::kMove_Verb:
2845 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2846 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2847 lastPt = lastMoveTo = pts[0];
2848 numIterPts += 1;
2849 break;
2850 case SkPath::kLine_Verb:
2851 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2852 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2853 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2854 lastPt = pts[1];
2855 numIterPts += 1;
2856 break;
2857 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002858 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002859 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2860 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2861 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2862 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2863 lastPt = pts[2];
2864 numIterPts += 2;
2865 break;
2866 case SkPath::kCubic_Verb:
2867 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2868 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2869 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2870 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2871 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2872 lastPt = pts[3];
2873 numIterPts += 3;
2874 break;
2875 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002876 lastPt = lastMoveTo;
2877 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002878 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002879 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002880 }
2881 }
2882 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2883 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2884 }
2885}
2886
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002887static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002888 const SkPath& path,
2889 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07002890 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002891 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002892 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
reed026beb52015-06-10 14:23:15 -07002893 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002894
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002895 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002896 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2897 }
2898}
2899
2900static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002901 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002902 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002903 SkPath tmp;
2904
2905 SkMatrix m;
2906 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2907 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002908 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07002909 if (SkPathPriv::kCCW_FirstDirection == dir) {
2910 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002911 } else {
reed026beb52015-06-10 14:23:15 -07002912 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
2913 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002914 }
2915 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002916}
2917
2918static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002919 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002920 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002921 SkPath tmp;
2922
2923 // translate at small offset
2924 SkMatrix m;
2925 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2926 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002927 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002928
2929 tmp.reset();
2930 m.reset();
2931
2932 // translate at a relatively big offset
2933 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2934 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002935 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002936}
2937
2938static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002939 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002940 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002941 for (int angle = 0; angle < 360; ++angle) {
2942 SkPath tmp;
2943 SkMatrix m;
2944 m.setRotate(SkIntToScalar(angle));
2945 path.transform(m, &tmp);
2946
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002947 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002948 // degrees is not an oval anymore, this can be improved. we made this
2949 // for the simplicity of our implementation.
2950 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002951 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002952 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002953 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002954 }
2955 }
2956}
2957
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002958static void test_circle_mirror_x(skiatest::Reporter* reporter,
2959 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002960 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002961 SkPath tmp;
2962 SkMatrix m;
2963 m.reset();
2964 m.setScaleX(-SK_Scalar1);
2965 path.transform(m, &tmp);
2966
reed026beb52015-06-10 14:23:15 -07002967 if (SkPathPriv::kCW_FirstDirection == dir) {
2968 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002969 } else {
reed026beb52015-06-10 14:23:15 -07002970 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2971 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002972 }
2973
2974 check_for_circle(reporter, tmp, true, dir);
2975}
2976
2977static void test_circle_mirror_y(skiatest::Reporter* reporter,
2978 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002979 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002980 SkPath tmp;
2981 SkMatrix m;
2982 m.reset();
2983 m.setScaleY(-SK_Scalar1);
2984 path.transform(m, &tmp);
2985
reed026beb52015-06-10 14:23:15 -07002986 if (SkPathPriv::kCW_FirstDirection == dir) {
2987 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002988 } else {
reed026beb52015-06-10 14:23:15 -07002989 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2990 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002991 }
2992
2993 check_for_circle(reporter, tmp, true, dir);
2994}
2995
2996static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2997 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002998 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002999 SkPath tmp;
3000 SkMatrix m;
3001 m.reset();
3002 m.setScaleX(-SK_Scalar1);
3003 m.setScaleY(-SK_Scalar1);
3004 path.transform(m, &tmp);
3005
3006 check_for_circle(reporter, tmp, true, dir);
3007}
3008
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003009static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003010 SkPath::Direction inDir) {
3011 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003012 SkPath path;
3013
3014 // circle at origin
reed026beb52015-06-10 14:23:15 -07003015 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3016
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003017 check_for_circle(reporter, path, true, dir);
3018 test_circle_rotate(reporter, path, dir);
3019 test_circle_translate(reporter, path, dir);
3020 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003021
3022 // circle at an offset at (10, 10)
3023 path.reset();
3024 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003025 SkIntToScalar(20), inDir);
3026
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003027 check_for_circle(reporter, path, true, dir);
3028 test_circle_rotate(reporter, path, dir);
3029 test_circle_translate(reporter, path, dir);
3030 test_circle_skew(reporter, path, dir);
3031 test_circle_mirror_x(reporter, path, dir);
3032 test_circle_mirror_y(reporter, path, dir);
3033 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003034}
3035
3036static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3037 SkPath path;
3038 SkPath circle;
3039 SkPath rect;
3040 SkPath empty;
3041
reed026beb52015-06-10 14:23:15 -07003042 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3043 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003044
3045 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003046 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3047 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3048
3049 SkMatrix translate;
3050 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3051
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003052 // Although all the path concatenation related operations leave
3053 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003054
3055 // empty + circle (translate)
3056 path = empty;
3057 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003058 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003059
3060 // circle + empty (translate)
3061 path = circle;
3062 path.addPath(empty, translate);
reed026beb52015-06-10 14:23:15 -07003063 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003064
3065 // test reverseAddPath
3066 path = circle;
3067 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003068 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003069}
3070
3071static void test_circle(skiatest::Reporter* reporter) {
3072 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3073 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3074
3075 // multiple addCircle()
3076 SkPath path;
3077 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3078 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003079 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003080
3081 // some extra lineTo() would make isOval() fail
3082 path.reset();
3083 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3084 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003085 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003086
3087 // not back to the original point
3088 path.reset();
3089 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3090 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003091 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003092
3093 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003094
3095 // test negative radius
3096 path.reset();
3097 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3098 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003099}
3100
3101static void test_oval(skiatest::Reporter* reporter) {
3102 SkRect rect;
3103 SkMatrix m;
3104 SkPath path;
3105
3106 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3107 path.addOval(rect);
3108
halcanary96fcdcc2015-08-27 07:41:13 -07003109 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003110
3111 m.setRotate(SkIntToScalar(90));
3112 SkPath tmp;
3113 path.transform(m, &tmp);
3114 // an oval rotated 90 degrees is still an oval.
halcanary96fcdcc2015-08-27 07:41:13 -07003115 REPORTER_ASSERT(reporter, tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003116
3117 m.reset();
3118 m.setRotate(SkIntToScalar(30));
3119 tmp.reset();
3120 path.transform(m, &tmp);
3121 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003122 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003123
3124 // since empty path being transformed.
3125 path.reset();
3126 tmp.reset();
3127 m.reset();
3128 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003129 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003130
3131 // empty path is not an oval
3132 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003133 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003134
3135 // only has moveTo()s
3136 tmp.reset();
3137 tmp.moveTo(0, 0);
3138 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003139 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003140
3141 // mimic WebKit's calling convention,
3142 // call moveTo() first and then call addOval()
3143 path.reset();
3144 path.moveTo(0, 0);
3145 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003146 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003147
3148 // copy path
3149 path.reset();
3150 tmp.reset();
3151 tmp.addOval(rect);
3152 path = tmp;
halcanary96fcdcc2015-08-27 07:41:13 -07003153 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003154}
3155
bungeman@google.coma5809a32013-06-21 15:13:34 +00003156static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3157 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003158
reed@android.com3abec1d2009-03-02 05:36:20 +00003159 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003160 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003161 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003162 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003163 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003164 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3165 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003166 REPORTER_ASSERT(reporter, p == empty);
3167 REPORTER_ASSERT(reporter, !(p != empty));
3168}
3169
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003170static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3171 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003172 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003173 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003174 path->setConvexity(SkPath::kUnknown_Convexity);
3175 REPORTER_ASSERT(reporter, path->isConvex());
3176 path->reset();
3177}
3178
caryclarkd3d1a982014-12-08 04:57:38 -08003179static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3180 SkPath::Direction dir) {
3181 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003182 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003183 path->setConvexity(SkPath::kUnknown_Convexity);
3184 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3185 path->reset();
3186}
3187
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003188static void test_rrect(skiatest::Reporter* reporter) {
3189 SkPath p;
3190 SkRRect rr;
3191 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3192 SkRect r = {10, 20, 30, 40};
3193 rr.setRectRadii(r, radii);
3194 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003195 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003196 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003197 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003198 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003199 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003200 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003201 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003202 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003203 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003204 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003205 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3206 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3207 SkVector save = radii[i];
3208 radii[i].set(0, 0);
3209 rr.setRectRadii(r, radii);
3210 p.addRRect(rr);
3211 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3212 radii[i] = save;
3213 }
3214 p.addRoundRect(r, 0, 0);
3215 SkRect returnedRect;
3216 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3217 REPORTER_ASSERT(reporter, returnedRect == r);
3218 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3219 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3220 rr.setRectRadii(r, zeroRadii);
3221 p.addRRect(rr);
3222 bool closed;
3223 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003224 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003225 REPORTER_ASSERT(reporter, closed);
3226 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3227 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3228 p.addRRect(rr, SkPath::kCW_Direction);
3229 p.addRRect(rr, SkPath::kCW_Direction);
3230 REPORTER_ASSERT(reporter, !p.isConvex());
3231 p.reset();
3232 p.addRRect(rr, SkPath::kCCW_Direction);
3233 p.addRRect(rr, SkPath::kCCW_Direction);
3234 REPORTER_ASSERT(reporter, !p.isConvex());
3235 p.reset();
3236 SkRect emptyR = {10, 20, 10, 30};
3237 rr.setRectRadii(emptyR, radii);
3238 p.addRRect(rr);
3239 REPORTER_ASSERT(reporter, p.isEmpty());
3240 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3241 rr.setRectRadii(largeR, radii);
3242 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003243 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003244
3245 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003246 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3247 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003248 REPORTER_ASSERT(reporter, rr.isEmpty());
3249
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003250 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3251 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3252 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003253}
3254
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003255static void test_arc(skiatest::Reporter* reporter) {
3256 SkPath p;
3257 SkRect emptyOval = {10, 20, 30, 20};
3258 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3259 p.addArc(emptyOval, 1, 2);
3260 REPORTER_ASSERT(reporter, p.isEmpty());
3261 p.reset();
3262 SkRect oval = {10, 20, 30, 40};
3263 p.addArc(oval, 1, 0);
3264 REPORTER_ASSERT(reporter, p.isEmpty());
3265 p.reset();
3266 SkPath cwOval;
3267 cwOval.addOval(oval);
3268 p.addArc(oval, 1, 360);
3269 REPORTER_ASSERT(reporter, p == cwOval);
3270 p.reset();
3271 SkPath ccwOval;
3272 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3273 p.addArc(oval, 1, -360);
3274 REPORTER_ASSERT(reporter, p == ccwOval);
3275 p.reset();
3276 p.addArc(oval, 1, 180);
3277 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003278 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003279 p.setConvexity(SkPath::kUnknown_Convexity);
3280 REPORTER_ASSERT(reporter, p.isConvex());
3281}
3282
3283static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3284 SkScalar x0, SkScalar y0) {
3285 SkPoint pts[4];
3286 SkPath::Verb v = iter->next(pts);
3287 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3288 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3289 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3290}
3291
3292static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3293 SkScalar x1, SkScalar y1) {
3294 SkPoint pts[4];
3295 SkPath::Verb v = iter->next(pts);
3296 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3297 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3298 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3299}
3300
3301static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3302 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3303 SkPoint pts[4];
3304 SkPath::Verb v = iter->next(pts);
3305 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3306 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3307 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3308 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3309 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3310}
3311
3312static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3313 SkPoint pts[4];
3314 SkPath::Verb v = iter->next(pts);
3315 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3316}
3317
3318static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3319 check_done(reporter, p, iter);
3320 p->reset();
3321}
3322
3323static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3324 SkScalar x0, SkScalar y0) {
3325 SkPath::RawIter iter(*p);
3326 check_move(reporter, &iter, x0, y0);
3327 check_done_and_reset(reporter, p, &iter);
3328}
3329
3330static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3331 SkScalar x1, SkScalar y1) {
3332 SkPath::RawIter iter(*p);
3333 check_move(reporter, &iter, 0, 0);
3334 check_line(reporter, &iter, x1, y1);
3335 check_done_and_reset(reporter, p, &iter);
3336}
3337
3338static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3339 SkScalar x1, SkScalar y1) {
3340 SkPath::RawIter iter(*p);
3341 check_move(reporter, &iter, 0, 0);
3342 check_line(reporter, &iter, x1, y1);
3343 check_done(reporter, p, &iter);
3344}
3345
3346static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3347 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3348 SkPath::RawIter iter(*p);
3349 check_move(reporter, &iter, 0, 0);
3350 check_line(reporter, &iter, x1, y1);
3351 check_line(reporter, &iter, x2, y2);
3352 check_done_and_reset(reporter, p, &iter);
3353}
3354
3355static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3356 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3357 SkPath::RawIter iter(*p);
3358 check_move(reporter, &iter, 0, 0);
3359 check_quad(reporter, &iter, x1, y1, x2, y2);
3360 check_done_and_reset(reporter, p, &iter);
3361}
3362
reedd5d27d92015-02-09 13:54:43 -08003363static bool nearly_equal(const SkRect& a, const SkRect& b) {
3364 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3365 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3366 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3367 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3368}
3369
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003370static void test_arcTo(skiatest::Reporter* reporter) {
3371 SkPath p;
3372 p.arcTo(0, 0, 1, 2, 1);
3373 check_path_is_line_and_reset(reporter, &p, 0, 0);
3374 p.arcTo(1, 2, 1, 2, 1);
3375 check_path_is_line_and_reset(reporter, &p, 1, 2);
3376 p.arcTo(1, 2, 3, 4, 0);
3377 check_path_is_line_and_reset(reporter, &p, 1, 2);
3378 p.arcTo(1, 2, 0, 0, 1);
3379 check_path_is_line_and_reset(reporter, &p, 1, 2);
3380 p.arcTo(1, 0, 1, 1, 1);
3381 SkPoint pt;
3382 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3383 p.reset();
3384 p.arcTo(1, 0, 1, -1, 1);
3385 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3386 p.reset();
3387 SkRect oval = {1, 2, 3, 4};
3388 p.arcTo(oval, 0, 0, true);
3389 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3390 p.arcTo(oval, 0, 0, false);
3391 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3392 p.arcTo(oval, 360, 0, true);
3393 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3394 p.arcTo(oval, 360, 0, false);
3395 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003396
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003397 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003398 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003399 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003400 sweep += delta;
3401 delta /= 2;
3402 }
3403 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003404 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003405 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003406 sweep -= delta;
3407 delta /= 2;
3408 }
3409 SkRect noOvalWidth = {1, 2, 0, 3};
3410 p.reset();
3411 p.arcTo(noOvalWidth, 0, 360, false);
3412 REPORTER_ASSERT(reporter, p.isEmpty());
3413
3414 SkRect noOvalHeight = {1, 2, 3, 1};
3415 p.reset();
3416 p.arcTo(noOvalHeight, 0, 360, false);
3417 REPORTER_ASSERT(reporter, p.isEmpty());
3418}
3419
3420static void test_addPath(skiatest::Reporter* reporter) {
3421 SkPath p, q;
3422 p.lineTo(1, 2);
3423 q.moveTo(4, 4);
3424 q.lineTo(7, 8);
3425 q.conicTo(8, 7, 6, 5, 0.5f);
3426 q.quadTo(6, 7, 8, 6);
3427 q.cubicTo(5, 6, 7, 8, 7, 5);
3428 q.close();
3429 p.addPath(q, -4, -4);
3430 SkRect expected = {0, 0, 4, 4};
3431 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3432 p.reset();
3433 p.reverseAddPath(q);
3434 SkRect reverseExpected = {4, 4, 8, 8};
3435 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3436}
3437
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003438static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3439 SkPath p, q;
3440 if (explicitMoveTo) {
3441 p.moveTo(1, 1);
3442 }
3443 p.lineTo(1, 2);
3444 if (explicitMoveTo) {
3445 q.moveTo(2, 1);
3446 }
3447 q.lineTo(2, 2);
3448 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3449 uint8_t verbs[4];
3450 int verbcount = p.getVerbs(verbs, 4);
3451 REPORTER_ASSERT(reporter, verbcount == 4);
3452 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3453 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3454 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3455 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3456}
3457
3458static void test_extendClosedPath(skiatest::Reporter* reporter) {
3459 SkPath p, q;
3460 p.moveTo(1, 1);
3461 p.lineTo(1, 2);
3462 p.lineTo(2, 2);
3463 p.close();
3464 q.moveTo(2, 1);
3465 q.lineTo(2, 3);
3466 p.addPath(q, SkPath::kExtend_AddPathMode);
3467 uint8_t verbs[7];
3468 int verbcount = p.getVerbs(verbs, 7);
3469 REPORTER_ASSERT(reporter, verbcount == 7);
3470 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3471 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3472 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3473 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3474 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3475 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3476 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3477
3478 SkPoint pt;
3479 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3480 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3481 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3482}
3483
3484static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3485 SkPath p, q, r;
3486 // case 1: dst is empty
3487 p.moveTo(2, 1);
3488 p.lineTo(2, 3);
3489 q.addPath(p, mode);
3490 REPORTER_ASSERT(reporter, q == p);
3491 // case 2: src is empty
3492 p.addPath(r, mode);
3493 REPORTER_ASSERT(reporter, q == p);
3494 // case 3: src and dst are empty
3495 q.reset();
3496 q.addPath(r, mode);
3497 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003498}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003499
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003500static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3501 SkPath p;
3502 p.conicTo(1, 2, 3, 4, -1);
3503 check_path_is_line_and_reset(reporter, &p, 3, 4);
3504 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3505 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3506 p.conicTo(1, 2, 3, 4, 1);
3507 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3508}
3509
3510static void test_get_point(skiatest::Reporter* reporter) {
3511 SkPath p;
3512 SkPoint pt = p.getPoint(0);
3513 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003514 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003515 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3516 p.setLastPt(10, 10);
3517 pt = p.getPoint(0);
3518 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003519 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003520 p.rMoveTo(10, 10);
3521 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3522}
3523
3524static void test_contains(skiatest::Reporter* reporter) {
3525 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003526 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3527 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
3528 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
3529 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3530 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
3531 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 -08003532 // this may return true or false, depending on the platform's numerics, but it should not crash
3533 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003534
3535 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003536 p.setFillType(SkPath::kInverseWinding_FillType);
3537 REPORTER_ASSERT(reporter, p.contains(0, 0));
3538 p.setFillType(SkPath::kWinding_FillType);
3539 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3540 p.moveTo(4, 4);
3541 p.lineTo(6, 8);
3542 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003543 // test on edge
3544 REPORTER_ASSERT(reporter, p.contains(6, 4));
3545 REPORTER_ASSERT(reporter, p.contains(5, 6));
3546 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003547 // test quick reject
3548 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3549 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3550 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3551 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3552 // test various crossings in x
3553 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3554 REPORTER_ASSERT(reporter, p.contains(6, 7));
3555 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3556 p.reset();
3557 p.moveTo(4, 4);
3558 p.lineTo(8, 6);
3559 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003560 // test on edge
3561 REPORTER_ASSERT(reporter, p.contains(4, 6));
3562 REPORTER_ASSERT(reporter, p.contains(6, 5));
3563 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003564 // test various crossings in y
3565 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3566 REPORTER_ASSERT(reporter, p.contains(7, 6));
3567 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003568 p.reset();
3569 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003570 p.lineTo(8, 4);
3571 p.lineTo(8, 8);
3572 p.lineTo(4, 8);
3573 // test on vertices
3574 REPORTER_ASSERT(reporter, p.contains(4, 4));
3575 REPORTER_ASSERT(reporter, p.contains(8, 4));
3576 REPORTER_ASSERT(reporter, p.contains(8, 8));
3577 REPORTER_ASSERT(reporter, p.contains(4, 8));
3578 p.reset();
3579 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003580 p.lineTo(6, 8);
3581 p.lineTo(2, 8);
3582 // test on edge
3583 REPORTER_ASSERT(reporter, p.contains(5, 6));
3584 REPORTER_ASSERT(reporter, p.contains(4, 8));
3585 REPORTER_ASSERT(reporter, p.contains(3, 6));
3586 p.reset();
3587 p.moveTo(4, 4);
3588 p.lineTo(0, 6);
3589 p.lineTo(4, 8);
3590 // test on edge
3591 REPORTER_ASSERT(reporter, p.contains(2, 5));
3592 REPORTER_ASSERT(reporter, p.contains(2, 7));
3593 REPORTER_ASSERT(reporter, p.contains(4, 6));
3594 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3595 p.reset();
3596 p.moveTo(4, 0);
3597 p.lineTo(6, 4);
3598 p.lineTo(2, 4);
3599 p.moveTo(4, 0);
3600 p.lineTo(0, 8);
3601 p.lineTo(8, 8);
3602 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3603 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3604 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3605 REPORTER_ASSERT(reporter, p.contains(4, 4));
3606
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003607 // test quads
3608 p.reset();
3609 p.moveTo(4, 4);
3610 p.quadTo(6, 6, 8, 8);
3611 p.quadTo(6, 8, 4, 8);
3612 p.quadTo(4, 6, 4, 4);
3613 REPORTER_ASSERT(reporter, p.contains(5, 6));
3614 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003615 // test quad edge
3616 REPORTER_ASSERT(reporter, p.contains(5, 5));
3617 REPORTER_ASSERT(reporter, p.contains(5, 8));
3618 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003619 // test quad endpoints
3620 REPORTER_ASSERT(reporter, p.contains(4, 4));
3621 REPORTER_ASSERT(reporter, p.contains(8, 8));
3622 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003623
3624 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003625 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3626 p.moveTo(qPts[0]);
3627 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3628 p.quadTo(qPts[index], qPts[index + 1]);
3629 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003630 REPORTER_ASSERT(reporter, p.contains(5, 6));
3631 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003632 // test quad edge
3633 SkPoint halfway;
3634 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3635 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3636 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3637 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003638
caryclark9aacd902015-12-14 08:38:09 -08003639 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003640 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003641 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3642 p.moveTo(kPts[0]);
3643 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3644 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3645 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003646 REPORTER_ASSERT(reporter, p.contains(5, 6));
3647 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003648 // test conic edge
3649 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3650 SkConic conic(&kPts[index], 0.5f);
3651 halfway = conic.evalAt(0.5f);
3652 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3653 }
caryclark9cb5d752015-12-18 04:35:24 -08003654 // test conic end points
3655 REPORTER_ASSERT(reporter, p.contains(4, 4));
3656 REPORTER_ASSERT(reporter, p.contains(8, 8));
3657 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003658
3659 // test cubics
3660 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3661 for (int i = 0; i < 3; ++i) {
3662 p.reset();
3663 p.setFillType(SkPath::kEvenOdd_FillType);
3664 p.moveTo(pts[i].fX, pts[i].fY);
3665 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);
3666 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);
3667 p.close();
3668 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3669 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003670 // test cubic edge
3671 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3672 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3673 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3674 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003675 // test cubic end points
3676 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3677 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3678 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003679 }
3680}
3681
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003682class PathRefTest_Private {
3683public:
3684 static void TestPathRef(skiatest::Reporter* reporter) {
3685 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003686
halcanary385fe4d2015-08-26 13:07:48 -07003687 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003688
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003689 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003690
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003691 {
3692 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3693 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3694 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3695 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3696 for (int i = 0; i < kRepeatCnt; ++i) {
3697 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3698 }
3699 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003700 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003701
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003702 {
3703 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3704 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3705 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3706 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3707 for (int i = 0; i < kRepeatCnt; ++i) {
3708 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3709 }
3710 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003711 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003712
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003713 {
3714 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3715 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3716 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3717 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3718 for (int i = 0; i < kRepeatCnt; ++i) {
3719 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3720 }
3721 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003722 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003723
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003724 {
halcanary96fcdcc2015-08-27 07:41:13 -07003725 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003726 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3727 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3728 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3729 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3730 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003731 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003732 for (int i = 0; i < kRepeatCnt; ++i) {
3733 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3734 }
3735 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003736 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003737
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003738 {
3739 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3740 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3741 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3742 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3743 for (int i = 0; i < kRepeatCnt; ++i) {
3744 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3745 }
3746 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003747 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003748 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003749};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003750
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003751static void test_operatorEqual(skiatest::Reporter* reporter) {
3752 SkPath a;
3753 SkPath b;
3754 REPORTER_ASSERT(reporter, a == a);
3755 REPORTER_ASSERT(reporter, a == b);
3756 a.setFillType(SkPath::kInverseWinding_FillType);
3757 REPORTER_ASSERT(reporter, a != b);
3758 a.reset();
3759 REPORTER_ASSERT(reporter, a == b);
3760 a.lineTo(1, 1);
3761 REPORTER_ASSERT(reporter, a != b);
3762 a.reset();
3763 REPORTER_ASSERT(reporter, a == b);
3764 a.lineTo(1, 1);
3765 b.lineTo(1, 2);
3766 REPORTER_ASSERT(reporter, a != b);
3767 a.reset();
3768 a.lineTo(1, 2);
3769 REPORTER_ASSERT(reporter, a == b);
3770}
3771
caryclark66a5d8b2014-06-24 08:30:15 -07003772static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003773 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003774 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003775 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003776 SkAutoDataUnref data(wStream.copyToData());
3777 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08003778 if (strlen(str) > 0) {
3779 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3780 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07003781 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08003782 }
caryclark66a5d8b2014-06-24 08:30:15 -07003783}
3784
3785static void test_dump(skiatest::Reporter* reporter) {
3786 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07003787 compare_dump(reporter, p, false, false, "");
3788 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07003789 p.moveTo(1, 2);
3790 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07003791 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3792 "path.lineTo(3, 4);\n");
3793 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
3794 "path.lineTo(3, 4);\n"
3795 "path.lineTo(1, 2);\n"
3796 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003797 p.reset();
3798 p.moveTo(1, 2);
3799 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07003800 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3801 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003802 p.reset();
3803 p.moveTo(1, 2);
3804 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07003805 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3806 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003807 p.reset();
3808 p.moveTo(1, 2);
3809 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07003810 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3811 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3812 p.reset();
3813 p.moveTo(1, 2);
3814 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07003815 compare_dump(reporter, p, false, true,
3816 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
3817 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07003818 p.reset();
3819 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
3820 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
3821 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3822 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003823}
3824
senorblanco84cd6212015-08-04 10:01:58 -07003825namespace {
3826
3827class ChangeListener : public SkPathRef::GenIDChangeListener {
3828public:
3829 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
3830 virtual ~ChangeListener() {}
3831 void onChange() override {
3832 *fChanged = true;
3833 }
3834private:
3835 bool* fChanged;
3836};
3837
3838}
3839
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003840class PathTest_Private {
3841public:
3842 static void TestPathTo(skiatest::Reporter* reporter) {
3843 SkPath p, q;
3844 p.lineTo(4, 4);
3845 p.reversePathTo(q);
3846 check_path_is_line(reporter, &p, 4, 4);
3847 q.moveTo(-4, -4);
3848 p.reversePathTo(q);
3849 check_path_is_line(reporter, &p, 4, 4);
3850 q.lineTo(7, 8);
3851 q.conicTo(8, 7, 6, 5, 0.5f);
3852 q.quadTo(6, 7, 8, 6);
3853 q.cubicTo(5, 6, 7, 8, 7, 5);
3854 q.close();
3855 p.reversePathTo(q);
3856 SkRect reverseExpected = {-4, -4, 8, 8};
3857 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3858 }
senorblanco84cd6212015-08-04 10:01:58 -07003859
3860 static void TestPathrefListeners(skiatest::Reporter* reporter) {
3861 SkPath p;
3862
3863 bool changed = false;
3864 p.moveTo(0, 0);
3865
3866 // Check that listener is notified on moveTo().
3867
halcanary385fe4d2015-08-26 13:07:48 -07003868 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003869 REPORTER_ASSERT(reporter, !changed);
3870 p.moveTo(10, 0);
3871 REPORTER_ASSERT(reporter, changed);
3872
3873 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07003874 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003875 REPORTER_ASSERT(reporter, !changed);
3876 p.lineTo(20, 0);
3877 REPORTER_ASSERT(reporter, changed);
3878
3879 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07003880 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003881 REPORTER_ASSERT(reporter, !changed);
3882 p.reset();
3883 REPORTER_ASSERT(reporter, changed);
3884
3885 p.moveTo(0, 0);
3886
3887 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07003888 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003889 REPORTER_ASSERT(reporter, !changed);
3890 p.rewind();
3891 REPORTER_ASSERT(reporter, changed);
3892
3893 // Check that listener is notified when pathref is deleted.
3894 {
3895 SkPath q;
3896 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07003897 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003898 REPORTER_ASSERT(reporter, !changed);
3899 }
3900 // q went out of scope.
3901 REPORTER_ASSERT(reporter, changed);
3902 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003903};
3904
caryclark8e7b19d2016-02-18 04:11:48 -08003905static void test_interp(skiatest::Reporter* reporter) {
3906 SkPath p1, p2, out;
3907 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
3908 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
3909 REPORTER_ASSERT(reporter, p1 == out);
3910 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
3911 REPORTER_ASSERT(reporter, p1 == out);
3912 p1.moveTo(0, 2);
3913 p1.lineTo(0, 4);
3914 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
3915 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
3916 p2.moveTo(6, 0);
3917 p2.lineTo(8, 0);
3918 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
3919 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
3920 REPORTER_ASSERT(reporter, p2 == out);
3921 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
3922 REPORTER_ASSERT(reporter, p1 == out);
3923 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
3924 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
3925 p1.reset();
3926 p1.moveTo(4, 4);
3927 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
3928 p2.reset();
3929 p2.moveTo(4, 2);
3930 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
3931 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
3932 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
3933 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
3934 p2.reset();
3935 p2.moveTo(4, 2);
3936 p2.conicTo(6, 3, 6, 5, 1);
3937 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
3938 p2.reset();
3939 p2.moveTo(4, 4);
3940 p2.conicTo(5, 4, 5, 5, 0.5f);
3941 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
3942}
3943
3944DEF_TEST(PathInterp, reporter) {
3945 test_interp(reporter);
3946}
3947
caryclark9aacd902015-12-14 08:38:09 -08003948DEF_TEST(PathContains, reporter) {
3949 test_contains(reporter);
3950}
3951
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003952DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003953 test_path_crbug364224();
3954
bungeman@google.coma5809a32013-06-21 15:13:34 +00003955 SkTSize<SkScalar>::Make(3,4);
3956
3957 SkPath p, empty;
3958 SkRect bounds, bounds2;
3959 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003960
reed@android.comd252db02009-04-01 18:31:44 +00003961 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003962
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003963 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3964 SkPath& self = p;
3965 p = self;
3966
3967 // this triggers a code path in SkPath::swap which is otherwise unexercised
3968 p.swap(self);
3969
reed@android.com3abec1d2009-03-02 05:36:20 +00003970 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003971
reed@android.com6b82d1a2009-06-03 02:35:01 +00003972 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3973 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003974 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08003975 REPORTER_ASSERT(reporter,
3976 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003977 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003978
reed@android.com6b82d1a2009-06-03 02:35:01 +00003979 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003980 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003981
reed@android.com6b82d1a2009-06-03 02:35:01 +00003982 p.addOval(bounds);
3983 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003984 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003985
bungeman@google.coma5809a32013-06-21 15:13:34 +00003986 p.rewind();
3987 test_empty(reporter, p);
3988
reed@android.com3abec1d2009-03-02 05:36:20 +00003989 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003990 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003991 // we have only lines
3992 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003993 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003994
bungeman@google.coma5809a32013-06-21 15:13:34 +00003995 REPORTER_ASSERT(reporter, p != empty);
3996 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003997
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003998 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07003999 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4000 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004001 SkPoint pts[4];
4002 int count = p.getPoints(pts, 4);
4003 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004004 uint8_t verbs[6];
4005 verbs[5] = 0xff;
4006 p.getVerbs(verbs, 5);
4007 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4008 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4009 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4010 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4011 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4012 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004013 bounds2.set(pts, 4);
4014 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004015
reed@android.com3abec1d2009-03-02 05:36:20 +00004016 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4017 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004018 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004019
halcanary96fcdcc2015-08-27 07:41:13 -07004020 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004021 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004022 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4023 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004024
reed@android.com3abec1d2009-03-02 05:36:20 +00004025 // now force p to not be a rect
4026 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4027 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004028 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004029
robertphillipsfe7c4272014-12-29 11:36:39 -08004030 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4031 // path has a trailing moveTo. Please see crbug.com\445368)
4032 {
4033 SkRect r;
4034 p.reset();
4035 p.addRect(bounds);
4036 REPORTER_ASSERT(reporter, p.isRect(&r));
4037 REPORTER_ASSERT(reporter, r == bounds);
4038 // add a moveTo outside of our bounds
4039 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4040 REPORTER_ASSERT(reporter, p.isRect(&r));
4041 REPORTER_ASSERT(reporter, r == bounds);
4042 }
4043
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004044 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004045 test_isLine(reporter);
4046 test_isRect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004047 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004048 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004049 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004050 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004051 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004052 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004053 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004054 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004055 test_flattening(reporter);
4056 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004057 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004058 test_iter(reporter);
4059 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004060 test_circle(reporter);
4061 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004062 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004063 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004064 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004065 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004066 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004067 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004068 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004069 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004070 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004071 test_tricky_cubic();
4072 test_clipped_cubic();
4073 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004074 test_crbug_493450(reporter);
4075 test_crbug_495894(reporter);
reed@google.com7a90daf2013-04-10 18:44:00 +00004076 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004077 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004078 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004079 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004080 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004081 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004082 test_arc(reporter);
4083 test_arcTo(reporter);
4084 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004085 test_addPathMode(reporter, false, false);
4086 test_addPathMode(reporter, true, false);
4087 test_addPathMode(reporter, false, true);
4088 test_addPathMode(reporter, true, true);
4089 test_extendClosedPath(reporter);
4090 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4091 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004092 test_conicTo_special_case(reporter);
4093 test_get_point(reporter);
4094 test_contains(reporter);
4095 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004096 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004097 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004098 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004099 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004100 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004101 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004102 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004103 test_bounds_crbug_513799(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00004104}