blob: 5bc3d95b6f4b33b5529c76a759a11420935179ac [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
bsalomon1978ce22016-05-31 10:42:16 -07008#include <cmath>
reed@google.com8cae8352012-09-14 15:18:41 +00009#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -080010#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000011#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000012#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000013#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070014#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000015#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000017#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000018#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000019#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070020#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070021#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000022#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000023#include "SkTypes.h"
24#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000025#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000026
reed5bcbe912014-12-15 12:28:33 -080027static void set_radii(SkVector radii[4], int index, float rad) {
28 sk_bzero(radii, sizeof(SkVector) * 4);
29 radii[index].set(rad, rad);
30}
31
32static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
33 const SkVector radii[4]) {
34 SkRRect rrect;
35 rrect.setRectRadii(bounds, radii);
36 REPORTER_ASSERT(reporter, bounds == rrect.rect());
37
38 SkPath path;
39 // this line should not assert in the debug build (from validate)
40 path.addRRect(rrect);
41 REPORTER_ASSERT(reporter, bounds == path.getBounds());
42}
43
caryclark5ccef572015-03-02 10:07:56 -080044static void test_skbug_3469(skiatest::Reporter* reporter) {
45 SkPath path;
46 path.moveTo(20, 20);
47 path.quadTo(20, 50, 80, 50);
48 path.quadTo(20, 50, 20, 80);
49 REPORTER_ASSERT(reporter, !path.isConvex());
50}
51
reed5bcbe912014-12-15 12:28:33 -080052static void test_skbug_3239(skiatest::Reporter* reporter) {
53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
56
57 const float rad = 33436320;
58
59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
61
62 SkVector radii[4];
63 for (int i = 0; i < 4; ++i) {
64 set_radii(radii, i, rad);
65 test_add_rrect(reporter, rectx, radii);
66 test_add_rrect(reporter, recty, radii);
67 }
68}
69
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000070static void make_path_crbug364224(SkPath* path) {
71 path->reset();
72 path->moveTo(3.747501373f, 2.724499941f);
73 path->lineTo(3.747501373f, 3.75f);
74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
75 path->lineTo(0.7475013733f, 4.0f);
76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
77 path->lineTo(0.4975013733f, 1.0f);
78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
79 path->lineTo(3.497501373f, 0.75f);
80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
81 path->lineTo(3.715001345f, 0.5512499809f);
82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
83 path->lineTo(0.7475013733f, 0.4999999702f);
84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
85 path->lineTo(0.2475013733f, 3.75f);
86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
87 path->lineTo(3.497501373f, 4.25f);
88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
89 path->lineTo(3.997501373f, 2.474750042f);
90 path->lineTo(3.747501373f, 2.724499941f);
91 path->close();
92}
93
94static void make_path_crbug364224_simplified(SkPath* path) {
95 path->moveTo(3.747501373f, 2.724499941f);
96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
97 path->close();
98}
99
100static void test_path_crbug364224() {
101 SkPath path;
102 SkPaint paint;
reede8f30622016-03-23 18:59:25 -0700103 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000104 SkCanvas* canvas = surface->getCanvas();
105
106 make_path_crbug364224_simplified(&path);
107 canvas->drawPath(path, paint);
108
109 make_path_crbug364224(&path);
110 canvas->drawPath(path, paint);
111}
112
piotaixrfac4e0e2014-08-26 11:59:04 -0700113/**
114 * In debug mode, this path was causing an assertion to fail in
115 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
116 */
117static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
118 SkPoint orig, p1, p2, p3;
119 orig = SkPoint::Make(1.f, 1.f);
120 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
121 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
122 p3 = SkPoint::Make(2.f, 2.f);
123
124 path->reset();
125 path->moveTo(orig);
126 path->cubicTo(p1, p2, p3);
127 path->close();
128}
129
130static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
131 SkPath path;
132 make_path_crbugskia2820(&path, reporter);
133
134 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
135 stroke.setStrokeStyle(2 * SK_Scalar1);
136 stroke.applyToPath(&path, path);
137}
138
reed@google.comcc8be772013-10-15 15:35:29 +0000139static void make_path0(SkPath* path) {
140 // from * https://code.google.com/p/skia/issues/detail?id=1706
141
142 path->moveTo(146.939f, 1012.84f);
143 path->lineTo(181.747f, 1009.18f);
144 path->lineTo(182.165f, 1013.16f);
145 path->lineTo(147.357f, 1016.82f);
146 path->lineTo(146.939f, 1012.84f);
147 path->close();
148}
149
150static void make_path1(SkPath* path) {
151 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
152}
153
154typedef void (*PathProc)(SkPath*);
155
156/*
157 * Regression test: we used to crash (overwrite internal storage) during
158 * construction of the region when the path was INVERSE. That is now fixed,
159 * so test these regions (which used to assert/crash).
160 *
161 * https://code.google.com/p/skia/issues/detail?id=1706
162 */
163static void test_path_to_region(skiatest::Reporter* reporter) {
164 PathProc procs[] = {
165 make_path0,
166 make_path1,
167 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000168
reed@google.comcc8be772013-10-15 15:35:29 +0000169 SkRegion clip;
170 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000171
reed@google.comcc8be772013-10-15 15:35:29 +0000172 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
173 SkPath path;
174 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000175
reed@google.comcc8be772013-10-15 15:35:29 +0000176 SkRegion rgn;
177 rgn.setPath(path, clip);
178 path.toggleInverseFillType();
179 rgn.setPath(path, clip);
180 }
181}
182
halcanary4dbbd042016-06-07 17:21:10 -0700183#ifdef SK_BUILD_FOR_WIN
caryclark@google.com56f233a2012-11-19 13:06:06 +0000184 #define SUPPRESS_VISIBILITY_WARNING
185#else
186 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
187#endif
188
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000189static void test_path_close_issue1474(skiatest::Reporter* reporter) {
190 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
191 // are relative to the point we close to, not relative to the point we close from.
192 SkPath path;
193 SkPoint last;
194
195 // Test rLineTo().
196 path.rLineTo(0, 100);
197 path.rLineTo(100, 0);
198 path.close(); // Returns us back to 0,0.
199 path.rLineTo(50, 50); // This should go to 50,50.
200
201 path.getLastPt(&last);
202 REPORTER_ASSERT(reporter, 50 == last.fX);
203 REPORTER_ASSERT(reporter, 50 == last.fY);
204
205 // Test rQuadTo().
206 path.rewind();
207 path.rLineTo(0, 100);
208 path.rLineTo(100, 0);
209 path.close();
210 path.rQuadTo(50, 50, 75, 75);
211
212 path.getLastPt(&last);
213 REPORTER_ASSERT(reporter, 75 == last.fX);
214 REPORTER_ASSERT(reporter, 75 == last.fY);
215
216 // Test rConicTo().
217 path.rewind();
218 path.rLineTo(0, 100);
219 path.rLineTo(100, 0);
220 path.close();
221 path.rConicTo(50, 50, 85, 85, 2);
222
223 path.getLastPt(&last);
224 REPORTER_ASSERT(reporter, 85 == last.fX);
225 REPORTER_ASSERT(reporter, 85 == last.fY);
226
227 // Test rCubicTo().
228 path.rewind();
229 path.rLineTo(0, 100);
230 path.rLineTo(100, 0);
231 path.close();
232 path.rCubicTo(50, 50, 85, 85, 95, 95);
233
234 path.getLastPt(&last);
235 REPORTER_ASSERT(reporter, 95 == last.fX);
236 REPORTER_ASSERT(reporter, 95 == last.fY);
237}
238
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000239static void test_gen_id(skiatest::Reporter* reporter) {
240 SkPath a, b;
241 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
242
243 a.moveTo(0, 0);
244 const uint32_t z = a.getGenerationID();
245 REPORTER_ASSERT(reporter, z != b.getGenerationID());
246
247 a.reset();
248 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
249
250 a.moveTo(1, 1);
251 const uint32_t y = a.getGenerationID();
252 REPORTER_ASSERT(reporter, z != y);
253
254 b.moveTo(2, 2);
255 const uint32_t x = b.getGenerationID();
256 REPORTER_ASSERT(reporter, x != y && x != z);
257
258 a.swap(b);
259 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
260
261 b = a;
262 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
263
264 SkPath c(a);
265 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
266
267 c.lineTo(3, 3);
268 const uint32_t w = c.getGenerationID();
269 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
270 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
271 REPORTER_ASSERT(reporter, w != x);
272
djsollen50da1d82015-02-17 09:09:53 -0800273#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000274 static bool kExpectGenIDToIgnoreFill = false;
275#else
276 static bool kExpectGenIDToIgnoreFill = true;
277#endif
278
279 c.toggleInverseFillType();
280 const uint32_t v = c.getGenerationID();
281 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
282
283 c.rewind();
284 REPORTER_ASSERT(reporter, v != c.getGenerationID());
285}
286
reed@google.com3eff3592013-05-08 21:08:21 +0000287// This used to assert in the debug build, as the edges did not all line-up.
288static void test_bad_cubic_crbug234190() {
289 SkPath path;
290 path.moveTo(13.8509f, 3.16858f);
291 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
292 -2.38991e+08f, -4.26573e+08f,
293 -2.41016e+08f, -4.30188e+08f);
294
295 SkPaint paint;
296 paint.setAntiAlias(true);
reede8f30622016-03-23 18:59:25 -0700297 auto surface(SkSurface::MakeRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000298 surface->getCanvas()->drawPath(path, paint);
299}
300
reed@google.com7a90daf2013-04-10 18:44:00 +0000301static void test_bad_cubic_crbug229478() {
302 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000303 { 4595.91064f, -11596.9873f },
304 { 4597.2168f, -11595.9414f },
305 { 4598.52344f, -11594.8955f },
306 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000307 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000308
reed@google.com7a90daf2013-04-10 18:44:00 +0000309 SkPath path;
310 path.moveTo(pts[0]);
311 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000312
reed@google.com7a90daf2013-04-10 18:44:00 +0000313 SkPaint paint;
314 paint.setStyle(SkPaint::kStroke_Style);
315 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000316
reed@google.com7a90daf2013-04-10 18:44:00 +0000317 SkPath dst;
318 // Before the fix, this would infinite-recurse, and run out of stack
319 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700320 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000321}
322
reed@google.com64d62952013-01-18 17:49:28 +0000323static void build_path_170666(SkPath& path) {
324 path.moveTo(17.9459f, 21.6344f);
325 path.lineTo(139.545f, -47.8105f);
326 path.lineTo(139.545f, -47.8105f);
327 path.lineTo(131.07f, -47.3888f);
328 path.lineTo(131.07f, -47.3888f);
329 path.lineTo(122.586f, -46.9532f);
330 path.lineTo(122.586f, -46.9532f);
331 path.lineTo(18076.6f, 31390.9f);
332 path.lineTo(18076.6f, 31390.9f);
333 path.lineTo(18085.1f, 31390.5f);
334 path.lineTo(18085.1f, 31390.5f);
335 path.lineTo(18076.6f, 31390.9f);
336 path.lineTo(18076.6f, 31390.9f);
337 path.lineTo(17955, 31460.3f);
338 path.lineTo(17955, 31460.3f);
339 path.lineTo(17963.5f, 31459.9f);
340 path.lineTo(17963.5f, 31459.9f);
341 path.lineTo(17971.9f, 31459.5f);
342 path.lineTo(17971.9f, 31459.5f);
343 path.lineTo(17.9551f, 21.6205f);
344 path.lineTo(17.9551f, 21.6205f);
345 path.lineTo(9.47091f, 22.0561f);
346 path.lineTo(9.47091f, 22.0561f);
347 path.lineTo(17.9459f, 21.6344f);
348 path.lineTo(17.9459f, 21.6344f);
349 path.close();path.moveTo(0.995934f, 22.4779f);
350 path.lineTo(0.986725f, 22.4918f);
351 path.lineTo(0.986725f, 22.4918f);
352 path.lineTo(17955, 31460.4f);
353 path.lineTo(17955, 31460.4f);
354 path.lineTo(17971.9f, 31459.5f);
355 path.lineTo(17971.9f, 31459.5f);
356 path.lineTo(18093.6f, 31390.1f);
357 path.lineTo(18093.6f, 31390.1f);
358 path.lineTo(18093.6f, 31390);
359 path.lineTo(18093.6f, 31390);
360 path.lineTo(139.555f, -47.8244f);
361 path.lineTo(139.555f, -47.8244f);
362 path.lineTo(122.595f, -46.9671f);
363 path.lineTo(122.595f, -46.9671f);
364 path.lineTo(0.995934f, 22.4779f);
365 path.lineTo(0.995934f, 22.4779f);
366 path.close();
367 path.moveTo(5.43941f, 25.5223f);
368 path.lineTo(798267, -28871.1f);
369 path.lineTo(798267, -28871.1f);
370 path.lineTo(3.12512e+06f, -113102);
371 path.lineTo(3.12512e+06f, -113102);
372 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
373 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
374 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
375 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
376 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
377 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
378 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
379 path.lineTo(2.78271e+08f, -1.00733e+07f);
380 path.lineTo(2.78271e+08f, -1.00733e+07f);
381 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
382 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
383 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
384 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
385 path.lineTo(2.77473e+08f, -1.00444e+07f);
386 path.lineTo(2.77473e+08f, -1.00444e+07f);
387 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
388 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
389 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
390 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
391 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
392 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
393 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
394 path.lineTo(798284, -28872);
395 path.lineTo(798284, -28872);
396 path.lineTo(22.4044f, 24.6677f);
397 path.lineTo(22.4044f, 24.6677f);
398 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
399 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
400 path.close();
401}
402
403static void build_path_simple_170666(SkPath& path) {
404 path.moveTo(126.677f, 24.1591f);
405 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
406}
407
408// This used to assert in the SK_DEBUG build, as the clip step would fail with
409// too-few interations in our cubic-line intersection code. That code now runs
410// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000411static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000412 SkPath path;
413 SkPaint paint;
414 paint.setAntiAlias(true);
415
reede8f30622016-03-23 18:59:25 -0700416 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000417
reed@google.com64d62952013-01-18 17:49:28 +0000418 build_path_simple_170666(path);
419 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000420
reed@google.com64d62952013-01-18 17:49:28 +0000421 build_path_170666(path);
422 surface->getCanvas()->drawPath(path, paint);
423}
424
caryclarke8c56662015-07-14 11:19:26 -0700425
426static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
427 SkScalar tx, SkScalar ty, SkScalar scale) {
428 SkPath smallPath;
429 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
430 bool smallConvex = smallPath.isConvex();
431 SkPath largePath;
432 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
433 SkMatrix matrix;
434 matrix.reset();
435 matrix.preTranslate(100, 100);
436 matrix.preScale(scale, scale);
437 largePath.transform(matrix);
438 bool largeConvex = largePath.isConvex();
439 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
440}
441
442static void test_crbug_493450(skiatest::Reporter* reporter) {
443 const char reducedCase[] =
444 "M0,0"
445 "L0.0002, 0"
446 "L0.0002, 0.0002"
447 "L0.0001, 0.0001"
448 "L0,0.0002"
449 "Z";
450 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
451 const char originalFiddleData[] =
452 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
453 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
454 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
455 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
456 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
457 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
458 826357.3384828606f);
459}
460
461static void test_crbug_495894(skiatest::Reporter* reporter) {
462 const char originalFiddleData[] =
463 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
464 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
465 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
466 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
467 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
468 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
469 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
470 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
471 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
472 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
473 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
474 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
475 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
476 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
477 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
478 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
479 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
480 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
481 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
482 "L-0.33993994441916414,-0.11288906492739594Z";
483 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
484 65536);
485}
486
mbarbella99600d02016-06-01 15:39:47 -0700487static void test_crbug_613918() {
488 SkPath path;
489 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f);
490 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f);
491 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f);
492
493 // This call could lead to an assert or uninitialized read due to a failure
494 // to check the return value from SkCubicClipper::ChopMonoAtY.
495 path.contains(-1.84817e-08f, 1.15465e-08f);
496}
497
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000498static void test_addrect(skiatest::Reporter* reporter) {
499 SkPath path;
500 path.lineTo(0, 0);
501 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700502 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000503
504 path.reset();
505 path.lineTo(FLT_EPSILON, FLT_EPSILON);
506 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700507 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000508
509 path.reset();
510 path.quadTo(0, 0, 0, 0);
511 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700512 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000513
514 path.reset();
515 path.conicTo(0, 0, 0, 0, 0.5f);
516 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700517 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000518
519 path.reset();
520 path.cubicTo(0, 0, 0, 0, 0, 0);
521 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700522 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000523}
524
reed@google.coma8790de2012-10-24 21:04:04 +0000525// Make sure we stay non-finite once we get there (unless we reset or rewind).
526static void test_addrect_isfinite(skiatest::Reporter* reporter) {
527 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000528
reed@google.coma8790de2012-10-24 21:04:04 +0000529 path.addRect(SkRect::MakeWH(50, 100));
530 REPORTER_ASSERT(reporter, path.isFinite());
531
532 path.moveTo(0, 0);
533 path.lineTo(SK_ScalarInfinity, 42);
534 REPORTER_ASSERT(reporter, !path.isFinite());
535
536 path.addRect(SkRect::MakeWH(50, 100));
537 REPORTER_ASSERT(reporter, !path.isFinite());
538
539 path.reset();
540 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000541
reed@google.coma8790de2012-10-24 21:04:04 +0000542 path.addRect(SkRect::MakeWH(50, 100));
543 REPORTER_ASSERT(reporter, path.isFinite());
544}
545
reed@google.com848148e2013-01-15 15:51:59 +0000546static void build_big_path(SkPath* path, bool reducedCase) {
547 if (reducedCase) {
548 path->moveTo(577330, 1971.72f);
549 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
550 } else {
551 path->moveTo(60.1631f, 7.70567f);
552 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
553 path->lineTo(577379, 1977.77f);
554 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
555 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
556 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
557 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
558 path->quadTo(577198, 1972, 577238, 1971.31f);
559 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
560 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
561 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
562 path->moveTo(306.718f, -32.912f);
563 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
564 }
565}
566
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000567static void test_clipped_cubic() {
reede8f30622016-03-23 18:59:25 -0700568 auto surface(SkSurface::MakeRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000569
570 // This path used to assert, because our cubic-chopping code incorrectly
571 // moved control points after the chop. This test should be run in SK_DEBUG
572 // mode to ensure that we no long assert.
573 SkPath path;
574 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
575 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000576
reed@google.com848148e2013-01-15 15:51:59 +0000577 SkPaint paint;
578 for (int doAA = 0; doAA <= 1; ++doAA) {
579 paint.setAntiAlias(SkToBool(doAA));
580 surface->getCanvas()->drawPath(path, paint);
581 }
582 }
583}
584
reed3b2fb982015-07-29 08:37:13 -0700585static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
586 if (expected != bounds) {
587 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
588 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
589 expected.left(), expected.top(), expected.right(), expected.bottom());
590 }
591}
592
reed91f283b2015-07-28 06:00:50 -0700593static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
594 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700595#if 0
596 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
597 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700598 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
599
600 path.moveTo(-5, -8);
601 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
602
603 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
604 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
605
606 path.moveTo(1, 2);
607 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700608#else
609 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
610
611 path.moveTo(-5, -8); // should set the bounds
612 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
613
614 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
615 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
616
617 path.moveTo(1, 2); // don't expect this to have changed the bounds
618 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
619#endif
reed91f283b2015-07-28 06:00:50 -0700620}
621
reed@google.com8cae8352012-09-14 15:18:41 +0000622// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
623// which triggered an assert, from a tricky cubic. This test replicates that
624// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
625// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000626static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000627 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000628 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
629 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
630 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
631 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000632 };
633
634 SkPath path;
635 path.moveTo(pts[0]);
636 path.cubicTo(pts[1], pts[2], pts[3]);
637
638 SkPaint paint;
639 paint.setAntiAlias(true);
640
reede8f30622016-03-23 18:59:25 -0700641 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint);
reed@google.com8cae8352012-09-14 15:18:41 +0000642}
reed@android.com3abec1d2009-03-02 05:36:20 +0000643
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000644// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
645//
646static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
647 SkPath path;
648 path.quadTo(157, 366, 286, 208);
649 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000650
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000651 SkMatrix matrix;
652 matrix.setScale(1000*1000, 1000*1000);
653
654 // Be sure that path::transform correctly updates isFinite and the bounds
655 // if the transformation overflows. The previous bug was that isFinite was
656 // set to true in this case, but the bounds were not set to empty (which
657 // they should be).
658 while (path.isFinite()) {
659 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
660 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
661 path.transform(matrix);
662 }
663 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
664
665 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
666 path.transform(matrix);
667 // we need to still be non-finite
668 REPORTER_ASSERT(reporter, !path.isFinite());
669 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
670}
671
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000672static void add_corner_arc(SkPath* path, const SkRect& rect,
673 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000674 int startAngle)
675{
676
677 SkScalar rx = SkMinScalar(rect.width(), xIn);
678 SkScalar ry = SkMinScalar(rect.height(), yIn);
679
680 SkRect arcRect;
681 arcRect.set(-rx, -ry, rx, ry);
682 switch (startAngle) {
683 case 0:
684 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
685 break;
686 case 90:
687 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
688 break;
689 case 180:
690 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
691 break;
692 case 270:
693 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
694 break;
695 default:
696 break;
697 }
698
699 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
700}
701
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000702static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000703 SkScalar xCorner, SkScalar yCorner) {
704 // we are lazy here and use the same x & y for each corner
705 add_corner_arc(path, r, xCorner, yCorner, 270);
706 add_corner_arc(path, r, xCorner, yCorner, 0);
707 add_corner_arc(path, r, xCorner, yCorner, 90);
708 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000709 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000710}
711
712// Chrome creates its own round rects with each corner possibly being different.
713// Performance will suffer if they are not convex.
714// Note: PathBench::ArbRoundRectBench performs almost exactly
715// the same test (but with drawing)
716static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000717 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000718 SkRect r;
719
720 for (int i = 0; i < 5000; ++i) {
721
robertphillips@google.com158618e2012-10-23 16:56:56 +0000722 SkScalar size = rand.nextUScalar1() * 30;
723 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000724 continue;
725 }
726 r.fLeft = rand.nextUScalar1() * 300;
727 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000728 r.fRight = r.fLeft + 2 * size;
729 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000730
731 SkPath temp;
732
733 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
734
735 REPORTER_ASSERT(reporter, temp.isConvex());
736 }
737}
738
robertphillips@google.com158618e2012-10-23 16:56:56 +0000739// Chrome will sometimes create a 0 radius round rect. The degenerate
740// quads prevent the path from being converted to a rect
741// Note: PathBench::ArbRoundRectBench performs almost exactly
742// the same test (but with drawing)
743static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000744 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000745 SkRect r;
746
747 for (int i = 0; i < 5000; ++i) {
748
749 SkScalar size = rand.nextUScalar1() * 30;
750 if (size < SK_Scalar1) {
751 continue;
752 }
753 r.fLeft = rand.nextUScalar1() * 300;
754 r.fTop = rand.nextUScalar1() * 300;
755 r.fRight = r.fLeft + 2 * size;
756 r.fBottom = r.fTop + 2 * size;
757
758 SkPath temp;
759
760 make_arb_round_rect(&temp, r, 0, 0);
761
robertphillips@google.com158618e2012-10-23 16:56:56 +0000762 SkRect result;
763 REPORTER_ASSERT(reporter, temp.isRect(&result));
764 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000765 }
766}
767
reed@google.com0bb18bb2012-07-26 15:20:36 +0000768static void test_rect_isfinite(skiatest::Reporter* reporter) {
769 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000770 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000771 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000772
reed@google.com0bb18bb2012-07-26 15:20:36 +0000773 SkRect r;
774 r.setEmpty();
775 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000776 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000777 REPORTER_ASSERT(reporter, !r.isFinite());
778 r.set(0, 0, nan, 0);
779 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000780
reed@google.com0bb18bb2012-07-26 15:20:36 +0000781 SkPoint pts[] = {
782 { 0, 0 },
783 { SK_Scalar1, 0 },
784 { 0, SK_Scalar1 },
785 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000786
reed@google.com0bb18bb2012-07-26 15:20:36 +0000787 bool isFine = r.setBoundsCheck(pts, 3);
788 REPORTER_ASSERT(reporter, isFine);
789 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000790
reed@google.com0bb18bb2012-07-26 15:20:36 +0000791 pts[1].set(inf, 0);
792 isFine = r.setBoundsCheck(pts, 3);
793 REPORTER_ASSERT(reporter, !isFine);
794 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000795
reed@google.com0bb18bb2012-07-26 15:20:36 +0000796 pts[1].set(nan, 0);
797 isFine = r.setBoundsCheck(pts, 3);
798 REPORTER_ASSERT(reporter, !isFine);
799 REPORTER_ASSERT(reporter, r.isEmpty());
800}
801
802static void test_path_isfinite(skiatest::Reporter* reporter) {
803 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000804 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000805 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000806
reed@google.com0bb18bb2012-07-26 15:20:36 +0000807 SkPath path;
808 REPORTER_ASSERT(reporter, path.isFinite());
809
810 path.reset();
811 REPORTER_ASSERT(reporter, path.isFinite());
812
813 path.reset();
814 path.moveTo(SK_Scalar1, 0);
815 REPORTER_ASSERT(reporter, path.isFinite());
816
817 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000818 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000819 REPORTER_ASSERT(reporter, !path.isFinite());
820
821 path.reset();
822 path.moveTo(nan, 0);
823 REPORTER_ASSERT(reporter, !path.isFinite());
824}
825
826static void test_isfinite(skiatest::Reporter* reporter) {
827 test_rect_isfinite(reporter);
828 test_path_isfinite(reporter);
829}
830
fsb1475b02016-01-20 09:51:07 -0800831static void test_islastcontourclosed(skiatest::Reporter* reporter) {
832 SkPath path;
833 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
834 path.moveTo(0, 0);
835 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
836 path.close();
837 REPORTER_ASSERT(reporter, path.isLastContourClosed());
838 path.lineTo(100, 100);
839 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
840 path.moveTo(200, 200);
841 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
842 path.close();
843 REPORTER_ASSERT(reporter, path.isLastContourClosed());
844 path.moveTo(0, 0);
845 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
846}
847
reed@google.com744faba2012-05-29 19:54:52 +0000848// assert that we always
849// start with a moveTo
850// only have 1 moveTo
851// only have Lines after that
852// end with a single close
853// only have (at most) 1 close
854//
855static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000856 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000857 SkPath::RawIter iter(path);
858 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000859
860 bool firstTime = true;
861 bool foundClose = false;
862 for (;;) {
863 switch (iter.next(pts)) {
864 case SkPath::kMove_Verb:
865 REPORTER_ASSERT(reporter, firstTime);
866 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
867 srcPts++;
868 firstTime = false;
869 break;
870 case SkPath::kLine_Verb:
871 REPORTER_ASSERT(reporter, !firstTime);
872 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
873 srcPts++;
874 break;
875 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000876 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000877 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000878 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000879 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000880 break;
reed@google.com744faba2012-05-29 19:54:52 +0000881 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000882 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000883 break;
884 case SkPath::kClose_Verb:
885 REPORTER_ASSERT(reporter, !firstTime);
886 REPORTER_ASSERT(reporter, !foundClose);
887 REPORTER_ASSERT(reporter, expectClose);
888 foundClose = true;
889 break;
890 case SkPath::kDone_Verb:
891 goto DONE;
892 }
893 }
894DONE:
895 REPORTER_ASSERT(reporter, foundClose == expectClose);
896}
897
898static void test_addPoly(skiatest::Reporter* reporter) {
899 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000900 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000901
reed@google.com744faba2012-05-29 19:54:52 +0000902 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
903 pts[i].fX = rand.nextSScalar1();
904 pts[i].fY = rand.nextSScalar1();
905 }
906
907 for (int doClose = 0; doClose <= 1; ++doClose) {
908 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
909 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800910 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000911 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000912 }
913 }
914}
915
reed@google.com8b06f1a2012-05-29 12:03:46 +0000916static void test_strokerec(skiatest::Reporter* reporter) {
917 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
918 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000919
reed@google.com8b06f1a2012-05-29 12:03:46 +0000920 rec.setHairlineStyle();
921 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000922
reed@google.com8b06f1a2012-05-29 12:03:46 +0000923 rec.setStrokeStyle(SK_Scalar1, false);
924 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000925
reed@google.com8b06f1a2012-05-29 12:03:46 +0000926 rec.setStrokeStyle(SK_Scalar1, true);
927 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000928
reed@google.com8b06f1a2012-05-29 12:03:46 +0000929 rec.setStrokeStyle(0, false);
930 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000931
reed@google.com8b06f1a2012-05-29 12:03:46 +0000932 rec.setStrokeStyle(0, true);
933 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
934}
935
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000936// Set this for paths that don't have a consistent direction such as a bowtie.
937// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700938const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000939
940static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -0700941 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000942 if (expected == kDontCheckDir) {
943 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000944 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000945 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
946
reed026beb52015-06-10 14:23:15 -0700947 SkPathPriv::FirstDirection dir;
948 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000949 REPORTER_ASSERT(reporter, dir == expected);
950 } else {
reed026beb52015-06-10 14:23:15 -0700951 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000952 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000953}
954
reed@google.com3e71a882012-01-10 18:44:37 +0000955static void test_direction(skiatest::Reporter* reporter) {
956 size_t i;
957 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -0700958 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -0700959 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
960 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
961 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +0000962
963 static const char* gDegen[] = {
964 "M 10 10",
965 "M 10 10 M 20 20",
966 "M 10 10 L 20 20",
967 "M 10 10 L 10 10 L 10 10",
968 "M 10 10 Q 10 10 10 10",
969 "M 10 10 C 10 10 10 10 10 10",
970 };
971 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
972 path.reset();
973 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
974 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -0700975 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +0000976 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000977
reed@google.com3e71a882012-01-10 18:44:37 +0000978 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000979 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000980 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000981 "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 +0000982 // rect with top two corners replaced by cubics with identical middle
983 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000984 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
985 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000986 };
987 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
988 path.reset();
989 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
990 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700991 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000992 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000993
reed@google.com3e71a882012-01-10 18:44:37 +0000994 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000995 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000996 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000997 "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 +0000998 // rect with top two corners replaced by cubics with identical middle
999 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +00001000 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
1001 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +00001002 };
1003 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
1004 path.reset();
1005 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
1006 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -07001007 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001008 }
reed@google.comac8543f2012-01-30 20:51:25 +00001009
1010 // Test two donuts, each wound a different direction. Only the outer contour
1011 // determines the cheap direction
1012 path.reset();
1013 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1014 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001015 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001016
reed@google.comac8543f2012-01-30 20:51:25 +00001017 path.reset();
1018 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1019 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001020 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001021
1022 // triangle with one point really far from the origin.
1023 path.reset();
1024 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001025 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001026 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1027 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001028 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001029
1030 path.reset();
1031 path.conicTo(20, 0, 20, 20, 0.5f);
1032 path.close();
reed026beb52015-06-10 14:23:15 -07001033 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001034
1035 path.reset();
1036 path.lineTo(1, 1e7f);
1037 path.lineTo(1e7f, 2e7f);
1038 path.close();
1039 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001040 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001041}
1042
reed@google.comffdb0182011-11-14 19:29:14 +00001043static void add_rect(SkPath* path, const SkRect& r) {
1044 path->moveTo(r.fLeft, r.fTop);
1045 path->lineTo(r.fRight, r.fTop);
1046 path->lineTo(r.fRight, r.fBottom);
1047 path->lineTo(r.fLeft, r.fBottom);
1048 path->close();
1049}
1050
1051static void test_bounds(skiatest::Reporter* reporter) {
1052 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001053 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1054 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1055 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1056 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001057 };
1058
1059 SkPath path0, path1;
1060 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1061 path0.addRect(rects[i]);
1062 add_rect(&path1, rects[i]);
1063 }
1064
1065 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1066}
1067
reed@google.com55b5f4b2011-09-07 12:23:41 +00001068static void stroke_cubic(const SkPoint pts[4]) {
1069 SkPath path;
1070 path.moveTo(pts[0]);
1071 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001072
reed@google.com55b5f4b2011-09-07 12:23:41 +00001073 SkPaint paint;
1074 paint.setStyle(SkPaint::kStroke_Style);
1075 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001076
reed@google.com55b5f4b2011-09-07 12:23:41 +00001077 SkPath fill;
1078 paint.getFillPath(path, &fill);
1079}
1080
1081// just ensure this can run w/o any SkASSERTS firing in the debug build
1082// we used to assert due to differences in how we determine a degenerate vector
1083// but that was fixed with the introduction of SkPoint::CanNormalize
1084static void stroke_tiny_cubic() {
1085 SkPoint p0[] = {
1086 { 372.0f, 92.0f },
1087 { 372.0f, 92.0f },
1088 { 372.0f, 92.0f },
1089 { 372.0f, 92.0f },
1090 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001091
reed@google.com55b5f4b2011-09-07 12:23:41 +00001092 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001093
reed@google.com55b5f4b2011-09-07 12:23:41 +00001094 SkPoint p1[] = {
1095 { 372.0f, 92.0f },
1096 { 372.0007f, 92.000755f },
1097 { 371.99927f, 92.003922f },
1098 { 371.99826f, 92.003899f },
1099 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001100
reed@google.com55b5f4b2011-09-07 12:23:41 +00001101 stroke_cubic(p1);
1102}
1103
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001104static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1105 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001106 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001107 SkPoint mv;
1108 SkPoint pts[4];
1109 SkPath::Verb v;
1110 int nMT = 0;
1111 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001112 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001113 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1114 switch (v) {
1115 case SkPath::kMove_Verb:
1116 mv = pts[0];
1117 ++nMT;
1118 break;
1119 case SkPath::kClose_Verb:
1120 REPORTER_ASSERT(reporter, mv == pts[0]);
1121 ++nCL;
1122 break;
1123 default:
1124 break;
1125 }
1126 }
1127 // if we force a close on the interator we should have a close
1128 // for every moveTo
1129 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1130 }
1131}
1132
1133static void test_close(skiatest::Reporter* reporter) {
1134 SkPath closePt;
1135 closePt.moveTo(0, 0);
1136 closePt.close();
1137 check_close(reporter, closePt);
1138
1139 SkPath openPt;
1140 openPt.moveTo(0, 0);
1141 check_close(reporter, openPt);
1142
1143 SkPath empty;
1144 check_close(reporter, empty);
1145 empty.close();
1146 check_close(reporter, empty);
1147
1148 SkPath rect;
1149 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1150 check_close(reporter, rect);
1151 rect.close();
1152 check_close(reporter, rect);
1153
1154 SkPath quad;
1155 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1156 check_close(reporter, quad);
1157 quad.close();
1158 check_close(reporter, quad);
1159
1160 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001161 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001162 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1163 check_close(reporter, cubic);
1164 cubic.close();
1165 check_close(reporter, cubic);
1166
1167 SkPath line;
1168 line.moveTo(SK_Scalar1, SK_Scalar1);
1169 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1170 check_close(reporter, line);
1171 line.close();
1172 check_close(reporter, line);
1173
1174 SkPath rect2;
1175 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1176 rect2.close();
1177 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1178 check_close(reporter, rect2);
1179 rect2.close();
1180 check_close(reporter, rect2);
1181
1182 SkPath oval3;
1183 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1184 oval3.close();
1185 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1186 check_close(reporter, oval3);
1187 oval3.close();
1188 check_close(reporter, oval3);
1189
1190 SkPath moves;
1191 moves.moveTo(SK_Scalar1, SK_Scalar1);
1192 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1193 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1194 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1195 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001196
1197 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001198}
1199
reed@google.com7c424812011-05-15 04:38:34 +00001200static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1201 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001202 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1203 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001204 REPORTER_ASSERT(reporter, c == expected);
1205}
1206
caryclarkb4216502015-03-02 13:02:34 -08001207static void test_path_crbug389050(skiatest::Reporter* reporter) {
1208 SkPath tinyConvexPolygon;
1209 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1210 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1211 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1212 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1213 tinyConvexPolygon.close();
1214 tinyConvexPolygon.getConvexity();
1215 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001216 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001217
1218 SkPath platTriangle;
1219 platTriangle.moveTo(0, 0);
1220 platTriangle.lineTo(200, 0);
1221 platTriangle.lineTo(100, 0.04f);
1222 platTriangle.close();
1223 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001224 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001225
1226 platTriangle.reset();
1227 platTriangle.moveTo(0, 0);
1228 platTriangle.lineTo(200, 0);
1229 platTriangle.lineTo(100, 0.03f);
1230 platTriangle.close();
1231 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001232 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001233}
1234
reed@google.com7c424812011-05-15 04:38:34 +00001235static void test_convexity2(skiatest::Reporter* reporter) {
1236 SkPath pt;
1237 pt.moveTo(0, 0);
1238 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001239 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001240 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001241
reed@google.com7c424812011-05-15 04:38:34 +00001242 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001243 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1244 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001245 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001246 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001247 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001248
reed@google.com7c424812011-05-15 04:38:34 +00001249 SkPath triLeft;
1250 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001251 triLeft.lineTo(SK_Scalar1, 0);
1252 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001253 triLeft.close();
1254 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001255 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001256
reed@google.com7c424812011-05-15 04:38:34 +00001257 SkPath triRight;
1258 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001259 triRight.lineTo(-SK_Scalar1, 0);
1260 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001261 triRight.close();
1262 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001263 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001264
reed@google.com7c424812011-05-15 04:38:34 +00001265 SkPath square;
1266 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001267 square.lineTo(SK_Scalar1, 0);
1268 square.lineTo(SK_Scalar1, SK_Scalar1);
1269 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001270 square.close();
1271 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001272 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001273
reed@google.com7c424812011-05-15 04:38:34 +00001274 SkPath redundantSquare;
1275 redundantSquare.moveTo(0, 0);
1276 redundantSquare.lineTo(0, 0);
1277 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001278 redundantSquare.lineTo(SK_Scalar1, 0);
1279 redundantSquare.lineTo(SK_Scalar1, 0);
1280 redundantSquare.lineTo(SK_Scalar1, 0);
1281 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1282 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1283 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1284 redundantSquare.lineTo(0, SK_Scalar1);
1285 redundantSquare.lineTo(0, SK_Scalar1);
1286 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001287 redundantSquare.close();
1288 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001289 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001290
reed@google.com7c424812011-05-15 04:38:34 +00001291 SkPath bowTie;
1292 bowTie.moveTo(0, 0);
1293 bowTie.lineTo(0, 0);
1294 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001295 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1296 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1297 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1298 bowTie.lineTo(SK_Scalar1, 0);
1299 bowTie.lineTo(SK_Scalar1, 0);
1300 bowTie.lineTo(SK_Scalar1, 0);
1301 bowTie.lineTo(0, SK_Scalar1);
1302 bowTie.lineTo(0, SK_Scalar1);
1303 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001304 bowTie.close();
1305 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001306 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001307
reed@google.com7c424812011-05-15 04:38:34 +00001308 SkPath spiral;
1309 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001310 spiral.lineTo(100*SK_Scalar1, 0);
1311 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1312 spiral.lineTo(0, 100*SK_Scalar1);
1313 spiral.lineTo(0, 50*SK_Scalar1);
1314 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1315 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001316 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001317 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001318 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001319
reed@google.com7c424812011-05-15 04:38:34 +00001320 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001321 dent.moveTo(0, 0);
1322 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1323 dent.lineTo(0, 100*SK_Scalar1);
1324 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1325 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001326 dent.close();
1327 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001328 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001329
halcanary6950de62015-11-07 05:29:00 -08001330 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001331 SkPath strokedSin;
1332 for (int i = 0; i < 2000; i++) {
1333 SkScalar x = SkIntToScalar(i) / 2;
1334 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1335 if (0 == i) {
1336 strokedSin.moveTo(x, y);
1337 } else {
1338 strokedSin.lineTo(x, y);
1339 }
1340 }
1341 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1342 stroke.setStrokeStyle(2 * SK_Scalar1);
1343 stroke.applyToPath(&strokedSin, strokedSin);
1344 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1345 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001346
1347 // http://crbug.com/412640
1348 SkPath degenerateConcave;
1349 degenerateConcave.moveTo(148.67912f, 191.875f);
1350 degenerateConcave.lineTo(470.37695f, 7.5f);
1351 degenerateConcave.lineTo(148.67912f, 191.875f);
1352 degenerateConcave.lineTo(41.446522f, 376.25f);
1353 degenerateConcave.lineTo(-55.971577f, 460.0f);
1354 degenerateConcave.lineTo(41.446522f, 376.25f);
1355 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001356 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001357
1358 // http://crbug.com/433683
1359 SkPath badFirstVector;
1360 badFirstVector.moveTo(501.087708f, 319.610352f);
1361 badFirstVector.lineTo(501.087708f, 319.610352f);
1362 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1363 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1364 badFirstVector.lineTo(301.557678f, 98.044601f);
1365 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1366 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1367 badFirstVector.lineTo(504.912292f, 316.389648f);
1368 badFirstVector.lineTo(501.087708f, 319.610352f);
1369 badFirstVector.close();
1370 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001371}
1372
reed@android.com6b82d1a2009-06-03 02:35:01 +00001373static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1374 const SkRect& bounds) {
1375 REPORTER_ASSERT(reporter, p.isConvex());
1376 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001377
reed@android.com6b82d1a2009-06-03 02:35:01 +00001378 SkPath p2(p);
1379 REPORTER_ASSERT(reporter, p2.isConvex());
1380 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1381
1382 SkPath other;
1383 other.swap(p2);
1384 REPORTER_ASSERT(reporter, other.isConvex());
1385 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1386}
1387
reed@google.com04863fa2011-05-15 04:08:24 +00001388static void setFromString(SkPath* path, const char str[]) {
1389 bool first = true;
1390 while (str) {
1391 SkScalar x, y;
1392 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001393 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001394 break;
1395 }
1396 str = SkParse::FindScalar(str, &y);
1397 SkASSERT(str);
1398 if (first) {
1399 path->moveTo(x, y);
1400 first = false;
1401 } else {
1402 path->lineTo(x, y);
1403 }
1404 }
1405}
1406
1407static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001408 SkPath path;
1409
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001410 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001411 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001412 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001413 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001414 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1415
reed@google.com04863fa2011-05-15 04:08:24 +00001416 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001417 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001418 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001419 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001420
reed@google.com04863fa2011-05-15 04:08:24 +00001421 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001422 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001423 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001424 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001425
reed@google.com04863fa2011-05-15 04:08:24 +00001426 static const struct {
reed026beb52015-06-10 14:23:15 -07001427 const char* fPathStr;
1428 SkPath::Convexity fExpectedConvexity;
1429 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001430 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001431 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1432 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1433 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1434 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1435 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1436 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001437 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001438 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001439 };
1440
1441 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1442 SkPath path;
1443 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001444 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1445 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001446 // check after setting the initial convex and direction
1447 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1448 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001449 SkPathPriv::FirstDirection dir;
1450 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1451 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001452 ^ foundDir);
1453 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1454 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1455 }
1456 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1457 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001458 }
caryclarkd3d1a982014-12-08 04:57:38 -08001459
1460 static const SkPoint nonFinitePts[] = {
1461 { SK_ScalarInfinity, 0 },
1462 { 0, SK_ScalarInfinity },
1463 { SK_ScalarInfinity, SK_ScalarInfinity },
1464 { SK_ScalarNegativeInfinity, 0},
1465 { 0, SK_ScalarNegativeInfinity },
1466 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1467 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1468 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1469 { SK_ScalarNaN, 0 },
1470 { 0, SK_ScalarNaN },
1471 { SK_ScalarNaN, SK_ScalarNaN },
1472 };
1473
1474 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1475
1476 static const SkPoint finitePts[] = {
1477 { SK_ScalarMax, 0 },
1478 { 0, SK_ScalarMax },
1479 { SK_ScalarMax, SK_ScalarMax },
1480 { SK_ScalarMin, 0 },
1481 { 0, SK_ScalarMin },
1482 { SK_ScalarMin, SK_ScalarMin },
1483 };
1484
1485 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1486
1487 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1488 int i = (int) (index % nonFinitePtsCount);
1489 int f = (int) (index % finitePtsCount);
1490 int g = (int) ((f + 1) % finitePtsCount);
1491 path.reset();
1492 switch (index % 13) {
1493 case 0: path.lineTo(nonFinitePts[i]); break;
1494 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1495 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1496 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1497 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1498 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1499 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1500 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1501 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1502 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1503 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1504 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1505 case 12: path.moveTo(nonFinitePts[i]); break;
1506 }
1507 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1508 }
1509
1510 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1511 int f = (int) (index % finitePtsCount);
1512 int g = (int) ((f + 1) % finitePtsCount);
1513 path.reset();
1514 int curveSelect = index % 11;
1515 switch (curveSelect) {
1516 case 0: path.moveTo(finitePts[f]); break;
1517 case 1: path.lineTo(finitePts[f]); break;
1518 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1519 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1520 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1521 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1522 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1523 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1524 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1525 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1526 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1527 }
1528 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1529 : SkPath::kUnknown_Convexity);
1530 }
1531
reed@google.com04863fa2011-05-15 04:08:24 +00001532}
1533
reed@google.com7e6c4d12012-05-10 14:05:43 +00001534static void test_isLine(skiatest::Reporter* reporter) {
1535 SkPath path;
1536 SkPoint pts[2];
1537 const SkScalar value = SkIntToScalar(5);
1538
halcanary96fcdcc2015-08-27 07:41:13 -07001539 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001540
reed@google.com7e6c4d12012-05-10 14:05:43 +00001541 // set some non-zero values
1542 pts[0].set(value, value);
1543 pts[1].set(value, value);
1544 REPORTER_ASSERT(reporter, !path.isLine(pts));
1545 // check that pts was untouched
1546 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1547 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1548
1549 const SkScalar moveX = SkIntToScalar(1);
1550 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001551 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001552
1553 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001554 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001555 REPORTER_ASSERT(reporter, !path.isLine(pts));
1556 // check that pts was untouched
1557 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1558 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1559
1560 const SkScalar lineX = SkIntToScalar(2);
1561 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001562 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001563
1564 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001565 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001566
1567 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1568 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1569 REPORTER_ASSERT(reporter, path.isLine(pts));
1570 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1571 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1572
1573 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001574 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001575 REPORTER_ASSERT(reporter, !path.isLine(pts));
1576 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1577 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001578
1579 path.reset();
1580 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001581 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001582}
1583
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001584static void test_conservativelyContains(skiatest::Reporter* reporter) {
1585 SkPath path;
1586
1587 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1588 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1589
1590 // A circle that bounds kBaseRect (with a significant amount of slop)
1591 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001592 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001593 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1594
1595 // round-rect radii
1596 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001597
caryclark@google.com56f233a2012-11-19 13:06:06 +00001598 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001599 SkRect fQueryRect;
1600 bool fInRect;
1601 bool fInCircle;
1602 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001603 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001604 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001605 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001606
1607 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001608 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1609 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1610 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1611 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001612 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001613
1614 // rects with edges off by one from kBaseRect's edges
1615 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1616 kBaseRect.width(), kBaseRect.height() + 1),
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, kBaseRect.fTop,
1619 kBaseRect.width() + 1, 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,
1622 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001623 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001624 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1625 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001626 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001627 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1628 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001629 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001630 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1631 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001632 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001633 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1634 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001635 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001636
1637 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001638 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1639 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1640 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1641 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001642
1643 // far away rect
1644 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1645 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001646 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001647
1648 // very large rect containing kBaseRect
1649 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1650 kBaseRect.fTop - 5 * kBaseRect.height(),
1651 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001652 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001653
1654 // skinny rect that spans same y-range as kBaseRect
1655 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1656 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001657 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001658
1659 // short rect that spans same x-range as kBaseRect
1660 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001661 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001662
1663 // skinny rect that spans slightly larger y-range than kBaseRect
1664 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1665 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001666 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001667
1668 // short rect that spans slightly larger x-range than kBaseRect
1669 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1670 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001671 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001672 };
1673
1674 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001675 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001676 SkRect qRect = kQueries[q].fQueryRect;
1677 if (inv & 0x1) {
1678 SkTSwap(qRect.fLeft, qRect.fRight);
1679 }
1680 if (inv & 0x2) {
1681 SkTSwap(qRect.fTop, qRect.fBottom);
1682 }
1683 for (int d = 0; d < 2; ++d) {
1684 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1685 path.reset();
1686 path.addRect(kBaseRect, dir);
1687 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1688 path.conservativelyContainsRect(qRect));
1689
1690 path.reset();
1691 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1692 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1693 path.conservativelyContainsRect(qRect));
1694
1695 path.reset();
1696 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1697 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1698 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001699
1700 path.reset();
1701 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1702 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1703 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1704 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1705 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1706 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1707 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1708 path.close();
1709 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1710 path.conservativelyContainsRect(qRect));
1711
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001712 }
1713 // Slightly non-convex shape, shouldn't contain any rects.
1714 path.reset();
1715 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001716 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001717 path.lineTo(SkIntToScalar(100), 0);
1718 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1719 path.lineTo(0, SkIntToScalar(100));
1720 path.close();
1721 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1722 }
1723 }
1724
1725 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1726 path.reset();
1727 path.moveTo(0, 0);
1728 path.lineTo(SkIntToScalar(100), 0);
1729 path.lineTo(0, SkIntToScalar(100));
1730
1731 // inside, on along top edge
1732 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1733 SkIntToScalar(10),
1734 SkIntToScalar(10))));
1735 // above
1736 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1737 SkRect::MakeXYWH(SkIntToScalar(50),
1738 SkIntToScalar(-10),
1739 SkIntToScalar(10),
1740 SkIntToScalar(10))));
1741 // to the left
1742 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1743 SkIntToScalar(5),
1744 SkIntToScalar(5),
1745 SkIntToScalar(5))));
1746
1747 // outside the diagonal edge
1748 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1749 SkIntToScalar(200),
1750 SkIntToScalar(20),
1751 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001752
bsalomonb17c1292014-08-28 14:04:55 -07001753
1754 // Test that multiple move commands do not cause asserts.
1755
1756 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001757 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001758 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1759#if 0
1760 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1761 SkIntToScalar(10),
1762 SkIntToScalar(10))));
1763#endif
1764
1765 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001766 path.reset();
1767 path.moveTo(100, 100);
1768 path.moveTo(0, 0);
1769 path.lineTo(SkIntToScalar(100), 0);
1770 path.lineTo(0, SkIntToScalar(100));
1771
1772 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1773 SkIntToScalar(10),
1774 SkIntToScalar(10))));
1775
bsalomonb17c1292014-08-28 14:04:55 -07001776 // Test that multiple move commands do not cause asserts and that the function
1777 // is not confused by the multiple moves.
1778 path.reset();
1779 path.moveTo(0, 0);
1780 path.lineTo(SkIntToScalar(100), 0);
1781 path.lineTo(0, SkIntToScalar(100));
1782 path.moveTo(0, SkIntToScalar(200));
1783 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1784 path.lineTo(0, SkIntToScalar(300));
1785
1786 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1787 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1788 SkIntToScalar(10),
1789 SkIntToScalar(10))));
1790
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001791 path.reset();
1792 path.lineTo(100, 100);
1793 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001794}
1795
reed@google.comf32322b2013-10-16 15:14:04 +00001796static void test_isRect_open_close(skiatest::Reporter* reporter) {
1797 SkPath path;
1798 bool isClosed;
1799
1800 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001801 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001802
halcanary96fcdcc2015-08-27 07:41:13 -07001803 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001804 REPORTER_ASSERT(reporter, isClosed);
1805}
1806
caryclark@google.comf1316942011-07-26 19:54:45 +00001807// Simple isRect test is inline TestPath, below.
1808// test_isRect provides more extensive testing.
1809static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001810 test_isRect_open_close(reporter);
1811
caryclark@google.comf1316942011-07-26 19:54:45 +00001812 // passing tests (all moveTo / lineTo...
1813 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1814 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1815 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1816 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1817 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1818 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1819 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1820 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1821 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001822 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1823 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 +00001824 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1825 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1826 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001827 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001828
caryclark@google.comf1316942011-07-26 19:54:45 +00001829 // failing tests
1830 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1831 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1832 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1833 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1834 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1835 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1836 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1837 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001838 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1839 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1840 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001841
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001842 // no close, but we should detect them as fillably the same as a rect
1843 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1844 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1845 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1846
1847 // like c2, but we double-back on ourselves
1848 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1849 // like c2, but we overshoot the start point
1850 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1851 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001852
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001853 struct IsRectTest {
1854 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001855 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001856 bool fClose;
1857 bool fIsRect;
1858 } tests[] = {
1859 { r1, SK_ARRAY_COUNT(r1), true, true },
1860 { r2, SK_ARRAY_COUNT(r2), true, true },
1861 { r3, SK_ARRAY_COUNT(r3), true, true },
1862 { r4, SK_ARRAY_COUNT(r4), true, true },
1863 { r5, SK_ARRAY_COUNT(r5), true, true },
1864 { r6, SK_ARRAY_COUNT(r6), true, true },
1865 { r7, SK_ARRAY_COUNT(r7), true, true },
1866 { r8, SK_ARRAY_COUNT(r8), true, true },
1867 { r9, SK_ARRAY_COUNT(r9), true, true },
1868 { ra, SK_ARRAY_COUNT(ra), true, true },
1869 { rb, SK_ARRAY_COUNT(rb), true, true },
1870 { rc, SK_ARRAY_COUNT(rc), true, true },
1871 { rd, SK_ARRAY_COUNT(rd), true, true },
1872 { re, SK_ARRAY_COUNT(re), true, true },
1873 { rf, SK_ARRAY_COUNT(rf), true, true },
1874
1875 { f1, SK_ARRAY_COUNT(f1), true, false },
1876 { f2, SK_ARRAY_COUNT(f2), true, false },
1877 { f3, SK_ARRAY_COUNT(f3), true, false },
1878 { f4, SK_ARRAY_COUNT(f4), true, false },
1879 { f5, SK_ARRAY_COUNT(f5), true, false },
1880 { f6, SK_ARRAY_COUNT(f6), true, false },
1881 { f7, SK_ARRAY_COUNT(f7), true, false },
1882 { f8, SK_ARRAY_COUNT(f8), true, false },
1883 { f9, SK_ARRAY_COUNT(f9), true, false },
1884 { fa, SK_ARRAY_COUNT(fa), true, false },
1885 { fb, SK_ARRAY_COUNT(fb), true, false },
1886
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001887 { c1, SK_ARRAY_COUNT(c1), false, true },
1888 { c2, SK_ARRAY_COUNT(c2), false, true },
1889 { c3, SK_ARRAY_COUNT(c3), false, true },
1890
1891 { d1, SK_ARRAY_COUNT(d1), false, false },
1892 { d2, SK_ARRAY_COUNT(d2), false, false },
1893 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001894 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001895
1896 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001897 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001898 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1899 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001900 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1901 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1902 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001903 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001904 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001905 path.close();
1906 }
halcanary96fcdcc2015-08-27 07:41:13 -07001907 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001908
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001909 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001910 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001911 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001912 SkPath::Direction direction;
1913 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001914 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001915 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001916 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1917 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001918 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001919 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001920 } else {
1921 SkRect computed;
1922 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001923 bool isClosed = (bool)-1;
1924 SkPath::Direction direction = (SkPath::Direction) - 1;
1925 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001926 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1927 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001928 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1929 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001930 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001931 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001932
caryclark@google.comf1316942011-07-26 19:54:45 +00001933 // fail, close then line
1934 SkPath path1;
1935 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001936 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001937 path1.lineTo(r1[index].fX, r1[index].fY);
1938 }
1939 path1.close();
1940 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001941 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001942
caryclark@google.comf1316942011-07-26 19:54:45 +00001943 // fail, move in the middle
1944 path1.reset();
1945 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001946 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001947 if (index == 2) {
1948 path1.moveTo(1, .5f);
1949 }
1950 path1.lineTo(r1[index].fX, r1[index].fY);
1951 }
1952 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001953 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001954
1955 // fail, move on the edge
1956 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08001957 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001958 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
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, quad
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.quadTo(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));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001975
caryclark@google.comf1316942011-07-26 19:54:45 +00001976 // fail, cubic
1977 path1.reset();
1978 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001979 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001980 if (index == 2) {
1981 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1982 }
1983 path1.lineTo(r1[index].fX, r1[index].fY);
1984 }
1985 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001986 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001987}
1988
bsalomonedc743a2016-06-01 09:42:31 -07001989static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path,
1990 const SkRect& rect, SkPath::Direction dir, unsigned start) {
1991 SkRect r = SkRect::MakeEmpty();
1992 SkPath::Direction d = SkPath::kCCW_Direction;
1993 unsigned s = ~0U;
1994
1995 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s));
1996 REPORTER_ASSERT(reporter, r == rect);
1997 REPORTER_ASSERT(reporter, d == dir);
1998 REPORTER_ASSERT(reporter, s == start);
1999}
2000
2001static void test_is_simple_closed_rect(skiatest::Reporter* reporter) {
2002 SkRect r = SkRect::MakeEmpty();
2003 SkPath::Direction d = SkPath::kCCW_Direction;
2004 unsigned s = ~0U;
2005
2006 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70);
2007 const SkRect emptyRect = SkRect::MakeEmpty();
2008 SkPath path;
2009 for (int start = 0; start < 4; ++start) {
2010 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) {
2011 SkPath path;
2012 path.addRect(testRect, dir, start);
2013 check_simple_closed_rect(reporter, path, testRect, dir, start);
2014 path.close();
2015 check_simple_closed_rect(reporter, path, testRect, dir, start);
2016 SkPath path2 = path;
2017 path2.lineTo(10, 10);
2018 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2019 path2 = path;
2020 path2.moveTo(10, 10);
2021 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2022 path2 = path;
2023 path2.addRect(testRect, dir, start);
2024 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2025 // Make the path by hand, manually closing it.
2026 path2.reset();
2027 SkPath::RawIter iter(path);
2028 SkPath::Verb v;
2029 SkPoint verbPts[4];
2030 SkPoint firstPt = {0.f, 0.f};
2031 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) {
2032 switch(v) {
2033 case SkPath::kMove_Verb:
2034 firstPt = verbPts[0];
2035 path2.moveTo(verbPts[0]);
2036 break;
2037 case SkPath::kLine_Verb:
2038 path2.lineTo(verbPts[1]);
2039 break;
2040 default:
2041 break;
2042 }
2043 }
2044 // We haven't closed it yet...
2045 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2046 // ... now we do and test again.
2047 path2.lineTo(firstPt);
2048 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2049 // A redundant close shouldn't cause a failure.
2050 path2.close();
2051 check_simple_closed_rect(reporter, path2, testRect, dir, start);
2052 // Degenerate point and line rects are not allowed
2053 path2.reset();
2054 path2.addRect(emptyRect, dir, start);
2055 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2056 SkRect degenRect = testRect;
2057 degenRect.fLeft = degenRect.fRight;
2058 path2.reset();
2059 path2.addRect(degenRect, dir, start);
2060 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2061 degenRect = testRect;
2062 degenRect.fTop = degenRect.fBottom;
2063 path2.reset();
2064 path2.addRect(degenRect, dir, start);
2065 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s));
2066 // An inverted rect makes a rect path, but changes the winding dir and start point.
2067 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction)
2068 ? SkPath::kCCW_Direction
2069 : SkPath::kCW_Direction;
2070 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 };
2071 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 };
2072 SkRect swapRect = testRect;
2073 SkTSwap(swapRect.fLeft, swapRect.fRight);
2074 path2.reset();
2075 path2.addRect(swapRect, dir, start);
2076 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]);
2077 swapRect = testRect;
2078 SkTSwap(swapRect.fTop, swapRect.fBottom);
2079 path2.reset();
2080 path2.addRect(swapRect, dir, start);
2081 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]);
2082 }
2083 }
2084}
2085
caryclark95bc5f32015-04-08 08:34:15 -07002086static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002087 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002088 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002089 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
2090 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
2091 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002092 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002093 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
2094 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
2095 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
2096 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002097 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
2098 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 +00002099 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
2100 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
2101 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00002102
2103 // failing tests
2104 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
2105 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
2106 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2107 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2108 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2109 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2110 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2111 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2112
caryclark95bc5f32015-04-08 08:34:15 -07002113 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002114 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2115 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2116
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002117 struct IsNestedRectTest {
2118 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002119 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002120 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002121 bool fClose;
2122 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2123 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002124 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2125 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2126 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2127 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2128 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2129 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2130 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2131 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2132 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2133 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2134 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2135 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2136 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2137 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002138
reed026beb52015-06-10 14:23:15 -07002139 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2140 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2141 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2142 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2143 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2144 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2145 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2146 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002147
reed026beb52015-06-10 14:23:15 -07002148 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2149 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002150 };
2151
2152 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002153 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002154 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002155 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2156 SkPath path;
2157 if (rectFirst) {
2158 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2159 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002160 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2161 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2162 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002163 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002164 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002165 path.close();
2166 }
2167 if (!rectFirst) {
2168 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2169 }
caryclark95bc5f32015-04-08 08:34:15 -07002170 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002171 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002172 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002173 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002174 SkPathPriv::FirstDirection expectedDirs[2];
2175 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002176 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002177 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002178 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2179 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002180 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002181 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002182 } else {
reed026beb52015-06-10 14:23:15 -07002183 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002184 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002185 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002186 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002187 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2188 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002189 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2190 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002191 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002192 }
2193
2194 // fail, close then line
2195 SkPath path1;
2196 if (rectFirst) {
2197 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2198 }
2199 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002200 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002201 path1.lineTo(r1[index].fX, r1[index].fY);
2202 }
2203 path1.close();
2204 path1.lineTo(1, 0);
2205 if (!rectFirst) {
2206 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2207 }
halcanary96fcdcc2015-08-27 07:41:13 -07002208 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002209
2210 // fail, move in the middle
2211 path1.reset();
2212 if (rectFirst) {
2213 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2214 }
2215 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002216 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002217 if (index == 2) {
2218 path1.moveTo(1, .5f);
2219 }
2220 path1.lineTo(r1[index].fX, r1[index].fY);
2221 }
2222 path1.close();
2223 if (!rectFirst) {
2224 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2225 }
halcanary96fcdcc2015-08-27 07:41:13 -07002226 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002227
2228 // fail, move on the edge
2229 path1.reset();
2230 if (rectFirst) {
2231 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2232 }
bsalomon98806072014-12-12 15:11:17 -08002233 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002234 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2235 path1.lineTo(r1[index].fX, r1[index].fY);
2236 }
2237 path1.close();
2238 if (!rectFirst) {
2239 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2240 }
halcanary96fcdcc2015-08-27 07:41:13 -07002241 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002242
2243 // fail, quad
2244 path1.reset();
2245 if (rectFirst) {
2246 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2247 }
2248 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002249 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002250 if (index == 2) {
2251 path1.quadTo(1, .5f, 1, .5f);
2252 }
2253 path1.lineTo(r1[index].fX, r1[index].fY);
2254 }
2255 path1.close();
2256 if (!rectFirst) {
2257 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2258 }
halcanary96fcdcc2015-08-27 07:41:13 -07002259 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002260
2261 // fail, cubic
2262 path1.reset();
2263 if (rectFirst) {
2264 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2265 }
2266 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002267 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002268 if (index == 2) {
2269 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2270 }
2271 path1.lineTo(r1[index].fX, r1[index].fY);
2272 }
2273 path1.close();
2274 if (!rectFirst) {
2275 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2276 }
halcanary96fcdcc2015-08-27 07:41:13 -07002277 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002278
caryclark@google.com56f233a2012-11-19 13:06:06 +00002279 // fail, not nested
2280 path1.reset();
2281 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2282 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002283 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002284 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002285
caryclark95bc5f32015-04-08 08:34:15 -07002286 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2287 SkPath path;
2288 path.moveTo(0, 0);
2289 path.lineTo(10, 0);
2290 path.lineTo(10, 10);
2291 path.lineTo(0, 10);
2292 path.lineTo(0, 0);
2293 path.moveTo(1, 1);
2294 path.lineTo(9, 1);
2295 path.lineTo(9, 9);
2296 path.lineTo(1, 9);
2297 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002298 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002299
caryclark@google.combfe90372012-11-21 13:56:20 +00002300 // pass, stroke rect
2301 SkPath src, dst;
2302 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2303 SkPaint strokePaint;
2304 strokePaint.setStyle(SkPaint::kStroke_Style);
2305 strokePaint.setStrokeWidth(2);
2306 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002307 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002308}
2309
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002310static void write_and_read_back(skiatest::Reporter* reporter,
2311 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002312 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002313 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002314 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002315 SkAutoMalloc storage(size);
2316 writer.flatten(storage.get());
2317 SkReader32 reader(storage.get(), size);
2318
2319 SkPath readBack;
2320 REPORTER_ASSERT(reporter, readBack != p);
2321 reader.readPath(&readBack);
2322 REPORTER_ASSERT(reporter, readBack == p);
2323
rmistry@google.comd6176b02012-08-23 18:14:13 +00002324 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002325 p.getConvexityOrUnknown());
2326
bsalomon78d58d12016-05-27 09:17:04 -07002327 SkRect oval0, oval1;
2328 SkPath::Direction dir0, dir1;
2329 unsigned start0, start1;
halcanary96fcdcc2015-08-27 07:41:13 -07002330 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
bsalomon78d58d12016-05-27 09:17:04 -07002331 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) {
2332 REPORTER_ASSERT(reporter, oval0 == oval1);
2333 REPORTER_ASSERT(reporter, dir0 == dir1);
2334 REPORTER_ASSERT(reporter, start0 == start1);
2335 }
2336 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr));
2337 SkRRect rrect0, rrect1;
2338 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) {
2339 REPORTER_ASSERT(reporter, rrect0 == rrect1);
2340 REPORTER_ASSERT(reporter, dir0 == dir1);
2341 REPORTER_ASSERT(reporter, start0 == start1);
2342 }
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002343 const SkRect& origBounds = p.getBounds();
2344 const SkRect& readBackBounds = readBack.getBounds();
2345
2346 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2347}
2348
reed@google.com53effc52011-09-21 19:05:12 +00002349static void test_flattening(skiatest::Reporter* reporter) {
2350 SkPath p;
2351
2352 static const SkPoint pts[] = {
2353 { 0, 0 },
2354 { SkIntToScalar(10), SkIntToScalar(10) },
2355 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2356 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2357 };
2358 p.moveTo(pts[0]);
2359 p.lineTo(pts[1]);
2360 p.quadTo(pts[2], pts[3]);
2361 p.cubicTo(pts[4], pts[5], pts[6]);
2362
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002363 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002364
2365 // create a buffer that should be much larger than the path so we don't
2366 // kill our stack if writer goes too far.
2367 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002368 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002369 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002370 REPORTER_ASSERT(reporter, size1 == size2);
2371
2372 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002373 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002374 REPORTER_ASSERT(reporter, size1 == size3);
2375 REPORTER_ASSERT(reporter, p == p2);
2376
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002377 size3 = p2.readFromMemory(buffer, 0);
2378 REPORTER_ASSERT(reporter, !size3);
2379
2380 SkPath tooShort;
2381 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2382 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2383
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002384 char buffer2[1024];
2385 size3 = p2.writeToMemory(buffer2);
2386 REPORTER_ASSERT(reporter, size1 == size3);
2387 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002388
2389 // test persistence of the oval flag & convexity
2390 {
2391 SkPath oval;
2392 SkRect rect = SkRect::MakeWH(10, 10);
2393 oval.addOval(rect);
2394
2395 write_and_read_back(reporter, oval);
2396 }
reed@google.com53effc52011-09-21 19:05:12 +00002397}
2398
2399static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002400 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002401
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002402#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002403 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002404 { 0, 0 }, // move
2405 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2406 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2407 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2408#if CONIC_PERSPECTIVE_BUG_FIXED
2409 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2410#endif
reed@google.com53effc52011-09-21 19:05:12 +00002411 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002412 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002413
reed@google.com53effc52011-09-21 19:05:12 +00002414 p.moveTo(pts[0]);
2415 p.lineTo(pts[1]);
2416 p.quadTo(pts[2], pts[3]);
2417 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002418#if CONIC_PERSPECTIVE_BUG_FIXED
2419 p.conicTo(pts[4], pts[5], 0.5f);
2420#endif
2421 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002422
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002423 {
2424 SkMatrix matrix;
2425 matrix.reset();
2426 SkPath p1;
2427 p.transform(matrix, &p1);
2428 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002429 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002430
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002431
2432 {
2433 SkMatrix matrix;
2434 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2435
2436 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2437
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002438 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002439 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002440 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002441 REPORTER_ASSERT(reporter, kPtCount == count);
2442 for (int i = 0; i < count; ++i) {
2443 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2444 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2445 }
2446 }
2447
2448 {
2449 SkMatrix matrix;
2450 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002451 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002452
2453 SkPath p1;
2454 p1.moveTo(SkPoint::Make(0, 0));
2455
2456 p.transform(matrix, &p1);
2457 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002458 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002459 SkRect pBounds = p.getBounds();
2460 SkRect p1Bounds = p1.getBounds();
2461 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2462 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2463 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2464 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2465 }
2466
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002467 p.reset();
2468 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002469
2470 {
2471 SkMatrix matrix;
2472 matrix.reset();
2473 SkPath p1;
2474 p1.moveTo(SkPoint::Make(0, 0));
2475
2476 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002477 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002478 }
2479
2480
2481 {
2482 SkMatrix matrix;
2483 matrix.reset();
2484 matrix.setScaleX(-1);
2485 SkPath p1;
2486 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2487
2488 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002489 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002490 }
2491
2492 {
2493 SkMatrix matrix;
2494 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2495 SkPath p1;
2496 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2497
2498 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002499 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002500 }
reed@google.com53effc52011-09-21 19:05:12 +00002501}
2502
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002503static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002504 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002505 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002506
caryclark@google.com56f233a2012-11-19 13:06:06 +00002507 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002508 const char* testPath;
2509 const size_t numResultPts;
2510 const SkRect resultBound;
2511 const SkPath::Verb* resultVerbs;
2512 const size_t numResultVerbs;
2513 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002514
schenney@chromium.org7e963602012-06-13 17:05:43 +00002515 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2516 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2517 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2518 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2519 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2520 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2521 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2522 static const SkPath::Verb resultVerbs8[] = {
2523 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2524 };
2525 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2526 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2527 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2528 static const SkPath::Verb resultVerbs12[] = {
2529 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2530 };
2531 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2532 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2533 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2534 static const SkPath::Verb resultVerbs16[] = {
2535 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2536 };
2537 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002538 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002539 { "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 -07002540 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002541 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2542 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2543 { "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) },
2544 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2545 { "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) },
2546 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2547 { "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) },
2548 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2549 { "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) },
2550 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2551 { "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 +00002552 SK_ARRAY_COUNT(resultVerbs14)
2553 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002554 { "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) },
2555 { "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 +00002556 SK_ARRAY_COUNT(resultVerbs16)
2557 }
2558 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002559
schenney@chromium.org7e963602012-06-13 17:05:43 +00002560 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2561 p.reset();
2562 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2563 REPORTER_ASSERT(reporter, valid);
2564 REPORTER_ASSERT(reporter, !p.isEmpty());
2565 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2566 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2567 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2568 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2569 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2570 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002571 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002572}
2573
2574struct SegmentInfo {
2575 SkPath fPath;
2576 int fPointCount;
2577};
2578
reed@google.com10296cc2011-09-21 12:29:05 +00002579#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2580
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002581static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002582 SkPath p, p2;
2583
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002584 p.moveTo(0, 0);
2585 p.quadTo(100, 100, 200, 200);
2586 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2587 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002588 p2 = p;
2589 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002590 p.cubicTo(100, 100, 200, 200, 300, 300);
2591 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2592 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002593 p2 = p;
2594 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2595
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002596 p.reset();
2597 p.moveTo(0, 0);
2598 p.cubicTo(100, 100, 200, 200, 300, 300);
2599 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002600 p2 = p;
2601 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002602
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002603 REPORTER_ASSERT(reporter, !p.isEmpty());
2604}
2605
2606static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002607 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002608 SkPoint pts[4];
2609
2610 // Test an iterator with no path
2611 SkPath::Iter noPathIter;
2612 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002613
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002614 // Test that setting an empty path works
2615 noPathIter.setPath(p, false);
2616 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002617
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002618 // Test that close path makes no difference for an empty path
2619 noPathIter.setPath(p, true);
2620 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002621
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002622 // Test an iterator with an initial empty path
2623 SkPath::Iter iter(p, false);
2624 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2625
2626 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002627 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002628 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2629
rmistry@google.comd6176b02012-08-23 18:14:13 +00002630
schenney@chromium.org7e963602012-06-13 17:05:43 +00002631 struct iterTestData {
2632 const char* testPath;
2633 const bool forceClose;
2634 const bool consumeDegenerates;
2635 const size_t* numResultPtsPerVerb;
2636 const SkPoint* resultPts;
2637 const SkPath::Verb* resultVerbs;
2638 const size_t numResultVerbs;
2639 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002640
schenney@chromium.org7e963602012-06-13 17:05:43 +00002641 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2642 static const SkPath::Verb resultVerbs2[] = {
2643 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2644 };
2645 static const SkPath::Verb resultVerbs3[] = {
2646 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2647 };
2648 static const SkPath::Verb resultVerbs4[] = {
2649 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2650 };
2651 static const SkPath::Verb resultVerbs5[] = {
2652 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2653 };
2654 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002655 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2656 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2657 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2658 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002659 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002660 static const SkPoint resultPts2[] = {
2661 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2662 };
2663 static const SkPoint resultPts3[] = {
2664 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2665 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2666 };
2667 static const SkPoint resultPts4[] = {
2668 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2669 };
2670 static const SkPoint resultPts5[] = {
2671 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2672 };
2673 static const struct iterTestData gIterTests[] = {
2674 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002675 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2676 { "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 +00002677 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2678 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2679 { "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) },
2680 { "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 +00002681 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2682 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2683 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2684 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2685 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2686 { "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 +00002687 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002688
schenney@chromium.org7e963602012-06-13 17:05:43 +00002689 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2690 p.reset();
2691 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2692 REPORTER_ASSERT(reporter, valid);
2693 iter.setPath(p, gIterTests[i].forceClose);
2694 int j = 0, l = 0;
2695 do {
2696 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2697 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2698 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2699 }
2700 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2701 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2702 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002703
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002704 p.reset();
2705 iter.setPath(p, false);
2706 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2707 p.lineTo(1, 1);
2708 p.close();
2709 iter.setPath(p, false);
2710 REPORTER_ASSERT(reporter, iter.isClosedContour());
2711 p.reset();
2712 iter.setPath(p, true);
2713 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2714 p.lineTo(1, 1);
2715 iter.setPath(p, true);
2716 REPORTER_ASSERT(reporter, iter.isClosedContour());
2717 p.moveTo(0, 0);
2718 p.lineTo(2, 2);
2719 iter.setPath(p, false);
2720 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2721
2722 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2723 // check to see if the result is correct.
2724 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2725 p.reset();
2726 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2727 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2728 iter.setPath(p, true);
2729 iter.next(pts, false);
2730 iter.next(pts, false);
2731 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2732 }
2733
2734 p.reset();
2735 p.quadTo(0, 0, 0, 0);
2736 iter.setPath(p, false);
2737 iter.next(pts, false);
2738 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2739 iter.setPath(p, false);
2740 iter.next(pts, false);
2741 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2742
2743 p.reset();
2744 p.conicTo(0, 0, 0, 0, 0.5f);
2745 iter.setPath(p, false);
2746 iter.next(pts, false);
2747 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2748 iter.setPath(p, false);
2749 iter.next(pts, false);
2750 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2751
2752 p.reset();
2753 p.cubicTo(0, 0, 0, 0, 0, 0);
2754 iter.setPath(p, false);
2755 iter.next(pts, false);
2756 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2757 iter.setPath(p, false);
2758 iter.next(pts, false);
2759 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2760
2761 p.moveTo(1, 1); // add a trailing moveto
2762 iter.setPath(p, false);
2763 iter.next(pts, false);
2764 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2765 iter.setPath(p, false);
2766 iter.next(pts, false);
2767 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2768
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002769 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002770
2771 // Test out mixed degenerate and non-degenerate geometry with Conics
2772 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2773 SkRect r = SkRect::MakeWH(100, 100);
2774 SkRRect rr;
2775 rr.setRectRadii(r, radii);
2776 p.reset();
2777 p.addRRect(rr);
2778 iter.setPath(p, false);
2779 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2780 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2781 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2782 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2783 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002784}
2785
2786static void test_raw_iter(skiatest::Reporter* reporter) {
2787 SkPath p;
2788 SkPoint pts[4];
2789
2790 // Test an iterator with no path
2791 SkPath::RawIter noPathIter;
2792 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2793 // Test that setting an empty path works
2794 noPathIter.setPath(p);
2795 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002796
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002797 // Test an iterator with an initial empty path
2798 SkPath::RawIter iter(p);
2799 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2800
2801 // Test that a move-only path returns the move.
2802 p.moveTo(SK_Scalar1, 0);
2803 iter.setPath(p);
2804 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2805 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2806 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2807 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2808
2809 // No matter how many moves we add, we should get them all back
2810 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2811 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2812 iter.setPath(p);
2813 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2814 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2815 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2816 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2817 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2818 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2819 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2820 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2821 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2822 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2823
2824 // Initial close is never ever stored
2825 p.reset();
2826 p.close();
2827 iter.setPath(p);
2828 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2829
2830 // Move/close sequences
2831 p.reset();
2832 p.close(); // Not stored, no purpose
2833 p.moveTo(SK_Scalar1, 0);
2834 p.close();
2835 p.close(); // Not stored, no purpose
2836 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2837 p.close();
2838 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2839 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2840 p.close();
2841 iter.setPath(p);
2842 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2843 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2844 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2845 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002846 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2847 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2848 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2849 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002850 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2851 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2852 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2853 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2854 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2855 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2856 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002857 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2858
2859 // Generate random paths and verify
2860 SkPoint randomPts[25];
2861 for (int i = 0; i < 5; ++i) {
2862 for (int j = 0; j < 5; ++j) {
2863 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2864 }
2865 }
2866
2867 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002868 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002869 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002870 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002871 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002872
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002873 for (int i = 0; i < 500; ++i) {
2874 p.reset();
2875 bool lastWasClose = true;
2876 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002877 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002878 int numPoints = 0;
2879 int numVerbs = (rand.nextU() >> 16) % 10;
2880 int numIterVerbs = 0;
2881 for (int j = 0; j < numVerbs; ++j) {
2882 do {
2883 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2884 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002885 switch (nextVerb) {
2886 case SkPath::kMove_Verb:
2887 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2888 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002889 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002890 numPoints += 1;
2891 lastWasClose = false;
2892 haveMoveTo = true;
2893 break;
2894 case SkPath::kLine_Verb:
2895 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002896 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002897 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2898 haveMoveTo = true;
2899 }
2900 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2901 p.lineTo(expectedPts[numPoints]);
2902 numPoints += 1;
2903 lastWasClose = false;
2904 break;
2905 case SkPath::kQuad_Verb:
2906 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002907 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002908 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2909 haveMoveTo = true;
2910 }
2911 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2912 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2913 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2914 numPoints += 2;
2915 lastWasClose = false;
2916 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002917 case SkPath::kConic_Verb:
2918 if (!haveMoveTo) {
2919 expectedPts[numPoints++] = lastMoveToPt;
2920 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2921 haveMoveTo = true;
2922 }
2923 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2924 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2925 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2926 rand.nextUScalar1() * 4);
2927 numPoints += 2;
2928 lastWasClose = false;
2929 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002930 case SkPath::kCubic_Verb:
2931 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002932 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002933 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2934 haveMoveTo = true;
2935 }
2936 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2937 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2938 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2939 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2940 expectedPts[numPoints + 2]);
2941 numPoints += 3;
2942 lastWasClose = false;
2943 break;
2944 case SkPath::kClose_Verb:
2945 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002946 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002947 lastWasClose = true;
2948 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002949 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002950 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002951 }
2952 expectedVerbs[numIterVerbs++] = nextVerb;
2953 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002954
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002955 iter.setPath(p);
2956 numVerbs = numIterVerbs;
2957 numIterVerbs = 0;
2958 int numIterPts = 0;
2959 SkPoint lastMoveTo;
2960 SkPoint lastPt;
2961 lastMoveTo.set(0, 0);
2962 lastPt.set(0, 0);
2963 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2964 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2965 numIterVerbs++;
2966 switch (nextVerb) {
2967 case SkPath::kMove_Verb:
2968 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2969 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2970 lastPt = lastMoveTo = pts[0];
2971 numIterPts += 1;
2972 break;
2973 case SkPath::kLine_Verb:
2974 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2975 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2976 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2977 lastPt = pts[1];
2978 numIterPts += 1;
2979 break;
2980 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002981 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002982 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2983 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2984 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2985 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2986 lastPt = pts[2];
2987 numIterPts += 2;
2988 break;
2989 case SkPath::kCubic_Verb:
2990 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2991 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2992 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2993 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2994 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2995 lastPt = pts[3];
2996 numIterPts += 3;
2997 break;
2998 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002999 lastPt = lastMoveTo;
3000 break;
reed@google.com277c3f82013-05-31 15:17:50 +00003001 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00003002 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003003 }
3004 }
3005 REPORTER_ASSERT(reporter, numIterPts == numPoints);
3006 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
3007 }
3008}
3009
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003010static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003011 const SkPath& path,
3012 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07003013 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00003014 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003015 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
bsalomon78d58d12016-05-27 09:17:04 -07003016 SkPath::Direction isOvalDir;
3017 unsigned isOvalStart;
3018 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003019 REPORTER_ASSERT(reporter, rect.height() == rect.width());
bsalomon78d58d12016-05-27 09:17:04 -07003020 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir);
3021 SkPath tmpPath;
3022 tmpPath.addOval(rect, isOvalDir, isOvalStart);
3023 REPORTER_ASSERT(reporter, path == tmpPath);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003024 }
bsalomon78d58d12016-05-27 09:17:04 -07003025 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003026}
3027
3028static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003029 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003030 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003031 SkPath tmp;
3032
3033 SkMatrix m;
3034 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
3035 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003036 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07003037 if (SkPathPriv::kCCW_FirstDirection == dir) {
3038 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003039 } else {
reed026beb52015-06-10 14:23:15 -07003040 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
3041 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003042 }
3043 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003044}
3045
3046static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003047 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003048 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003049 SkPath tmp;
3050
3051 // translate at small offset
3052 SkMatrix m;
3053 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
3054 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003055 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003056
3057 tmp.reset();
3058 m.reset();
3059
3060 // translate at a relatively big offset
3061 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
3062 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003063 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003064}
3065
3066static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003067 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003068 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003069 for (int angle = 0; angle < 360; ++angle) {
3070 SkPath tmp;
3071 SkMatrix m;
3072 m.setRotate(SkIntToScalar(angle));
3073 path.transform(m, &tmp);
3074
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003075 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003076 // degrees is not an oval anymore, this can be improved. we made this
3077 // for the simplicity of our implementation.
3078 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003079 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003080 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003081 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003082 }
3083 }
3084}
3085
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003086static void test_circle_mirror_x(skiatest::Reporter* reporter,
3087 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003088 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003089 SkPath tmp;
3090 SkMatrix m;
3091 m.reset();
3092 m.setScaleX(-SK_Scalar1);
3093 path.transform(m, &tmp);
reed026beb52015-06-10 14:23:15 -07003094 if (SkPathPriv::kCW_FirstDirection == dir) {
3095 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003096 } else {
reed026beb52015-06-10 14:23:15 -07003097 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3098 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003099 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003100 check_for_circle(reporter, tmp, true, dir);
3101}
3102
3103static void test_circle_mirror_y(skiatest::Reporter* reporter,
3104 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003105 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003106 SkPath tmp;
3107 SkMatrix m;
3108 m.reset();
3109 m.setScaleY(-SK_Scalar1);
3110 path.transform(m, &tmp);
3111
reed026beb52015-06-10 14:23:15 -07003112 if (SkPathPriv::kCW_FirstDirection == dir) {
3113 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003114 } else {
reed026beb52015-06-10 14:23:15 -07003115 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
3116 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003117 }
3118
3119 check_for_circle(reporter, tmp, true, dir);
3120}
3121
3122static void test_circle_mirror_xy(skiatest::Reporter* reporter,
3123 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003124 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003125 SkPath tmp;
3126 SkMatrix m;
3127 m.reset();
3128 m.setScaleX(-SK_Scalar1);
3129 m.setScaleY(-SK_Scalar1);
3130 path.transform(m, &tmp);
3131
3132 check_for_circle(reporter, tmp, true, dir);
3133}
3134
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003135static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003136 SkPath::Direction inDir) {
3137 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003138 SkPath path;
3139
3140 // circle at origin
reed026beb52015-06-10 14:23:15 -07003141 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3142
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003143 check_for_circle(reporter, path, true, dir);
3144 test_circle_rotate(reporter, path, dir);
3145 test_circle_translate(reporter, path, dir);
3146 test_circle_skew(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003147 test_circle_mirror_x(reporter, path, dir);
3148 test_circle_mirror_y(reporter, path, dir);
3149 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003150
3151 // circle at an offset at (10, 10)
3152 path.reset();
3153 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003154 SkIntToScalar(20), inDir);
3155
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003156 check_for_circle(reporter, path, true, dir);
3157 test_circle_rotate(reporter, path, dir);
3158 test_circle_translate(reporter, path, dir);
3159 test_circle_skew(reporter, path, dir);
3160 test_circle_mirror_x(reporter, path, dir);
3161 test_circle_mirror_y(reporter, path, dir);
3162 test_circle_mirror_xy(reporter, path, dir);
bsalomon78d58d12016-05-27 09:17:04 -07003163
3164 // Try different starting points for the contour.
3165 for (unsigned start = 0; start < 4; ++start) {
3166 path.reset();
3167 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start);
3168 test_circle_rotate(reporter, path, dir);
3169 test_circle_translate(reporter, path, dir);
3170 test_circle_skew(reporter, path, dir);
3171 test_circle_mirror_x(reporter, path, dir);
3172 test_circle_mirror_y(reporter, path, dir);
3173 test_circle_mirror_xy(reporter, path, dir);
3174 }
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003175}
3176
3177static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3178 SkPath path;
3179 SkPath circle;
3180 SkPath rect;
3181 SkPath empty;
3182
reed026beb52015-06-10 14:23:15 -07003183 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3184 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003185
3186 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003187 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3188 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3189
3190 SkMatrix translate;
3191 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3192
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003193 // Although all the path concatenation related operations leave
3194 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003195
3196 // empty + circle (translate)
3197 path = empty;
3198 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003199 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003200
3201 // circle + empty (translate)
3202 path = circle;
3203 path.addPath(empty, translate);
bsalomon78d58d12016-05-27 09:17:04 -07003204
reed026beb52015-06-10 14:23:15 -07003205 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003206
3207 // test reverseAddPath
3208 path = circle;
3209 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003210 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003211}
3212
3213static void test_circle(skiatest::Reporter* reporter) {
3214 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3215 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3216
3217 // multiple addCircle()
3218 SkPath path;
3219 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3220 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003221 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003222
3223 // some extra lineTo() would make isOval() fail
3224 path.reset();
3225 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3226 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003227 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003228
3229 // not back to the original point
3230 path.reset();
3231 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3232 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003233 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003234
3235 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003236
3237 // test negative radius
3238 path.reset();
3239 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3240 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003241}
3242
3243static void test_oval(skiatest::Reporter* reporter) {
3244 SkRect rect;
3245 SkMatrix m;
3246 SkPath path;
bsalomon78d58d12016-05-27 09:17:04 -07003247 unsigned start = 0;
3248 SkPath::Direction dir = SkPath::kCCW_Direction;
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003249
3250 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3251 path.addOval(rect);
3252
bsalomon78d58d12016-05-27 09:17:04 -07003253 // Defaults to dir = CW and start = 1
halcanary96fcdcc2015-08-27 07:41:13 -07003254 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003255
3256 m.setRotate(SkIntToScalar(90));
3257 SkPath tmp;
3258 path.transform(m, &tmp);
bsalomon78d58d12016-05-27 09:17:04 -07003259 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction
3260 // is unchanged.
3261 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start));
3262 REPORTER_ASSERT(reporter, 2 == start);
3263 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003264
3265 m.reset();
3266 m.setRotate(SkIntToScalar(30));
3267 tmp.reset();
3268 path.transform(m, &tmp);
3269 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003270 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003271
3272 // since empty path being transformed.
3273 path.reset();
3274 tmp.reset();
3275 m.reset();
3276 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003277 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003278
3279 // empty path is not an oval
3280 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003281 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003282
3283 // only has moveTo()s
3284 tmp.reset();
3285 tmp.moveTo(0, 0);
3286 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003287 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003288
3289 // mimic WebKit's calling convention,
3290 // call moveTo() first and then call addOval()
3291 path.reset();
3292 path.moveTo(0, 0);
3293 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003294 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003295
3296 // copy path
3297 path.reset();
3298 tmp.reset();
3299 tmp.addOval(rect);
3300 path = tmp;
bsalomon78d58d12016-05-27 09:17:04 -07003301 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start));
3302 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3303 REPORTER_ASSERT(reporter, 1 == start);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003304}
3305
bungeman@google.coma5809a32013-06-21 15:13:34 +00003306static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3307 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003308
reed@android.com3abec1d2009-03-02 05:36:20 +00003309 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003310 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003311 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003312 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003313 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003314 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3315 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003316 REPORTER_ASSERT(reporter, p == empty);
3317 REPORTER_ASSERT(reporter, !(p != empty));
3318}
3319
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003320static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3321 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003322 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003323 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003324 path->setConvexity(SkPath::kUnknown_Convexity);
3325 REPORTER_ASSERT(reporter, path->isConvex());
3326 path->reset();
3327}
3328
caryclarkd3d1a982014-12-08 04:57:38 -08003329static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3330 SkPath::Direction dir) {
3331 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003332 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003333 path->setConvexity(SkPath::kUnknown_Convexity);
3334 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3335 path->reset();
3336}
3337
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003338static void test_rrect(skiatest::Reporter* reporter) {
3339 SkPath p;
3340 SkRRect rr;
3341 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3342 SkRect r = {10, 20, 30, 40};
3343 rr.setRectRadii(r, radii);
3344 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003345 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003346 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003347 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003348 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003349 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003350 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003351 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003352 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003353 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003354 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003355 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3356 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3357 SkVector save = radii[i];
3358 radii[i].set(0, 0);
3359 rr.setRectRadii(r, radii);
3360 p.addRRect(rr);
3361 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3362 radii[i] = save;
3363 }
3364 p.addRoundRect(r, 0, 0);
3365 SkRect returnedRect;
3366 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3367 REPORTER_ASSERT(reporter, returnedRect == r);
3368 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3369 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3370 rr.setRectRadii(r, zeroRadii);
3371 p.addRRect(rr);
3372 bool closed;
3373 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003374 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003375 REPORTER_ASSERT(reporter, closed);
3376 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3377 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3378 p.addRRect(rr, SkPath::kCW_Direction);
3379 p.addRRect(rr, SkPath::kCW_Direction);
3380 REPORTER_ASSERT(reporter, !p.isConvex());
3381 p.reset();
3382 p.addRRect(rr, SkPath::kCCW_Direction);
3383 p.addRRect(rr, SkPath::kCCW_Direction);
3384 REPORTER_ASSERT(reporter, !p.isConvex());
3385 p.reset();
3386 SkRect emptyR = {10, 20, 10, 30};
3387 rr.setRectRadii(emptyR, radii);
3388 p.addRRect(rr);
3389 REPORTER_ASSERT(reporter, p.isEmpty());
3390 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3391 rr.setRectRadii(largeR, radii);
3392 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003393 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003394
3395 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003396 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3397 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003398 REPORTER_ASSERT(reporter, rr.isEmpty());
3399
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003400 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3401 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3402 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003403}
3404
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003405static void test_arc(skiatest::Reporter* reporter) {
3406 SkPath p;
3407 SkRect emptyOval = {10, 20, 30, 20};
3408 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3409 p.addArc(emptyOval, 1, 2);
3410 REPORTER_ASSERT(reporter, p.isEmpty());
3411 p.reset();
3412 SkRect oval = {10, 20, 30, 40};
3413 p.addArc(oval, 1, 0);
3414 REPORTER_ASSERT(reporter, p.isEmpty());
3415 p.reset();
3416 SkPath cwOval;
3417 cwOval.addOval(oval);
bsalomon1978ce22016-05-31 10:42:16 -07003418 p.addArc(oval, 0, 360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003419 REPORTER_ASSERT(reporter, p == cwOval);
3420 p.reset();
3421 SkPath ccwOval;
3422 ccwOval.addOval(oval, SkPath::kCCW_Direction);
bsalomon1978ce22016-05-31 10:42:16 -07003423 p.addArc(oval, 0, -360);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003424 REPORTER_ASSERT(reporter, p == ccwOval);
3425 p.reset();
3426 p.addArc(oval, 1, 180);
3427 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003428 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003429 p.setConvexity(SkPath::kUnknown_Convexity);
3430 REPORTER_ASSERT(reporter, p.isConvex());
3431}
3432
bsalomon1978ce22016-05-31 10:42:16 -07003433static inline SkScalar oval_start_index_to_angle(unsigned start) {
3434 switch (start) {
3435 case 0:
3436 return 270.f;
3437 case 1:
3438 return 0.f;
3439 case 2:
3440 return 90.f;
3441 case 3:
3442 return 180.f;
3443 default:
3444 return -1.f;
3445 }
3446}
3447
3448static inline SkScalar canonical_start_angle(float angle) {
3449 while (angle < 0.f) {
3450 angle += 360.f;
3451 }
3452 while (angle >= 360.f) {
3453 angle -= 360.f;
3454 }
3455 return angle;
3456}
3457
3458static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep,
3459 const SkPath& path) {
3460 SkRect r = SkRect::MakeEmpty();
3461 SkPath::Direction d = SkPath::kCCW_Direction;
3462 unsigned s = ~0U;
3463 bool isOval = path.isOval(&r, &d, &s);
3464 REPORTER_ASSERT(reporter, isOval);
3465 SkPath recreatedPath;
3466 recreatedPath.addOval(r, d, s);
3467 REPORTER_ASSERT(reporter, path == recreatedPath);
3468 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start));
3469 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f));
3470}
3471
3472static void test_arc_ovals(skiatest::Reporter* reporter) {
3473 SkRect oval = SkRect::MakeWH(10, 20);
3474 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) {
3475 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) {
3476 SkPath path;
3477 path.addArc(oval, start, sweep);
3478 // SkPath's interfaces for inserting and extracting ovals only allow contours
3479 // to start at multiples of 90 degrees.
3480 if (std::fmod(start, 90.f) == 0) {
3481 check_oval_arc(reporter, start, sweep, path);
3482 } else {
3483 REPORTER_ASSERT(reporter, !path.isOval(nullptr));
3484 }
3485 }
3486 // Test start angles that are nearly at valid oval start angles.
3487 for (float start : {-180.f, -90.f, 90.f, 180.f}) {
3488 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) {
3489 SkPath path;
3490 path.addArc(oval, start + delta, sweep);
3491 check_oval_arc(reporter, start, sweep, path);
3492 }
3493 }
3494 }
3495}
3496
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003497static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3498 SkScalar x0, SkScalar y0) {
3499 SkPoint pts[4];
3500 SkPath::Verb v = iter->next(pts);
3501 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3502 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3503 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3504}
3505
3506static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3507 SkScalar x1, SkScalar y1) {
3508 SkPoint pts[4];
3509 SkPath::Verb v = iter->next(pts);
3510 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3511 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3512 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3513}
3514
3515static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3516 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3517 SkPoint pts[4];
3518 SkPath::Verb v = iter->next(pts);
3519 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3520 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3521 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3522 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3523 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3524}
3525
3526static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3527 SkPoint pts[4];
3528 SkPath::Verb v = iter->next(pts);
3529 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3530}
3531
3532static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3533 check_done(reporter, p, iter);
3534 p->reset();
3535}
3536
3537static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3538 SkScalar x0, SkScalar y0) {
3539 SkPath::RawIter iter(*p);
3540 check_move(reporter, &iter, x0, y0);
3541 check_done_and_reset(reporter, p, &iter);
3542}
3543
3544static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3545 SkScalar x1, SkScalar y1) {
3546 SkPath::RawIter iter(*p);
3547 check_move(reporter, &iter, 0, 0);
3548 check_line(reporter, &iter, x1, y1);
3549 check_done_and_reset(reporter, p, &iter);
3550}
3551
3552static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3553 SkScalar x1, SkScalar y1) {
3554 SkPath::RawIter iter(*p);
3555 check_move(reporter, &iter, 0, 0);
3556 check_line(reporter, &iter, x1, y1);
3557 check_done(reporter, p, &iter);
3558}
3559
3560static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3561 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3562 SkPath::RawIter iter(*p);
3563 check_move(reporter, &iter, 0, 0);
3564 check_line(reporter, &iter, x1, y1);
3565 check_line(reporter, &iter, x2, y2);
3566 check_done_and_reset(reporter, p, &iter);
3567}
3568
3569static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3570 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3571 SkPath::RawIter iter(*p);
3572 check_move(reporter, &iter, 0, 0);
3573 check_quad(reporter, &iter, x1, y1, x2, y2);
3574 check_done_and_reset(reporter, p, &iter);
3575}
3576
reedd5d27d92015-02-09 13:54:43 -08003577static bool nearly_equal(const SkRect& a, const SkRect& b) {
3578 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3579 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3580 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3581 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3582}
3583
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003584static void test_arcTo(skiatest::Reporter* reporter) {
3585 SkPath p;
3586 p.arcTo(0, 0, 1, 2, 1);
3587 check_path_is_line_and_reset(reporter, &p, 0, 0);
3588 p.arcTo(1, 2, 1, 2, 1);
3589 check_path_is_line_and_reset(reporter, &p, 1, 2);
3590 p.arcTo(1, 2, 3, 4, 0);
3591 check_path_is_line_and_reset(reporter, &p, 1, 2);
3592 p.arcTo(1, 2, 0, 0, 1);
3593 check_path_is_line_and_reset(reporter, &p, 1, 2);
3594 p.arcTo(1, 0, 1, 1, 1);
3595 SkPoint pt;
3596 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3597 p.reset();
3598 p.arcTo(1, 0, 1, -1, 1);
3599 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3600 p.reset();
3601 SkRect oval = {1, 2, 3, 4};
3602 p.arcTo(oval, 0, 0, true);
3603 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3604 p.arcTo(oval, 0, 0, false);
3605 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3606 p.arcTo(oval, 360, 0, true);
3607 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3608 p.arcTo(oval, 360, 0, false);
3609 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003610
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003611 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003612 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003613 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003614 sweep += delta;
3615 delta /= 2;
3616 }
3617 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003618 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003619 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003620 sweep -= delta;
3621 delta /= 2;
3622 }
3623 SkRect noOvalWidth = {1, 2, 0, 3};
3624 p.reset();
3625 p.arcTo(noOvalWidth, 0, 360, false);
3626 REPORTER_ASSERT(reporter, p.isEmpty());
3627
3628 SkRect noOvalHeight = {1, 2, 3, 1};
3629 p.reset();
3630 p.arcTo(noOvalHeight, 0, 360, false);
3631 REPORTER_ASSERT(reporter, p.isEmpty());
3632}
3633
3634static void test_addPath(skiatest::Reporter* reporter) {
3635 SkPath p, q;
3636 p.lineTo(1, 2);
3637 q.moveTo(4, 4);
3638 q.lineTo(7, 8);
3639 q.conicTo(8, 7, 6, 5, 0.5f);
3640 q.quadTo(6, 7, 8, 6);
3641 q.cubicTo(5, 6, 7, 8, 7, 5);
3642 q.close();
3643 p.addPath(q, -4, -4);
3644 SkRect expected = {0, 0, 4, 4};
3645 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3646 p.reset();
3647 p.reverseAddPath(q);
3648 SkRect reverseExpected = {4, 4, 8, 8};
3649 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3650}
3651
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003652static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3653 SkPath p, q;
3654 if (explicitMoveTo) {
3655 p.moveTo(1, 1);
3656 }
3657 p.lineTo(1, 2);
3658 if (explicitMoveTo) {
3659 q.moveTo(2, 1);
3660 }
3661 q.lineTo(2, 2);
3662 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3663 uint8_t verbs[4];
3664 int verbcount = p.getVerbs(verbs, 4);
3665 REPORTER_ASSERT(reporter, verbcount == 4);
3666 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3667 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3668 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3669 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3670}
3671
3672static void test_extendClosedPath(skiatest::Reporter* reporter) {
3673 SkPath p, q;
3674 p.moveTo(1, 1);
3675 p.lineTo(1, 2);
3676 p.lineTo(2, 2);
3677 p.close();
3678 q.moveTo(2, 1);
3679 q.lineTo(2, 3);
3680 p.addPath(q, SkPath::kExtend_AddPathMode);
3681 uint8_t verbs[7];
3682 int verbcount = p.getVerbs(verbs, 7);
3683 REPORTER_ASSERT(reporter, verbcount == 7);
3684 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3685 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3686 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3687 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3688 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3689 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3690 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3691
3692 SkPoint pt;
3693 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3694 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3695 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3696}
3697
3698static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3699 SkPath p, q, r;
3700 // case 1: dst is empty
3701 p.moveTo(2, 1);
3702 p.lineTo(2, 3);
3703 q.addPath(p, mode);
3704 REPORTER_ASSERT(reporter, q == p);
3705 // case 2: src is empty
3706 p.addPath(r, mode);
3707 REPORTER_ASSERT(reporter, q == p);
3708 // case 3: src and dst are empty
3709 q.reset();
3710 q.addPath(r, mode);
3711 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003712}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003713
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003714static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3715 SkPath p;
3716 p.conicTo(1, 2, 3, 4, -1);
3717 check_path_is_line_and_reset(reporter, &p, 3, 4);
3718 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3719 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3720 p.conicTo(1, 2, 3, 4, 1);
3721 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3722}
3723
3724static void test_get_point(skiatest::Reporter* reporter) {
3725 SkPath p;
3726 SkPoint pt = p.getPoint(0);
3727 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003728 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003729 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3730 p.setLastPt(10, 10);
3731 pt = p.getPoint(0);
3732 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003733 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003734 p.rMoveTo(10, 10);
3735 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3736}
3737
3738static void test_contains(skiatest::Reporter* reporter) {
3739 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003740 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3741 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
3742 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
3743 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3744 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
3745 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 -08003746 // this may return true or false, depending on the platform's numerics, but it should not crash
3747 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003748
3749 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003750 p.setFillType(SkPath::kInverseWinding_FillType);
3751 REPORTER_ASSERT(reporter, p.contains(0, 0));
3752 p.setFillType(SkPath::kWinding_FillType);
3753 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3754 p.moveTo(4, 4);
3755 p.lineTo(6, 8);
3756 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003757 // test on edge
3758 REPORTER_ASSERT(reporter, p.contains(6, 4));
3759 REPORTER_ASSERT(reporter, p.contains(5, 6));
3760 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003761 // test quick reject
3762 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3763 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3764 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3765 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3766 // test various crossings in x
3767 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3768 REPORTER_ASSERT(reporter, p.contains(6, 7));
3769 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3770 p.reset();
3771 p.moveTo(4, 4);
3772 p.lineTo(8, 6);
3773 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003774 // test on edge
3775 REPORTER_ASSERT(reporter, p.contains(4, 6));
3776 REPORTER_ASSERT(reporter, p.contains(6, 5));
3777 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003778 // test various crossings in y
3779 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3780 REPORTER_ASSERT(reporter, p.contains(7, 6));
3781 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003782 p.reset();
3783 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003784 p.lineTo(8, 4);
3785 p.lineTo(8, 8);
3786 p.lineTo(4, 8);
3787 // test on vertices
3788 REPORTER_ASSERT(reporter, p.contains(4, 4));
3789 REPORTER_ASSERT(reporter, p.contains(8, 4));
3790 REPORTER_ASSERT(reporter, p.contains(8, 8));
3791 REPORTER_ASSERT(reporter, p.contains(4, 8));
3792 p.reset();
3793 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003794 p.lineTo(6, 8);
3795 p.lineTo(2, 8);
3796 // test on edge
3797 REPORTER_ASSERT(reporter, p.contains(5, 6));
3798 REPORTER_ASSERT(reporter, p.contains(4, 8));
3799 REPORTER_ASSERT(reporter, p.contains(3, 6));
3800 p.reset();
3801 p.moveTo(4, 4);
3802 p.lineTo(0, 6);
3803 p.lineTo(4, 8);
3804 // test on edge
3805 REPORTER_ASSERT(reporter, p.contains(2, 5));
3806 REPORTER_ASSERT(reporter, p.contains(2, 7));
3807 REPORTER_ASSERT(reporter, p.contains(4, 6));
3808 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3809 p.reset();
3810 p.moveTo(4, 0);
3811 p.lineTo(6, 4);
3812 p.lineTo(2, 4);
3813 p.moveTo(4, 0);
3814 p.lineTo(0, 8);
3815 p.lineTo(8, 8);
3816 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3817 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3818 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3819 REPORTER_ASSERT(reporter, p.contains(4, 4));
3820
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003821 // test quads
3822 p.reset();
3823 p.moveTo(4, 4);
3824 p.quadTo(6, 6, 8, 8);
3825 p.quadTo(6, 8, 4, 8);
3826 p.quadTo(4, 6, 4, 4);
3827 REPORTER_ASSERT(reporter, p.contains(5, 6));
3828 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003829 // test quad edge
3830 REPORTER_ASSERT(reporter, p.contains(5, 5));
3831 REPORTER_ASSERT(reporter, p.contains(5, 8));
3832 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003833 // test quad endpoints
3834 REPORTER_ASSERT(reporter, p.contains(4, 4));
3835 REPORTER_ASSERT(reporter, p.contains(8, 8));
3836 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003837
3838 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003839 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3840 p.moveTo(qPts[0]);
3841 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3842 p.quadTo(qPts[index], qPts[index + 1]);
3843 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003844 REPORTER_ASSERT(reporter, p.contains(5, 6));
3845 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003846 // test quad edge
3847 SkPoint halfway;
3848 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3849 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3850 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3851 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003852
caryclark9aacd902015-12-14 08:38:09 -08003853 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003854 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003855 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3856 p.moveTo(kPts[0]);
3857 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3858 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3859 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003860 REPORTER_ASSERT(reporter, p.contains(5, 6));
3861 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003862 // test conic edge
3863 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3864 SkConic conic(&kPts[index], 0.5f);
3865 halfway = conic.evalAt(0.5f);
3866 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3867 }
caryclark9cb5d752015-12-18 04:35:24 -08003868 // test conic end points
3869 REPORTER_ASSERT(reporter, p.contains(4, 4));
3870 REPORTER_ASSERT(reporter, p.contains(8, 8));
3871 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003872
3873 // test cubics
3874 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3875 for (int i = 0; i < 3; ++i) {
3876 p.reset();
3877 p.setFillType(SkPath::kEvenOdd_FillType);
3878 p.moveTo(pts[i].fX, pts[i].fY);
3879 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);
3880 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);
3881 p.close();
3882 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3883 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003884 // test cubic edge
3885 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3886 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3887 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3888 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003889 // test cubic end points
3890 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3891 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3892 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003893 }
3894}
3895
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003896class PathRefTest_Private {
3897public:
3898 static void TestPathRef(skiatest::Reporter* reporter) {
3899 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003900
halcanary385fe4d2015-08-26 13:07:48 -07003901 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003902
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003903 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003904
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003905 {
3906 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3907 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3908 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3909 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3910 for (int i = 0; i < kRepeatCnt; ++i) {
3911 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3912 }
3913 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003914 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003915
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003916 {
3917 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3918 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3919 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3920 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3921 for (int i = 0; i < kRepeatCnt; ++i) {
3922 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3923 }
3924 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003925 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003926
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003927 {
3928 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3929 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3930 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3931 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3932 for (int i = 0; i < kRepeatCnt; ++i) {
3933 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3934 }
3935 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003936 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003937
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003938 {
halcanary96fcdcc2015-08-27 07:41:13 -07003939 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003940 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3941 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3942 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3943 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3944 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003945 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003946 for (int i = 0; i < kRepeatCnt; ++i) {
3947 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3948 }
3949 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003950 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003951
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003952 {
3953 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3954 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3955 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3956 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3957 for (int i = 0; i < kRepeatCnt; ++i) {
3958 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3959 }
3960 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003961 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003962 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003963};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003964
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003965static void test_operatorEqual(skiatest::Reporter* reporter) {
3966 SkPath a;
3967 SkPath b;
3968 REPORTER_ASSERT(reporter, a == a);
3969 REPORTER_ASSERT(reporter, a == b);
3970 a.setFillType(SkPath::kInverseWinding_FillType);
3971 REPORTER_ASSERT(reporter, a != b);
3972 a.reset();
3973 REPORTER_ASSERT(reporter, a == b);
3974 a.lineTo(1, 1);
3975 REPORTER_ASSERT(reporter, a != b);
3976 a.reset();
3977 REPORTER_ASSERT(reporter, a == b);
3978 a.lineTo(1, 1);
3979 b.lineTo(1, 2);
3980 REPORTER_ASSERT(reporter, a != b);
3981 a.reset();
3982 a.lineTo(1, 2);
3983 REPORTER_ASSERT(reporter, a == b);
3984}
3985
caryclark66a5d8b2014-06-24 08:30:15 -07003986static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003987 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003988 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003989 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003990 SkAutoDataUnref data(wStream.copyToData());
3991 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08003992 if (strlen(str) > 0) {
3993 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3994 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07003995 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08003996 }
caryclark66a5d8b2014-06-24 08:30:15 -07003997}
3998
3999static void test_dump(skiatest::Reporter* reporter) {
4000 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07004001 compare_dump(reporter, p, false, false, "");
4002 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07004003 p.moveTo(1, 2);
4004 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07004005 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4006 "path.lineTo(3, 4);\n");
4007 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
4008 "path.lineTo(3, 4);\n"
4009 "path.lineTo(1, 2);\n"
4010 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004011 p.reset();
4012 p.moveTo(1, 2);
4013 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07004014 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4015 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004016 p.reset();
4017 p.moveTo(1, 2);
4018 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07004019 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4020 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004021 p.reset();
4022 p.moveTo(1, 2);
4023 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07004024 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4025 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
4026 p.reset();
4027 p.moveTo(1, 2);
4028 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07004029 compare_dump(reporter, p, false, true,
4030 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
4031 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07004032 p.reset();
4033 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
4034 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
4035 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
4036 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07004037}
4038
senorblanco84cd6212015-08-04 10:01:58 -07004039namespace {
4040
4041class ChangeListener : public SkPathRef::GenIDChangeListener {
4042public:
4043 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
4044 virtual ~ChangeListener() {}
4045 void onChange() override {
4046 *fChanged = true;
4047 }
4048private:
4049 bool* fChanged;
4050};
4051
4052}
4053
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004054class PathTest_Private {
4055public:
4056 static void TestPathTo(skiatest::Reporter* reporter) {
4057 SkPath p, q;
4058 p.lineTo(4, 4);
4059 p.reversePathTo(q);
4060 check_path_is_line(reporter, &p, 4, 4);
4061 q.moveTo(-4, -4);
4062 p.reversePathTo(q);
4063 check_path_is_line(reporter, &p, 4, 4);
4064 q.lineTo(7, 8);
4065 q.conicTo(8, 7, 6, 5, 0.5f);
4066 q.quadTo(6, 7, 8, 6);
4067 q.cubicTo(5, 6, 7, 8, 7, 5);
4068 q.close();
4069 p.reversePathTo(q);
4070 SkRect reverseExpected = {-4, -4, 8, 8};
4071 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
4072 }
senorblanco84cd6212015-08-04 10:01:58 -07004073
4074 static void TestPathrefListeners(skiatest::Reporter* reporter) {
4075 SkPath p;
4076
4077 bool changed = false;
4078 p.moveTo(0, 0);
4079
4080 // Check that listener is notified on moveTo().
4081
halcanary385fe4d2015-08-26 13:07:48 -07004082 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004083 REPORTER_ASSERT(reporter, !changed);
4084 p.moveTo(10, 0);
4085 REPORTER_ASSERT(reporter, changed);
4086
4087 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07004088 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004089 REPORTER_ASSERT(reporter, !changed);
4090 p.lineTo(20, 0);
4091 REPORTER_ASSERT(reporter, changed);
4092
4093 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07004094 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004095 REPORTER_ASSERT(reporter, !changed);
4096 p.reset();
4097 REPORTER_ASSERT(reporter, changed);
4098
4099 p.moveTo(0, 0);
4100
4101 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07004102 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004103 REPORTER_ASSERT(reporter, !changed);
4104 p.rewind();
4105 REPORTER_ASSERT(reporter, changed);
4106
4107 // Check that listener is notified when pathref is deleted.
4108 {
4109 SkPath q;
4110 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07004111 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07004112 REPORTER_ASSERT(reporter, !changed);
4113 }
4114 // q went out of scope.
4115 REPORTER_ASSERT(reporter, changed);
4116 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004117};
4118
caryclark8e7b19d2016-02-18 04:11:48 -08004119static void test_interp(skiatest::Reporter* reporter) {
4120 SkPath p1, p2, out;
4121 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4122 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4123 REPORTER_ASSERT(reporter, p1 == out);
4124 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4125 REPORTER_ASSERT(reporter, p1 == out);
4126 p1.moveTo(0, 2);
4127 p1.lineTo(0, 4);
4128 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4129 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out));
4130 p2.moveTo(6, 0);
4131 p2.lineTo(8, 0);
4132 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4133 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out));
4134 REPORTER_ASSERT(reporter, p2 == out);
4135 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out));
4136 REPORTER_ASSERT(reporter, p1 == out);
4137 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4138 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2));
4139 p1.reset();
4140 p1.moveTo(4, 4);
4141 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2));
4142 p2.reset();
4143 p2.moveTo(4, 2);
4144 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2));
4145 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2));
4146 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out));
4147 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5));
4148 p2.reset();
4149 p2.moveTo(4, 2);
4150 p2.conicTo(6, 3, 6, 5, 1);
4151 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4152 p2.reset();
4153 p2.moveTo(4, 4);
4154 p2.conicTo(5, 4, 5, 5, 0.5f);
4155 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2));
4156}
4157
4158DEF_TEST(PathInterp, reporter) {
4159 test_interp(reporter);
4160}
4161
caryclark9aacd902015-12-14 08:38:09 -08004162DEF_TEST(PathContains, reporter) {
4163 test_contains(reporter);
4164}
4165
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00004166DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00004167 test_path_crbug364224();
4168
bungeman@google.coma5809a32013-06-21 15:13:34 +00004169 SkTSize<SkScalar>::Make(3,4);
4170
4171 SkPath p, empty;
4172 SkRect bounds, bounds2;
4173 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00004174
reed@android.comd252db02009-04-01 18:31:44 +00004175 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00004176
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004177 // this triggers a code path in SkPath::operator= which is otherwise unexercised
4178 SkPath& self = p;
4179 p = self;
4180
4181 // this triggers a code path in SkPath::swap which is otherwise unexercised
4182 p.swap(self);
4183
reed@android.com3abec1d2009-03-02 05:36:20 +00004184 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004185
reed@android.com6b82d1a2009-06-03 02:35:01 +00004186 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
4187 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004188 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08004189 REPORTER_ASSERT(reporter,
4190 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004191 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004192
reed@android.com6b82d1a2009-06-03 02:35:01 +00004193 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00004194 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00004195
reed@android.com6b82d1a2009-06-03 02:35:01 +00004196 p.addOval(bounds);
4197 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004198 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00004199
bungeman@google.coma5809a32013-06-21 15:13:34 +00004200 p.rewind();
4201 test_empty(reporter, p);
4202
reed@android.com3abec1d2009-03-02 05:36:20 +00004203 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00004204 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00004205 // we have only lines
4206 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004207 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00004208
bungeman@google.coma5809a32013-06-21 15:13:34 +00004209 REPORTER_ASSERT(reporter, p != empty);
4210 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00004211
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004212 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07004213 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
4214 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00004215 SkPoint pts[4];
4216 int count = p.getPoints(pts, 4);
4217 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00004218 uint8_t verbs[6];
4219 verbs[5] = 0xff;
4220 p.getVerbs(verbs, 5);
4221 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
4222 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
4223 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
4224 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
4225 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
4226 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00004227 bounds2.set(pts, 4);
4228 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004229
reed@android.com3abec1d2009-03-02 05:36:20 +00004230 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
4231 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00004232 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00004233
halcanary96fcdcc2015-08-27 07:41:13 -07004234 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00004235 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00004236 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
4237 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00004238
reed@android.com3abec1d2009-03-02 05:36:20 +00004239 // now force p to not be a rect
4240 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
4241 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07004242 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00004243
robertphillipsfe7c4272014-12-29 11:36:39 -08004244 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
4245 // path has a trailing moveTo. Please see crbug.com\445368)
4246 {
4247 SkRect r;
4248 p.reset();
4249 p.addRect(bounds);
4250 REPORTER_ASSERT(reporter, p.isRect(&r));
4251 REPORTER_ASSERT(reporter, r == bounds);
4252 // add a moveTo outside of our bounds
4253 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
4254 REPORTER_ASSERT(reporter, p.isRect(&r));
4255 REPORTER_ASSERT(reporter, r == bounds);
4256 }
4257
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004258 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004259 test_isLine(reporter);
4260 test_isRect(reporter);
bsalomonedc743a2016-06-01 09:42:31 -07004261 test_is_simple_closed_rect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004262 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004263 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004264 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004265 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004266 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004267 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004268 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004269 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004270 test_flattening(reporter);
4271 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004272 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004273 test_iter(reporter);
4274 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004275 test_circle(reporter);
4276 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004277 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004278 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004279 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004280 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004281 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004282 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004283 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004284 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004285 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004286 test_tricky_cubic();
4287 test_clipped_cubic();
4288 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004289 test_crbug_493450(reporter);
4290 test_crbug_495894(reporter);
mbarbella99600d02016-06-01 15:39:47 -07004291 test_crbug_613918();
reed@google.com7a90daf2013-04-10 18:44:00 +00004292 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004293 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004294 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004295 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004296 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004297 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004298 test_arc(reporter);
bsalomon1978ce22016-05-31 10:42:16 -07004299 test_arc_ovals(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004300 test_arcTo(reporter);
4301 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004302 test_addPathMode(reporter, false, false);
4303 test_addPathMode(reporter, true, false);
4304 test_addPathMode(reporter, false, true);
4305 test_addPathMode(reporter, true, true);
4306 test_extendClosedPath(reporter);
4307 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4308 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004309 test_conicTo_special_case(reporter);
4310 test_get_point(reporter);
4311 test_contains(reporter);
4312 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004313 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004314 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004315 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004316 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004317 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004318 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004319 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004320 test_bounds_crbug_513799(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00004321}