blob: faa091c997fca89779ff54b8c3e05a649563a4a0 [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
reed@google.com744faba2012-05-29 19:54:52 +0000821// assert that we always
822// start with a moveTo
823// only have 1 moveTo
824// only have Lines after that
825// end with a single close
826// only have (at most) 1 close
827//
828static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000829 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000830 SkPath::RawIter iter(path);
831 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000832
833 bool firstTime = true;
834 bool foundClose = false;
835 for (;;) {
836 switch (iter.next(pts)) {
837 case SkPath::kMove_Verb:
838 REPORTER_ASSERT(reporter, firstTime);
839 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
840 srcPts++;
841 firstTime = false;
842 break;
843 case SkPath::kLine_Verb:
844 REPORTER_ASSERT(reporter, !firstTime);
845 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
846 srcPts++;
847 break;
848 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000849 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000850 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000851 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000852 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000853 break;
reed@google.com744faba2012-05-29 19:54:52 +0000854 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000855 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000856 break;
857 case SkPath::kClose_Verb:
858 REPORTER_ASSERT(reporter, !firstTime);
859 REPORTER_ASSERT(reporter, !foundClose);
860 REPORTER_ASSERT(reporter, expectClose);
861 foundClose = true;
862 break;
863 case SkPath::kDone_Verb:
864 goto DONE;
865 }
866 }
867DONE:
868 REPORTER_ASSERT(reporter, foundClose == expectClose);
869}
870
871static void test_addPoly(skiatest::Reporter* reporter) {
872 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000873 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000874
reed@google.com744faba2012-05-29 19:54:52 +0000875 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
876 pts[i].fX = rand.nextSScalar1();
877 pts[i].fY = rand.nextSScalar1();
878 }
879
880 for (int doClose = 0; doClose <= 1; ++doClose) {
881 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
882 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800883 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000884 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000885 }
886 }
887}
888
reed@google.com8b06f1a2012-05-29 12:03:46 +0000889static void test_strokerec(skiatest::Reporter* reporter) {
890 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
891 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000892
reed@google.com8b06f1a2012-05-29 12:03:46 +0000893 rec.setHairlineStyle();
894 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000895
reed@google.com8b06f1a2012-05-29 12:03:46 +0000896 rec.setStrokeStyle(SK_Scalar1, false);
897 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000898
reed@google.com8b06f1a2012-05-29 12:03:46 +0000899 rec.setStrokeStyle(SK_Scalar1, true);
900 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000901
reed@google.com8b06f1a2012-05-29 12:03:46 +0000902 rec.setStrokeStyle(0, false);
903 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000904
reed@google.com8b06f1a2012-05-29 12:03:46 +0000905 rec.setStrokeStyle(0, true);
906 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
907}
908
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000909// Set this for paths that don't have a consistent direction such as a bowtie.
910// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700911const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000912
913static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -0700914 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000915 if (expected == kDontCheckDir) {
916 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000917 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000918 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
919
reed026beb52015-06-10 14:23:15 -0700920 SkPathPriv::FirstDirection dir;
921 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000922 REPORTER_ASSERT(reporter, dir == expected);
923 } else {
reed026beb52015-06-10 14:23:15 -0700924 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000925 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000926}
927
reed@google.com3e71a882012-01-10 18:44:37 +0000928static void test_direction(skiatest::Reporter* reporter) {
929 size_t i;
930 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -0700931 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -0700932 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
933 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
934 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +0000935
936 static const char* gDegen[] = {
937 "M 10 10",
938 "M 10 10 M 20 20",
939 "M 10 10 L 20 20",
940 "M 10 10 L 10 10 L 10 10",
941 "M 10 10 Q 10 10 10 10",
942 "M 10 10 C 10 10 10 10 10 10",
943 };
944 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
945 path.reset();
946 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
947 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -0700948 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +0000949 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000950
reed@google.com3e71a882012-01-10 18:44:37 +0000951 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000952 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000953 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000954 "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 +0000955 // rect with top two corners replaced by cubics with identical middle
956 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000957 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
958 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000959 };
960 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
961 path.reset();
962 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
963 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700964 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000965 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000966
reed@google.com3e71a882012-01-10 18:44:37 +0000967 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000968 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000969 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000970 "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 +0000971 // rect with top two corners replaced by cubics with identical middle
972 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000973 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
974 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000975 };
976 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
977 path.reset();
978 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
979 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700980 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000981 }
reed@google.comac8543f2012-01-30 20:51:25 +0000982
983 // Test two donuts, each wound a different direction. Only the outer contour
984 // determines the cheap direction
985 path.reset();
986 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
987 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -0700988 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000989
reed@google.comac8543f2012-01-30 20:51:25 +0000990 path.reset();
991 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
992 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -0700993 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000994
995 // triangle with one point really far from the origin.
996 path.reset();
997 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000998 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000999 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
1000 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001001 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001002
1003 path.reset();
1004 path.conicTo(20, 0, 20, 20, 0.5f);
1005 path.close();
reed026beb52015-06-10 14:23:15 -07001006 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001007
1008 path.reset();
1009 path.lineTo(1, 1e7f);
1010 path.lineTo(1e7f, 2e7f);
1011 path.close();
1012 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001013 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001014}
1015
reed@google.comffdb0182011-11-14 19:29:14 +00001016static void add_rect(SkPath* path, const SkRect& r) {
1017 path->moveTo(r.fLeft, r.fTop);
1018 path->lineTo(r.fRight, r.fTop);
1019 path->lineTo(r.fRight, r.fBottom);
1020 path->lineTo(r.fLeft, r.fBottom);
1021 path->close();
1022}
1023
1024static void test_bounds(skiatest::Reporter* reporter) {
1025 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001026 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1027 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1028 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1029 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001030 };
1031
1032 SkPath path0, path1;
1033 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1034 path0.addRect(rects[i]);
1035 add_rect(&path1, rects[i]);
1036 }
1037
1038 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1039}
1040
reed@google.com55b5f4b2011-09-07 12:23:41 +00001041static void stroke_cubic(const SkPoint pts[4]) {
1042 SkPath path;
1043 path.moveTo(pts[0]);
1044 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001045
reed@google.com55b5f4b2011-09-07 12:23:41 +00001046 SkPaint paint;
1047 paint.setStyle(SkPaint::kStroke_Style);
1048 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001049
reed@google.com55b5f4b2011-09-07 12:23:41 +00001050 SkPath fill;
1051 paint.getFillPath(path, &fill);
1052}
1053
1054// just ensure this can run w/o any SkASSERTS firing in the debug build
1055// we used to assert due to differences in how we determine a degenerate vector
1056// but that was fixed with the introduction of SkPoint::CanNormalize
1057static void stroke_tiny_cubic() {
1058 SkPoint p0[] = {
1059 { 372.0f, 92.0f },
1060 { 372.0f, 92.0f },
1061 { 372.0f, 92.0f },
1062 { 372.0f, 92.0f },
1063 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001064
reed@google.com55b5f4b2011-09-07 12:23:41 +00001065 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001066
reed@google.com55b5f4b2011-09-07 12:23:41 +00001067 SkPoint p1[] = {
1068 { 372.0f, 92.0f },
1069 { 372.0007f, 92.000755f },
1070 { 371.99927f, 92.003922f },
1071 { 371.99826f, 92.003899f },
1072 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001073
reed@google.com55b5f4b2011-09-07 12:23:41 +00001074 stroke_cubic(p1);
1075}
1076
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001077static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1078 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001079 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001080 SkPoint mv;
1081 SkPoint pts[4];
1082 SkPath::Verb v;
1083 int nMT = 0;
1084 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001085 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001086 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1087 switch (v) {
1088 case SkPath::kMove_Verb:
1089 mv = pts[0];
1090 ++nMT;
1091 break;
1092 case SkPath::kClose_Verb:
1093 REPORTER_ASSERT(reporter, mv == pts[0]);
1094 ++nCL;
1095 break;
1096 default:
1097 break;
1098 }
1099 }
1100 // if we force a close on the interator we should have a close
1101 // for every moveTo
1102 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1103 }
1104}
1105
1106static void test_close(skiatest::Reporter* reporter) {
1107 SkPath closePt;
1108 closePt.moveTo(0, 0);
1109 closePt.close();
1110 check_close(reporter, closePt);
1111
1112 SkPath openPt;
1113 openPt.moveTo(0, 0);
1114 check_close(reporter, openPt);
1115
1116 SkPath empty;
1117 check_close(reporter, empty);
1118 empty.close();
1119 check_close(reporter, empty);
1120
1121 SkPath rect;
1122 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1123 check_close(reporter, rect);
1124 rect.close();
1125 check_close(reporter, rect);
1126
1127 SkPath quad;
1128 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1129 check_close(reporter, quad);
1130 quad.close();
1131 check_close(reporter, quad);
1132
1133 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001134 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001135 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1136 check_close(reporter, cubic);
1137 cubic.close();
1138 check_close(reporter, cubic);
1139
1140 SkPath line;
1141 line.moveTo(SK_Scalar1, SK_Scalar1);
1142 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1143 check_close(reporter, line);
1144 line.close();
1145 check_close(reporter, line);
1146
1147 SkPath rect2;
1148 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1149 rect2.close();
1150 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1151 check_close(reporter, rect2);
1152 rect2.close();
1153 check_close(reporter, rect2);
1154
1155 SkPath oval3;
1156 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1157 oval3.close();
1158 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1159 check_close(reporter, oval3);
1160 oval3.close();
1161 check_close(reporter, oval3);
1162
1163 SkPath moves;
1164 moves.moveTo(SK_Scalar1, SK_Scalar1);
1165 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1166 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1167 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1168 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001169
1170 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001171}
1172
reed@google.com7c424812011-05-15 04:38:34 +00001173static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1174 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001175 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1176 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001177 REPORTER_ASSERT(reporter, c == expected);
1178}
1179
caryclarkb4216502015-03-02 13:02:34 -08001180static void test_path_crbug389050(skiatest::Reporter* reporter) {
1181 SkPath tinyConvexPolygon;
1182 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1183 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1184 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1185 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1186 tinyConvexPolygon.close();
1187 tinyConvexPolygon.getConvexity();
1188 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001189 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001190
1191 SkPath platTriangle;
1192 platTriangle.moveTo(0, 0);
1193 platTriangle.lineTo(200, 0);
1194 platTriangle.lineTo(100, 0.04f);
1195 platTriangle.close();
1196 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001197 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001198
1199 platTriangle.reset();
1200 platTriangle.moveTo(0, 0);
1201 platTriangle.lineTo(200, 0);
1202 platTriangle.lineTo(100, 0.03f);
1203 platTriangle.close();
1204 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001205 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001206}
1207
reed@google.com7c424812011-05-15 04:38:34 +00001208static void test_convexity2(skiatest::Reporter* reporter) {
1209 SkPath pt;
1210 pt.moveTo(0, 0);
1211 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001212 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001213 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001214
reed@google.com7c424812011-05-15 04:38:34 +00001215 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001216 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1217 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001218 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001219 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001220 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001221
reed@google.com7c424812011-05-15 04:38:34 +00001222 SkPath triLeft;
1223 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001224 triLeft.lineTo(SK_Scalar1, 0);
1225 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001226 triLeft.close();
1227 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001228 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001229
reed@google.com7c424812011-05-15 04:38:34 +00001230 SkPath triRight;
1231 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001232 triRight.lineTo(-SK_Scalar1, 0);
1233 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001234 triRight.close();
1235 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001236 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001237
reed@google.com7c424812011-05-15 04:38:34 +00001238 SkPath square;
1239 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001240 square.lineTo(SK_Scalar1, 0);
1241 square.lineTo(SK_Scalar1, SK_Scalar1);
1242 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001243 square.close();
1244 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001245 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001246
reed@google.com7c424812011-05-15 04:38:34 +00001247 SkPath redundantSquare;
1248 redundantSquare.moveTo(0, 0);
1249 redundantSquare.lineTo(0, 0);
1250 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001251 redundantSquare.lineTo(SK_Scalar1, 0);
1252 redundantSquare.lineTo(SK_Scalar1, 0);
1253 redundantSquare.lineTo(SK_Scalar1, 0);
1254 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1255 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1256 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1257 redundantSquare.lineTo(0, SK_Scalar1);
1258 redundantSquare.lineTo(0, SK_Scalar1);
1259 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001260 redundantSquare.close();
1261 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001262 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001263
reed@google.com7c424812011-05-15 04:38:34 +00001264 SkPath bowTie;
1265 bowTie.moveTo(0, 0);
1266 bowTie.lineTo(0, 0);
1267 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001268 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1269 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1270 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1271 bowTie.lineTo(SK_Scalar1, 0);
1272 bowTie.lineTo(SK_Scalar1, 0);
1273 bowTie.lineTo(SK_Scalar1, 0);
1274 bowTie.lineTo(0, SK_Scalar1);
1275 bowTie.lineTo(0, SK_Scalar1);
1276 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001277 bowTie.close();
1278 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001279 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001280
reed@google.com7c424812011-05-15 04:38:34 +00001281 SkPath spiral;
1282 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001283 spiral.lineTo(100*SK_Scalar1, 0);
1284 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1285 spiral.lineTo(0, 100*SK_Scalar1);
1286 spiral.lineTo(0, 50*SK_Scalar1);
1287 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1288 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001289 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001290 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001291 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001292
reed@google.com7c424812011-05-15 04:38:34 +00001293 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001294 dent.moveTo(0, 0);
1295 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1296 dent.lineTo(0, 100*SK_Scalar1);
1297 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1298 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001299 dent.close();
1300 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001301 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001302
halcanary6950de62015-11-07 05:29:00 -08001303 // https://bug.skia.org/2235
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001304 SkPath strokedSin;
1305 for (int i = 0; i < 2000; i++) {
1306 SkScalar x = SkIntToScalar(i) / 2;
1307 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1308 if (0 == i) {
1309 strokedSin.moveTo(x, y);
1310 } else {
1311 strokedSin.lineTo(x, y);
1312 }
1313 }
1314 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1315 stroke.setStrokeStyle(2 * SK_Scalar1);
1316 stroke.applyToPath(&strokedSin, strokedSin);
1317 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1318 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001319
1320 // http://crbug.com/412640
1321 SkPath degenerateConcave;
1322 degenerateConcave.moveTo(148.67912f, 191.875f);
1323 degenerateConcave.lineTo(470.37695f, 7.5f);
1324 degenerateConcave.lineTo(148.67912f, 191.875f);
1325 degenerateConcave.lineTo(41.446522f, 376.25f);
1326 degenerateConcave.lineTo(-55.971577f, 460.0f);
1327 degenerateConcave.lineTo(41.446522f, 376.25f);
1328 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001329 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001330
1331 // http://crbug.com/433683
1332 SkPath badFirstVector;
1333 badFirstVector.moveTo(501.087708f, 319.610352f);
1334 badFirstVector.lineTo(501.087708f, 319.610352f);
1335 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1336 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1337 badFirstVector.lineTo(301.557678f, 98.044601f);
1338 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1339 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1340 badFirstVector.lineTo(504.912292f, 316.389648f);
1341 badFirstVector.lineTo(501.087708f, 319.610352f);
1342 badFirstVector.close();
1343 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001344}
1345
reed@android.com6b82d1a2009-06-03 02:35:01 +00001346static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1347 const SkRect& bounds) {
1348 REPORTER_ASSERT(reporter, p.isConvex());
1349 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001350
reed@android.com6b82d1a2009-06-03 02:35:01 +00001351 SkPath p2(p);
1352 REPORTER_ASSERT(reporter, p2.isConvex());
1353 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1354
1355 SkPath other;
1356 other.swap(p2);
1357 REPORTER_ASSERT(reporter, other.isConvex());
1358 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1359}
1360
reed@google.com04863fa2011-05-15 04:08:24 +00001361static void setFromString(SkPath* path, const char str[]) {
1362 bool first = true;
1363 while (str) {
1364 SkScalar x, y;
1365 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001366 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001367 break;
1368 }
1369 str = SkParse::FindScalar(str, &y);
1370 SkASSERT(str);
1371 if (first) {
1372 path->moveTo(x, y);
1373 first = false;
1374 } else {
1375 path->lineTo(x, y);
1376 }
1377 }
1378}
1379
1380static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001381 SkPath path;
1382
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001383 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001384 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001385 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001386 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001387 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1388
reed@google.com04863fa2011-05-15 04:08:24 +00001389 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001390 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001391 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001392 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001393
reed@google.com04863fa2011-05-15 04:08:24 +00001394 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001395 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001396 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001397 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001398
reed@google.com04863fa2011-05-15 04:08:24 +00001399 static const struct {
reed026beb52015-06-10 14:23:15 -07001400 const char* fPathStr;
1401 SkPath::Convexity fExpectedConvexity;
1402 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001403 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001404 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1405 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1406 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1407 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1408 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1409 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001410 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001411 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001412 };
1413
1414 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1415 SkPath path;
1416 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001417 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1418 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001419 // check after setting the initial convex and direction
1420 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1421 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001422 SkPathPriv::FirstDirection dir;
1423 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1424 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001425 ^ foundDir);
1426 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1427 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1428 }
1429 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1430 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001431 }
caryclarkd3d1a982014-12-08 04:57:38 -08001432
1433 static const SkPoint nonFinitePts[] = {
1434 { SK_ScalarInfinity, 0 },
1435 { 0, SK_ScalarInfinity },
1436 { SK_ScalarInfinity, SK_ScalarInfinity },
1437 { SK_ScalarNegativeInfinity, 0},
1438 { 0, SK_ScalarNegativeInfinity },
1439 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1440 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1441 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1442 { SK_ScalarNaN, 0 },
1443 { 0, SK_ScalarNaN },
1444 { SK_ScalarNaN, SK_ScalarNaN },
1445 };
1446
1447 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1448
1449 static const SkPoint finitePts[] = {
1450 { SK_ScalarMax, 0 },
1451 { 0, SK_ScalarMax },
1452 { SK_ScalarMax, SK_ScalarMax },
1453 { SK_ScalarMin, 0 },
1454 { 0, SK_ScalarMin },
1455 { SK_ScalarMin, SK_ScalarMin },
1456 };
1457
1458 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1459
1460 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1461 int i = (int) (index % nonFinitePtsCount);
1462 int f = (int) (index % finitePtsCount);
1463 int g = (int) ((f + 1) % finitePtsCount);
1464 path.reset();
1465 switch (index % 13) {
1466 case 0: path.lineTo(nonFinitePts[i]); break;
1467 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1468 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1469 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1470 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1471 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1472 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1473 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1474 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1475 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1476 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1477 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1478 case 12: path.moveTo(nonFinitePts[i]); break;
1479 }
1480 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1481 }
1482
1483 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1484 int f = (int) (index % finitePtsCount);
1485 int g = (int) ((f + 1) % finitePtsCount);
1486 path.reset();
1487 int curveSelect = index % 11;
1488 switch (curveSelect) {
1489 case 0: path.moveTo(finitePts[f]); break;
1490 case 1: path.lineTo(finitePts[f]); break;
1491 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1492 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1493 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1494 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1495 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1496 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1497 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1498 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1499 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1500 }
1501 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1502 : SkPath::kUnknown_Convexity);
1503 }
1504
reed@google.com04863fa2011-05-15 04:08:24 +00001505}
1506
reed@google.com7e6c4d12012-05-10 14:05:43 +00001507static void test_isLine(skiatest::Reporter* reporter) {
1508 SkPath path;
1509 SkPoint pts[2];
1510 const SkScalar value = SkIntToScalar(5);
1511
halcanary96fcdcc2015-08-27 07:41:13 -07001512 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001513
reed@google.com7e6c4d12012-05-10 14:05:43 +00001514 // set some non-zero values
1515 pts[0].set(value, value);
1516 pts[1].set(value, value);
1517 REPORTER_ASSERT(reporter, !path.isLine(pts));
1518 // check that pts was untouched
1519 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1520 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1521
1522 const SkScalar moveX = SkIntToScalar(1);
1523 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001524 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001525
1526 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001527 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001528 REPORTER_ASSERT(reporter, !path.isLine(pts));
1529 // check that pts was untouched
1530 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1531 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1532
1533 const SkScalar lineX = SkIntToScalar(2);
1534 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001535 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001536
1537 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001538 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001539
1540 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1541 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1542 REPORTER_ASSERT(reporter, path.isLine(pts));
1543 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1544 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1545
1546 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001547 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001548 REPORTER_ASSERT(reporter, !path.isLine(pts));
1549 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1550 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001551
1552 path.reset();
1553 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001554 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001555}
1556
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001557static void test_conservativelyContains(skiatest::Reporter* reporter) {
1558 SkPath path;
1559
1560 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1561 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1562
1563 // A circle that bounds kBaseRect (with a significant amount of slop)
1564 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001565 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001566 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1567
1568 // round-rect radii
1569 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001570
caryclark@google.com56f233a2012-11-19 13:06:06 +00001571 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001572 SkRect fQueryRect;
1573 bool fInRect;
1574 bool fInCircle;
1575 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001576 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001577 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001578 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001579
1580 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001581 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1582 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1583 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1584 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001585 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001586
1587 // rects with edges off by one from kBaseRect's edges
1588 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1589 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001590 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001591 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1592 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001593 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001594 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1595 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001596 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001597 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1598 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001599 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001600 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1601 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001602 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001603 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1604 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001605 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001606 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1607 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001608 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001609
1610 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001611 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1612 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1613 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1614 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001615
1616 // far away rect
1617 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1618 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001619 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001620
1621 // very large rect containing kBaseRect
1622 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1623 kBaseRect.fTop - 5 * kBaseRect.height(),
1624 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001625 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001626
1627 // skinny rect that spans same y-range as kBaseRect
1628 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1629 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001630 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001631
1632 // short rect that spans same x-range as kBaseRect
1633 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001634 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001635
1636 // skinny rect that spans slightly larger y-range than kBaseRect
1637 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1638 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001639 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001640
1641 // short rect that spans slightly larger x-range than kBaseRect
1642 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1643 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001644 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001645 };
1646
1647 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001648 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001649 SkRect qRect = kQueries[q].fQueryRect;
1650 if (inv & 0x1) {
1651 SkTSwap(qRect.fLeft, qRect.fRight);
1652 }
1653 if (inv & 0x2) {
1654 SkTSwap(qRect.fTop, qRect.fBottom);
1655 }
1656 for (int d = 0; d < 2; ++d) {
1657 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1658 path.reset();
1659 path.addRect(kBaseRect, dir);
1660 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1661 path.conservativelyContainsRect(qRect));
1662
1663 path.reset();
1664 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1665 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1666 path.conservativelyContainsRect(qRect));
1667
1668 path.reset();
1669 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1670 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1671 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001672
1673 path.reset();
1674 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1675 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1676 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1677 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1678 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1679 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1680 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1681 path.close();
1682 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1683 path.conservativelyContainsRect(qRect));
1684
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001685 }
1686 // Slightly non-convex shape, shouldn't contain any rects.
1687 path.reset();
1688 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001689 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001690 path.lineTo(SkIntToScalar(100), 0);
1691 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1692 path.lineTo(0, SkIntToScalar(100));
1693 path.close();
1694 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1695 }
1696 }
1697
1698 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1699 path.reset();
1700 path.moveTo(0, 0);
1701 path.lineTo(SkIntToScalar(100), 0);
1702 path.lineTo(0, SkIntToScalar(100));
1703
1704 // inside, on along top edge
1705 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1706 SkIntToScalar(10),
1707 SkIntToScalar(10))));
1708 // above
1709 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1710 SkRect::MakeXYWH(SkIntToScalar(50),
1711 SkIntToScalar(-10),
1712 SkIntToScalar(10),
1713 SkIntToScalar(10))));
1714 // to the left
1715 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1716 SkIntToScalar(5),
1717 SkIntToScalar(5),
1718 SkIntToScalar(5))));
1719
1720 // outside the diagonal edge
1721 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1722 SkIntToScalar(200),
1723 SkIntToScalar(20),
1724 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001725
bsalomonb17c1292014-08-28 14:04:55 -07001726
1727 // Test that multiple move commands do not cause asserts.
1728
1729 // At the time of writing, this would not modify cached convexity. This caused an assert while
halcanary6950de62015-11-07 05:29:00 -08001730 // checking conservative containment again. https://bug.skia.org/1460
bsalomonb17c1292014-08-28 14:04:55 -07001731 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1732#if 0
1733 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1734 SkIntToScalar(10),
1735 SkIntToScalar(10))));
1736#endif
1737
1738 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001739 path.reset();
1740 path.moveTo(100, 100);
1741 path.moveTo(0, 0);
1742 path.lineTo(SkIntToScalar(100), 0);
1743 path.lineTo(0, SkIntToScalar(100));
1744
1745 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1746 SkIntToScalar(10),
1747 SkIntToScalar(10))));
1748
bsalomonb17c1292014-08-28 14:04:55 -07001749 // Test that multiple move commands do not cause asserts and that the function
1750 // is not confused by the multiple moves.
1751 path.reset();
1752 path.moveTo(0, 0);
1753 path.lineTo(SkIntToScalar(100), 0);
1754 path.lineTo(0, SkIntToScalar(100));
1755 path.moveTo(0, SkIntToScalar(200));
1756 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1757 path.lineTo(0, SkIntToScalar(300));
1758
1759 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1760 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1761 SkIntToScalar(10),
1762 SkIntToScalar(10))));
1763
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001764 path.reset();
1765 path.lineTo(100, 100);
1766 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001767}
1768
reed@google.comf32322b2013-10-16 15:14:04 +00001769static void test_isRect_open_close(skiatest::Reporter* reporter) {
1770 SkPath path;
1771 bool isClosed;
1772
1773 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001774 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001775
halcanary96fcdcc2015-08-27 07:41:13 -07001776 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001777 REPORTER_ASSERT(reporter, isClosed);
1778}
1779
caryclark@google.comf1316942011-07-26 19:54:45 +00001780// Simple isRect test is inline TestPath, below.
1781// test_isRect provides more extensive testing.
1782static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001783 test_isRect_open_close(reporter);
1784
caryclark@google.comf1316942011-07-26 19:54:45 +00001785 // passing tests (all moveTo / lineTo...
1786 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1787 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1788 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1789 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1790 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1791 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1792 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1793 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1794 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001795 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1796 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 +00001797 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1798 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1799 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001800 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001801
caryclark@google.comf1316942011-07-26 19:54:45 +00001802 // failing tests
1803 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1804 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1805 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1806 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1807 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1808 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1809 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1810 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001811 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1812 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1813 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001814
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001815 // no close, but we should detect them as fillably the same as a rect
1816 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1817 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1818 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1819
1820 // like c2, but we double-back on ourselves
1821 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1822 // like c2, but we overshoot the start point
1823 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1824 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001825
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001826 struct IsRectTest {
1827 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001828 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001829 bool fClose;
1830 bool fIsRect;
1831 } tests[] = {
1832 { r1, SK_ARRAY_COUNT(r1), true, true },
1833 { r2, SK_ARRAY_COUNT(r2), true, true },
1834 { r3, SK_ARRAY_COUNT(r3), true, true },
1835 { r4, SK_ARRAY_COUNT(r4), true, true },
1836 { r5, SK_ARRAY_COUNT(r5), true, true },
1837 { r6, SK_ARRAY_COUNT(r6), true, true },
1838 { r7, SK_ARRAY_COUNT(r7), true, true },
1839 { r8, SK_ARRAY_COUNT(r8), true, true },
1840 { r9, SK_ARRAY_COUNT(r9), true, true },
1841 { ra, SK_ARRAY_COUNT(ra), true, true },
1842 { rb, SK_ARRAY_COUNT(rb), true, true },
1843 { rc, SK_ARRAY_COUNT(rc), true, true },
1844 { rd, SK_ARRAY_COUNT(rd), true, true },
1845 { re, SK_ARRAY_COUNT(re), true, true },
1846 { rf, SK_ARRAY_COUNT(rf), true, true },
1847
1848 { f1, SK_ARRAY_COUNT(f1), true, false },
1849 { f2, SK_ARRAY_COUNT(f2), true, false },
1850 { f3, SK_ARRAY_COUNT(f3), true, false },
1851 { f4, SK_ARRAY_COUNT(f4), true, false },
1852 { f5, SK_ARRAY_COUNT(f5), true, false },
1853 { f6, SK_ARRAY_COUNT(f6), true, false },
1854 { f7, SK_ARRAY_COUNT(f7), true, false },
1855 { f8, SK_ARRAY_COUNT(f8), true, false },
1856 { f9, SK_ARRAY_COUNT(f9), true, false },
1857 { fa, SK_ARRAY_COUNT(fa), true, false },
1858 { fb, SK_ARRAY_COUNT(fb), true, false },
1859
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001860 { c1, SK_ARRAY_COUNT(c1), false, true },
1861 { c2, SK_ARRAY_COUNT(c2), false, true },
1862 { c3, SK_ARRAY_COUNT(c3), false, true },
1863
1864 { d1, SK_ARRAY_COUNT(d1), false, false },
1865 { d2, SK_ARRAY_COUNT(d2), false, false },
1866 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001867 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001868
1869 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001870 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001871 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1872 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001873 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1874 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1875 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001876 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001877 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001878 path.close();
1879 }
halcanary96fcdcc2015-08-27 07:41:13 -07001880 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001881
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001882 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001883 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001884 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001885 SkPath::Direction direction;
1886 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001887 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001888 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001889 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1890 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001891 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001892 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001893 } else {
1894 SkRect computed;
1895 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001896 bool isClosed = (bool)-1;
1897 SkPath::Direction direction = (SkPath::Direction) - 1;
1898 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001899 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1900 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001901 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1902 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001903 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001904 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001905
caryclark@google.comf1316942011-07-26 19:54:45 +00001906 // fail, close then line
1907 SkPath path1;
1908 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001909 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001910 path1.lineTo(r1[index].fX, r1[index].fY);
1911 }
1912 path1.close();
1913 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001914 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001915
caryclark@google.comf1316942011-07-26 19:54:45 +00001916 // fail, move in the middle
1917 path1.reset();
1918 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001919 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001920 if (index == 2) {
1921 path1.moveTo(1, .5f);
1922 }
1923 path1.lineTo(r1[index].fX, r1[index].fY);
1924 }
1925 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001926 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001927
1928 // fail, move on the edge
1929 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08001930 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001931 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1932 path1.lineTo(r1[index].fX, r1[index].fY);
1933 }
1934 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001935 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001936
caryclark@google.comf1316942011-07-26 19:54:45 +00001937 // fail, quad
1938 path1.reset();
1939 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001940 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001941 if (index == 2) {
1942 path1.quadTo(1, .5f, 1, .5f);
1943 }
1944 path1.lineTo(r1[index].fX, r1[index].fY);
1945 }
1946 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001947 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001948
caryclark@google.comf1316942011-07-26 19:54:45 +00001949 // fail, cubic
1950 path1.reset();
1951 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001952 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001953 if (index == 2) {
1954 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1955 }
1956 path1.lineTo(r1[index].fX, r1[index].fY);
1957 }
1958 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001959 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001960}
1961
caryclark95bc5f32015-04-08 08:34:15 -07001962static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001963 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001964 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001965 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1966 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1967 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001968 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001969 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1970 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1971 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1972 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001973 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1974 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 +00001975 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1976 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1977 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001978
1979 // failing tests
1980 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1981 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1982 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1983 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1984 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1985 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1986 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1987 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1988
caryclark95bc5f32015-04-08 08:34:15 -07001989 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00001990 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1991 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1992
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001993 struct IsNestedRectTest {
1994 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001995 int fPointCount;
reed026beb52015-06-10 14:23:15 -07001996 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001997 bool fClose;
1998 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1999 } tests[] = {
reed026beb52015-06-10 14:23:15 -07002000 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2001 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2002 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2003 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2004 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2005 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2006 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2007 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2008 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2009 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2010 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2011 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2012 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2013 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002014
reed026beb52015-06-10 14:23:15 -07002015 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2016 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2017 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2018 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2019 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2020 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2021 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2022 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002023
reed026beb52015-06-10 14:23:15 -07002024 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2025 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002026 };
2027
2028 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002029 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002030 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002031 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2032 SkPath path;
2033 if (rectFirst) {
2034 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2035 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002036 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2037 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2038 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002039 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002040 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002041 path.close();
2042 }
2043 if (!rectFirst) {
2044 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2045 }
caryclark95bc5f32015-04-08 08:34:15 -07002046 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002047 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002048 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002049 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002050 SkPathPriv::FirstDirection expectedDirs[2];
2051 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002052 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002053 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002054 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2055 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002056 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002057 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002058 } else {
reed026beb52015-06-10 14:23:15 -07002059 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002060 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002061 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002062 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002063 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2064 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002065 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2066 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002067 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002068 }
2069
2070 // fail, close then line
2071 SkPath path1;
2072 if (rectFirst) {
2073 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2074 }
2075 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002076 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002077 path1.lineTo(r1[index].fX, r1[index].fY);
2078 }
2079 path1.close();
2080 path1.lineTo(1, 0);
2081 if (!rectFirst) {
2082 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2083 }
halcanary96fcdcc2015-08-27 07:41:13 -07002084 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002085
2086 // fail, move in the middle
2087 path1.reset();
2088 if (rectFirst) {
2089 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2090 }
2091 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002092 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002093 if (index == 2) {
2094 path1.moveTo(1, .5f);
2095 }
2096 path1.lineTo(r1[index].fX, r1[index].fY);
2097 }
2098 path1.close();
2099 if (!rectFirst) {
2100 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2101 }
halcanary96fcdcc2015-08-27 07:41:13 -07002102 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002103
2104 // fail, move on the edge
2105 path1.reset();
2106 if (rectFirst) {
2107 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2108 }
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 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2111 path1.lineTo(r1[index].fX, r1[index].fY);
2112 }
2113 path1.close();
2114 if (!rectFirst) {
2115 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2116 }
halcanary96fcdcc2015-08-27 07:41:13 -07002117 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002118
2119 // fail, quad
2120 path1.reset();
2121 if (rectFirst) {
2122 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2123 }
2124 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002125 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002126 if (index == 2) {
2127 path1.quadTo(1, .5f, 1, .5f);
2128 }
2129 path1.lineTo(r1[index].fX, r1[index].fY);
2130 }
2131 path1.close();
2132 if (!rectFirst) {
2133 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2134 }
halcanary96fcdcc2015-08-27 07:41:13 -07002135 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002136
2137 // fail, cubic
2138 path1.reset();
2139 if (rectFirst) {
2140 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2141 }
2142 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002143 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002144 if (index == 2) {
2145 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2146 }
2147 path1.lineTo(r1[index].fX, r1[index].fY);
2148 }
2149 path1.close();
2150 if (!rectFirst) {
2151 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2152 }
halcanary96fcdcc2015-08-27 07:41:13 -07002153 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002154
caryclark@google.com56f233a2012-11-19 13:06:06 +00002155 // fail, not nested
2156 path1.reset();
2157 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2158 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002159 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002160 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002161
caryclark95bc5f32015-04-08 08:34:15 -07002162 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2163 SkPath path;
2164 path.moveTo(0, 0);
2165 path.lineTo(10, 0);
2166 path.lineTo(10, 10);
2167 path.lineTo(0, 10);
2168 path.lineTo(0, 0);
2169 path.moveTo(1, 1);
2170 path.lineTo(9, 1);
2171 path.lineTo(9, 9);
2172 path.lineTo(1, 9);
2173 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002174 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002175
caryclark@google.combfe90372012-11-21 13:56:20 +00002176 // pass, stroke rect
2177 SkPath src, dst;
2178 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2179 SkPaint strokePaint;
2180 strokePaint.setStyle(SkPaint::kStroke_Style);
2181 strokePaint.setStrokeWidth(2);
2182 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002183 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002184}
2185
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002186static void write_and_read_back(skiatest::Reporter* reporter,
2187 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002188 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002189 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002190 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002191 SkAutoMalloc storage(size);
2192 writer.flatten(storage.get());
2193 SkReader32 reader(storage.get(), size);
2194
2195 SkPath readBack;
2196 REPORTER_ASSERT(reporter, readBack != p);
2197 reader.readPath(&readBack);
2198 REPORTER_ASSERT(reporter, readBack == p);
2199
rmistry@google.comd6176b02012-08-23 18:14:13 +00002200 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002201 p.getConvexityOrUnknown());
2202
halcanary96fcdcc2015-08-27 07:41:13 -07002203 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002204
2205 const SkRect& origBounds = p.getBounds();
2206 const SkRect& readBackBounds = readBack.getBounds();
2207
2208 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2209}
2210
reed@google.com53effc52011-09-21 19:05:12 +00002211static void test_flattening(skiatest::Reporter* reporter) {
2212 SkPath p;
2213
2214 static const SkPoint pts[] = {
2215 { 0, 0 },
2216 { SkIntToScalar(10), SkIntToScalar(10) },
2217 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2218 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2219 };
2220 p.moveTo(pts[0]);
2221 p.lineTo(pts[1]);
2222 p.quadTo(pts[2], pts[3]);
2223 p.cubicTo(pts[4], pts[5], pts[6]);
2224
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002225 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002226
2227 // create a buffer that should be much larger than the path so we don't
2228 // kill our stack if writer goes too far.
2229 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002230 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002231 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002232 REPORTER_ASSERT(reporter, size1 == size2);
2233
2234 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002235 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002236 REPORTER_ASSERT(reporter, size1 == size3);
2237 REPORTER_ASSERT(reporter, p == p2);
2238
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002239 size3 = p2.readFromMemory(buffer, 0);
2240 REPORTER_ASSERT(reporter, !size3);
2241
2242 SkPath tooShort;
2243 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2244 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2245
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002246 char buffer2[1024];
2247 size3 = p2.writeToMemory(buffer2);
2248 REPORTER_ASSERT(reporter, size1 == size3);
2249 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002250
2251 // test persistence of the oval flag & convexity
2252 {
2253 SkPath oval;
2254 SkRect rect = SkRect::MakeWH(10, 10);
2255 oval.addOval(rect);
2256
2257 write_and_read_back(reporter, oval);
2258 }
reed@google.com53effc52011-09-21 19:05:12 +00002259}
2260
2261static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002262 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002263
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002264#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002265 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002266 { 0, 0 }, // move
2267 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2268 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2269 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2270#if CONIC_PERSPECTIVE_BUG_FIXED
2271 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2272#endif
reed@google.com53effc52011-09-21 19:05:12 +00002273 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002274 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002275
reed@google.com53effc52011-09-21 19:05:12 +00002276 p.moveTo(pts[0]);
2277 p.lineTo(pts[1]);
2278 p.quadTo(pts[2], pts[3]);
2279 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002280#if CONIC_PERSPECTIVE_BUG_FIXED
2281 p.conicTo(pts[4], pts[5], 0.5f);
2282#endif
2283 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002284
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002285 {
2286 SkMatrix matrix;
2287 matrix.reset();
2288 SkPath p1;
2289 p.transform(matrix, &p1);
2290 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002291 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002292
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002293
2294 {
2295 SkMatrix matrix;
2296 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2297
2298 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2299
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002300 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002301 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002302 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002303 REPORTER_ASSERT(reporter, kPtCount == count);
2304 for (int i = 0; i < count; ++i) {
2305 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2306 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2307 }
2308 }
2309
2310 {
2311 SkMatrix matrix;
2312 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002313 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002314
2315 SkPath p1;
2316 p1.moveTo(SkPoint::Make(0, 0));
2317
2318 p.transform(matrix, &p1);
2319 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002320 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002321 SkRect pBounds = p.getBounds();
2322 SkRect p1Bounds = p1.getBounds();
2323 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2324 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2325 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2326 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2327 }
2328
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002329 p.reset();
2330 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002331
2332 {
2333 SkMatrix matrix;
2334 matrix.reset();
2335 SkPath p1;
2336 p1.moveTo(SkPoint::Make(0, 0));
2337
2338 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002339 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002340 }
2341
2342
2343 {
2344 SkMatrix matrix;
2345 matrix.reset();
2346 matrix.setScaleX(-1);
2347 SkPath p1;
2348 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2349
2350 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002351 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002352 }
2353
2354 {
2355 SkMatrix matrix;
2356 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2357 SkPath p1;
2358 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2359
2360 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002361 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002362 }
reed@google.com53effc52011-09-21 19:05:12 +00002363}
2364
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002365static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002366 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002367 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002368
caryclark@google.com56f233a2012-11-19 13:06:06 +00002369 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002370 const char* testPath;
2371 const size_t numResultPts;
2372 const SkRect resultBound;
2373 const SkPath::Verb* resultVerbs;
2374 const size_t numResultVerbs;
2375 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002376
schenney@chromium.org7e963602012-06-13 17:05:43 +00002377 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2378 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2379 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2380 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2381 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2382 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2383 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2384 static const SkPath::Verb resultVerbs8[] = {
2385 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2386 };
2387 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2388 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2389 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2390 static const SkPath::Verb resultVerbs12[] = {
2391 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2392 };
2393 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2394 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2395 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2396 static const SkPath::Verb resultVerbs16[] = {
2397 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2398 };
2399 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002400 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002401 { "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 -07002402 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002403 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2404 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2405 { "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) },
2406 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2407 { "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) },
2408 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2409 { "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) },
2410 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2411 { "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) },
2412 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2413 { "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 +00002414 SK_ARRAY_COUNT(resultVerbs14)
2415 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002416 { "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) },
2417 { "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 +00002418 SK_ARRAY_COUNT(resultVerbs16)
2419 }
2420 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002421
schenney@chromium.org7e963602012-06-13 17:05:43 +00002422 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2423 p.reset();
2424 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2425 REPORTER_ASSERT(reporter, valid);
2426 REPORTER_ASSERT(reporter, !p.isEmpty());
2427 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2428 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2429 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2430 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2431 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2432 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002433 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002434}
2435
2436struct SegmentInfo {
2437 SkPath fPath;
2438 int fPointCount;
2439};
2440
reed@google.com10296cc2011-09-21 12:29:05 +00002441#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2442
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002443static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002444 SkPath p, p2;
2445
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002446 p.moveTo(0, 0);
2447 p.quadTo(100, 100, 200, 200);
2448 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2449 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002450 p2 = p;
2451 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002452 p.cubicTo(100, 100, 200, 200, 300, 300);
2453 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2454 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002455 p2 = p;
2456 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2457
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002458 p.reset();
2459 p.moveTo(0, 0);
2460 p.cubicTo(100, 100, 200, 200, 300, 300);
2461 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002462 p2 = p;
2463 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002464
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002465 REPORTER_ASSERT(reporter, !p.isEmpty());
2466}
2467
2468static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002469 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002470 SkPoint pts[4];
2471
2472 // Test an iterator with no path
2473 SkPath::Iter noPathIter;
2474 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002475
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002476 // Test that setting an empty path works
2477 noPathIter.setPath(p, false);
2478 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002479
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002480 // Test that close path makes no difference for an empty path
2481 noPathIter.setPath(p, true);
2482 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002483
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002484 // Test an iterator with an initial empty path
2485 SkPath::Iter iter(p, false);
2486 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2487
2488 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002489 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002490 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2491
rmistry@google.comd6176b02012-08-23 18:14:13 +00002492
schenney@chromium.org7e963602012-06-13 17:05:43 +00002493 struct iterTestData {
2494 const char* testPath;
2495 const bool forceClose;
2496 const bool consumeDegenerates;
2497 const size_t* numResultPtsPerVerb;
2498 const SkPoint* resultPts;
2499 const SkPath::Verb* resultVerbs;
2500 const size_t numResultVerbs;
2501 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002502
schenney@chromium.org7e963602012-06-13 17:05:43 +00002503 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2504 static const SkPath::Verb resultVerbs2[] = {
2505 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2506 };
2507 static const SkPath::Verb resultVerbs3[] = {
2508 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2509 };
2510 static const SkPath::Verb resultVerbs4[] = {
2511 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2512 };
2513 static const SkPath::Verb resultVerbs5[] = {
2514 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2515 };
2516 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002517 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2518 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2519 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2520 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002521 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002522 static const SkPoint resultPts2[] = {
2523 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2524 };
2525 static const SkPoint resultPts3[] = {
2526 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2527 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2528 };
2529 static const SkPoint resultPts4[] = {
2530 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2531 };
2532 static const SkPoint resultPts5[] = {
2533 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2534 };
2535 static const struct iterTestData gIterTests[] = {
2536 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002537 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2538 { "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 +00002539 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2540 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2541 { "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) },
2542 { "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 +00002543 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2544 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2545 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2546 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2547 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2548 { "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 +00002549 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002550
schenney@chromium.org7e963602012-06-13 17:05:43 +00002551 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2552 p.reset();
2553 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2554 REPORTER_ASSERT(reporter, valid);
2555 iter.setPath(p, gIterTests[i].forceClose);
2556 int j = 0, l = 0;
2557 do {
2558 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2559 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2560 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2561 }
2562 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2563 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2564 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002565
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002566 p.reset();
2567 iter.setPath(p, false);
2568 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2569 p.lineTo(1, 1);
2570 p.close();
2571 iter.setPath(p, false);
2572 REPORTER_ASSERT(reporter, iter.isClosedContour());
2573 p.reset();
2574 iter.setPath(p, true);
2575 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2576 p.lineTo(1, 1);
2577 iter.setPath(p, true);
2578 REPORTER_ASSERT(reporter, iter.isClosedContour());
2579 p.moveTo(0, 0);
2580 p.lineTo(2, 2);
2581 iter.setPath(p, false);
2582 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2583
2584 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2585 // check to see if the result is correct.
2586 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2587 p.reset();
2588 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2589 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2590 iter.setPath(p, true);
2591 iter.next(pts, false);
2592 iter.next(pts, false);
2593 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2594 }
2595
2596 p.reset();
2597 p.quadTo(0, 0, 0, 0);
2598 iter.setPath(p, false);
2599 iter.next(pts, false);
2600 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2601 iter.setPath(p, false);
2602 iter.next(pts, false);
2603 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2604
2605 p.reset();
2606 p.conicTo(0, 0, 0, 0, 0.5f);
2607 iter.setPath(p, false);
2608 iter.next(pts, false);
2609 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2610 iter.setPath(p, false);
2611 iter.next(pts, false);
2612 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2613
2614 p.reset();
2615 p.cubicTo(0, 0, 0, 0, 0, 0);
2616 iter.setPath(p, false);
2617 iter.next(pts, false);
2618 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2619 iter.setPath(p, false);
2620 iter.next(pts, false);
2621 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2622
2623 p.moveTo(1, 1); // add a trailing moveto
2624 iter.setPath(p, false);
2625 iter.next(pts, false);
2626 REPORTER_ASSERT(reporter, SkPath::kCubic_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
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002631 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002632
2633 // Test out mixed degenerate and non-degenerate geometry with Conics
2634 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2635 SkRect r = SkRect::MakeWH(100, 100);
2636 SkRRect rr;
2637 rr.setRectRadii(r, radii);
2638 p.reset();
2639 p.addRRect(rr);
2640 iter.setPath(p, false);
2641 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2642 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2643 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2644 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2645 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002646}
2647
2648static void test_raw_iter(skiatest::Reporter* reporter) {
2649 SkPath p;
2650 SkPoint pts[4];
2651
2652 // Test an iterator with no path
2653 SkPath::RawIter noPathIter;
2654 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2655 // Test that setting an empty path works
2656 noPathIter.setPath(p);
2657 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002658
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002659 // Test an iterator with an initial empty path
2660 SkPath::RawIter iter(p);
2661 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2662
2663 // Test that a move-only path returns the move.
2664 p.moveTo(SK_Scalar1, 0);
2665 iter.setPath(p);
2666 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2667 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2668 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2669 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2670
2671 // No matter how many moves we add, we should get them all back
2672 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2673 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2674 iter.setPath(p);
2675 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2676 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2677 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2678 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2679 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2680 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2681 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2682 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2683 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2684 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2685
2686 // Initial close is never ever stored
2687 p.reset();
2688 p.close();
2689 iter.setPath(p);
2690 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2691
2692 // Move/close sequences
2693 p.reset();
2694 p.close(); // Not stored, no purpose
2695 p.moveTo(SK_Scalar1, 0);
2696 p.close();
2697 p.close(); // Not stored, no purpose
2698 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2699 p.close();
2700 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2701 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2702 p.close();
2703 iter.setPath(p);
2704 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2705 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2706 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2707 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002708 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2709 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2710 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2711 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002712 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2713 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2714 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2715 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2716 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2717 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2718 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002719 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2720
2721 // Generate random paths and verify
2722 SkPoint randomPts[25];
2723 for (int i = 0; i < 5; ++i) {
2724 for (int j = 0; j < 5; ++j) {
2725 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2726 }
2727 }
2728
2729 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002730 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002731 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002732 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002733 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002734
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002735 for (int i = 0; i < 500; ++i) {
2736 p.reset();
2737 bool lastWasClose = true;
2738 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002739 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002740 int numPoints = 0;
2741 int numVerbs = (rand.nextU() >> 16) % 10;
2742 int numIterVerbs = 0;
2743 for (int j = 0; j < numVerbs; ++j) {
2744 do {
2745 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2746 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002747 switch (nextVerb) {
2748 case SkPath::kMove_Verb:
2749 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2750 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002751 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002752 numPoints += 1;
2753 lastWasClose = false;
2754 haveMoveTo = true;
2755 break;
2756 case SkPath::kLine_Verb:
2757 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002758 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002759 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2760 haveMoveTo = true;
2761 }
2762 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2763 p.lineTo(expectedPts[numPoints]);
2764 numPoints += 1;
2765 lastWasClose = false;
2766 break;
2767 case SkPath::kQuad_Verb:
2768 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002769 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002770 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2771 haveMoveTo = true;
2772 }
2773 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2774 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2775 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2776 numPoints += 2;
2777 lastWasClose = false;
2778 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002779 case SkPath::kConic_Verb:
2780 if (!haveMoveTo) {
2781 expectedPts[numPoints++] = lastMoveToPt;
2782 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2783 haveMoveTo = true;
2784 }
2785 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2786 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2787 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2788 rand.nextUScalar1() * 4);
2789 numPoints += 2;
2790 lastWasClose = false;
2791 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002792 case SkPath::kCubic_Verb:
2793 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002794 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002795 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2796 haveMoveTo = true;
2797 }
2798 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2799 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2800 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2801 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2802 expectedPts[numPoints + 2]);
2803 numPoints += 3;
2804 lastWasClose = false;
2805 break;
2806 case SkPath::kClose_Verb:
2807 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002808 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002809 lastWasClose = true;
2810 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002811 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002812 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002813 }
2814 expectedVerbs[numIterVerbs++] = nextVerb;
2815 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002816
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002817 iter.setPath(p);
2818 numVerbs = numIterVerbs;
2819 numIterVerbs = 0;
2820 int numIterPts = 0;
2821 SkPoint lastMoveTo;
2822 SkPoint lastPt;
2823 lastMoveTo.set(0, 0);
2824 lastPt.set(0, 0);
2825 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2826 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2827 numIterVerbs++;
2828 switch (nextVerb) {
2829 case SkPath::kMove_Verb:
2830 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2831 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2832 lastPt = lastMoveTo = pts[0];
2833 numIterPts += 1;
2834 break;
2835 case SkPath::kLine_Verb:
2836 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2837 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2838 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2839 lastPt = pts[1];
2840 numIterPts += 1;
2841 break;
2842 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002843 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002844 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2845 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2846 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2847 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2848 lastPt = pts[2];
2849 numIterPts += 2;
2850 break;
2851 case SkPath::kCubic_Verb:
2852 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2853 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2854 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2855 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2856 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2857 lastPt = pts[3];
2858 numIterPts += 3;
2859 break;
2860 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002861 lastPt = lastMoveTo;
2862 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002863 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002864 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002865 }
2866 }
2867 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2868 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2869 }
2870}
2871
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002872static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002873 const SkPath& path,
2874 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07002875 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002876 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002877 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
reed026beb52015-06-10 14:23:15 -07002878 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002879
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002880 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002881 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2882 }
2883}
2884
2885static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002886 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002887 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002888 SkPath tmp;
2889
2890 SkMatrix m;
2891 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2892 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002893 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07002894 if (SkPathPriv::kCCW_FirstDirection == dir) {
2895 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002896 } else {
reed026beb52015-06-10 14:23:15 -07002897 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
2898 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002899 }
2900 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002901}
2902
2903static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002904 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002905 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002906 SkPath tmp;
2907
2908 // translate at small offset
2909 SkMatrix m;
2910 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2911 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002912 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002913
2914 tmp.reset();
2915 m.reset();
2916
2917 // translate at a relatively big offset
2918 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2919 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002920 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002921}
2922
2923static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002924 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002925 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002926 for (int angle = 0; angle < 360; ++angle) {
2927 SkPath tmp;
2928 SkMatrix m;
2929 m.setRotate(SkIntToScalar(angle));
2930 path.transform(m, &tmp);
2931
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002932 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002933 // degrees is not an oval anymore, this can be improved. we made this
2934 // for the simplicity of our implementation.
2935 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002936 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002937 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002938 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002939 }
2940 }
2941}
2942
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002943static void test_circle_mirror_x(skiatest::Reporter* reporter,
2944 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002945 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002946 SkPath tmp;
2947 SkMatrix m;
2948 m.reset();
2949 m.setScaleX(-SK_Scalar1);
2950 path.transform(m, &tmp);
2951
reed026beb52015-06-10 14:23:15 -07002952 if (SkPathPriv::kCW_FirstDirection == dir) {
2953 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002954 } else {
reed026beb52015-06-10 14:23:15 -07002955 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2956 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002957 }
2958
2959 check_for_circle(reporter, tmp, true, dir);
2960}
2961
2962static void test_circle_mirror_y(skiatest::Reporter* reporter,
2963 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002964 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002965 SkPath tmp;
2966 SkMatrix m;
2967 m.reset();
2968 m.setScaleY(-SK_Scalar1);
2969 path.transform(m, &tmp);
2970
reed026beb52015-06-10 14:23:15 -07002971 if (SkPathPriv::kCW_FirstDirection == dir) {
2972 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002973 } else {
reed026beb52015-06-10 14:23:15 -07002974 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2975 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002976 }
2977
2978 check_for_circle(reporter, tmp, true, dir);
2979}
2980
2981static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2982 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002983 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002984 SkPath tmp;
2985 SkMatrix m;
2986 m.reset();
2987 m.setScaleX(-SK_Scalar1);
2988 m.setScaleY(-SK_Scalar1);
2989 path.transform(m, &tmp);
2990
2991 check_for_circle(reporter, tmp, true, dir);
2992}
2993
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002994static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07002995 SkPath::Direction inDir) {
2996 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002997 SkPath path;
2998
2999 // circle at origin
reed026beb52015-06-10 14:23:15 -07003000 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3001
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003002 check_for_circle(reporter, path, true, dir);
3003 test_circle_rotate(reporter, path, dir);
3004 test_circle_translate(reporter, path, dir);
3005 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003006
3007 // circle at an offset at (10, 10)
3008 path.reset();
3009 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003010 SkIntToScalar(20), inDir);
3011
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003012 check_for_circle(reporter, path, true, dir);
3013 test_circle_rotate(reporter, path, dir);
3014 test_circle_translate(reporter, path, dir);
3015 test_circle_skew(reporter, path, dir);
3016 test_circle_mirror_x(reporter, path, dir);
3017 test_circle_mirror_y(reporter, path, dir);
3018 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003019}
3020
3021static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3022 SkPath path;
3023 SkPath circle;
3024 SkPath rect;
3025 SkPath empty;
3026
reed026beb52015-06-10 14:23:15 -07003027 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3028 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003029
3030 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003031 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3032 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3033
3034 SkMatrix translate;
3035 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3036
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003037 // Although all the path concatenation related operations leave
3038 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003039
3040 // empty + circle (translate)
3041 path = empty;
3042 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003043 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003044
3045 // circle + empty (translate)
3046 path = circle;
3047 path.addPath(empty, translate);
reed026beb52015-06-10 14:23:15 -07003048 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003049
3050 // test reverseAddPath
3051 path = circle;
3052 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003053 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003054}
3055
3056static void test_circle(skiatest::Reporter* reporter) {
3057 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3058 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3059
3060 // multiple addCircle()
3061 SkPath path;
3062 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3063 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003064 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003065
3066 // some extra lineTo() would make isOval() fail
3067 path.reset();
3068 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3069 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003070 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003071
3072 // not back to the original point
3073 path.reset();
3074 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3075 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003076 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003077
3078 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003079
3080 // test negative radius
3081 path.reset();
3082 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3083 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003084}
3085
3086static void test_oval(skiatest::Reporter* reporter) {
3087 SkRect rect;
3088 SkMatrix m;
3089 SkPath path;
3090
3091 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3092 path.addOval(rect);
3093
halcanary96fcdcc2015-08-27 07:41:13 -07003094 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003095
3096 m.setRotate(SkIntToScalar(90));
3097 SkPath tmp;
3098 path.transform(m, &tmp);
3099 // an oval rotated 90 degrees is still an oval.
halcanary96fcdcc2015-08-27 07:41:13 -07003100 REPORTER_ASSERT(reporter, tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003101
3102 m.reset();
3103 m.setRotate(SkIntToScalar(30));
3104 tmp.reset();
3105 path.transform(m, &tmp);
3106 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003107 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003108
3109 // since empty path being transformed.
3110 path.reset();
3111 tmp.reset();
3112 m.reset();
3113 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003114 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003115
3116 // empty path is not an oval
3117 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003118 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003119
3120 // only has moveTo()s
3121 tmp.reset();
3122 tmp.moveTo(0, 0);
3123 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003124 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003125
3126 // mimic WebKit's calling convention,
3127 // call moveTo() first and then call addOval()
3128 path.reset();
3129 path.moveTo(0, 0);
3130 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003131 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003132
3133 // copy path
3134 path.reset();
3135 tmp.reset();
3136 tmp.addOval(rect);
3137 path = tmp;
halcanary96fcdcc2015-08-27 07:41:13 -07003138 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003139}
3140
bungeman@google.coma5809a32013-06-21 15:13:34 +00003141static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3142 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003143
reed@android.com3abec1d2009-03-02 05:36:20 +00003144 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003145 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003146 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003147 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003148 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003149 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3150 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003151 REPORTER_ASSERT(reporter, p == empty);
3152 REPORTER_ASSERT(reporter, !(p != empty));
3153}
3154
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003155static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3156 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003157 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003158 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003159 path->setConvexity(SkPath::kUnknown_Convexity);
3160 REPORTER_ASSERT(reporter, path->isConvex());
3161 path->reset();
3162}
3163
caryclarkd3d1a982014-12-08 04:57:38 -08003164static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3165 SkPath::Direction dir) {
3166 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003167 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003168 path->setConvexity(SkPath::kUnknown_Convexity);
3169 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3170 path->reset();
3171}
3172
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003173static void test_rrect(skiatest::Reporter* reporter) {
3174 SkPath p;
3175 SkRRect rr;
3176 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3177 SkRect r = {10, 20, 30, 40};
3178 rr.setRectRadii(r, radii);
3179 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003180 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003181 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003182 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003183 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003184 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003185 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003186 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003187 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003188 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003189 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003190 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3191 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3192 SkVector save = radii[i];
3193 radii[i].set(0, 0);
3194 rr.setRectRadii(r, radii);
3195 p.addRRect(rr);
3196 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3197 radii[i] = save;
3198 }
3199 p.addRoundRect(r, 0, 0);
3200 SkRect returnedRect;
3201 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3202 REPORTER_ASSERT(reporter, returnedRect == r);
3203 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3204 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3205 rr.setRectRadii(r, zeroRadii);
3206 p.addRRect(rr);
3207 bool closed;
3208 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003209 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003210 REPORTER_ASSERT(reporter, closed);
3211 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3212 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3213 p.addRRect(rr, SkPath::kCW_Direction);
3214 p.addRRect(rr, SkPath::kCW_Direction);
3215 REPORTER_ASSERT(reporter, !p.isConvex());
3216 p.reset();
3217 p.addRRect(rr, SkPath::kCCW_Direction);
3218 p.addRRect(rr, SkPath::kCCW_Direction);
3219 REPORTER_ASSERT(reporter, !p.isConvex());
3220 p.reset();
3221 SkRect emptyR = {10, 20, 10, 30};
3222 rr.setRectRadii(emptyR, radii);
3223 p.addRRect(rr);
3224 REPORTER_ASSERT(reporter, p.isEmpty());
3225 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3226 rr.setRectRadii(largeR, radii);
3227 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003228 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003229
3230 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003231 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3232 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003233 REPORTER_ASSERT(reporter, rr.isEmpty());
3234
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003235 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3236 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3237 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003238}
3239
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003240static void test_arc(skiatest::Reporter* reporter) {
3241 SkPath p;
3242 SkRect emptyOval = {10, 20, 30, 20};
3243 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3244 p.addArc(emptyOval, 1, 2);
3245 REPORTER_ASSERT(reporter, p.isEmpty());
3246 p.reset();
3247 SkRect oval = {10, 20, 30, 40};
3248 p.addArc(oval, 1, 0);
3249 REPORTER_ASSERT(reporter, p.isEmpty());
3250 p.reset();
3251 SkPath cwOval;
3252 cwOval.addOval(oval);
3253 p.addArc(oval, 1, 360);
3254 REPORTER_ASSERT(reporter, p == cwOval);
3255 p.reset();
3256 SkPath ccwOval;
3257 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3258 p.addArc(oval, 1, -360);
3259 REPORTER_ASSERT(reporter, p == ccwOval);
3260 p.reset();
3261 p.addArc(oval, 1, 180);
3262 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003263 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003264 p.setConvexity(SkPath::kUnknown_Convexity);
3265 REPORTER_ASSERT(reporter, p.isConvex());
3266}
3267
3268static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3269 SkScalar x0, SkScalar y0) {
3270 SkPoint pts[4];
3271 SkPath::Verb v = iter->next(pts);
3272 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3273 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3274 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3275}
3276
3277static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3278 SkScalar x1, SkScalar y1) {
3279 SkPoint pts[4];
3280 SkPath::Verb v = iter->next(pts);
3281 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3282 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3283 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3284}
3285
3286static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3287 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3288 SkPoint pts[4];
3289 SkPath::Verb v = iter->next(pts);
3290 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3291 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3292 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3293 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3294 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3295}
3296
3297static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3298 SkPoint pts[4];
3299 SkPath::Verb v = iter->next(pts);
3300 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3301}
3302
3303static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3304 check_done(reporter, p, iter);
3305 p->reset();
3306}
3307
3308static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3309 SkScalar x0, SkScalar y0) {
3310 SkPath::RawIter iter(*p);
3311 check_move(reporter, &iter, x0, y0);
3312 check_done_and_reset(reporter, p, &iter);
3313}
3314
3315static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3316 SkScalar x1, SkScalar y1) {
3317 SkPath::RawIter iter(*p);
3318 check_move(reporter, &iter, 0, 0);
3319 check_line(reporter, &iter, x1, y1);
3320 check_done_and_reset(reporter, p, &iter);
3321}
3322
3323static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3324 SkScalar x1, SkScalar y1) {
3325 SkPath::RawIter iter(*p);
3326 check_move(reporter, &iter, 0, 0);
3327 check_line(reporter, &iter, x1, y1);
3328 check_done(reporter, p, &iter);
3329}
3330
3331static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3332 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3333 SkPath::RawIter iter(*p);
3334 check_move(reporter, &iter, 0, 0);
3335 check_line(reporter, &iter, x1, y1);
3336 check_line(reporter, &iter, x2, y2);
3337 check_done_and_reset(reporter, p, &iter);
3338}
3339
3340static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3341 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3342 SkPath::RawIter iter(*p);
3343 check_move(reporter, &iter, 0, 0);
3344 check_quad(reporter, &iter, x1, y1, x2, y2);
3345 check_done_and_reset(reporter, p, &iter);
3346}
3347
reedd5d27d92015-02-09 13:54:43 -08003348static bool nearly_equal(const SkRect& a, const SkRect& b) {
3349 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3350 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3351 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3352 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3353}
3354
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003355static void test_arcTo(skiatest::Reporter* reporter) {
3356 SkPath p;
3357 p.arcTo(0, 0, 1, 2, 1);
3358 check_path_is_line_and_reset(reporter, &p, 0, 0);
3359 p.arcTo(1, 2, 1, 2, 1);
3360 check_path_is_line_and_reset(reporter, &p, 1, 2);
3361 p.arcTo(1, 2, 3, 4, 0);
3362 check_path_is_line_and_reset(reporter, &p, 1, 2);
3363 p.arcTo(1, 2, 0, 0, 1);
3364 check_path_is_line_and_reset(reporter, &p, 1, 2);
3365 p.arcTo(1, 0, 1, 1, 1);
3366 SkPoint pt;
3367 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3368 p.reset();
3369 p.arcTo(1, 0, 1, -1, 1);
3370 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3371 p.reset();
3372 SkRect oval = {1, 2, 3, 4};
3373 p.arcTo(oval, 0, 0, true);
3374 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3375 p.arcTo(oval, 0, 0, false);
3376 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3377 p.arcTo(oval, 360, 0, true);
3378 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3379 p.arcTo(oval, 360, 0, false);
3380 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003381
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003382 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003383 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003384 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003385 sweep += delta;
3386 delta /= 2;
3387 }
3388 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003389 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003390 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003391 sweep -= delta;
3392 delta /= 2;
3393 }
3394 SkRect noOvalWidth = {1, 2, 0, 3};
3395 p.reset();
3396 p.arcTo(noOvalWidth, 0, 360, false);
3397 REPORTER_ASSERT(reporter, p.isEmpty());
3398
3399 SkRect noOvalHeight = {1, 2, 3, 1};
3400 p.reset();
3401 p.arcTo(noOvalHeight, 0, 360, false);
3402 REPORTER_ASSERT(reporter, p.isEmpty());
3403}
3404
3405static void test_addPath(skiatest::Reporter* reporter) {
3406 SkPath p, q;
3407 p.lineTo(1, 2);
3408 q.moveTo(4, 4);
3409 q.lineTo(7, 8);
3410 q.conicTo(8, 7, 6, 5, 0.5f);
3411 q.quadTo(6, 7, 8, 6);
3412 q.cubicTo(5, 6, 7, 8, 7, 5);
3413 q.close();
3414 p.addPath(q, -4, -4);
3415 SkRect expected = {0, 0, 4, 4};
3416 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3417 p.reset();
3418 p.reverseAddPath(q);
3419 SkRect reverseExpected = {4, 4, 8, 8};
3420 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3421}
3422
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003423static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3424 SkPath p, q;
3425 if (explicitMoveTo) {
3426 p.moveTo(1, 1);
3427 }
3428 p.lineTo(1, 2);
3429 if (explicitMoveTo) {
3430 q.moveTo(2, 1);
3431 }
3432 q.lineTo(2, 2);
3433 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3434 uint8_t verbs[4];
3435 int verbcount = p.getVerbs(verbs, 4);
3436 REPORTER_ASSERT(reporter, verbcount == 4);
3437 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3438 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3439 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3440 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3441}
3442
3443static void test_extendClosedPath(skiatest::Reporter* reporter) {
3444 SkPath p, q;
3445 p.moveTo(1, 1);
3446 p.lineTo(1, 2);
3447 p.lineTo(2, 2);
3448 p.close();
3449 q.moveTo(2, 1);
3450 q.lineTo(2, 3);
3451 p.addPath(q, SkPath::kExtend_AddPathMode);
3452 uint8_t verbs[7];
3453 int verbcount = p.getVerbs(verbs, 7);
3454 REPORTER_ASSERT(reporter, verbcount == 7);
3455 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3456 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3457 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3458 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3459 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3460 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3461 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3462
3463 SkPoint pt;
3464 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3465 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3466 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3467}
3468
3469static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3470 SkPath p, q, r;
3471 // case 1: dst is empty
3472 p.moveTo(2, 1);
3473 p.lineTo(2, 3);
3474 q.addPath(p, mode);
3475 REPORTER_ASSERT(reporter, q == p);
3476 // case 2: src is empty
3477 p.addPath(r, mode);
3478 REPORTER_ASSERT(reporter, q == p);
3479 // case 3: src and dst are empty
3480 q.reset();
3481 q.addPath(r, mode);
3482 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003483}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003484
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003485static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3486 SkPath p;
3487 p.conicTo(1, 2, 3, 4, -1);
3488 check_path_is_line_and_reset(reporter, &p, 3, 4);
3489 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3490 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3491 p.conicTo(1, 2, 3, 4, 1);
3492 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3493}
3494
3495static void test_get_point(skiatest::Reporter* reporter) {
3496 SkPath p;
3497 SkPoint pt = p.getPoint(0);
3498 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003499 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003500 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3501 p.setLastPt(10, 10);
3502 pt = p.getPoint(0);
3503 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003504 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003505 p.rMoveTo(10, 10);
3506 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3507}
3508
3509static void test_contains(skiatest::Reporter* reporter) {
3510 SkPath p;
3511 p.setFillType(SkPath::kInverseWinding_FillType);
3512 REPORTER_ASSERT(reporter, p.contains(0, 0));
3513 p.setFillType(SkPath::kWinding_FillType);
3514 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3515 p.moveTo(4, 4);
3516 p.lineTo(6, 8);
3517 p.lineTo(8, 4);
caryclark9aacd902015-12-14 08:38:09 -08003518 // test on edge
3519 REPORTER_ASSERT(reporter, p.contains(6, 4));
3520 REPORTER_ASSERT(reporter, p.contains(5, 6));
3521 REPORTER_ASSERT(reporter, p.contains(7, 6));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003522 // test quick reject
3523 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3524 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3525 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3526 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3527 // test various crossings in x
3528 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3529 REPORTER_ASSERT(reporter, p.contains(6, 7));
3530 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3531 p.reset();
3532 p.moveTo(4, 4);
3533 p.lineTo(8, 6);
3534 p.lineTo(4, 8);
caryclark9aacd902015-12-14 08:38:09 -08003535 // test on edge
3536 REPORTER_ASSERT(reporter, p.contains(4, 6));
3537 REPORTER_ASSERT(reporter, p.contains(6, 5));
3538 REPORTER_ASSERT(reporter, p.contains(6, 7));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003539 // test various crossings in y
3540 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3541 REPORTER_ASSERT(reporter, p.contains(7, 6));
3542 REPORTER_ASSERT(reporter, !p.contains(7, 7));
caryclark9aacd902015-12-14 08:38:09 -08003543 p.reset();
3544 p.moveTo(4, 4);
fsc91065d2015-12-17 09:03:27 -08003545 p.lineTo(8, 4);
3546 p.lineTo(8, 8);
3547 p.lineTo(4, 8);
3548 // test on vertices
3549 REPORTER_ASSERT(reporter, p.contains(4, 4));
3550 REPORTER_ASSERT(reporter, p.contains(8, 4));
3551 REPORTER_ASSERT(reporter, p.contains(8, 8));
3552 REPORTER_ASSERT(reporter, p.contains(4, 8));
3553 p.reset();
3554 p.moveTo(4, 4);
caryclark9aacd902015-12-14 08:38:09 -08003555 p.lineTo(6, 8);
3556 p.lineTo(2, 8);
3557 // test on edge
3558 REPORTER_ASSERT(reporter, p.contains(5, 6));
3559 REPORTER_ASSERT(reporter, p.contains(4, 8));
3560 REPORTER_ASSERT(reporter, p.contains(3, 6));
3561 p.reset();
3562 p.moveTo(4, 4);
3563 p.lineTo(0, 6);
3564 p.lineTo(4, 8);
3565 // test on edge
3566 REPORTER_ASSERT(reporter, p.contains(2, 5));
3567 REPORTER_ASSERT(reporter, p.contains(2, 7));
3568 REPORTER_ASSERT(reporter, p.contains(4, 6));
3569 // test canceling coincident edge (a smaller triangle is coincident with a larger one)
3570 p.reset();
3571 p.moveTo(4, 0);
3572 p.lineTo(6, 4);
3573 p.lineTo(2, 4);
3574 p.moveTo(4, 0);
3575 p.lineTo(0, 8);
3576 p.lineTo(8, 8);
3577 REPORTER_ASSERT(reporter, !p.contains(1, 2));
3578 REPORTER_ASSERT(reporter, !p.contains(3, 2));
3579 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3580 REPORTER_ASSERT(reporter, p.contains(4, 4));
3581
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003582 // test quads
3583 p.reset();
3584 p.moveTo(4, 4);
3585 p.quadTo(6, 6, 8, 8);
3586 p.quadTo(6, 8, 4, 8);
3587 p.quadTo(4, 6, 4, 4);
3588 REPORTER_ASSERT(reporter, p.contains(5, 6));
3589 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003590 // test quad edge
3591 REPORTER_ASSERT(reporter, p.contains(5, 5));
3592 REPORTER_ASSERT(reporter, p.contains(5, 8));
3593 REPORTER_ASSERT(reporter, p.contains(4, 5));
caryclark9cb5d752015-12-18 04:35:24 -08003594 // test quad endpoints
3595 REPORTER_ASSERT(reporter, p.contains(4, 4));
3596 REPORTER_ASSERT(reporter, p.contains(8, 8));
3597 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003598
3599 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003600 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}};
3601 p.moveTo(qPts[0]);
3602 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) {
3603 p.quadTo(qPts[index], qPts[index + 1]);
3604 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003605 REPORTER_ASSERT(reporter, p.contains(5, 6));
3606 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003607 // test quad edge
3608 SkPoint halfway;
3609 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) {
3610 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr);
3611 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3612 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003613
caryclark9aacd902015-12-14 08:38:09 -08003614 // test conics
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003615 p.reset();
caryclark9aacd902015-12-14 08:38:09 -08003616 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}};
3617 p.moveTo(kPts[0]);
3618 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) {
3619 p.conicTo(kPts[index], kPts[index + 1], 0.5f);
3620 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003621 REPORTER_ASSERT(reporter, p.contains(5, 6));
3622 REPORTER_ASSERT(reporter, !p.contains(6, 5));
caryclark9aacd902015-12-14 08:38:09 -08003623 // test conic edge
3624 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) {
3625 SkConic conic(&kPts[index], 0.5f);
3626 halfway = conic.evalAt(0.5f);
3627 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3628 }
caryclark9cb5d752015-12-18 04:35:24 -08003629 // test conic end points
3630 REPORTER_ASSERT(reporter, p.contains(4, 4));
3631 REPORTER_ASSERT(reporter, p.contains(8, 8));
3632 REPORTER_ASSERT(reporter, p.contains(4, 8));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003633
3634 // test cubics
3635 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3636 for (int i = 0; i < 3; ++i) {
3637 p.reset();
3638 p.setFillType(SkPath::kEvenOdd_FillType);
3639 p.moveTo(pts[i].fX, pts[i].fY);
3640 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);
3641 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);
3642 p.close();
3643 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3644 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
caryclark9aacd902015-12-14 08:38:09 -08003645 // test cubic edge
3646 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr);
3647 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
3648 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr);
3649 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY));
caryclark9cb5d752015-12-18 04:35:24 -08003650 // test cubic end points
3651 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY));
3652 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY));
3653 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003654 }
3655}
3656
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003657class PathRefTest_Private {
3658public:
3659 static void TestPathRef(skiatest::Reporter* reporter) {
3660 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003661
halcanary385fe4d2015-08-26 13:07:48 -07003662 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003663
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003664 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003665
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003666 {
3667 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3668 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3669 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3670 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3671 for (int i = 0; i < kRepeatCnt; ++i) {
3672 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3673 }
3674 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003675 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003676
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003677 {
3678 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3679 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3680 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3681 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3682 for (int i = 0; i < kRepeatCnt; ++i) {
3683 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3684 }
3685 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003686 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003687
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003688 {
3689 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3690 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3691 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3692 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3693 for (int i = 0; i < kRepeatCnt; ++i) {
3694 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3695 }
3696 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003697 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003698
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003699 {
halcanary96fcdcc2015-08-27 07:41:13 -07003700 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003701 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3702 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3703 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3704 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3705 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003706 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003707 for (int i = 0; i < kRepeatCnt; ++i) {
3708 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3709 }
3710 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003711 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003712
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003713 {
3714 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3715 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3716 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3717 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3718 for (int i = 0; i < kRepeatCnt; ++i) {
3719 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3720 }
3721 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003722 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003723 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003724};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003725
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003726static void test_operatorEqual(skiatest::Reporter* reporter) {
3727 SkPath a;
3728 SkPath b;
3729 REPORTER_ASSERT(reporter, a == a);
3730 REPORTER_ASSERT(reporter, a == b);
3731 a.setFillType(SkPath::kInverseWinding_FillType);
3732 REPORTER_ASSERT(reporter, a != b);
3733 a.reset();
3734 REPORTER_ASSERT(reporter, a == b);
3735 a.lineTo(1, 1);
3736 REPORTER_ASSERT(reporter, a != b);
3737 a.reset();
3738 REPORTER_ASSERT(reporter, a == b);
3739 a.lineTo(1, 1);
3740 b.lineTo(1, 2);
3741 REPORTER_ASSERT(reporter, a != b);
3742 a.reset();
3743 a.lineTo(1, 2);
3744 REPORTER_ASSERT(reporter, a == b);
3745}
3746
caryclark66a5d8b2014-06-24 08:30:15 -07003747static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003748 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003749 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003750 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003751 SkAutoDataUnref data(wStream.copyToData());
3752 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08003753 if (strlen(str) > 0) {
3754 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3755 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07003756 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08003757 }
caryclark66a5d8b2014-06-24 08:30:15 -07003758}
3759
3760static void test_dump(skiatest::Reporter* reporter) {
3761 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07003762 compare_dump(reporter, p, false, false, "");
3763 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07003764 p.moveTo(1, 2);
3765 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07003766 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3767 "path.lineTo(3, 4);\n");
3768 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
3769 "path.lineTo(3, 4);\n"
3770 "path.lineTo(1, 2);\n"
3771 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003772 p.reset();
3773 p.moveTo(1, 2);
3774 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07003775 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3776 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003777 p.reset();
3778 p.moveTo(1, 2);
3779 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07003780 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3781 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003782 p.reset();
3783 p.moveTo(1, 2);
3784 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07003785 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3786 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3787 p.reset();
3788 p.moveTo(1, 2);
3789 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07003790 compare_dump(reporter, p, false, true,
3791 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
3792 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07003793 p.reset();
3794 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
3795 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
3796 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3797 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003798}
3799
senorblanco84cd6212015-08-04 10:01:58 -07003800namespace {
3801
3802class ChangeListener : public SkPathRef::GenIDChangeListener {
3803public:
3804 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
3805 virtual ~ChangeListener() {}
3806 void onChange() override {
3807 *fChanged = true;
3808 }
3809private:
3810 bool* fChanged;
3811};
3812
3813}
3814
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003815class PathTest_Private {
3816public:
3817 static void TestPathTo(skiatest::Reporter* reporter) {
3818 SkPath p, q;
3819 p.lineTo(4, 4);
3820 p.reversePathTo(q);
3821 check_path_is_line(reporter, &p, 4, 4);
3822 q.moveTo(-4, -4);
3823 p.reversePathTo(q);
3824 check_path_is_line(reporter, &p, 4, 4);
3825 q.lineTo(7, 8);
3826 q.conicTo(8, 7, 6, 5, 0.5f);
3827 q.quadTo(6, 7, 8, 6);
3828 q.cubicTo(5, 6, 7, 8, 7, 5);
3829 q.close();
3830 p.reversePathTo(q);
3831 SkRect reverseExpected = {-4, -4, 8, 8};
3832 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3833 }
senorblanco84cd6212015-08-04 10:01:58 -07003834
3835 static void TestPathrefListeners(skiatest::Reporter* reporter) {
3836 SkPath p;
3837
3838 bool changed = false;
3839 p.moveTo(0, 0);
3840
3841 // Check that listener is notified on moveTo().
3842
halcanary385fe4d2015-08-26 13:07:48 -07003843 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003844 REPORTER_ASSERT(reporter, !changed);
3845 p.moveTo(10, 0);
3846 REPORTER_ASSERT(reporter, changed);
3847
3848 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07003849 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003850 REPORTER_ASSERT(reporter, !changed);
3851 p.lineTo(20, 0);
3852 REPORTER_ASSERT(reporter, changed);
3853
3854 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07003855 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003856 REPORTER_ASSERT(reporter, !changed);
3857 p.reset();
3858 REPORTER_ASSERT(reporter, changed);
3859
3860 p.moveTo(0, 0);
3861
3862 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07003863 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003864 REPORTER_ASSERT(reporter, !changed);
3865 p.rewind();
3866 REPORTER_ASSERT(reporter, changed);
3867
3868 // Check that listener is notified when pathref is deleted.
3869 {
3870 SkPath q;
3871 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07003872 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003873 REPORTER_ASSERT(reporter, !changed);
3874 }
3875 // q went out of scope.
3876 REPORTER_ASSERT(reporter, changed);
3877 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003878};
3879
caryclark9aacd902015-12-14 08:38:09 -08003880DEF_TEST(PathContains, reporter) {
3881 test_contains(reporter);
3882}
3883
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003884DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003885 test_path_crbug364224();
3886
bungeman@google.coma5809a32013-06-21 15:13:34 +00003887 SkTSize<SkScalar>::Make(3,4);
3888
3889 SkPath p, empty;
3890 SkRect bounds, bounds2;
3891 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003892
reed@android.comd252db02009-04-01 18:31:44 +00003893 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003894
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003895 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3896 SkPath& self = p;
3897 p = self;
3898
3899 // this triggers a code path in SkPath::swap which is otherwise unexercised
3900 p.swap(self);
3901
reed@android.com3abec1d2009-03-02 05:36:20 +00003902 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003903
reed@android.com6b82d1a2009-06-03 02:35:01 +00003904 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3905 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003906 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08003907 REPORTER_ASSERT(reporter,
3908 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003909 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003910
reed@android.com6b82d1a2009-06-03 02:35:01 +00003911 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003912 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003913
reed@android.com6b82d1a2009-06-03 02:35:01 +00003914 p.addOval(bounds);
3915 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003916 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003917
bungeman@google.coma5809a32013-06-21 15:13:34 +00003918 p.rewind();
3919 test_empty(reporter, p);
3920
reed@android.com3abec1d2009-03-02 05:36:20 +00003921 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003922 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003923 // we have only lines
3924 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003925 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003926
bungeman@google.coma5809a32013-06-21 15:13:34 +00003927 REPORTER_ASSERT(reporter, p != empty);
3928 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003929
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003930 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07003931 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
3932 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003933 SkPoint pts[4];
3934 int count = p.getPoints(pts, 4);
3935 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003936 uint8_t verbs[6];
3937 verbs[5] = 0xff;
3938 p.getVerbs(verbs, 5);
3939 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3940 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3941 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3942 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3943 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3944 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003945 bounds2.set(pts, 4);
3946 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003947
reed@android.com3abec1d2009-03-02 05:36:20 +00003948 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3949 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003950 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003951
halcanary96fcdcc2015-08-27 07:41:13 -07003952 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00003953 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003954 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3955 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003956
reed@android.com3abec1d2009-03-02 05:36:20 +00003957 // now force p to not be a rect
3958 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3959 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07003960 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00003961
robertphillipsfe7c4272014-12-29 11:36:39 -08003962 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
3963 // path has a trailing moveTo. Please see crbug.com\445368)
3964 {
3965 SkRect r;
3966 p.reset();
3967 p.addRect(bounds);
3968 REPORTER_ASSERT(reporter, p.isRect(&r));
3969 REPORTER_ASSERT(reporter, r == bounds);
3970 // add a moveTo outside of our bounds
3971 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
3972 REPORTER_ASSERT(reporter, p.isRect(&r));
3973 REPORTER_ASSERT(reporter, r == bounds);
3974 }
3975
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003976 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003977 test_isLine(reporter);
3978 test_isRect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07003979 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003980 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003981 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003982 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003983 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003984 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003985 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003986 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003987 test_flattening(reporter);
3988 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003989 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003990 test_iter(reporter);
3991 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003992 test_circle(reporter);
3993 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003994 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003995 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003996 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003997 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003998 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003999 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004000 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00004001 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00004002 test_tricky_cubic();
4003 test_clipped_cubic();
4004 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07004005 test_crbug_493450(reporter);
4006 test_crbug_495894(reporter);
reed@google.com7a90daf2013-04-10 18:44:00 +00004007 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00004008 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00004009 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00004010 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00004011 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00004012 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004013 test_arc(reporter);
4014 test_arcTo(reporter);
4015 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00004016 test_addPathMode(reporter, false, false);
4017 test_addPathMode(reporter, true, false);
4018 test_addPathMode(reporter, false, true);
4019 test_addPathMode(reporter, true, true);
4020 test_extendClosedPath(reporter);
4021 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
4022 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00004023 test_conicTo_special_case(reporter);
4024 test_get_point(reporter);
4025 test_contains(reporter);
4026 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00004027 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07004028 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07004029 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08004030 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07004031 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08004032 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08004033 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07004034 test_bounds_crbug_513799(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00004035}