blob: 1048fcd97920242d667ac10df1e4050b87aee478 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.comcc8be772013-10-15 15:35:29 +00007
reed@google.com8cae8352012-09-14 15:18:41 +00008#include "SkCanvas.h"
caryclark9aacd902015-12-14 08:38:09 -08009#include "SkGeometry.h"
reed@google.com55b5f4b2011-09-07 12:23:41 +000010#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000011#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000012#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070013#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000014#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000016#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000017#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000018#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070019#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070020#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000021#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000022#include "SkTypes.h"
23#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000024#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000025
reed5bcbe912014-12-15 12:28:33 -080026static void set_radii(SkVector radii[4], int index, float rad) {
27 sk_bzero(radii, sizeof(SkVector) * 4);
28 radii[index].set(rad, rad);
29}
30
31static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
32 const SkVector radii[4]) {
33 SkRRect rrect;
34 rrect.setRectRadii(bounds, radii);
35 REPORTER_ASSERT(reporter, bounds == rrect.rect());
36
37 SkPath path;
38 // this line should not assert in the debug build (from validate)
39 path.addRRect(rrect);
40 REPORTER_ASSERT(reporter, bounds == path.getBounds());
41}
42
caryclark5ccef572015-03-02 10:07:56 -080043static void test_skbug_3469(skiatest::Reporter* reporter) {
44 SkPath path;
45 path.moveTo(20, 20);
46 path.quadTo(20, 50, 80, 50);
47 path.quadTo(20, 50, 20, 80);
48 REPORTER_ASSERT(reporter, !path.isConvex());
49}
50
reed5bcbe912014-12-15 12:28:33 -080051static void test_skbug_3239(skiatest::Reporter* reporter) {
52 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
53 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
54 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
55
56 const float rad = 33436320;
57
58 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
59 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
60
61 SkVector radii[4];
62 for (int i = 0; i < 4; ++i) {
63 set_radii(radii, i, rad);
64 test_add_rrect(reporter, rectx, radii);
65 test_add_rrect(reporter, recty, radii);
66 }
67}
68
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000069static void make_path_crbug364224(SkPath* path) {
70 path->reset();
71 path->moveTo(3.747501373f, 2.724499941f);
72 path->lineTo(3.747501373f, 3.75f);
73 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
74 path->lineTo(0.7475013733f, 4.0f);
75 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
76 path->lineTo(0.4975013733f, 1.0f);
77 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
78 path->lineTo(3.497501373f, 0.75f);
79 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
80 path->lineTo(3.715001345f, 0.5512499809f);
81 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
82 path->lineTo(0.7475013733f, 0.4999999702f);
83 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
84 path->lineTo(0.2475013733f, 3.75f);
85 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
86 path->lineTo(3.497501373f, 4.25f);
87 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
88 path->lineTo(3.997501373f, 2.474750042f);
89 path->lineTo(3.747501373f, 2.724499941f);
90 path->close();
91}
92
93static void make_path_crbug364224_simplified(SkPath* path) {
94 path->moveTo(3.747501373f, 2.724499941f);
95 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
96 path->close();
97}
98
99static void test_path_crbug364224() {
100 SkPath path;
101 SkPaint paint;
reed3054be12014-12-10 07:24:28 -0800102 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000103 SkCanvas* canvas = surface->getCanvas();
104
105 make_path_crbug364224_simplified(&path);
106 canvas->drawPath(path, paint);
107
108 make_path_crbug364224(&path);
109 canvas->drawPath(path, paint);
110}
111
piotaixrfac4e0e2014-08-26 11:59:04 -0700112/**
113 * In debug mode, this path was causing an assertion to fail in
114 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
115 */
116static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
117 SkPoint orig, p1, p2, p3;
118 orig = SkPoint::Make(1.f, 1.f);
119 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
120 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
121 p3 = SkPoint::Make(2.f, 2.f);
122
123 path->reset();
124 path->moveTo(orig);
125 path->cubicTo(p1, p2, p3);
126 path->close();
127}
128
129static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
130 SkPath path;
131 make_path_crbugskia2820(&path, reporter);
132
133 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
134 stroke.setStrokeStyle(2 * SK_Scalar1);
135 stroke.applyToPath(&path, path);
136}
137
reed@google.comcc8be772013-10-15 15:35:29 +0000138static void make_path0(SkPath* path) {
139 // from * https://code.google.com/p/skia/issues/detail?id=1706
140
141 path->moveTo(146.939f, 1012.84f);
142 path->lineTo(181.747f, 1009.18f);
143 path->lineTo(182.165f, 1013.16f);
144 path->lineTo(147.357f, 1016.82f);
145 path->lineTo(146.939f, 1012.84f);
146 path->close();
147}
148
149static void make_path1(SkPath* path) {
150 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
151}
152
153typedef void (*PathProc)(SkPath*);
154
155/*
156 * Regression test: we used to crash (overwrite internal storage) during
157 * construction of the region when the path was INVERSE. That is now fixed,
158 * so test these regions (which used to assert/crash).
159 *
160 * https://code.google.com/p/skia/issues/detail?id=1706
161 */
162static void test_path_to_region(skiatest::Reporter* reporter) {
163 PathProc procs[] = {
164 make_path0,
165 make_path1,
166 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000167
reed@google.comcc8be772013-10-15 15:35:29 +0000168 SkRegion clip;
169 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000170
reed@google.comcc8be772013-10-15 15:35:29 +0000171 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
172 SkPath path;
173 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000174
reed@google.comcc8be772013-10-15 15:35:29 +0000175 SkRegion rgn;
176 rgn.setPath(path, clip);
177 path.toggleInverseFillType();
178 rgn.setPath(path, clip);
179 }
180}
181
caryclark@google.com56f233a2012-11-19 13:06:06 +0000182#if defined(WIN32)
183 #define SUPPRESS_VISIBILITY_WARNING
184#else
185 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
186#endif
187
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000188static void test_path_close_issue1474(skiatest::Reporter* reporter) {
189 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
190 // are relative to the point we close to, not relative to the point we close from.
191 SkPath path;
192 SkPoint last;
193
194 // Test rLineTo().
195 path.rLineTo(0, 100);
196 path.rLineTo(100, 0);
197 path.close(); // Returns us back to 0,0.
198 path.rLineTo(50, 50); // This should go to 50,50.
199
200 path.getLastPt(&last);
201 REPORTER_ASSERT(reporter, 50 == last.fX);
202 REPORTER_ASSERT(reporter, 50 == last.fY);
203
204 // Test rQuadTo().
205 path.rewind();
206 path.rLineTo(0, 100);
207 path.rLineTo(100, 0);
208 path.close();
209 path.rQuadTo(50, 50, 75, 75);
210
211 path.getLastPt(&last);
212 REPORTER_ASSERT(reporter, 75 == last.fX);
213 REPORTER_ASSERT(reporter, 75 == last.fY);
214
215 // Test rConicTo().
216 path.rewind();
217 path.rLineTo(0, 100);
218 path.rLineTo(100, 0);
219 path.close();
220 path.rConicTo(50, 50, 85, 85, 2);
221
222 path.getLastPt(&last);
223 REPORTER_ASSERT(reporter, 85 == last.fX);
224 REPORTER_ASSERT(reporter, 85 == last.fY);
225
226 // Test rCubicTo().
227 path.rewind();
228 path.rLineTo(0, 100);
229 path.rLineTo(100, 0);
230 path.close();
231 path.rCubicTo(50, 50, 85, 85, 95, 95);
232
233 path.getLastPt(&last);
234 REPORTER_ASSERT(reporter, 95 == last.fX);
235 REPORTER_ASSERT(reporter, 95 == last.fY);
236}
237
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000238static void test_gen_id(skiatest::Reporter* reporter) {
239 SkPath a, b;
240 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
241
242 a.moveTo(0, 0);
243 const uint32_t z = a.getGenerationID();
244 REPORTER_ASSERT(reporter, z != b.getGenerationID());
245
246 a.reset();
247 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
248
249 a.moveTo(1, 1);
250 const uint32_t y = a.getGenerationID();
251 REPORTER_ASSERT(reporter, z != y);
252
253 b.moveTo(2, 2);
254 const uint32_t x = b.getGenerationID();
255 REPORTER_ASSERT(reporter, x != y && x != z);
256
257 a.swap(b);
258 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
259
260 b = a;
261 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
262
263 SkPath c(a);
264 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
265
266 c.lineTo(3, 3);
267 const uint32_t w = c.getGenerationID();
268 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
269 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
270 REPORTER_ASSERT(reporter, w != x);
271
djsollen50da1d82015-02-17 09:09:53 -0800272#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000273 static bool kExpectGenIDToIgnoreFill = false;
274#else
275 static bool kExpectGenIDToIgnoreFill = true;
276#endif
277
278 c.toggleInverseFillType();
279 const uint32_t v = c.getGenerationID();
280 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
281
282 c.rewind();
283 REPORTER_ASSERT(reporter, v != c.getGenerationID());
284}
285
reed@google.com3eff3592013-05-08 21:08:21 +0000286// This used to assert in the debug build, as the edges did not all line-up.
287static void test_bad_cubic_crbug234190() {
288 SkPath path;
289 path.moveTo(13.8509f, 3.16858f);
290 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
291 -2.38991e+08f, -4.26573e+08f,
292 -2.41016e+08f, -4.30188e+08f);
293
294 SkPaint paint;
295 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800296 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000297 surface->getCanvas()->drawPath(path, paint);
298}
299
reed@google.com7a90daf2013-04-10 18:44:00 +0000300static void test_bad_cubic_crbug229478() {
301 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000302 { 4595.91064f, -11596.9873f },
303 { 4597.2168f, -11595.9414f },
304 { 4598.52344f, -11594.8955f },
305 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000306 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000307
reed@google.com7a90daf2013-04-10 18:44:00 +0000308 SkPath path;
309 path.moveTo(pts[0]);
310 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000311
reed@google.com7a90daf2013-04-10 18:44:00 +0000312 SkPaint paint;
313 paint.setStyle(SkPaint::kStroke_Style);
314 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000315
reed@google.com7a90daf2013-04-10 18:44:00 +0000316 SkPath dst;
317 // Before the fix, this would infinite-recurse, and run out of stack
318 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700319 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000320}
321
reed@google.com64d62952013-01-18 17:49:28 +0000322static void build_path_170666(SkPath& path) {
323 path.moveTo(17.9459f, 21.6344f);
324 path.lineTo(139.545f, -47.8105f);
325 path.lineTo(139.545f, -47.8105f);
326 path.lineTo(131.07f, -47.3888f);
327 path.lineTo(131.07f, -47.3888f);
328 path.lineTo(122.586f, -46.9532f);
329 path.lineTo(122.586f, -46.9532f);
330 path.lineTo(18076.6f, 31390.9f);
331 path.lineTo(18076.6f, 31390.9f);
332 path.lineTo(18085.1f, 31390.5f);
333 path.lineTo(18085.1f, 31390.5f);
334 path.lineTo(18076.6f, 31390.9f);
335 path.lineTo(18076.6f, 31390.9f);
336 path.lineTo(17955, 31460.3f);
337 path.lineTo(17955, 31460.3f);
338 path.lineTo(17963.5f, 31459.9f);
339 path.lineTo(17963.5f, 31459.9f);
340 path.lineTo(17971.9f, 31459.5f);
341 path.lineTo(17971.9f, 31459.5f);
342 path.lineTo(17.9551f, 21.6205f);
343 path.lineTo(17.9551f, 21.6205f);
344 path.lineTo(9.47091f, 22.0561f);
345 path.lineTo(9.47091f, 22.0561f);
346 path.lineTo(17.9459f, 21.6344f);
347 path.lineTo(17.9459f, 21.6344f);
348 path.close();path.moveTo(0.995934f, 22.4779f);
349 path.lineTo(0.986725f, 22.4918f);
350 path.lineTo(0.986725f, 22.4918f);
351 path.lineTo(17955, 31460.4f);
352 path.lineTo(17955, 31460.4f);
353 path.lineTo(17971.9f, 31459.5f);
354 path.lineTo(17971.9f, 31459.5f);
355 path.lineTo(18093.6f, 31390.1f);
356 path.lineTo(18093.6f, 31390.1f);
357 path.lineTo(18093.6f, 31390);
358 path.lineTo(18093.6f, 31390);
359 path.lineTo(139.555f, -47.8244f);
360 path.lineTo(139.555f, -47.8244f);
361 path.lineTo(122.595f, -46.9671f);
362 path.lineTo(122.595f, -46.9671f);
363 path.lineTo(0.995934f, 22.4779f);
364 path.lineTo(0.995934f, 22.4779f);
365 path.close();
366 path.moveTo(5.43941f, 25.5223f);
367 path.lineTo(798267, -28871.1f);
368 path.lineTo(798267, -28871.1f);
369 path.lineTo(3.12512e+06f, -113102);
370 path.lineTo(3.12512e+06f, -113102);
371 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
372 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
373 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
374 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
375 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
376 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
377 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
378 path.lineTo(2.78271e+08f, -1.00733e+07f);
379 path.lineTo(2.78271e+08f, -1.00733e+07f);
380 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
381 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
382 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
383 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
384 path.lineTo(2.77473e+08f, -1.00444e+07f);
385 path.lineTo(2.77473e+08f, -1.00444e+07f);
386 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
387 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
388 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
389 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
390 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
391 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
392 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
393 path.lineTo(798284, -28872);
394 path.lineTo(798284, -28872);
395 path.lineTo(22.4044f, 24.6677f);
396 path.lineTo(22.4044f, 24.6677f);
397 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
398 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
399 path.close();
400}
401
402static void build_path_simple_170666(SkPath& path) {
403 path.moveTo(126.677f, 24.1591f);
404 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
405}
406
407// This used to assert in the SK_DEBUG build, as the clip step would fail with
408// too-few interations in our cubic-line intersection code. That code now runs
409// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000410static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000411 SkPath path;
412 SkPaint paint;
413 paint.setAntiAlias(true);
414
reed3054be12014-12-10 07:24:28 -0800415 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000416
reed@google.com64d62952013-01-18 17:49:28 +0000417 build_path_simple_170666(path);
418 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000419
reed@google.com64d62952013-01-18 17:49:28 +0000420 build_path_170666(path);
421 surface->getCanvas()->drawPath(path, paint);
422}
423
caryclarke8c56662015-07-14 11:19:26 -0700424
425static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
426 SkScalar tx, SkScalar ty, SkScalar scale) {
427 SkPath smallPath;
428 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
429 bool smallConvex = smallPath.isConvex();
430 SkPath largePath;
431 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
432 SkMatrix matrix;
433 matrix.reset();
434 matrix.preTranslate(100, 100);
435 matrix.preScale(scale, scale);
436 largePath.transform(matrix);
437 bool largeConvex = largePath.isConvex();
438 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
439}
440
441static void test_crbug_493450(skiatest::Reporter* reporter) {
442 const char reducedCase[] =
443 "M0,0"
444 "L0.0002, 0"
445 "L0.0002, 0.0002"
446 "L0.0001, 0.0001"
447 "L0,0.0002"
448 "Z";
449 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
450 const char originalFiddleData[] =
451 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
452 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
453 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
454 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
455 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
456 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
457 826357.3384828606f);
458}
459
460static void test_crbug_495894(skiatest::Reporter* reporter) {
461 const char originalFiddleData[] =
462 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
463 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
464 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
465 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
466 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
467 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
468 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
469 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
470 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
471 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
472 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
473 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
474 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
475 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
476 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
477 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
478 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
479 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
480 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
481 "L-0.33993994441916414,-0.11288906492739594Z";
482 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
483 65536);
484}
485
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000486static void test_addrect(skiatest::Reporter* reporter) {
487 SkPath path;
488 path.lineTo(0, 0);
489 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700490 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000491
492 path.reset();
493 path.lineTo(FLT_EPSILON, FLT_EPSILON);
494 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700495 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000496
497 path.reset();
498 path.quadTo(0, 0, 0, 0);
499 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700500 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000501
502 path.reset();
503 path.conicTo(0, 0, 0, 0, 0.5f);
504 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700505 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000506
507 path.reset();
508 path.cubicTo(0, 0, 0, 0, 0, 0);
509 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700510 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000511}
512
reed@google.coma8790de2012-10-24 21:04:04 +0000513// Make sure we stay non-finite once we get there (unless we reset or rewind).
514static void test_addrect_isfinite(skiatest::Reporter* reporter) {
515 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000516
reed@google.coma8790de2012-10-24 21:04:04 +0000517 path.addRect(SkRect::MakeWH(50, 100));
518 REPORTER_ASSERT(reporter, path.isFinite());
519
520 path.moveTo(0, 0);
521 path.lineTo(SK_ScalarInfinity, 42);
522 REPORTER_ASSERT(reporter, !path.isFinite());
523
524 path.addRect(SkRect::MakeWH(50, 100));
525 REPORTER_ASSERT(reporter, !path.isFinite());
526
527 path.reset();
528 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000529
reed@google.coma8790de2012-10-24 21:04:04 +0000530 path.addRect(SkRect::MakeWH(50, 100));
531 REPORTER_ASSERT(reporter, path.isFinite());
532}
533
reed@google.com848148e2013-01-15 15:51:59 +0000534static void build_big_path(SkPath* path, bool reducedCase) {
535 if (reducedCase) {
536 path->moveTo(577330, 1971.72f);
537 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
538 } else {
539 path->moveTo(60.1631f, 7.70567f);
540 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
541 path->lineTo(577379, 1977.77f);
542 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
543 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
544 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
545 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
546 path->quadTo(577198, 1972, 577238, 1971.31f);
547 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
548 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
549 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
550 path->moveTo(306.718f, -32.912f);
551 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
552 }
553}
554
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000555static void test_clipped_cubic() {
reed3054be12014-12-10 07:24:28 -0800556 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000557
558 // This path used to assert, because our cubic-chopping code incorrectly
559 // moved control points after the chop. This test should be run in SK_DEBUG
560 // mode to ensure that we no long assert.
561 SkPath path;
562 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
563 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000564
reed@google.com848148e2013-01-15 15:51:59 +0000565 SkPaint paint;
566 for (int doAA = 0; doAA <= 1; ++doAA) {
567 paint.setAntiAlias(SkToBool(doAA));
568 surface->getCanvas()->drawPath(path, paint);
569 }
570 }
571}
572
reed3b2fb982015-07-29 08:37:13 -0700573static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
574 if (expected != bounds) {
575 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
576 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
577 expected.left(), expected.top(), expected.right(), expected.bottom());
578 }
579}
580
reed91f283b2015-07-28 06:00:50 -0700581static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
582 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700583#if 0
584 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
585 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700586 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
587
588 path.moveTo(-5, -8);
589 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
590
591 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
592 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
593
594 path.moveTo(1, 2);
595 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700596#else
597 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
598
599 path.moveTo(-5, -8); // should set the bounds
600 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
601
602 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
603 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
604
605 path.moveTo(1, 2); // don't expect this to have changed the bounds
606 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
607#endif
reed91f283b2015-07-28 06:00:50 -0700608}
609
reed@google.com8cae8352012-09-14 15:18:41 +0000610// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
611// which triggered an assert, from a tricky cubic. This test replicates that
612// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
613// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000614static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000615 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000616 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
617 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
618 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
619 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000620 };
621
622 SkPath path;
623 path.moveTo(pts[0]);
624 path.cubicTo(pts[1], pts[2], pts[3]);
625
626 SkPaint paint;
627 paint.setAntiAlias(true);
628
reed3054be12014-12-10 07:24:28 -0800629 SkSurface* surface = SkSurface::NewRasterN32Premul(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000630 surface->getCanvas()->drawPath(path, paint);
631 surface->unref();
632}
reed@android.com3abec1d2009-03-02 05:36:20 +0000633
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000634// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
635//
636static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
637 SkPath path;
638 path.quadTo(157, 366, 286, 208);
639 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000640
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000641 SkMatrix matrix;
642 matrix.setScale(1000*1000, 1000*1000);
643
644 // Be sure that path::transform correctly updates isFinite and the bounds
645 // if the transformation overflows. The previous bug was that isFinite was
646 // set to true in this case, but the bounds were not set to empty (which
647 // they should be).
648 while (path.isFinite()) {
649 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
650 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
651 path.transform(matrix);
652 }
653 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
654
655 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
656 path.transform(matrix);
657 // we need to still be non-finite
658 REPORTER_ASSERT(reporter, !path.isFinite());
659 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
660}
661
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000662static void add_corner_arc(SkPath* path, const SkRect& rect,
663 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000664 int startAngle)
665{
666
667 SkScalar rx = SkMinScalar(rect.width(), xIn);
668 SkScalar ry = SkMinScalar(rect.height(), yIn);
669
670 SkRect arcRect;
671 arcRect.set(-rx, -ry, rx, ry);
672 switch (startAngle) {
673 case 0:
674 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
675 break;
676 case 90:
677 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
678 break;
679 case 180:
680 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
681 break;
682 case 270:
683 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
684 break;
685 default:
686 break;
687 }
688
689 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
690}
691
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000692static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000693 SkScalar xCorner, SkScalar yCorner) {
694 // we are lazy here and use the same x & y for each corner
695 add_corner_arc(path, r, xCorner, yCorner, 270);
696 add_corner_arc(path, r, xCorner, yCorner, 0);
697 add_corner_arc(path, r, xCorner, yCorner, 90);
698 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000699 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000700}
701
702// Chrome creates its own round rects with each corner possibly being different.
703// Performance will suffer if they are not convex.
704// Note: PathBench::ArbRoundRectBench performs almost exactly
705// the same test (but with drawing)
706static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000707 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000708 SkRect r;
709
710 for (int i = 0; i < 5000; ++i) {
711
robertphillips@google.com158618e2012-10-23 16:56:56 +0000712 SkScalar size = rand.nextUScalar1() * 30;
713 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000714 continue;
715 }
716 r.fLeft = rand.nextUScalar1() * 300;
717 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000718 r.fRight = r.fLeft + 2 * size;
719 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000720
721 SkPath temp;
722
723 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
724
725 REPORTER_ASSERT(reporter, temp.isConvex());
726 }
727}
728
robertphillips@google.com158618e2012-10-23 16:56:56 +0000729// Chrome will sometimes create a 0 radius round rect. The degenerate
730// quads prevent the path from being converted to a rect
731// Note: PathBench::ArbRoundRectBench performs almost exactly
732// the same test (but with drawing)
733static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000734 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000735 SkRect r;
736
737 for (int i = 0; i < 5000; ++i) {
738
739 SkScalar size = rand.nextUScalar1() * 30;
740 if (size < SK_Scalar1) {
741 continue;
742 }
743 r.fLeft = rand.nextUScalar1() * 300;
744 r.fTop = rand.nextUScalar1() * 300;
745 r.fRight = r.fLeft + 2 * size;
746 r.fBottom = r.fTop + 2 * size;
747
748 SkPath temp;
749
750 make_arb_round_rect(&temp, r, 0, 0);
751
robertphillips@google.com158618e2012-10-23 16:56:56 +0000752 SkRect result;
753 REPORTER_ASSERT(reporter, temp.isRect(&result));
754 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000755 }
756}
757
reed@google.com0bb18bb2012-07-26 15:20:36 +0000758static void test_rect_isfinite(skiatest::Reporter* reporter) {
759 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000760 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000761 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000762
reed@google.com0bb18bb2012-07-26 15:20:36 +0000763 SkRect r;
764 r.setEmpty();
765 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000766 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000767 REPORTER_ASSERT(reporter, !r.isFinite());
768 r.set(0, 0, nan, 0);
769 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000770
reed@google.com0bb18bb2012-07-26 15:20:36 +0000771 SkPoint pts[] = {
772 { 0, 0 },
773 { SK_Scalar1, 0 },
774 { 0, SK_Scalar1 },
775 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000776
reed@google.com0bb18bb2012-07-26 15:20:36 +0000777 bool isFine = r.setBoundsCheck(pts, 3);
778 REPORTER_ASSERT(reporter, isFine);
779 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000780
reed@google.com0bb18bb2012-07-26 15:20:36 +0000781 pts[1].set(inf, 0);
782 isFine = r.setBoundsCheck(pts, 3);
783 REPORTER_ASSERT(reporter, !isFine);
784 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000785
reed@google.com0bb18bb2012-07-26 15:20:36 +0000786 pts[1].set(nan, 0);
787 isFine = r.setBoundsCheck(pts, 3);
788 REPORTER_ASSERT(reporter, !isFine);
789 REPORTER_ASSERT(reporter, r.isEmpty());
790}
791
792static void test_path_isfinite(skiatest::Reporter* reporter) {
793 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000794 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000795 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000796
reed@google.com0bb18bb2012-07-26 15:20:36 +0000797 SkPath path;
798 REPORTER_ASSERT(reporter, path.isFinite());
799
800 path.reset();
801 REPORTER_ASSERT(reporter, path.isFinite());
802
803 path.reset();
804 path.moveTo(SK_Scalar1, 0);
805 REPORTER_ASSERT(reporter, path.isFinite());
806
807 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000808 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000809 REPORTER_ASSERT(reporter, !path.isFinite());
810
811 path.reset();
812 path.moveTo(nan, 0);
813 REPORTER_ASSERT(reporter, !path.isFinite());
814}
815
816static void test_isfinite(skiatest::Reporter* reporter) {
817 test_rect_isfinite(reporter);
818 test_path_isfinite(reporter);
819}
820
fsb1475b02016-01-20 09:51:07 -0800821static void test_islastcontourclosed(skiatest::Reporter* reporter) {
822 SkPath path;
823 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
824 path.moveTo(0, 0);
825 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
826 path.close();
827 REPORTER_ASSERT(reporter, path.isLastContourClosed());
828 path.lineTo(100, 100);
829 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
830 path.moveTo(200, 200);
831 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
832 path.close();
833 REPORTER_ASSERT(reporter, path.isLastContourClosed());
834 path.moveTo(0, 0);
835 REPORTER_ASSERT(reporter, !path.isLastContourClosed());
836}
837
reed@google.com744faba2012-05-29 19:54:52 +0000838// assert that we always
839// start with a moveTo
840// only have 1 moveTo
841// only have Lines after that
842// end with a single close
843// only have (at most) 1 close
844//
845static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000846 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000847 SkPath::RawIter iter(path);
848 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000849
850 bool firstTime = true;
851 bool foundClose = false;
852 for (;;) {
853 switch (iter.next(pts)) {
854 case SkPath::kMove_Verb:
855 REPORTER_ASSERT(reporter, firstTime);
856 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
857 srcPts++;
858 firstTime = false;
859 break;
860 case SkPath::kLine_Verb:
861 REPORTER_ASSERT(reporter, !firstTime);
862 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
863 srcPts++;
864 break;
865 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000866 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000867 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000868 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000869 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000870 break;
reed@google.com744faba2012-05-29 19:54:52 +0000871 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000872 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000873 break;
874 case SkPath::kClose_Verb:
875 REPORTER_ASSERT(reporter, !firstTime);
876 REPORTER_ASSERT(reporter, !foundClose);
877 REPORTER_ASSERT(reporter, expectClose);
878 foundClose = true;
879 break;
880 case SkPath::kDone_Verb:
881 goto DONE;
882 }
883 }
884DONE:
885 REPORTER_ASSERT(reporter, foundClose == expectClose);
886}
887
888static void test_addPoly(skiatest::Reporter* reporter) {
889 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000890 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891
reed@google.com744faba2012-05-29 19:54:52 +0000892 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
893 pts[i].fX = rand.nextSScalar1();
894 pts[i].fY = rand.nextSScalar1();
895 }
896
897 for (int doClose = 0; doClose <= 1; ++doClose) {
898 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
899 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800900 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000901 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000902 }
903 }
904}
905
reed@google.com8b06f1a2012-05-29 12:03:46 +0000906static void test_strokerec(skiatest::Reporter* reporter) {
907 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
908 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000909
reed@google.com8b06f1a2012-05-29 12:03:46 +0000910 rec.setHairlineStyle();
911 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000912
reed@google.com8b06f1a2012-05-29 12:03:46 +0000913 rec.setStrokeStyle(SK_Scalar1, false);
914 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000915
reed@google.com8b06f1a2012-05-29 12:03:46 +0000916 rec.setStrokeStyle(SK_Scalar1, true);
917 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918
reed@google.com8b06f1a2012-05-29 12:03:46 +0000919 rec.setStrokeStyle(0, false);
920 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000921
reed@google.com8b06f1a2012-05-29 12:03:46 +0000922 rec.setStrokeStyle(0, true);
923 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
924}
925
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000926// Set this for paths that don't have a consistent direction such as a bowtie.
927// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700928const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000929
930static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -0700931 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000932 if (expected == kDontCheckDir) {
933 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000934 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000935 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
936
reed026beb52015-06-10 14:23:15 -0700937 SkPathPriv::FirstDirection dir;
938 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000939 REPORTER_ASSERT(reporter, dir == expected);
940 } else {
reed026beb52015-06-10 14:23:15 -0700941 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000942 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000943}
944
reed@google.com3e71a882012-01-10 18:44:37 +0000945static void test_direction(skiatest::Reporter* reporter) {
946 size_t i;
947 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -0700948 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -0700949 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
950 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
951 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +0000952
953 static const char* gDegen[] = {
954 "M 10 10",
955 "M 10 10 M 20 20",
956 "M 10 10 L 20 20",
957 "M 10 10 L 10 10 L 10 10",
958 "M 10 10 Q 10 10 10 10",
959 "M 10 10 C 10 10 10 10 10 10",
960 };
961 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
962 path.reset();
963 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
964 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -0700965 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +0000966 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000967
reed@google.com3e71a882012-01-10 18:44:37 +0000968 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000969 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000970 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000971 "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 +0000972 // rect with top two corners replaced by cubics with identical middle
973 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000974 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
975 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000976 };
977 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
978 path.reset();
979 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
980 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700981 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000982 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000983
reed@google.com3e71a882012-01-10 18:44:37 +0000984 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000985 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000986 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000987 "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 +0000988 // rect with top two corners replaced by cubics with identical middle
989 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000990 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
991 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000992 };
993 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
994 path.reset();
995 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
996 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700997 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000998 }
reed@google.comac8543f2012-01-30 20:51:25 +0000999
1000 // Test two donuts, each wound a different direction. Only the outer contour
1001 // determines the cheap direction
1002 path.reset();
1003 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
1004 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001005 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001006
reed@google.comac8543f2012-01-30 20:51:25 +00001007 path.reset();
1008 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
1009 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -07001010 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +00001011
1012 // triangle with one point really far from the origin.
1013 path.reset();
1014 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001015 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +00001016 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1017 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001018 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001019
1020 path.reset();
1021 path.conicTo(20, 0, 20, 20, 0.5f);
1022 path.close();
reed026beb52015-06-10 14:23:15 -07001023 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001024
1025 path.reset();
1026 path.lineTo(1, 1e7f);
1027 path.lineTo(1e7f, 2e7f);
1028 path.close();
1029 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001030 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001031}
1032
reed@google.comffdb0182011-11-14 19:29:14 +00001033static void add_rect(SkPath* path, const SkRect& r) {
1034 path->moveTo(r.fLeft, r.fTop);
1035 path->lineTo(r.fRight, r.fTop);
1036 path->lineTo(r.fRight, r.fBottom);
1037 path->lineTo(r.fLeft, r.fBottom);
1038 path->close();
1039}
1040
1041static void test_bounds(skiatest::Reporter* reporter) {
1042 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001043 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1044 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1045 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1046 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001047 };
1048
1049 SkPath path0, path1;
1050 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1051 path0.addRect(rects[i]);
1052 add_rect(&path1, rects[i]);
1053 }
1054
1055 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1056}
1057
reed@google.com55b5f4b2011-09-07 12:23:41 +00001058static void stroke_cubic(const SkPoint pts[4]) {
1059 SkPath path;
1060 path.moveTo(pts[0]);
1061 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001062
reed@google.com55b5f4b2011-09-07 12:23:41 +00001063 SkPaint paint;
1064 paint.setStyle(SkPaint::kStroke_Style);
1065 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001066
reed@google.com55b5f4b2011-09-07 12:23:41 +00001067 SkPath fill;
1068 paint.getFillPath(path, &fill);
1069}
1070
1071// just ensure this can run w/o any SkASSERTS firing in the debug build
1072// we used to assert due to differences in how we determine a degenerate vector
1073// but that was fixed with the introduction of SkPoint::CanNormalize
1074static void stroke_tiny_cubic() {
1075 SkPoint p0[] = {
1076 { 372.0f, 92.0f },
1077 { 372.0f, 92.0f },
1078 { 372.0f, 92.0f },
1079 { 372.0f, 92.0f },
1080 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001081
reed@google.com55b5f4b2011-09-07 12:23:41 +00001082 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001083
reed@google.com55b5f4b2011-09-07 12:23:41 +00001084 SkPoint p1[] = {
1085 { 372.0f, 92.0f },
1086 { 372.0007f, 92.000755f },
1087 { 371.99927f, 92.003922f },
1088 { 371.99826f, 92.003899f },
1089 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001090
reed@google.com55b5f4b2011-09-07 12:23:41 +00001091 stroke_cubic(p1);
1092}
1093
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001094static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1095 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001096 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001097 SkPoint mv;
1098 SkPoint pts[4];
1099 SkPath::Verb v;
1100 int nMT = 0;
1101 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001102 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001103 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1104 switch (v) {
1105 case SkPath::kMove_Verb:
1106 mv = pts[0];
1107 ++nMT;
1108 break;
1109 case SkPath::kClose_Verb:
1110 REPORTER_ASSERT(reporter, mv == pts[0]);
1111 ++nCL;
1112 break;
1113 default:
1114 break;
1115 }
1116 }
1117 // if we force a close on the interator we should have a close
1118 // for every moveTo
1119 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1120 }
1121}
1122
1123static void test_close(skiatest::Reporter* reporter) {
1124 SkPath closePt;
1125 closePt.moveTo(0, 0);
1126 closePt.close();
1127 check_close(reporter, closePt);
1128
1129 SkPath openPt;
1130 openPt.moveTo(0, 0);
1131 check_close(reporter, openPt);
1132
1133 SkPath empty;
1134 check_close(reporter, empty);
1135 empty.close();
1136 check_close(reporter, empty);
1137
1138 SkPath rect;
1139 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1140 check_close(reporter, rect);
1141 rect.close();
1142 check_close(reporter, rect);
1143
1144 SkPath quad;
1145 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1146 check_close(reporter, quad);
1147 quad.close();
1148 check_close(reporter, quad);
1149
1150 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001151 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001152 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1153 check_close(reporter, cubic);
1154 cubic.close();
1155 check_close(reporter, cubic);
1156
1157 SkPath line;
1158 line.moveTo(SK_Scalar1, SK_Scalar1);
1159 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1160 check_close(reporter, line);
1161 line.close();
1162 check_close(reporter, line);
1163
1164 SkPath rect2;
1165 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1166 rect2.close();
1167 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1168 check_close(reporter, rect2);
1169 rect2.close();
1170 check_close(reporter, rect2);
1171
1172 SkPath oval3;
1173 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1174 oval3.close();
1175 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1176 check_close(reporter, oval3);
1177 oval3.close();
1178 check_close(reporter, oval3);
1179
1180 SkPath moves;
1181 moves.moveTo(SK_Scalar1, SK_Scalar1);
1182 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1183 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1184 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1185 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001186
1187 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001188}
1189
reed@google.com7c424812011-05-15 04:38:34 +00001190static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1191 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001192 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1193 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001194 REPORTER_ASSERT(reporter, c == expected);
1195}
1196
caryclarkb4216502015-03-02 13:02:34 -08001197static void test_path_crbug389050(skiatest::Reporter* reporter) {
1198 SkPath tinyConvexPolygon;
1199 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1200 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1201 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1202 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1203 tinyConvexPolygon.close();
1204 tinyConvexPolygon.getConvexity();
1205 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001206 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001207
1208 SkPath platTriangle;
1209 platTriangle.moveTo(0, 0);
1210 platTriangle.lineTo(200, 0);
1211 platTriangle.lineTo(100, 0.04f);
1212 platTriangle.close();
1213 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001214 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001215
1216 platTriangle.reset();
1217 platTriangle.moveTo(0, 0);
1218 platTriangle.lineTo(200, 0);
1219 platTriangle.lineTo(100, 0.03f);
1220 platTriangle.close();
1221 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001222 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001223}
1224
reed@google.com7c424812011-05-15 04:38:34 +00001225static void test_convexity2(skiatest::Reporter* reporter) {
1226 SkPath pt;
1227 pt.moveTo(0, 0);
1228 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001229 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001230 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001231
reed@google.com7c424812011-05-15 04:38:34 +00001232 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001233 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1234 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001235 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001236 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001237 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001238
reed@google.com7c424812011-05-15 04:38:34 +00001239 SkPath triLeft;
1240 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001241 triLeft.lineTo(SK_Scalar1, 0);
1242 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001243 triLeft.close();
1244 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001245 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001246
reed@google.com7c424812011-05-15 04:38:34 +00001247 SkPath triRight;
1248 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001249 triRight.lineTo(-SK_Scalar1, 0);
1250 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001251 triRight.close();
1252 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001253 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001254
reed@google.com7c424812011-05-15 04:38:34 +00001255 SkPath square;
1256 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001257 square.lineTo(SK_Scalar1, 0);
1258 square.lineTo(SK_Scalar1, SK_Scalar1);
1259 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001260 square.close();
1261 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001262 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001263
reed@google.com7c424812011-05-15 04:38:34 +00001264 SkPath redundantSquare;
1265 redundantSquare.moveTo(0, 0);
1266 redundantSquare.lineTo(0, 0);
1267 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001268 redundantSquare.lineTo(SK_Scalar1, 0);
1269 redundantSquare.lineTo(SK_Scalar1, 0);
1270 redundantSquare.lineTo(SK_Scalar1, 0);
1271 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1272 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1273 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1274 redundantSquare.lineTo(0, SK_Scalar1);
1275 redundantSquare.lineTo(0, SK_Scalar1);
1276 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001277 redundantSquare.close();
1278 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001279 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001280
reed@google.com7c424812011-05-15 04:38:34 +00001281 SkPath bowTie;
1282 bowTie.moveTo(0, 0);
1283 bowTie.lineTo(0, 0);
1284 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001285 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1286 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1287 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1288 bowTie.lineTo(SK_Scalar1, 0);
1289 bowTie.lineTo(SK_Scalar1, 0);
1290 bowTie.lineTo(SK_Scalar1, 0);
1291 bowTie.lineTo(0, SK_Scalar1);
1292 bowTie.lineTo(0, SK_Scalar1);
1293 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001294 bowTie.close();
1295 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001296 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001297
reed@google.com7c424812011-05-15 04:38:34 +00001298 SkPath spiral;
1299 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001300 spiral.lineTo(100*SK_Scalar1, 0);
1301 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1302 spiral.lineTo(0, 100*SK_Scalar1);
1303 spiral.lineTo(0, 50*SK_Scalar1);
1304 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1305 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001306 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001307 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001308 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001309
reed@google.com7c424812011-05-15 04:38:34 +00001310 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001311 dent.moveTo(0, 0);
1312 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1313 dent.lineTo(0, 100*SK_Scalar1);
1314 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1315 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001316 dent.close();
1317 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001318 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001319
halcanary6950de62015-11-07 05:29:00 -08001320 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001321 SkPath strokedSin;
1322 for (int i = 0; i < 2000; i++) {
1323 SkScalar x = SkIntToScalar(i) / 2;
1324 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1325 if (0 == i) {
1326 strokedSin.moveTo(x, y);
1327 } else {
1328 strokedSin.lineTo(x, y);
1329 }
1330 }
1331 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1332 stroke.setStrokeStyle(2 * SK_Scalar1);
1333 stroke.applyToPath(&strokedSin, strokedSin);
1334 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1335 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001336
1337 // http://crbug.com/412640
1338 SkPath degenerateConcave;
1339 degenerateConcave.moveTo(148.67912f, 191.875f);
1340 degenerateConcave.lineTo(470.37695f, 7.5f);
1341 degenerateConcave.lineTo(148.67912f, 191.875f);
1342 degenerateConcave.lineTo(41.446522f, 376.25f);
1343 degenerateConcave.lineTo(-55.971577f, 460.0f);
1344 degenerateConcave.lineTo(41.446522f, 376.25f);
1345 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001346 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001347
1348 // http://crbug.com/433683
1349 SkPath badFirstVector;
1350 badFirstVector.moveTo(501.087708f, 319.610352f);
1351 badFirstVector.lineTo(501.087708f, 319.610352f);
1352 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1353 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1354 badFirstVector.lineTo(301.557678f, 98.044601f);
1355 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1356 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1357 badFirstVector.lineTo(504.912292f, 316.389648f);
1358 badFirstVector.lineTo(501.087708f, 319.610352f);
1359 badFirstVector.close();
1360 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001361}
1362
reed@android.com6b82d1a2009-06-03 02:35:01 +00001363static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1364 const SkRect& bounds) {
1365 REPORTER_ASSERT(reporter, p.isConvex());
1366 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001367
reed@android.com6b82d1a2009-06-03 02:35:01 +00001368 SkPath p2(p);
1369 REPORTER_ASSERT(reporter, p2.isConvex());
1370 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1371
1372 SkPath other;
1373 other.swap(p2);
1374 REPORTER_ASSERT(reporter, other.isConvex());
1375 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1376}
1377
reed@google.com04863fa2011-05-15 04:08:24 +00001378static void setFromString(SkPath* path, const char str[]) {
1379 bool first = true;
1380 while (str) {
1381 SkScalar x, y;
1382 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001383 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001384 break;
1385 }
1386 str = SkParse::FindScalar(str, &y);
1387 SkASSERT(str);
1388 if (first) {
1389 path->moveTo(x, y);
1390 first = false;
1391 } else {
1392 path->lineTo(x, y);
1393 }
1394 }
1395}
1396
1397static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001398 SkPath path;
1399
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001400 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001401 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001402 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001403 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001404 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1405
reed@google.com04863fa2011-05-15 04:08:24 +00001406 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001407 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001408 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001409 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001410
reed@google.com04863fa2011-05-15 04:08:24 +00001411 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001412 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001413 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001414 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001415
reed@google.com04863fa2011-05-15 04:08:24 +00001416 static const struct {
reed026beb52015-06-10 14:23:15 -07001417 const char* fPathStr;
1418 SkPath::Convexity fExpectedConvexity;
1419 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001420 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001421 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1422 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1423 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1424 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1425 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1426 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001427 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001428 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001429 };
1430
1431 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1432 SkPath path;
1433 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001434 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1435 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001436 // check after setting the initial convex and direction
1437 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1438 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001439 SkPathPriv::FirstDirection dir;
1440 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1441 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001442 ^ foundDir);
1443 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1444 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1445 }
1446 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1447 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001448 }
caryclarkd3d1a982014-12-08 04:57:38 -08001449
1450 static const SkPoint nonFinitePts[] = {
1451 { SK_ScalarInfinity, 0 },
1452 { 0, SK_ScalarInfinity },
1453 { SK_ScalarInfinity, SK_ScalarInfinity },
1454 { SK_ScalarNegativeInfinity, 0},
1455 { 0, SK_ScalarNegativeInfinity },
1456 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1457 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1458 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1459 { SK_ScalarNaN, 0 },
1460 { 0, SK_ScalarNaN },
1461 { SK_ScalarNaN, SK_ScalarNaN },
1462 };
1463
1464 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1465
1466 static const SkPoint finitePts[] = {
1467 { SK_ScalarMax, 0 },
1468 { 0, SK_ScalarMax },
1469 { SK_ScalarMax, SK_ScalarMax },
1470 { SK_ScalarMin, 0 },
1471 { 0, SK_ScalarMin },
1472 { SK_ScalarMin, SK_ScalarMin },
1473 };
1474
1475 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1476
1477 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1478 int i = (int) (index % nonFinitePtsCount);
1479 int f = (int) (index % finitePtsCount);
1480 int g = (int) ((f + 1) % finitePtsCount);
1481 path.reset();
1482 switch (index % 13) {
1483 case 0: path.lineTo(nonFinitePts[i]); break;
1484 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1485 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1486 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1487 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1488 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1489 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1490 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1491 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1492 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1493 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1494 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1495 case 12: path.moveTo(nonFinitePts[i]); break;
1496 }
1497 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1498 }
1499
1500 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1501 int f = (int) (index % finitePtsCount);
1502 int g = (int) ((f + 1) % finitePtsCount);
1503 path.reset();
1504 int curveSelect = index % 11;
1505 switch (curveSelect) {
1506 case 0: path.moveTo(finitePts[f]); break;
1507 case 1: path.lineTo(finitePts[f]); break;
1508 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1509 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1510 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1511 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1512 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1513 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1514 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1515 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1516 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1517 }
1518 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1519 : SkPath::kUnknown_Convexity);
1520 }
1521
reed@google.com04863fa2011-05-15 04:08:24 +00001522}
1523
reed@google.com7e6c4d12012-05-10 14:05:43 +00001524static void test_isLine(skiatest::Reporter* reporter) {
1525 SkPath path;
1526 SkPoint pts[2];
1527 const SkScalar value = SkIntToScalar(5);
1528
halcanary96fcdcc2015-08-27 07:41:13 -07001529 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001530
reed@google.com7e6c4d12012-05-10 14:05:43 +00001531 // set some non-zero values
1532 pts[0].set(value, value);
1533 pts[1].set(value, value);
1534 REPORTER_ASSERT(reporter, !path.isLine(pts));
1535 // check that pts was untouched
1536 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1537 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1538
1539 const SkScalar moveX = SkIntToScalar(1);
1540 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001541 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001542
1543 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001544 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001545 REPORTER_ASSERT(reporter, !path.isLine(pts));
1546 // check that pts was untouched
1547 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1548 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1549
1550 const SkScalar lineX = SkIntToScalar(2);
1551 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001552 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001553
1554 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001555 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001556
1557 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1558 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1559 REPORTER_ASSERT(reporter, path.isLine(pts));
1560 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1561 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1562
1563 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001564 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001565 REPORTER_ASSERT(reporter, !path.isLine(pts));
1566 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1567 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001568
1569 path.reset();
1570 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001571 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001572}
1573
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001574static void test_conservativelyContains(skiatest::Reporter* reporter) {
1575 SkPath path;
1576
1577 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1578 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1579
1580 // A circle that bounds kBaseRect (with a significant amount of slop)
1581 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001582 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001583 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1584
1585 // round-rect radii
1586 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001587
caryclark@google.com56f233a2012-11-19 13:06:06 +00001588 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001589 SkRect fQueryRect;
1590 bool fInRect;
1591 bool fInCircle;
1592 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001593 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001594 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001595 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001596
1597 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001598 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1599 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1600 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1601 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001602 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001603
1604 // rects with edges off by one from kBaseRect's edges
1605 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1606 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001607 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001608 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1609 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001610 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001611 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1612 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001613 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001614 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1615 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001616 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001617 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1618 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001619 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001620 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1621 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001622 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001623 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1624 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001625 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001626
1627 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001628 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1629 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1630 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1631 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001632
1633 // far away rect
1634 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1635 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001636 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001637
1638 // very large rect containing kBaseRect
1639 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1640 kBaseRect.fTop - 5 * kBaseRect.height(),
1641 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001642 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001643
1644 // skinny rect that spans same y-range as kBaseRect
1645 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1646 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001647 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001648
1649 // short rect that spans same x-range as kBaseRect
1650 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001651 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001652
1653 // skinny rect that spans slightly larger y-range than kBaseRect
1654 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1655 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001656 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001657
1658 // short rect that spans slightly larger x-range than kBaseRect
1659 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1660 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001661 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001662 };
1663
1664 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001665 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001666 SkRect qRect = kQueries[q].fQueryRect;
1667 if (inv & 0x1) {
1668 SkTSwap(qRect.fLeft, qRect.fRight);
1669 }
1670 if (inv & 0x2) {
1671 SkTSwap(qRect.fTop, qRect.fBottom);
1672 }
1673 for (int d = 0; d < 2; ++d) {
1674 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1675 path.reset();
1676 path.addRect(kBaseRect, dir);
1677 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1678 path.conservativelyContainsRect(qRect));
1679
1680 path.reset();
1681 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1682 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1683 path.conservativelyContainsRect(qRect));
1684
1685 path.reset();
1686 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1687 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1688 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001689
1690 path.reset();
1691 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1692 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1693 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1694 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1695 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1696 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1697 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1698 path.close();
1699 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1700 path.conservativelyContainsRect(qRect));
1701
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001702 }
1703 // Slightly non-convex shape, shouldn't contain any rects.
1704 path.reset();
1705 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001706 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001707 path.lineTo(SkIntToScalar(100), 0);
1708 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1709 path.lineTo(0, SkIntToScalar(100));
1710 path.close();
1711 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1712 }
1713 }
1714
1715 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1716 path.reset();
1717 path.moveTo(0, 0);
1718 path.lineTo(SkIntToScalar(100), 0);
1719 path.lineTo(0, SkIntToScalar(100));
1720
1721 // inside, on along top edge
1722 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1723 SkIntToScalar(10),
1724 SkIntToScalar(10))));
1725 // above
1726 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1727 SkRect::MakeXYWH(SkIntToScalar(50),
1728 SkIntToScalar(-10),
1729 SkIntToScalar(10),
1730 SkIntToScalar(10))));
1731 // to the left
1732 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1733 SkIntToScalar(5),
1734 SkIntToScalar(5),
1735 SkIntToScalar(5))));
1736
1737 // outside the diagonal edge
1738 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1739 SkIntToScalar(200),
1740 SkIntToScalar(20),
1741 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001742
bsalomonb17c1292014-08-28 14:04:55 -07001743
1744 // Test that multiple move commands do not cause asserts.
1745
1746 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001747 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001748 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1749#if 0
1750 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1751 SkIntToScalar(10),
1752 SkIntToScalar(10))));
1753#endif
1754
1755 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001756 path.reset();
1757 path.moveTo(100, 100);
1758 path.moveTo(0, 0);
1759 path.lineTo(SkIntToScalar(100), 0);
1760 path.lineTo(0, SkIntToScalar(100));
1761
1762 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1763 SkIntToScalar(10),
1764 SkIntToScalar(10))));
1765
bsalomonb17c1292014-08-28 14:04:55 -07001766 // Test that multiple move commands do not cause asserts and that the function
1767 // is not confused by the multiple moves.
1768 path.reset();
1769 path.moveTo(0, 0);
1770 path.lineTo(SkIntToScalar(100), 0);
1771 path.lineTo(0, SkIntToScalar(100));
1772 path.moveTo(0, SkIntToScalar(200));
1773 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1774 path.lineTo(0, SkIntToScalar(300));
1775
1776 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1777 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1778 SkIntToScalar(10),
1779 SkIntToScalar(10))));
1780
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001781 path.reset();
1782 path.lineTo(100, 100);
1783 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001784}
1785
reed@google.comf32322b2013-10-16 15:14:04 +00001786static void test_isRect_open_close(skiatest::Reporter* reporter) {
1787 SkPath path;
1788 bool isClosed;
1789
1790 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001791 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001792
halcanary96fcdcc2015-08-27 07:41:13 -07001793 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001794 REPORTER_ASSERT(reporter, isClosed);
1795}
1796
caryclark@google.comf1316942011-07-26 19:54:45 +00001797// Simple isRect test is inline TestPath, below.
1798// test_isRect provides more extensive testing.
1799static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001800 test_isRect_open_close(reporter);
1801
caryclark@google.comf1316942011-07-26 19:54:45 +00001802 // passing tests (all moveTo / lineTo...
1803 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1804 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1805 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1806 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1807 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1808 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1809 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1810 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1811 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001812 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1813 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 +00001814 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1815 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1816 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001817 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001818
caryclark@google.comf1316942011-07-26 19:54:45 +00001819 // failing tests
1820 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1821 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1822 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1823 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1824 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1825 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1826 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1827 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001828 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1829 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1830 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001831
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001832 // no close, but we should detect them as fillably the same as a rect
1833 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1834 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1835 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1836
1837 // like c2, but we double-back on ourselves
1838 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1839 // like c2, but we overshoot the start point
1840 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1841 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001842
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001843 struct IsRectTest {
1844 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001845 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001846 bool fClose;
1847 bool fIsRect;
1848 } tests[] = {
1849 { r1, SK_ARRAY_COUNT(r1), true, true },
1850 { r2, SK_ARRAY_COUNT(r2), true, true },
1851 { r3, SK_ARRAY_COUNT(r3), true, true },
1852 { r4, SK_ARRAY_COUNT(r4), true, true },
1853 { r5, SK_ARRAY_COUNT(r5), true, true },
1854 { r6, SK_ARRAY_COUNT(r6), true, true },
1855 { r7, SK_ARRAY_COUNT(r7), true, true },
1856 { r8, SK_ARRAY_COUNT(r8), true, true },
1857 { r9, SK_ARRAY_COUNT(r9), true, true },
1858 { ra, SK_ARRAY_COUNT(ra), true, true },
1859 { rb, SK_ARRAY_COUNT(rb), true, true },
1860 { rc, SK_ARRAY_COUNT(rc), true, true },
1861 { rd, SK_ARRAY_COUNT(rd), true, true },
1862 { re, SK_ARRAY_COUNT(re), true, true },
1863 { rf, SK_ARRAY_COUNT(rf), true, true },
1864
1865 { f1, SK_ARRAY_COUNT(f1), true, false },
1866 { f2, SK_ARRAY_COUNT(f2), true, false },
1867 { f3, SK_ARRAY_COUNT(f3), true, false },
1868 { f4, SK_ARRAY_COUNT(f4), true, false },
1869 { f5, SK_ARRAY_COUNT(f5), true, false },
1870 { f6, SK_ARRAY_COUNT(f6), true, false },
1871 { f7, SK_ARRAY_COUNT(f7), true, false },
1872 { f8, SK_ARRAY_COUNT(f8), true, false },
1873 { f9, SK_ARRAY_COUNT(f9), true, false },
1874 { fa, SK_ARRAY_COUNT(fa), true, false },
1875 { fb, SK_ARRAY_COUNT(fb), true, false },
1876
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001877 { c1, SK_ARRAY_COUNT(c1), false, true },
1878 { c2, SK_ARRAY_COUNT(c2), false, true },
1879 { c3, SK_ARRAY_COUNT(c3), false, true },
1880
1881 { d1, SK_ARRAY_COUNT(d1), false, false },
1882 { d2, SK_ARRAY_COUNT(d2), false, false },
1883 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001884 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001885
1886 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001887 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001888 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1889 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001890 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1891 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1892 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001893 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001894 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001895 path.close();
1896 }
halcanary96fcdcc2015-08-27 07:41:13 -07001897 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001898
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001899 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001900 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001901 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001902 SkPath::Direction direction;
1903 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001904 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001905 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001906 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1907 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001908 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001909 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001910 } else {
1911 SkRect computed;
1912 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001913 bool isClosed = (bool)-1;
1914 SkPath::Direction direction = (SkPath::Direction) - 1;
1915 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001916 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1917 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001918 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1919 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001920 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001921 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001922
caryclark@google.comf1316942011-07-26 19:54:45 +00001923 // fail, close then line
1924 SkPath path1;
1925 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001926 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001927 path1.lineTo(r1[index].fX, r1[index].fY);
1928 }
1929 path1.close();
1930 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001931 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001932
caryclark@google.comf1316942011-07-26 19:54:45 +00001933 // fail, move in the middle
1934 path1.reset();
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 if (index == 2) {
1938 path1.moveTo(1, .5f);
1939 }
1940 path1.lineTo(r1[index].fX, r1[index].fY);
1941 }
1942 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001943 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001944
1945 // fail, move on the edge
1946 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08001947 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001948 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1949 path1.lineTo(r1[index].fX, r1[index].fY);
1950 }
1951 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001952 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001953
caryclark@google.comf1316942011-07-26 19:54:45 +00001954 // fail, quad
1955 path1.reset();
1956 path1.moveTo(r1[0].fX, r1[0].fY);
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 if (index == 2) {
1959 path1.quadTo(1, .5f, 1, .5f);
1960 }
1961 path1.lineTo(r1[index].fX, r1[index].fY);
1962 }
1963 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001964 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001965
caryclark@google.comf1316942011-07-26 19:54:45 +00001966 // fail, cubic
1967 path1.reset();
1968 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001969 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001970 if (index == 2) {
1971 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1972 }
1973 path1.lineTo(r1[index].fX, r1[index].fY);
1974 }
1975 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001976 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001977}
1978
caryclark95bc5f32015-04-08 08:34:15 -07001979static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001980 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001981 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001982 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1983 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1984 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001985 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001986 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1987 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1988 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1989 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001990 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1991 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 +00001992 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1993 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1994 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001995
1996 // failing tests
1997 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1998 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1999 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
2000 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
2001 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
2002 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
2003 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
2004 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
2005
caryclark95bc5f32015-04-08 08:34:15 -07002006 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00002007 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
2008 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
2009
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002010 struct IsNestedRectTest {
2011 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08002012 int fPointCount;
reed026beb52015-06-10 14:23:15 -07002013 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002014 bool fClose;
2015 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
2016 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002017 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2018 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2019 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2020 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2021 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2022 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2023 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2024 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2025 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2026 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2027 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2028 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2029 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2030 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002031
reed026beb52015-06-10 14:23:15 -07002032 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2033 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2034 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2035 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2036 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2037 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2038 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2039 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002040
reed026beb52015-06-10 14:23:15 -07002041 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2042 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002043 };
2044
2045 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002046 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002047 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002048 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2049 SkPath path;
2050 if (rectFirst) {
2051 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2052 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002053 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2054 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2055 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002056 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002057 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002058 path.close();
2059 }
2060 if (!rectFirst) {
2061 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2062 }
caryclark95bc5f32015-04-08 08:34:15 -07002063 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002064 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002065 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002066 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002067 SkPathPriv::FirstDirection expectedDirs[2];
2068 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002069 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002070 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002071 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2072 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002073 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002074 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002075 } else {
reed026beb52015-06-10 14:23:15 -07002076 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002077 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002078 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002079 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002080 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2081 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002082 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2083 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002084 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002085 }
2086
2087 // fail, close then line
2088 SkPath path1;
2089 if (rectFirst) {
2090 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2091 }
2092 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002093 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002094 path1.lineTo(r1[index].fX, r1[index].fY);
2095 }
2096 path1.close();
2097 path1.lineTo(1, 0);
2098 if (!rectFirst) {
2099 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2100 }
halcanary96fcdcc2015-08-27 07:41:13 -07002101 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002102
2103 // fail, move in the middle
2104 path1.reset();
2105 if (rectFirst) {
2106 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2107 }
2108 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002109 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002110 if (index == 2) {
2111 path1.moveTo(1, .5f);
2112 }
2113 path1.lineTo(r1[index].fX, r1[index].fY);
2114 }
2115 path1.close();
2116 if (!rectFirst) {
2117 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2118 }
halcanary96fcdcc2015-08-27 07:41:13 -07002119 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002120
2121 // fail, move on the edge
2122 path1.reset();
2123 if (rectFirst) {
2124 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2125 }
bsalomon98806072014-12-12 15:11:17 -08002126 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002127 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2128 path1.lineTo(r1[index].fX, r1[index].fY);
2129 }
2130 path1.close();
2131 if (!rectFirst) {
2132 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2133 }
halcanary96fcdcc2015-08-27 07:41:13 -07002134 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002135
2136 // fail, quad
2137 path1.reset();
2138 if (rectFirst) {
2139 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2140 }
2141 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002142 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002143 if (index == 2) {
2144 path1.quadTo(1, .5f, 1, .5f);
2145 }
2146 path1.lineTo(r1[index].fX, r1[index].fY);
2147 }
2148 path1.close();
2149 if (!rectFirst) {
2150 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2151 }
halcanary96fcdcc2015-08-27 07:41:13 -07002152 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002153
2154 // fail, cubic
2155 path1.reset();
2156 if (rectFirst) {
2157 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2158 }
2159 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002160 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002161 if (index == 2) {
2162 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2163 }
2164 path1.lineTo(r1[index].fX, r1[index].fY);
2165 }
2166 path1.close();
2167 if (!rectFirst) {
2168 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2169 }
halcanary96fcdcc2015-08-27 07:41:13 -07002170 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002171
caryclark@google.com56f233a2012-11-19 13:06:06 +00002172 // fail, not nested
2173 path1.reset();
2174 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2175 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002176 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002177 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002178
caryclark95bc5f32015-04-08 08:34:15 -07002179 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2180 SkPath path;
2181 path.moveTo(0, 0);
2182 path.lineTo(10, 0);
2183 path.lineTo(10, 10);
2184 path.lineTo(0, 10);
2185 path.lineTo(0, 0);
2186 path.moveTo(1, 1);
2187 path.lineTo(9, 1);
2188 path.lineTo(9, 9);
2189 path.lineTo(1, 9);
2190 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002191 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002192
caryclark@google.combfe90372012-11-21 13:56:20 +00002193 // pass, stroke rect
2194 SkPath src, dst;
2195 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2196 SkPaint strokePaint;
2197 strokePaint.setStyle(SkPaint::kStroke_Style);
2198 strokePaint.setStrokeWidth(2);
2199 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002200 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002201}
2202
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002203static void write_and_read_back(skiatest::Reporter* reporter,
2204 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002205 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002206 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002207 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002208 SkAutoMalloc storage(size);
2209 writer.flatten(storage.get());
2210 SkReader32 reader(storage.get(), size);
2211
2212 SkPath readBack;
2213 REPORTER_ASSERT(reporter, readBack != p);
2214 reader.readPath(&readBack);
2215 REPORTER_ASSERT(reporter, readBack == p);
2216
rmistry@google.comd6176b02012-08-23 18:14:13 +00002217 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002218 p.getConvexityOrUnknown());
2219
halcanary96fcdcc2015-08-27 07:41:13 -07002220 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002221
2222 const SkRect& origBounds = p.getBounds();
2223 const SkRect& readBackBounds = readBack.getBounds();
2224
2225 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2226}
2227
reed@google.com53effc52011-09-21 19:05:12 +00002228static void test_flattening(skiatest::Reporter* reporter) {
2229 SkPath p;
2230
2231 static const SkPoint pts[] = {
2232 { 0, 0 },
2233 { SkIntToScalar(10), SkIntToScalar(10) },
2234 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2235 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2236 };
2237 p.moveTo(pts[0]);
2238 p.lineTo(pts[1]);
2239 p.quadTo(pts[2], pts[3]);
2240 p.cubicTo(pts[4], pts[5], pts[6]);
2241
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002242 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002243
2244 // create a buffer that should be much larger than the path so we don't
2245 // kill our stack if writer goes too far.
2246 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002247 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002248 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002249 REPORTER_ASSERT(reporter, size1 == size2);
2250
2251 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002252 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002253 REPORTER_ASSERT(reporter, size1 == size3);
2254 REPORTER_ASSERT(reporter, p == p2);
2255
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002256 size3 = p2.readFromMemory(buffer, 0);
2257 REPORTER_ASSERT(reporter, !size3);
2258
2259 SkPath tooShort;
2260 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2261 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2262
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002263 char buffer2[1024];
2264 size3 = p2.writeToMemory(buffer2);
2265 REPORTER_ASSERT(reporter, size1 == size3);
2266 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002267
2268 // test persistence of the oval flag & convexity
2269 {
2270 SkPath oval;
2271 SkRect rect = SkRect::MakeWH(10, 10);
2272 oval.addOval(rect);
2273
2274 write_and_read_back(reporter, oval);
2275 }
reed@google.com53effc52011-09-21 19:05:12 +00002276}
2277
2278static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002279 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002280
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002281#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002282 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002283 { 0, 0 }, // move
2284 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2285 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2286 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2287#if CONIC_PERSPECTIVE_BUG_FIXED
2288 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2289#endif
reed@google.com53effc52011-09-21 19:05:12 +00002290 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002291 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002292
reed@google.com53effc52011-09-21 19:05:12 +00002293 p.moveTo(pts[0]);
2294 p.lineTo(pts[1]);
2295 p.quadTo(pts[2], pts[3]);
2296 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002297#if CONIC_PERSPECTIVE_BUG_FIXED
2298 p.conicTo(pts[4], pts[5], 0.5f);
2299#endif
2300 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002301
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002302 {
2303 SkMatrix matrix;
2304 matrix.reset();
2305 SkPath p1;
2306 p.transform(matrix, &p1);
2307 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002308 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002309
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002310
2311 {
2312 SkMatrix matrix;
2313 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2314
2315 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2316
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002317 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002318 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002319 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002320 REPORTER_ASSERT(reporter, kPtCount == count);
2321 for (int i = 0; i < count; ++i) {
2322 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2323 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2324 }
2325 }
2326
2327 {
2328 SkMatrix matrix;
2329 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002330 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002331
2332 SkPath p1;
2333 p1.moveTo(SkPoint::Make(0, 0));
2334
2335 p.transform(matrix, &p1);
2336 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002337 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002338 SkRect pBounds = p.getBounds();
2339 SkRect p1Bounds = p1.getBounds();
2340 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2341 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2342 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2343 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2344 }
2345
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002346 p.reset();
2347 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002348
2349 {
2350 SkMatrix matrix;
2351 matrix.reset();
2352 SkPath p1;
2353 p1.moveTo(SkPoint::Make(0, 0));
2354
2355 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002356 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002357 }
2358
2359
2360 {
2361 SkMatrix matrix;
2362 matrix.reset();
2363 matrix.setScaleX(-1);
2364 SkPath p1;
2365 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2366
2367 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002368 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002369 }
2370
2371 {
2372 SkMatrix matrix;
2373 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2374 SkPath p1;
2375 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2376
2377 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002378 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002379 }
reed@google.com53effc52011-09-21 19:05:12 +00002380}
2381
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002382static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002383 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002384 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002385
caryclark@google.com56f233a2012-11-19 13:06:06 +00002386 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002387 const char* testPath;
2388 const size_t numResultPts;
2389 const SkRect resultBound;
2390 const SkPath::Verb* resultVerbs;
2391 const size_t numResultVerbs;
2392 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002393
schenney@chromium.org7e963602012-06-13 17:05:43 +00002394 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2395 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2396 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2397 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2398 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2399 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2400 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2401 static const SkPath::Verb resultVerbs8[] = {
2402 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2403 };
2404 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2405 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2406 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2407 static const SkPath::Verb resultVerbs12[] = {
2408 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2409 };
2410 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2411 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2412 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2413 static const SkPath::Verb resultVerbs16[] = {
2414 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2415 };
2416 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002417 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002418 { "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 -07002419 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002420 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2421 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2422 { "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) },
2423 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2424 { "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) },
2425 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2426 { "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) },
2427 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2428 { "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) },
2429 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2430 { "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 +00002431 SK_ARRAY_COUNT(resultVerbs14)
2432 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002433 { "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) },
2434 { "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 +00002435 SK_ARRAY_COUNT(resultVerbs16)
2436 }
2437 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002438
schenney@chromium.org7e963602012-06-13 17:05:43 +00002439 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2440 p.reset();
2441 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2442 REPORTER_ASSERT(reporter, valid);
2443 REPORTER_ASSERT(reporter, !p.isEmpty());
2444 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2445 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2446 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2447 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2448 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2449 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002450 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002451}
2452
2453struct SegmentInfo {
2454 SkPath fPath;
2455 int fPointCount;
2456};
2457
reed@google.com10296cc2011-09-21 12:29:05 +00002458#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2459
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002460static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002461 SkPath p, p2;
2462
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002463 p.moveTo(0, 0);
2464 p.quadTo(100, 100, 200, 200);
2465 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2466 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002467 p2 = p;
2468 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002469 p.cubicTo(100, 100, 200, 200, 300, 300);
2470 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2471 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002472 p2 = p;
2473 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2474
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002475 p.reset();
2476 p.moveTo(0, 0);
2477 p.cubicTo(100, 100, 200, 200, 300, 300);
2478 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002479 p2 = p;
2480 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002481
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002482 REPORTER_ASSERT(reporter, !p.isEmpty());
2483}
2484
2485static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002486 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002487 SkPoint pts[4];
2488
2489 // Test an iterator with no path
2490 SkPath::Iter noPathIter;
2491 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002492
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002493 // Test that setting an empty path works
2494 noPathIter.setPath(p, false);
2495 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002496
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002497 // Test that close path makes no difference for an empty path
2498 noPathIter.setPath(p, true);
2499 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002500
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002501 // Test an iterator with an initial empty path
2502 SkPath::Iter iter(p, false);
2503 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2504
2505 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002506 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002507 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2508
rmistry@google.comd6176b02012-08-23 18:14:13 +00002509
schenney@chromium.org7e963602012-06-13 17:05:43 +00002510 struct iterTestData {
2511 const char* testPath;
2512 const bool forceClose;
2513 const bool consumeDegenerates;
2514 const size_t* numResultPtsPerVerb;
2515 const SkPoint* resultPts;
2516 const SkPath::Verb* resultVerbs;
2517 const size_t numResultVerbs;
2518 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002519
schenney@chromium.org7e963602012-06-13 17:05:43 +00002520 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2521 static const SkPath::Verb resultVerbs2[] = {
2522 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2523 };
2524 static const SkPath::Verb resultVerbs3[] = {
2525 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2526 };
2527 static const SkPath::Verb resultVerbs4[] = {
2528 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2529 };
2530 static const SkPath::Verb resultVerbs5[] = {
2531 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2532 };
2533 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002534 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2535 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2536 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2537 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002538 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002539 static const SkPoint resultPts2[] = {
2540 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2541 };
2542 static const SkPoint resultPts3[] = {
2543 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2544 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2545 };
2546 static const SkPoint resultPts4[] = {
2547 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2548 };
2549 static const SkPoint resultPts5[] = {
2550 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2551 };
2552 static const struct iterTestData gIterTests[] = {
2553 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002554 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2555 { "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 +00002556 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2557 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2558 { "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) },
2559 { "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 +00002560 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2561 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2562 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2563 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2564 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2565 { "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 +00002566 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002567
schenney@chromium.org7e963602012-06-13 17:05:43 +00002568 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2569 p.reset();
2570 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2571 REPORTER_ASSERT(reporter, valid);
2572 iter.setPath(p, gIterTests[i].forceClose);
2573 int j = 0, l = 0;
2574 do {
2575 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2576 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2577 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2578 }
2579 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2580 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2581 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002582
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002583 p.reset();
2584 iter.setPath(p, false);
2585 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2586 p.lineTo(1, 1);
2587 p.close();
2588 iter.setPath(p, false);
2589 REPORTER_ASSERT(reporter, iter.isClosedContour());
2590 p.reset();
2591 iter.setPath(p, true);
2592 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2593 p.lineTo(1, 1);
2594 iter.setPath(p, true);
2595 REPORTER_ASSERT(reporter, iter.isClosedContour());
2596 p.moveTo(0, 0);
2597 p.lineTo(2, 2);
2598 iter.setPath(p, false);
2599 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2600
2601 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2602 // check to see if the result is correct.
2603 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2604 p.reset();
2605 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2606 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2607 iter.setPath(p, true);
2608 iter.next(pts, false);
2609 iter.next(pts, false);
2610 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2611 }
2612
2613 p.reset();
2614 p.quadTo(0, 0, 0, 0);
2615 iter.setPath(p, false);
2616 iter.next(pts, false);
2617 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2618 iter.setPath(p, false);
2619 iter.next(pts, false);
2620 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2621
2622 p.reset();
2623 p.conicTo(0, 0, 0, 0, 0.5f);
2624 iter.setPath(p, false);
2625 iter.next(pts, false);
2626 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2627 iter.setPath(p, false);
2628 iter.next(pts, false);
2629 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2630
2631 p.reset();
2632 p.cubicTo(0, 0, 0, 0, 0, 0);
2633 iter.setPath(p, false);
2634 iter.next(pts, false);
2635 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2636 iter.setPath(p, false);
2637 iter.next(pts, false);
2638 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2639
2640 p.moveTo(1, 1); // add a trailing moveto
2641 iter.setPath(p, false);
2642 iter.next(pts, false);
2643 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2644 iter.setPath(p, false);
2645 iter.next(pts, false);
2646 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2647
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002648 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002649
2650 // Test out mixed degenerate and non-degenerate geometry with Conics
2651 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2652 SkRect r = SkRect::MakeWH(100, 100);
2653 SkRRect rr;
2654 rr.setRectRadii(r, radii);
2655 p.reset();
2656 p.addRRect(rr);
2657 iter.setPath(p, false);
2658 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2659 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2660 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2661 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2662 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002663}
2664
2665static void test_raw_iter(skiatest::Reporter* reporter) {
2666 SkPath p;
2667 SkPoint pts[4];
2668
2669 // Test an iterator with no path
2670 SkPath::RawIter noPathIter;
2671 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2672 // Test that setting an empty path works
2673 noPathIter.setPath(p);
2674 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002675
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002676 // Test an iterator with an initial empty path
2677 SkPath::RawIter iter(p);
2678 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2679
2680 // Test that a move-only path returns the move.
2681 p.moveTo(SK_Scalar1, 0);
2682 iter.setPath(p);
2683 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2684 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2685 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2686 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2687
2688 // No matter how many moves we add, we should get them all back
2689 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2690 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2691 iter.setPath(p);
2692 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2693 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2694 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2695 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2696 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2697 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2698 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2699 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2700 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2701 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2702
2703 // Initial close is never ever stored
2704 p.reset();
2705 p.close();
2706 iter.setPath(p);
2707 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2708
2709 // Move/close sequences
2710 p.reset();
2711 p.close(); // Not stored, no purpose
2712 p.moveTo(SK_Scalar1, 0);
2713 p.close();
2714 p.close(); // Not stored, no purpose
2715 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2716 p.close();
2717 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2718 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2719 p.close();
2720 iter.setPath(p);
2721 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2722 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2723 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2724 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002725 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2726 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2727 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2728 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002729 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2730 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2731 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2732 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2733 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2734 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2735 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002736 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2737
2738 // Generate random paths and verify
2739 SkPoint randomPts[25];
2740 for (int i = 0; i < 5; ++i) {
2741 for (int j = 0; j < 5; ++j) {
2742 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2743 }
2744 }
2745
2746 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002747 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002748 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002749 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002750 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002751
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002752 for (int i = 0; i < 500; ++i) {
2753 p.reset();
2754 bool lastWasClose = true;
2755 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002756 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002757 int numPoints = 0;
2758 int numVerbs = (rand.nextU() >> 16) % 10;
2759 int numIterVerbs = 0;
2760 for (int j = 0; j < numVerbs; ++j) {
2761 do {
2762 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2763 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002764 switch (nextVerb) {
2765 case SkPath::kMove_Verb:
2766 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2767 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002768 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002769 numPoints += 1;
2770 lastWasClose = false;
2771 haveMoveTo = true;
2772 break;
2773 case SkPath::kLine_Verb:
2774 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002775 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002776 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2777 haveMoveTo = true;
2778 }
2779 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2780 p.lineTo(expectedPts[numPoints]);
2781 numPoints += 1;
2782 lastWasClose = false;
2783 break;
2784 case SkPath::kQuad_Verb:
2785 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002786 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002787 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2788 haveMoveTo = true;
2789 }
2790 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2791 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2792 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2793 numPoints += 2;
2794 lastWasClose = false;
2795 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002796 case SkPath::kConic_Verb:
2797 if (!haveMoveTo) {
2798 expectedPts[numPoints++] = lastMoveToPt;
2799 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2800 haveMoveTo = true;
2801 }
2802 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2803 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2804 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2805 rand.nextUScalar1() * 4);
2806 numPoints += 2;
2807 lastWasClose = false;
2808 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002809 case SkPath::kCubic_Verb:
2810 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002811 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002812 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2813 haveMoveTo = true;
2814 }
2815 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2816 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2817 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2818 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2819 expectedPts[numPoints + 2]);
2820 numPoints += 3;
2821 lastWasClose = false;
2822 break;
2823 case SkPath::kClose_Verb:
2824 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002825 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002826 lastWasClose = true;
2827 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002828 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002829 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002830 }
2831 expectedVerbs[numIterVerbs++] = nextVerb;
2832 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002833
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002834 iter.setPath(p);
2835 numVerbs = numIterVerbs;
2836 numIterVerbs = 0;
2837 int numIterPts = 0;
2838 SkPoint lastMoveTo;
2839 SkPoint lastPt;
2840 lastMoveTo.set(0, 0);
2841 lastPt.set(0, 0);
2842 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2843 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2844 numIterVerbs++;
2845 switch (nextVerb) {
2846 case SkPath::kMove_Verb:
2847 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2848 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2849 lastPt = lastMoveTo = pts[0];
2850 numIterPts += 1;
2851 break;
2852 case SkPath::kLine_Verb:
2853 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2854 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2855 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2856 lastPt = pts[1];
2857 numIterPts += 1;
2858 break;
2859 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002860 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002861 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2862 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2863 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2864 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2865 lastPt = pts[2];
2866 numIterPts += 2;
2867 break;
2868 case SkPath::kCubic_Verb:
2869 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2870 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2871 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2872 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2873 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2874 lastPt = pts[3];
2875 numIterPts += 3;
2876 break;
2877 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002878 lastPt = lastMoveTo;
2879 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002880 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002881 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002882 }
2883 }
2884 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2885 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2886 }
2887}
2888
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002889static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002890 const SkPath& path,
2891 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07002892 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002893 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002894 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
reed026beb52015-06-10 14:23:15 -07002895 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002896
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002897 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002898 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2899 }
2900}
2901
2902static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002903 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002904 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002905 SkPath tmp;
2906
2907 SkMatrix m;
2908 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2909 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002910 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07002911 if (SkPathPriv::kCCW_FirstDirection == dir) {
2912 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002913 } else {
reed026beb52015-06-10 14:23:15 -07002914 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
2915 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002916 }
2917 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002918}
2919
2920static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002921 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002922 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002923 SkPath tmp;
2924
2925 // translate at small offset
2926 SkMatrix m;
2927 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2928 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002929 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002930
2931 tmp.reset();
2932 m.reset();
2933
2934 // translate at a relatively big offset
2935 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2936 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002937 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002938}
2939
2940static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002941 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002942 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002943 for (int angle = 0; angle < 360; ++angle) {
2944 SkPath tmp;
2945 SkMatrix m;
2946 m.setRotate(SkIntToScalar(angle));
2947 path.transform(m, &tmp);
2948
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002949 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002950 // degrees is not an oval anymore, this can be improved. we made this
2951 // for the simplicity of our implementation.
2952 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002953 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002954 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002955 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002956 }
2957 }
2958}
2959
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002960static void test_circle_mirror_x(skiatest::Reporter* reporter,
2961 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002962 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002963 SkPath tmp;
2964 SkMatrix m;
2965 m.reset();
2966 m.setScaleX(-SK_Scalar1);
2967 path.transform(m, &tmp);
2968
reed026beb52015-06-10 14:23:15 -07002969 if (SkPathPriv::kCW_FirstDirection == dir) {
2970 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002971 } else {
reed026beb52015-06-10 14:23:15 -07002972 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2973 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002974 }
2975
2976 check_for_circle(reporter, tmp, true, dir);
2977}
2978
2979static void test_circle_mirror_y(skiatest::Reporter* reporter,
2980 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002981 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002982 SkPath tmp;
2983 SkMatrix m;
2984 m.reset();
2985 m.setScaleY(-SK_Scalar1);
2986 path.transform(m, &tmp);
2987
reed026beb52015-06-10 14:23:15 -07002988 if (SkPathPriv::kCW_FirstDirection == dir) {
2989 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002990 } else {
reed026beb52015-06-10 14:23:15 -07002991 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2992 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002993 }
2994
2995 check_for_circle(reporter, tmp, true, dir);
2996}
2997
2998static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2999 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07003000 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003001 SkPath tmp;
3002 SkMatrix m;
3003 m.reset();
3004 m.setScaleX(-SK_Scalar1);
3005 m.setScaleY(-SK_Scalar1);
3006 path.transform(m, &tmp);
3007
3008 check_for_circle(reporter, tmp, true, dir);
3009}
3010
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003011static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07003012 SkPath::Direction inDir) {
3013 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003014 SkPath path;
3015
3016 // circle at origin
reed026beb52015-06-10 14:23:15 -07003017 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3018
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003019 check_for_circle(reporter, path, true, dir);
3020 test_circle_rotate(reporter, path, dir);
3021 test_circle_translate(reporter, path, dir);
3022 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003023
3024 // circle at an offset at (10, 10)
3025 path.reset();
3026 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003027 SkIntToScalar(20), inDir);
3028
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003029 check_for_circle(reporter, path, true, dir);
3030 test_circle_rotate(reporter, path, dir);
3031 test_circle_translate(reporter, path, dir);
3032 test_circle_skew(reporter, path, dir);
3033 test_circle_mirror_x(reporter, path, dir);
3034 test_circle_mirror_y(reporter, path, dir);
3035 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003036}
3037
3038static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3039 SkPath path;
3040 SkPath circle;
3041 SkPath rect;
3042 SkPath empty;
3043
reed026beb52015-06-10 14:23:15 -07003044 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3045 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003046
3047 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003048 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3049 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3050
3051 SkMatrix translate;
3052 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3053
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003054 // Although all the path concatenation related operations leave
3055 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003056
3057 // empty + circle (translate)
3058 path = empty;
3059 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003060 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003061
3062 // circle + empty (translate)
3063 path = circle;
3064 path.addPath(empty, translate);
reed026beb52015-06-10 14:23:15 -07003065 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003066
3067 // test reverseAddPath
3068 path = circle;
3069 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003070 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003071}
3072
3073static void test_circle(skiatest::Reporter* reporter) {
3074 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3075 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3076
3077 // multiple addCircle()
3078 SkPath path;
3079 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3080 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003081 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003082
3083 // some extra lineTo() would make isOval() fail
3084 path.reset();
3085 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3086 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003087 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003088
3089 // not back to the original point
3090 path.reset();
3091 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3092 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003093 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003094
3095 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003096
3097 // test negative radius
3098 path.reset();
3099 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3100 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003101}
3102
3103static void test_oval(skiatest::Reporter* reporter) {
3104 SkRect rect;
3105 SkMatrix m;
3106 SkPath path;
3107
3108 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3109 path.addOval(rect);
3110
halcanary96fcdcc2015-08-27 07:41:13 -07003111 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003112
3113 m.setRotate(SkIntToScalar(90));
3114 SkPath tmp;
3115 path.transform(m, &tmp);
3116 // an oval rotated 90 degrees is still an oval.
halcanary96fcdcc2015-08-27 07:41:13 -07003117 REPORTER_ASSERT(reporter, tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003118
3119 m.reset();
3120 m.setRotate(SkIntToScalar(30));
3121 tmp.reset();
3122 path.transform(m, &tmp);
3123 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003124 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003125
3126 // since empty path being transformed.
3127 path.reset();
3128 tmp.reset();
3129 m.reset();
3130 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003131 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003132
3133 // empty path is not an oval
3134 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003135 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003136
3137 // only has moveTo()s
3138 tmp.reset();
3139 tmp.moveTo(0, 0);
3140 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003141 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003142
3143 // mimic WebKit's calling convention,
3144 // call moveTo() first and then call addOval()
3145 path.reset();
3146 path.moveTo(0, 0);
3147 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003148 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003149
3150 // copy path
3151 path.reset();
3152 tmp.reset();
3153 tmp.addOval(rect);
3154 path = tmp;
halcanary96fcdcc2015-08-27 07:41:13 -07003155 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003156}
3157
bungeman@google.coma5809a32013-06-21 15:13:34 +00003158static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3159 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003160
reed@android.com3abec1d2009-03-02 05:36:20 +00003161 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003162 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003163 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003164 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003165 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003166 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3167 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003168 REPORTER_ASSERT(reporter, p == empty);
3169 REPORTER_ASSERT(reporter, !(p != empty));
3170}
3171
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003172static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3173 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003174 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003175 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003176 path->setConvexity(SkPath::kUnknown_Convexity);
3177 REPORTER_ASSERT(reporter, path->isConvex());
3178 path->reset();
3179}
3180
caryclarkd3d1a982014-12-08 04:57:38 -08003181static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3182 SkPath::Direction dir) {
3183 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003184 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003185 path->setConvexity(SkPath::kUnknown_Convexity);
3186 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3187 path->reset();
3188}
3189
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003190static void test_rrect(skiatest::Reporter* reporter) {
3191 SkPath p;
3192 SkRRect rr;
3193 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3194 SkRect r = {10, 20, 30, 40};
3195 rr.setRectRadii(r, radii);
3196 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003197 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003198 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003199 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003200 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003201 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003202 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003203 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003204 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003205 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003206 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003207 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3208 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3209 SkVector save = radii[i];
3210 radii[i].set(0, 0);
3211 rr.setRectRadii(r, radii);
3212 p.addRRect(rr);
3213 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3214 radii[i] = save;
3215 }
3216 p.addRoundRect(r, 0, 0);
3217 SkRect returnedRect;
3218 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3219 REPORTER_ASSERT(reporter, returnedRect == r);
3220 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3221 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3222 rr.setRectRadii(r, zeroRadii);
3223 p.addRRect(rr);
3224 bool closed;
3225 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003226 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003227 REPORTER_ASSERT(reporter, closed);
3228 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3229 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3230 p.addRRect(rr, SkPath::kCW_Direction);
3231 p.addRRect(rr, SkPath::kCW_Direction);
3232 REPORTER_ASSERT(reporter, !p.isConvex());
3233 p.reset();
3234 p.addRRect(rr, SkPath::kCCW_Direction);
3235 p.addRRect(rr, SkPath::kCCW_Direction);
3236 REPORTER_ASSERT(reporter, !p.isConvex());
3237 p.reset();
3238 SkRect emptyR = {10, 20, 10, 30};
3239 rr.setRectRadii(emptyR, radii);
3240 p.addRRect(rr);
3241 REPORTER_ASSERT(reporter, p.isEmpty());
3242 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3243 rr.setRectRadii(largeR, radii);
3244 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003245 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003246
3247 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003248 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3249 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003250 REPORTER_ASSERT(reporter, rr.isEmpty());
3251
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003252 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3253 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3254 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003255}
3256
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003257static void test_arc(skiatest::Reporter* reporter) {
3258 SkPath p;
3259 SkRect emptyOval = {10, 20, 30, 20};
3260 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3261 p.addArc(emptyOval, 1, 2);
3262 REPORTER_ASSERT(reporter, p.isEmpty());
3263 p.reset();
3264 SkRect oval = {10, 20, 30, 40};
3265 p.addArc(oval, 1, 0);
3266 REPORTER_ASSERT(reporter, p.isEmpty());
3267 p.reset();
3268 SkPath cwOval;
3269 cwOval.addOval(oval);
3270 p.addArc(oval, 1, 360);
3271 REPORTER_ASSERT(reporter, p == cwOval);
3272 p.reset();
3273 SkPath ccwOval;
3274 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3275 p.addArc(oval, 1, -360);
3276 REPORTER_ASSERT(reporter, p == ccwOval);
3277 p.reset();
3278 p.addArc(oval, 1, 180);
3279 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003280 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003281 p.setConvexity(SkPath::kUnknown_Convexity);
3282 REPORTER_ASSERT(reporter, p.isConvex());
3283}
3284
3285static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3286 SkScalar x0, SkScalar y0) {
3287 SkPoint pts[4];
3288 SkPath::Verb v = iter->next(pts);
3289 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3290 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3291 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3292}
3293
3294static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3295 SkScalar x1, SkScalar y1) {
3296 SkPoint pts[4];
3297 SkPath::Verb v = iter->next(pts);
3298 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3299 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3300 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3301}
3302
3303static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3304 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3305 SkPoint pts[4];
3306 SkPath::Verb v = iter->next(pts);
3307 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3308 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3309 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3310 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3311 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3312}
3313
3314static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3315 SkPoint pts[4];
3316 SkPath::Verb v = iter->next(pts);
3317 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3318}
3319
3320static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3321 check_done(reporter, p, iter);
3322 p->reset();
3323}
3324
3325static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3326 SkScalar x0, SkScalar y0) {
3327 SkPath::RawIter iter(*p);
3328 check_move(reporter, &iter, x0, y0);
3329 check_done_and_reset(reporter, p, &iter);
3330}
3331
3332static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3333 SkScalar x1, SkScalar y1) {
3334 SkPath::RawIter iter(*p);
3335 check_move(reporter, &iter, 0, 0);
3336 check_line(reporter, &iter, x1, y1);
3337 check_done_and_reset(reporter, p, &iter);
3338}
3339
3340static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3341 SkScalar x1, SkScalar y1) {
3342 SkPath::RawIter iter(*p);
3343 check_move(reporter, &iter, 0, 0);
3344 check_line(reporter, &iter, x1, y1);
3345 check_done(reporter, p, &iter);
3346}
3347
3348static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3349 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3350 SkPath::RawIter iter(*p);
3351 check_move(reporter, &iter, 0, 0);
3352 check_line(reporter, &iter, x1, y1);
3353 check_line(reporter, &iter, x2, y2);
3354 check_done_and_reset(reporter, p, &iter);
3355}
3356
3357static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3358 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3359 SkPath::RawIter iter(*p);
3360 check_move(reporter, &iter, 0, 0);
3361 check_quad(reporter, &iter, x1, y1, x2, y2);
3362 check_done_and_reset(reporter, p, &iter);
3363}
3364
reedd5d27d92015-02-09 13:54:43 -08003365static bool nearly_equal(const SkRect& a, const SkRect& b) {
3366 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3367 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3368 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3369 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3370}
3371
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003372static void test_arcTo(skiatest::Reporter* reporter) {
3373 SkPath p;
3374 p.arcTo(0, 0, 1, 2, 1);
3375 check_path_is_line_and_reset(reporter, &p, 0, 0);
3376 p.arcTo(1, 2, 1, 2, 1);
3377 check_path_is_line_and_reset(reporter, &p, 1, 2);
3378 p.arcTo(1, 2, 3, 4, 0);
3379 check_path_is_line_and_reset(reporter, &p, 1, 2);
3380 p.arcTo(1, 2, 0, 0, 1);
3381 check_path_is_line_and_reset(reporter, &p, 1, 2);
3382 p.arcTo(1, 0, 1, 1, 1);
3383 SkPoint pt;
3384 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3385 p.reset();
3386 p.arcTo(1, 0, 1, -1, 1);
3387 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3388 p.reset();
3389 SkRect oval = {1, 2, 3, 4};
3390 p.arcTo(oval, 0, 0, true);
3391 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3392 p.arcTo(oval, 0, 0, false);
3393 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3394 p.arcTo(oval, 360, 0, true);
3395 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3396 p.arcTo(oval, 360, 0, false);
3397 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003398
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003399 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003400 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003401 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003402 sweep += delta;
3403 delta /= 2;
3404 }
3405 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003406 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003407 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003408 sweep -= delta;
3409 delta /= 2;
3410 }
3411 SkRect noOvalWidth = {1, 2, 0, 3};
3412 p.reset();
3413 p.arcTo(noOvalWidth, 0, 360, false);
3414 REPORTER_ASSERT(reporter, p.isEmpty());
3415
3416 SkRect noOvalHeight = {1, 2, 3, 1};
3417 p.reset();
3418 p.arcTo(noOvalHeight, 0, 360, false);
3419 REPORTER_ASSERT(reporter, p.isEmpty());
3420}
3421
3422static void test_addPath(skiatest::Reporter* reporter) {
3423 SkPath p, q;
3424 p.lineTo(1, 2);
3425 q.moveTo(4, 4);
3426 q.lineTo(7, 8);
3427 q.conicTo(8, 7, 6, 5, 0.5f);
3428 q.quadTo(6, 7, 8, 6);
3429 q.cubicTo(5, 6, 7, 8, 7, 5);
3430 q.close();
3431 p.addPath(q, -4, -4);
3432 SkRect expected = {0, 0, 4, 4};
3433 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3434 p.reset();
3435 p.reverseAddPath(q);
3436 SkRect reverseExpected = {4, 4, 8, 8};
3437 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3438}
3439
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003440static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3441 SkPath p, q;
3442 if (explicitMoveTo) {
3443 p.moveTo(1, 1);
3444 }
3445 p.lineTo(1, 2);
3446 if (explicitMoveTo) {
3447 q.moveTo(2, 1);
3448 }
3449 q.lineTo(2, 2);
3450 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3451 uint8_t verbs[4];
3452 int verbcount = p.getVerbs(verbs, 4);
3453 REPORTER_ASSERT(reporter, verbcount == 4);
3454 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3455 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3456 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3457 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3458}
3459
3460static void test_extendClosedPath(skiatest::Reporter* reporter) {
3461 SkPath p, q;
3462 p.moveTo(1, 1);
3463 p.lineTo(1, 2);
3464 p.lineTo(2, 2);
3465 p.close();
3466 q.moveTo(2, 1);
3467 q.lineTo(2, 3);
3468 p.addPath(q, SkPath::kExtend_AddPathMode);
3469 uint8_t verbs[7];
3470 int verbcount = p.getVerbs(verbs, 7);
3471 REPORTER_ASSERT(reporter, verbcount == 7);
3472 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3473 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3474 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3475 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3476 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3477 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3478 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3479
3480 SkPoint pt;
3481 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3482 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3483 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3484}
3485
3486static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3487 SkPath p, q, r;
3488 // case 1: dst is empty
3489 p.moveTo(2, 1);
3490 p.lineTo(2, 3);
3491 q.addPath(p, mode);
3492 REPORTER_ASSERT(reporter, q == p);
3493 // case 2: src is empty
3494 p.addPath(r, mode);
3495 REPORTER_ASSERT(reporter, q == p);
3496 // case 3: src and dst are empty
3497 q.reset();
3498 q.addPath(r, mode);
3499 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003500}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003501
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003502static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3503 SkPath p;
3504 p.conicTo(1, 2, 3, 4, -1);
3505 check_path_is_line_and_reset(reporter, &p, 3, 4);
3506 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3507 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3508 p.conicTo(1, 2, 3, 4, 1);
3509 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3510}
3511
3512static void test_get_point(skiatest::Reporter* reporter) {
3513 SkPath p;
3514 SkPoint pt = p.getPoint(0);
3515 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003516 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003517 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3518 p.setLastPt(10, 10);
3519 pt = p.getPoint(0);
3520 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003521 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003522 p.rMoveTo(10, 10);
3523 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3524}
3525
3526static void test_contains(skiatest::Reporter* reporter) {
3527 SkPath p;
caryclarkdbaec732016-01-05 06:20:09 -08003528 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f
3529 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
3530 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
3531 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f
3532 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
3533 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 -08003534 // this may return true or false, depending on the platform's numerics, but it should not crash
3535 (void) p.contains(-77.2027664f, 15.3066053f);
caryclarkdbaec732016-01-05 06:20:09 -08003536
3537 p.reset();
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003538 p.setFillType(SkPath::kInverseWinding_FillType);
3539 REPORTER_ASSERT(reporter, p.contains(0, 0));
3540 p.setFillType(SkPath::kWinding_FillType);
3541 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3542 p.moveTo(4, 4);
3543 p.lineTo(6, 8);
3544 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003545 // test on edge
3546 REPORTER_ASSERT(reporter, p.contains(6, 4));
3547 REPORTER_ASSERT(reporter, p.contains(5, 6));
3548 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003549 // test quick reject
3550 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3551 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3552 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3553 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3554 // test various crossings in x
3555 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3556 REPORTER_ASSERT(reporter, p.contains(6, 7));
3557 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3558 p.reset();
3559 p.moveTo(4, 4);
3560 p.lineTo(8, 6);
3561 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003562 // test on edge
3563 REPORTER_ASSERT(reporter, p.contains(4, 6));
3564 REPORTER_ASSERT(reporter, p.contains(6, 5));
3565 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003566 // test various crossings in y
3567 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3568 REPORTER_ASSERT(reporter, p.contains(7, 6));
3569 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003570 p.reset();
3571 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003572 p.lineTo(8, 4);
3573 p.lineTo(8, 8);
3574 p.lineTo(4, 8);
3575 // test on vertices
3576 REPORTER_ASSERT(reporter, p.contains(4, 4));
3577 REPORTER_ASSERT(reporter, p.contains(8, 4));
3578 REPORTER_ASSERT(reporter, p.contains(8, 8));
3579 REPORTER_ASSERT(reporter, p.contains(4, 8));
3580 p.reset();
3581 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003582 p.lineTo(6, 8);
3583 p.lineTo(2, 8);
3584 // test on edge
3585 REPORTER_ASSERT(reporter, p.contains(5, 6));
3586 REPORTER_ASSERT(reporter, p.contains(4, 8));
3587 REPORTER_ASSERT(reporter, p.contains(3, 6));
3588 p.reset();
3589 p.moveTo(4, 4);
3590 p.lineTo(0, 6);
3591 p.lineTo(4, 8);
3592 // test on edge
3593 REPORTER_ASSERT(reporter, p.contains(2, 5));
3594 REPORTER_ASSERT(reporter, p.contains(2, 7));
3595 REPORTER_ASSERT(reporter, p.contains(4, 6));
3596 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3597 p.reset();
3598 p.moveTo(4, 0);
3599 p.lineTo(6, 4);
3600 p.lineTo(2, 4);
3601 p.moveTo(4, 0);
3602 p.lineTo(0, 8);
3603 p.lineTo(8, 8);
3604 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3605 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3606 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3607 REPORTER_ASSERT(reporter, p.contains(4, 4));
3608
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003609 // test quads
3610 p.reset();
3611 p.moveTo(4, 4);
3612 p.quadTo(6, 6, 8, 8);
3613 p.quadTo(6, 8, 4, 8);
3614 p.quadTo(4, 6, 4, 4);
3615 REPORTER_ASSERT(reporter, p.contains(5, 6));
3616 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003617 // test quad edge
3618 REPORTER_ASSERT(reporter, p.contains(5, 5));
3619 REPORTER_ASSERT(reporter, p.contains(5, 8));
3620 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003621 // test quad endpoints
3622 REPORTER_ASSERT(reporter, p.contains(4, 4));
3623 REPORTER_ASSERT(reporter, p.contains(8, 8));
3624 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003625
3626 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003627 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3628 p.moveTo(qPts[0]);
3629 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3630 p.quadTo(qPts[index], qPts[index + 1]);
3631 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003632 REPORTER_ASSERT(reporter, p.contains(5, 6));
3633 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003634 // test quad edge
3635 SkPoint halfway;
3636 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3637 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3638 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3639 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003640
caryclark9aacd902015-12-14 08:38:09 -08003641 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003642 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003643 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3644 p.moveTo(kPts[0]);
3645 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3646 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3647 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003648 REPORTER_ASSERT(reporter, p.contains(5, 6));
3649 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003650 // test conic edge
3651 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3652 SkConic conic(&kPts[index], 0.5f);
3653 halfway = conic.evalAt(0.5f);
3654 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3655 }
caryclark9cb5d752015-12-18 04:35:24 -08003656 // test conic end points
3657 REPORTER_ASSERT(reporter, p.contains(4, 4));
3658 REPORTER_ASSERT(reporter, p.contains(8, 8));
3659 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003660
3661 // test cubics
3662 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3663 for (int i = 0; i < 3; ++i) {
3664 p.reset();
3665 p.setFillType(SkPath::kEvenOdd_FillType);
3666 p.moveTo(pts[i].fX, pts[i].fY);
3667 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);
3668 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);
3669 p.close();
3670 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3671 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003672 // test cubic edge
3673 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3674 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3675 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3676 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003677 // test cubic end points
3678 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3679 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3680 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003681 }
3682}
3683
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003684class PathRefTest_Private {
3685public:
3686 static void TestPathRef(skiatest::Reporter* reporter) {
3687 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003688
halcanary385fe4d2015-08-26 13:07:48 -07003689 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003690
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003691 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003692
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003693 {
3694 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3695 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3696 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3697 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3698 for (int i = 0; i < kRepeatCnt; ++i) {
3699 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3700 }
3701 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003702 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003703
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003704 {
3705 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3706 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3707 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3708 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3709 for (int i = 0; i < kRepeatCnt; ++i) {
3710 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3711 }
3712 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003713 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003714
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003715 {
3716 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3717 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3718 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3719 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3720 for (int i = 0; i < kRepeatCnt; ++i) {
3721 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3722 }
3723 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003724 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003725
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003726 {
halcanary96fcdcc2015-08-27 07:41:13 -07003727 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003728 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3729 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3730 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3731 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3732 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003733 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003734 for (int i = 0; i < kRepeatCnt; ++i) {
3735 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3736 }
3737 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003738 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003739
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003740 {
3741 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3742 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3743 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3744 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3745 for (int i = 0; i < kRepeatCnt; ++i) {
3746 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3747 }
3748 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003749 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003750 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003751};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003752
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003753static void test_operatorEqual(skiatest::Reporter* reporter) {
3754 SkPath a;
3755 SkPath b;
3756 REPORTER_ASSERT(reporter, a == a);
3757 REPORTER_ASSERT(reporter, a == b);
3758 a.setFillType(SkPath::kInverseWinding_FillType);
3759 REPORTER_ASSERT(reporter, a != b);
3760 a.reset();
3761 REPORTER_ASSERT(reporter, a == b);
3762 a.lineTo(1, 1);
3763 REPORTER_ASSERT(reporter, a != b);
3764 a.reset();
3765 REPORTER_ASSERT(reporter, a == b);
3766 a.lineTo(1, 1);
3767 b.lineTo(1, 2);
3768 REPORTER_ASSERT(reporter, a != b);
3769 a.reset();
3770 a.lineTo(1, 2);
3771 REPORTER_ASSERT(reporter, a == b);
3772}
3773
caryclark66a5d8b2014-06-24 08:30:15 -07003774static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003775 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003776 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003777 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003778 SkAutoDataUnref data(wStream.copyToData());
3779 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08003780 if (strlen(str) > 0) {
3781 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3782 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07003783 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08003784 }
caryclark66a5d8b2014-06-24 08:30:15 -07003785}
3786
3787static void test_dump(skiatest::Reporter* reporter) {
3788 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07003789 compare_dump(reporter, p, false, false, "");
3790 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07003791 p.moveTo(1, 2);
3792 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07003793 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3794 "path.lineTo(3, 4);\n");
3795 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
3796 "path.lineTo(3, 4);\n"
3797 "path.lineTo(1, 2);\n"
3798 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003799 p.reset();
3800 p.moveTo(1, 2);
3801 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07003802 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3803 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003804 p.reset();
3805 p.moveTo(1, 2);
3806 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07003807 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3808 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003809 p.reset();
3810 p.moveTo(1, 2);
3811 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07003812 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3813 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3814 p.reset();
3815 p.moveTo(1, 2);
3816 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07003817 compare_dump(reporter, p, false, true,
3818 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
3819 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07003820 p.reset();
3821 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
3822 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
3823 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3824 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003825}
3826
senorblanco84cd6212015-08-04 10:01:58 -07003827namespace {
3828
3829class ChangeListener : public SkPathRef::GenIDChangeListener {
3830public:
3831 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
3832 virtual ~ChangeListener() {}
3833 void onChange() override {
3834 *fChanged = true;
3835 }
3836private:
3837 bool* fChanged;
3838};
3839
3840}
3841
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003842class PathTest_Private {
3843public:
3844 static void TestPathTo(skiatest::Reporter* reporter) {
3845 SkPath p, q;
3846 p.lineTo(4, 4);
3847 p.reversePathTo(q);
3848 check_path_is_line(reporter, &p, 4, 4);
3849 q.moveTo(-4, -4);
3850 p.reversePathTo(q);
3851 check_path_is_line(reporter, &p, 4, 4);
3852 q.lineTo(7, 8);
3853 q.conicTo(8, 7, 6, 5, 0.5f);
3854 q.quadTo(6, 7, 8, 6);
3855 q.cubicTo(5, 6, 7, 8, 7, 5);
3856 q.close();
3857 p.reversePathTo(q);
3858 SkRect reverseExpected = {-4, -4, 8, 8};
3859 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3860 }
senorblanco84cd6212015-08-04 10:01:58 -07003861
3862 static void TestPathrefListeners(skiatest::Reporter* reporter) {
3863 SkPath p;
3864
3865 bool changed = false;
3866 p.moveTo(0, 0);
3867
3868 // Check that listener is notified on moveTo().
3869
halcanary385fe4d2015-08-26 13:07:48 -07003870 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003871 REPORTER_ASSERT(reporter, !changed);
3872 p.moveTo(10, 0);
3873 REPORTER_ASSERT(reporter, changed);
3874
3875 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07003876 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003877 REPORTER_ASSERT(reporter, !changed);
3878 p.lineTo(20, 0);
3879 REPORTER_ASSERT(reporter, changed);
3880
3881 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07003882 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003883 REPORTER_ASSERT(reporter, !changed);
3884 p.reset();
3885 REPORTER_ASSERT(reporter, changed);
3886
3887 p.moveTo(0, 0);
3888
3889 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07003890 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003891 REPORTER_ASSERT(reporter, !changed);
3892 p.rewind();
3893 REPORTER_ASSERT(reporter, changed);
3894
3895 // Check that listener is notified when pathref is deleted.
3896 {
3897 SkPath q;
3898 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07003899 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003900 REPORTER_ASSERT(reporter, !changed);
3901 }
3902 // q went out of scope.
3903 REPORTER_ASSERT(reporter, changed);
3904 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003905};
3906
caryclark9aacd902015-12-14 08:38:09 -08003907DEF_TEST(PathContains, reporter) {
3908 test_contains(reporter);
3909}
3910
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003911DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003912 test_path_crbug364224();
3913
bungeman@google.coma5809a32013-06-21 15:13:34 +00003914 SkTSize<SkScalar>::Make(3,4);
3915
3916 SkPath p, empty;
3917 SkRect bounds, bounds2;
3918 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003919
reed@android.comd252db02009-04-01 18:31:44 +00003920 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003921
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003922 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3923 SkPath& self = p;
3924 p = self;
3925
3926 // this triggers a code path in SkPath::swap which is otherwise unexercised
3927 p.swap(self);
3928
reed@android.com3abec1d2009-03-02 05:36:20 +00003929 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003930
reed@android.com6b82d1a2009-06-03 02:35:01 +00003931 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3932 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003933 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08003934 REPORTER_ASSERT(reporter,
3935 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003936 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003937
reed@android.com6b82d1a2009-06-03 02:35:01 +00003938 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003939 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003940
reed@android.com6b82d1a2009-06-03 02:35:01 +00003941 p.addOval(bounds);
3942 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003943 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003944
bungeman@google.coma5809a32013-06-21 15:13:34 +00003945 p.rewind();
3946 test_empty(reporter, p);
3947
reed@android.com3abec1d2009-03-02 05:36:20 +00003948 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003949 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003950 // we have only lines
3951 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003952 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003953
bungeman@google.coma5809a32013-06-21 15:13:34 +00003954 REPORTER_ASSERT(reporter, p != empty);
3955 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003956
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003957 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07003958 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
3959 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003960 SkPoint pts[4];
3961 int count = p.getPoints(pts, 4);
3962 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003963 uint8_t verbs[6];
3964 verbs[5] = 0xff;
3965 p.getVerbs(verbs, 5);
3966 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3967 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3968 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3969 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3970 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3971 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003972 bounds2.set(pts, 4);
3973 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003974
reed@android.com3abec1d2009-03-02 05:36:20 +00003975 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3976 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003977 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003978
halcanary96fcdcc2015-08-27 07:41:13 -07003979 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00003980 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003981 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3982 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003983
reed@android.com3abec1d2009-03-02 05:36:20 +00003984 // now force p to not be a rect
3985 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3986 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07003987 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00003988
robertphillipsfe7c4272014-12-29 11:36:39 -08003989 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
3990 // path has a trailing moveTo. Please see crbug.com\445368)
3991 {
3992 SkRect r;
3993 p.reset();
3994 p.addRect(bounds);
3995 REPORTER_ASSERT(reporter, p.isRect(&r));
3996 REPORTER_ASSERT(reporter, r == bounds);
3997 // add a moveTo outside of our bounds
3998 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
3999 REPORTER_ASSERT(reporter, p.isRect(&r));
4000 REPORTER_ASSERT(reporter, r == bounds);
4001 }
4002
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004003 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00004004 test_isLine(reporter);
4005 test_isRect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07004006 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00004007 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00004008 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00004009 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00004010 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00004011 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00004012 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004013 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00004014 test_flattening(reporter);
4015 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00004016 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00004017 test_iter(reporter);
4018 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00004019 test_circle(reporter);
4020 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00004021 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00004022 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00004023 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00004024 test_isfinite_after_transform(reporter);
fsb1475b02016-01-20 09:51:07 -08004025 test_islastcontourclosed(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00004026 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00004027 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004028 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004029 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004030 test_tricky_cubic();
4031 test_clipped_cubic();
4032 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004033 test_crbug_493450(reporter);
4034 test_crbug_495894(reporter);
reed@google.com7a90daf2013-04-10 18:44:00 +00004035 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004036 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004037 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004038 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004039 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004040 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004041 test_arc(reporter);
4042 test_arcTo(reporter);
4043 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004044 test_addPathMode(reporter, false, false);
4045 test_addPathMode(reporter, true, false);
4046 test_addPathMode(reporter, false, true);
4047 test_addPathMode(reporter, true, true);
4048 test_extendClosedPath(reporter);
4049 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4050 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004051 test_conicTo_special_case(reporter);
4052 test_get_point(reporter);
4053 test_contains(reporter);
4054 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004055 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004056 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004057 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004058 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004059 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004060 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004061 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004062 test_bounds_crbug_513799(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00004063}