blob: 9c3bcced9a700085001b8196fb559649c46df27e [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"
reed@google.com55b5f4b2011-09-07 12:23:41 +00009#include "SkPaint.h"
reed@google.com04863fa2011-05-15 04:08:24 +000010#include "SkParse.h"
reed@google.com3e71a882012-01-10 18:44:37 +000011#include "SkParsePath.h"
reed026beb52015-06-10 14:23:15 -070012#include "SkPathPriv.h"
reed@google.com8b06f1a2012-05-29 12:03:46 +000013#include "SkPathEffect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "SkRRect.h"
schenney@chromium.org6630d8d2012-01-04 21:05:51 +000015#include "SkRandom.h"
reed@google.com53effc52011-09-21 19:05:12 +000016#include "SkReader32.h"
reed@android.com60bc6d52010-02-11 11:09:39 +000017#include "SkSize.h"
caryclark66a5d8b2014-06-24 08:30:15 -070018#include "SkStream.h"
halcanary435657f2015-09-15 12:53:07 -070019#include "SkStrokeRec.h"
reed@google.com8cae8352012-09-14 15:18:41 +000020#include "SkSurface.h"
mtklein@google.com9c9d4a72013-08-07 19:17:53 +000021#include "SkTypes.h"
22#include "SkWriter32.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "Test.h"
reed@google.com8cae8352012-09-14 15:18:41 +000024
reed5bcbe912014-12-15 12:28:33 -080025static void set_radii(SkVector radii[4], int index, float rad) {
26 sk_bzero(radii, sizeof(SkVector) * 4);
27 radii[index].set(rad, rad);
28}
29
30static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
31 const SkVector radii[4]) {
32 SkRRect rrect;
33 rrect.setRectRadii(bounds, radii);
34 REPORTER_ASSERT(reporter, bounds == rrect.rect());
35
36 SkPath path;
37 // this line should not assert in the debug build (from validate)
38 path.addRRect(rrect);
39 REPORTER_ASSERT(reporter, bounds == path.getBounds());
40}
41
caryclark5ccef572015-03-02 10:07:56 -080042static void test_skbug_3469(skiatest::Reporter* reporter) {
43 SkPath path;
44 path.moveTo(20, 20);
45 path.quadTo(20, 50, 80, 50);
46 path.quadTo(20, 50, 20, 80);
47 REPORTER_ASSERT(reporter, !path.isConvex());
48}
49
reed5bcbe912014-12-15 12:28:33 -080050static void test_skbug_3239(skiatest::Reporter* reporter) {
51 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
52 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
53 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
54
55 const float rad = 33436320;
56
57 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
58 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
59
60 SkVector radii[4];
61 for (int i = 0; i < 4; ++i) {
62 set_radii(radii, i, rad);
63 test_add_rrect(reporter, rectx, radii);
64 test_add_rrect(reporter, recty, radii);
65 }
66}
67
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000068static void make_path_crbug364224(SkPath* path) {
69 path->reset();
70 path->moveTo(3.747501373f, 2.724499941f);
71 path->lineTo(3.747501373f, 3.75f);
72 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f);
73 path->lineTo(0.7475013733f, 4.0f);
74 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f);
75 path->lineTo(0.4975013733f, 1.0f);
76 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f);
77 path->lineTo(3.497501373f, 0.75f);
78 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f);
79 path->lineTo(3.715001345f, 0.5512499809f);
80 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
81 path->lineTo(0.7475013733f, 0.4999999702f);
82 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f);
83 path->lineTo(0.2475013733f, 3.75f);
84 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f);
85 path->lineTo(3.497501373f, 4.25f);
86 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f);
87 path->lineTo(3.997501373f, 2.474750042f);
88 path->lineTo(3.747501373f, 2.724499941f);
89 path->close();
90}
91
92static void make_path_crbug364224_simplified(SkPath* path) {
93 path->moveTo(3.747501373f, 2.724499941f);
94 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f);
95 path->close();
96}
97
98static void test_path_crbug364224() {
99 SkPath path;
100 SkPaint paint;
reed3054be12014-12-10 07:24:28 -0800101 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88));
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000102 SkCanvas* canvas = surface->getCanvas();
103
104 make_path_crbug364224_simplified(&path);
105 canvas->drawPath(path, paint);
106
107 make_path_crbug364224(&path);
108 canvas->drawPath(path, paint);
109}
110
piotaixrfac4e0e2014-08-26 11:59:04 -0700111/**
112 * In debug mode, this path was causing an assertion to fail in
113 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value.
114 */
115static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) {
116 SkPoint orig, p1, p2, p3;
117 orig = SkPoint::Make(1.f, 1.f);
118 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f);
119 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero);
120 p3 = SkPoint::Make(2.f, 2.f);
121
122 path->reset();
123 path->moveTo(orig);
124 path->cubicTo(p1, p2, p3);
125 path->close();
126}
127
128static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) {
129 SkPath path;
130 make_path_crbugskia2820(&path, reporter);
131
132 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
133 stroke.setStrokeStyle(2 * SK_Scalar1);
134 stroke.applyToPath(&path, path);
135}
136
reed@google.comcc8be772013-10-15 15:35:29 +0000137static void make_path0(SkPath* path) {
138 // from * https://code.google.com/p/skia/issues/detail?id=1706
139
140 path->moveTo(146.939f, 1012.84f);
141 path->lineTo(181.747f, 1009.18f);
142 path->lineTo(182.165f, 1013.16f);
143 path->lineTo(147.357f, 1016.82f);
144 path->lineTo(146.939f, 1012.84f);
145 path->close();
146}
147
148static void make_path1(SkPath* path) {
149 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1));
150}
151
152typedef void (*PathProc)(SkPath*);
153
154/*
155 * Regression test: we used to crash (overwrite internal storage) during
156 * construction of the region when the path was INVERSE. That is now fixed,
157 * so test these regions (which used to assert/crash).
158 *
159 * https://code.google.com/p/skia/issues/detail?id=1706
160 */
161static void test_path_to_region(skiatest::Reporter* reporter) {
162 PathProc procs[] = {
163 make_path0,
164 make_path1,
165 };
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000166
reed@google.comcc8be772013-10-15 15:35:29 +0000167 SkRegion clip;
168 clip.setRect(0, 0, 1255, 1925);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000169
reed@google.comcc8be772013-10-15 15:35:29 +0000170 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) {
171 SkPath path;
172 procs[i](&path);
skia.committer@gmail.com47262912013-10-16 07:02:24 +0000173
reed@google.comcc8be772013-10-15 15:35:29 +0000174 SkRegion rgn;
175 rgn.setPath(path, clip);
176 path.toggleInverseFillType();
177 rgn.setPath(path, clip);
178 }
179}
180
caryclark@google.com56f233a2012-11-19 13:06:06 +0000181#if defined(WIN32)
182 #define SUPPRESS_VISIBILITY_WARNING
183#else
184 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden")))
185#endif
186
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +0000187static void test_path_close_issue1474(skiatest::Reporter* reporter) {
188 // This test checks that r{Line,Quad,Conic,Cubic}To following a close()
189 // are relative to the point we close to, not relative to the point we close from.
190 SkPath path;
191 SkPoint last;
192
193 // Test rLineTo().
194 path.rLineTo(0, 100);
195 path.rLineTo(100, 0);
196 path.close(); // Returns us back to 0,0.
197 path.rLineTo(50, 50); // This should go to 50,50.
198
199 path.getLastPt(&last);
200 REPORTER_ASSERT(reporter, 50 == last.fX);
201 REPORTER_ASSERT(reporter, 50 == last.fY);
202
203 // Test rQuadTo().
204 path.rewind();
205 path.rLineTo(0, 100);
206 path.rLineTo(100, 0);
207 path.close();
208 path.rQuadTo(50, 50, 75, 75);
209
210 path.getLastPt(&last);
211 REPORTER_ASSERT(reporter, 75 == last.fX);
212 REPORTER_ASSERT(reporter, 75 == last.fY);
213
214 // Test rConicTo().
215 path.rewind();
216 path.rLineTo(0, 100);
217 path.rLineTo(100, 0);
218 path.close();
219 path.rConicTo(50, 50, 85, 85, 2);
220
221 path.getLastPt(&last);
222 REPORTER_ASSERT(reporter, 85 == last.fX);
223 REPORTER_ASSERT(reporter, 85 == last.fY);
224
225 // Test rCubicTo().
226 path.rewind();
227 path.rLineTo(0, 100);
228 path.rLineTo(100, 0);
229 path.close();
230 path.rCubicTo(50, 50, 85, 85, 95, 95);
231
232 path.getLastPt(&last);
233 REPORTER_ASSERT(reporter, 95 == last.fX);
234 REPORTER_ASSERT(reporter, 95 == last.fY);
235}
236
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000237static void test_gen_id(skiatest::Reporter* reporter) {
238 SkPath a, b;
239 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
240
241 a.moveTo(0, 0);
242 const uint32_t z = a.getGenerationID();
243 REPORTER_ASSERT(reporter, z != b.getGenerationID());
244
245 a.reset();
246 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID());
247
248 a.moveTo(1, 1);
249 const uint32_t y = a.getGenerationID();
250 REPORTER_ASSERT(reporter, z != y);
251
252 b.moveTo(2, 2);
253 const uint32_t x = b.getGenerationID();
254 REPORTER_ASSERT(reporter, x != y && x != z);
255
256 a.swap(b);
257 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x);
258
259 b = a;
260 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
261
262 SkPath c(a);
263 REPORTER_ASSERT(reporter, c.getGenerationID() == x);
264
265 c.lineTo(3, 3);
266 const uint32_t w = c.getGenerationID();
267 REPORTER_ASSERT(reporter, b.getGenerationID() == x);
268 REPORTER_ASSERT(reporter, a.getGenerationID() == x);
269 REPORTER_ASSERT(reporter, w != x);
270
djsollen50da1d82015-02-17 09:09:53 -0800271#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +0000272 static bool kExpectGenIDToIgnoreFill = false;
273#else
274 static bool kExpectGenIDToIgnoreFill = true;
275#endif
276
277 c.toggleInverseFillType();
278 const uint32_t v = c.getGenerationID();
279 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill);
280
281 c.rewind();
282 REPORTER_ASSERT(reporter, v != c.getGenerationID());
283}
284
reed@google.com3eff3592013-05-08 21:08:21 +0000285// This used to assert in the debug build, as the edges did not all line-up.
286static void test_bad_cubic_crbug234190() {
287 SkPath path;
288 path.moveTo(13.8509f, 3.16858f);
289 path.cubicTo(-2.35893e+08f, -4.21044e+08f,
290 -2.38991e+08f, -4.26573e+08f,
291 -2.41016e+08f, -4.30188e+08f);
292
293 SkPaint paint;
294 paint.setAntiAlias(true);
reed3054be12014-12-10 07:24:28 -0800295 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88));
reed@google.com3eff3592013-05-08 21:08:21 +0000296 surface->getCanvas()->drawPath(path, paint);
297}
298
reed@google.com7a90daf2013-04-10 18:44:00 +0000299static void test_bad_cubic_crbug229478() {
300 const SkPoint pts[] = {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000301 { 4595.91064f, -11596.9873f },
302 { 4597.2168f, -11595.9414f },
303 { 4598.52344f, -11594.8955f },
304 { 4599.83008f, -11593.8496f },
reed@google.com7a90daf2013-04-10 18:44:00 +0000305 };
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000306
reed@google.com7a90daf2013-04-10 18:44:00 +0000307 SkPath path;
308 path.moveTo(pts[0]);
309 path.cubicTo(pts[1], pts[2], pts[3]);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000310
reed@google.com7a90daf2013-04-10 18:44:00 +0000311 SkPaint paint;
312 paint.setStyle(SkPaint::kStroke_Style);
313 paint.setStrokeWidth(20);
skia.committer@gmail.com391ca662013-04-11 07:01:45 +0000314
reed@google.com7a90daf2013-04-10 18:44:00 +0000315 SkPath dst;
316 // Before the fix, this would infinite-recurse, and run out of stack
317 // because we would keep trying to subdivide a degenerate cubic segment.
halcanary96fcdcc2015-08-27 07:41:13 -0700318 paint.getFillPath(path, &dst, nullptr);
reed@google.com7a90daf2013-04-10 18:44:00 +0000319}
320
reed@google.com64d62952013-01-18 17:49:28 +0000321static void build_path_170666(SkPath& path) {
322 path.moveTo(17.9459f, 21.6344f);
323 path.lineTo(139.545f, -47.8105f);
324 path.lineTo(139.545f, -47.8105f);
325 path.lineTo(131.07f, -47.3888f);
326 path.lineTo(131.07f, -47.3888f);
327 path.lineTo(122.586f, -46.9532f);
328 path.lineTo(122.586f, -46.9532f);
329 path.lineTo(18076.6f, 31390.9f);
330 path.lineTo(18076.6f, 31390.9f);
331 path.lineTo(18085.1f, 31390.5f);
332 path.lineTo(18085.1f, 31390.5f);
333 path.lineTo(18076.6f, 31390.9f);
334 path.lineTo(18076.6f, 31390.9f);
335 path.lineTo(17955, 31460.3f);
336 path.lineTo(17955, 31460.3f);
337 path.lineTo(17963.5f, 31459.9f);
338 path.lineTo(17963.5f, 31459.9f);
339 path.lineTo(17971.9f, 31459.5f);
340 path.lineTo(17971.9f, 31459.5f);
341 path.lineTo(17.9551f, 21.6205f);
342 path.lineTo(17.9551f, 21.6205f);
343 path.lineTo(9.47091f, 22.0561f);
344 path.lineTo(9.47091f, 22.0561f);
345 path.lineTo(17.9459f, 21.6344f);
346 path.lineTo(17.9459f, 21.6344f);
347 path.close();path.moveTo(0.995934f, 22.4779f);
348 path.lineTo(0.986725f, 22.4918f);
349 path.lineTo(0.986725f, 22.4918f);
350 path.lineTo(17955, 31460.4f);
351 path.lineTo(17955, 31460.4f);
352 path.lineTo(17971.9f, 31459.5f);
353 path.lineTo(17971.9f, 31459.5f);
354 path.lineTo(18093.6f, 31390.1f);
355 path.lineTo(18093.6f, 31390.1f);
356 path.lineTo(18093.6f, 31390);
357 path.lineTo(18093.6f, 31390);
358 path.lineTo(139.555f, -47.8244f);
359 path.lineTo(139.555f, -47.8244f);
360 path.lineTo(122.595f, -46.9671f);
361 path.lineTo(122.595f, -46.9671f);
362 path.lineTo(0.995934f, 22.4779f);
363 path.lineTo(0.995934f, 22.4779f);
364 path.close();
365 path.moveTo(5.43941f, 25.5223f);
366 path.lineTo(798267, -28871.1f);
367 path.lineTo(798267, -28871.1f);
368 path.lineTo(3.12512e+06f, -113102);
369 path.lineTo(3.12512e+06f, -113102);
370 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813);
371 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f);
372 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f);
373 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f);
374 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f);
375 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f);
376 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f);
377 path.lineTo(2.78271e+08f, -1.00733e+07f);
378 path.lineTo(2.78271e+08f, -1.00733e+07f);
379 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f);
380 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f);
381 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f);
382 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
383 path.lineTo(2.77473e+08f, -1.00444e+07f);
384 path.lineTo(2.77473e+08f, -1.00444e+07f);
385 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f);
386 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f);
387 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f);
388 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f);
389 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f);
390 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814);
391 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103);
392 path.lineTo(798284, -28872);
393 path.lineTo(798284, -28872);
394 path.lineTo(22.4044f, 24.6677f);
395 path.lineTo(22.4044f, 24.6677f);
396 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f);
397 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f);
398 path.close();
399}
400
401static void build_path_simple_170666(SkPath& path) {
402 path.moveTo(126.677f, 24.1591f);
403 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f);
404}
405
406// This used to assert in the SK_DEBUG build, as the clip step would fail with
407// too-few interations in our cubic-line intersection code. That code now runs
408// 24 interations (instead of 16).
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000409static void test_crbug_170666() {
reed@google.com64d62952013-01-18 17:49:28 +0000410 SkPath path;
411 SkPaint paint;
412 paint.setAntiAlias(true);
413
reed3054be12014-12-10 07:24:28 -0800414 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(1000, 1000));
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000415
reed@google.com64d62952013-01-18 17:49:28 +0000416 build_path_simple_170666(path);
417 surface->getCanvas()->drawPath(path, paint);
skia.committer@gmail.com36df7ed2013-01-19 07:05:38 +0000418
reed@google.com64d62952013-01-18 17:49:28 +0000419 build_path_170666(path);
420 surface->getCanvas()->drawPath(path, paint);
421}
422
caryclarke8c56662015-07-14 11:19:26 -0700423
424static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug,
425 SkScalar tx, SkScalar ty, SkScalar scale) {
426 SkPath smallPath;
427 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath));
428 bool smallConvex = smallPath.isConvex();
429 SkPath largePath;
430 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath));
431 SkMatrix matrix;
432 matrix.reset();
433 matrix.preTranslate(100, 100);
434 matrix.preScale(scale, scale);
435 largePath.transform(matrix);
436 bool largeConvex = largePath.isConvex();
437 REPORTER_ASSERT(reporter, smallConvex == largeConvex);
438}
439
440static void test_crbug_493450(skiatest::Reporter* reporter) {
441 const char reducedCase[] =
442 "M0,0"
443 "L0.0002, 0"
444 "L0.0002, 0.0002"
445 "L0.0001, 0.0001"
446 "L0,0.0002"
447 "Z";
448 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000);
449 const char originalFiddleData[] =
450 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281"
451 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573"
452 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548"
453 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182"
454 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z";
455 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f,
456 826357.3384828606f);
457}
458
459static void test_crbug_495894(skiatest::Reporter* reporter) {
460 const char originalFiddleData[] =
461 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951"
462 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889"
463 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701"
464 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343"
465 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192"
466 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939"
467 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405"
468 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302"
469 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197"
470 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437"
471 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839"
472 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951"
473 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951"
474 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307"
475 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977"
476 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668"
477 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z"
478 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433"
479 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612"
480 "L-0.33993994441916414,-0.11288906492739594Z";
481 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f,
482 65536);
483}
484
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000485static void test_addrect(skiatest::Reporter* reporter) {
486 SkPath path;
487 path.lineTo(0, 0);
488 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700489 REPORTER_ASSERT(reporter, path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000490
491 path.reset();
492 path.lineTo(FLT_EPSILON, FLT_EPSILON);
493 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700494 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000495
496 path.reset();
497 path.quadTo(0, 0, 0, 0);
498 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700499 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000500
501 path.reset();
502 path.conicTo(0, 0, 0, 0, 0.5f);
503 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700504 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000505
506 path.reset();
507 path.cubicTo(0, 0, 0, 0, 0, 0);
508 path.addRect(SkRect::MakeWH(50, 100));
halcanary96fcdcc2015-08-27 07:41:13 -0700509 REPORTER_ASSERT(reporter, !path.isRect(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000510}
511
reed@google.coma8790de2012-10-24 21:04:04 +0000512// Make sure we stay non-finite once we get there (unless we reset or rewind).
513static void test_addrect_isfinite(skiatest::Reporter* reporter) {
514 SkPath path;
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000515
reed@google.coma8790de2012-10-24 21:04:04 +0000516 path.addRect(SkRect::MakeWH(50, 100));
517 REPORTER_ASSERT(reporter, path.isFinite());
518
519 path.moveTo(0, 0);
520 path.lineTo(SK_ScalarInfinity, 42);
521 REPORTER_ASSERT(reporter, !path.isFinite());
522
523 path.addRect(SkRect::MakeWH(50, 100));
524 REPORTER_ASSERT(reporter, !path.isFinite());
525
526 path.reset();
527 REPORTER_ASSERT(reporter, path.isFinite());
skia.committer@gmail.com8b0e2342012-10-25 02:01:20 +0000528
reed@google.coma8790de2012-10-24 21:04:04 +0000529 path.addRect(SkRect::MakeWH(50, 100));
530 REPORTER_ASSERT(reporter, path.isFinite());
531}
532
reed@google.com848148e2013-01-15 15:51:59 +0000533static void build_big_path(SkPath* path, bool reducedCase) {
534 if (reducedCase) {
535 path->moveTo(577330, 1971.72f);
536 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
537 } else {
538 path->moveTo(60.1631f, 7.70567f);
539 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f);
540 path->lineTo(577379, 1977.77f);
541 path->quadTo(577364, 1979.57f, 577325, 1980.26f);
542 path->quadTo(577286, 1980.95f, 577245, 1980.13f);
543 path->quadTo(577205, 1979.3f, 577187, 1977.45f);
544 path->quadTo(577168, 1975.6f, 577183, 1973.8f);
545 path->quadTo(577198, 1972, 577238, 1971.31f);
546 path->quadTo(577277, 1970.62f, 577317, 1971.45f);
547 path->quadTo(577330, 1971.72f, 577341, 1972.11f);
548 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f);
549 path->moveTo(306.718f, -32.912f);
550 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f);
551 }
552}
553
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000554static void test_clipped_cubic() {
reed3054be12014-12-10 07:24:28 -0800555 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480));
reed@google.com848148e2013-01-15 15:51:59 +0000556
557 // This path used to assert, because our cubic-chopping code incorrectly
558 // moved control points after the chop. This test should be run in SK_DEBUG
559 // mode to ensure that we no long assert.
560 SkPath path;
561 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) {
562 build_big_path(&path, SkToBool(doReducedCase));
skia.committer@gmail.comff21c2e2013-01-16 07:05:56 +0000563
reed@google.com848148e2013-01-15 15:51:59 +0000564 SkPaint paint;
565 for (int doAA = 0; doAA <= 1; ++doAA) {
566 paint.setAntiAlias(SkToBool(doAA));
567 surface->getCanvas()->drawPath(path, paint);
568 }
569 }
570}
571
reed3b2fb982015-07-29 08:37:13 -0700572static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) {
573 if (expected != bounds) {
574 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]",
575 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(),
576 expected.left(), expected.top(), expected.right(), expected.bottom());
577 }
578}
579
reed91f283b2015-07-28 06:00:50 -0700580static void test_bounds_crbug_513799(skiatest::Reporter* reporter) {
581 SkPath path;
reed3b2fb982015-07-29 08:37:13 -0700582#if 0
583 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've
584 // rewritten them to avoid this (compiler-bug?).
reed91f283b2015-07-28 06:00:50 -0700585 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds());
586
587 path.moveTo(-5, -8);
588 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds());
589
590 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4));
591 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
592
593 path.moveTo(1, 2);
594 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds());
reed3b2fb982015-07-29 08:37:13 -0700595#else
596 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds());
597
598 path.moveTo(-5, -8); // should set the bounds
599 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds());
600
601 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds
602 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
603
604 path.moveTo(1, 2); // don't expect this to have changed the bounds
605 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds());
606#endif
reed91f283b2015-07-28 06:00:50 -0700607}
608
reed@google.com8cae8352012-09-14 15:18:41 +0000609// Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/
610// which triggered an assert, from a tricky cubic. This test replicates that
611// example, so we can ensure that we handle it (in SkEdge.cpp), and don't
612// assert in the SK_DEBUG build.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000613static void test_tricky_cubic() {
reed@google.com8cae8352012-09-14 15:18:41 +0000614 const SkPoint pts[] = {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000615 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) },
616 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) },
617 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) },
618 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) },
reed@google.com8cae8352012-09-14 15:18:41 +0000619 };
620
621 SkPath path;
622 path.moveTo(pts[0]);
623 path.cubicTo(pts[1], pts[2], pts[3]);
624
625 SkPaint paint;
626 paint.setAntiAlias(true);
627
reed3054be12014-12-10 07:24:28 -0800628 SkSurface* surface = SkSurface::NewRasterN32Premul(19, 130);
reed@google.com8cae8352012-09-14 15:18:41 +0000629 surface->getCanvas()->drawPath(path, paint);
630 surface->unref();
631}
reed@android.com3abec1d2009-03-02 05:36:20 +0000632
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000633// Inspired by http://code.google.com/p/chromium/issues/detail?id=141651
634//
635static void test_isfinite_after_transform(skiatest::Reporter* reporter) {
636 SkPath path;
637 path.quadTo(157, 366, 286, 208);
638 path.arcTo(37, 442, 315, 163, 957494590897113.0f);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000639
tomhudson@google.comed02c4d2012-08-10 14:10:45 +0000640 SkMatrix matrix;
641 matrix.setScale(1000*1000, 1000*1000);
642
643 // Be sure that path::transform correctly updates isFinite and the bounds
644 // if the transformation overflows. The previous bug was that isFinite was
645 // set to true in this case, but the bounds were not set to empty (which
646 // they should be).
647 while (path.isFinite()) {
648 REPORTER_ASSERT(reporter, path.getBounds().isFinite());
649 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty());
650 path.transform(matrix);
651 }
652 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
653
654 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
655 path.transform(matrix);
656 // we need to still be non-finite
657 REPORTER_ASSERT(reporter, !path.isFinite());
658 REPORTER_ASSERT(reporter, path.getBounds().isEmpty());
659}
660
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000661static void add_corner_arc(SkPath* path, const SkRect& rect,
662 SkScalar xIn, SkScalar yIn,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000663 int startAngle)
664{
665
666 SkScalar rx = SkMinScalar(rect.width(), xIn);
667 SkScalar ry = SkMinScalar(rect.height(), yIn);
668
669 SkRect arcRect;
670 arcRect.set(-rx, -ry, rx, ry);
671 switch (startAngle) {
672 case 0:
673 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
674 break;
675 case 90:
676 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
677 break;
678 case 180:
679 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
680 break;
681 case 270:
682 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
683 break;
684 default:
685 break;
686 }
687
688 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
689}
690
skia.committer@gmail.com6a748ad2012-10-19 02:01:19 +0000691static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000692 SkScalar xCorner, SkScalar yCorner) {
693 // we are lazy here and use the same x & y for each corner
694 add_corner_arc(path, r, xCorner, yCorner, 270);
695 add_corner_arc(path, r, xCorner, yCorner, 0);
696 add_corner_arc(path, r, xCorner, yCorner, 90);
697 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000698 path->close();
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000699}
700
701// Chrome creates its own round rects with each corner possibly being different.
702// Performance will suffer if they are not convex.
703// Note: PathBench::ArbRoundRectBench performs almost exactly
704// the same test (but with drawing)
705static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000706 SkRandom rand;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000707 SkRect r;
708
709 for (int i = 0; i < 5000; ++i) {
710
robertphillips@google.com158618e2012-10-23 16:56:56 +0000711 SkScalar size = rand.nextUScalar1() * 30;
712 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000713 continue;
714 }
715 r.fLeft = rand.nextUScalar1() * 300;
716 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000717 r.fRight = r.fLeft + 2 * size;
718 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000719
720 SkPath temp;
721
722 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
723
724 REPORTER_ASSERT(reporter, temp.isConvex());
725 }
726}
727
robertphillips@google.com158618e2012-10-23 16:56:56 +0000728// Chrome will sometimes create a 0 radius round rect. The degenerate
729// quads prevent the path from being converted to a rect
730// Note: PathBench::ArbRoundRectBench performs almost exactly
731// the same test (but with drawing)
732static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000733 SkRandom rand;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000734 SkRect r;
735
736 for (int i = 0; i < 5000; ++i) {
737
738 SkScalar size = rand.nextUScalar1() * 30;
739 if (size < SK_Scalar1) {
740 continue;
741 }
742 r.fLeft = rand.nextUScalar1() * 300;
743 r.fTop = rand.nextUScalar1() * 300;
744 r.fRight = r.fLeft + 2 * size;
745 r.fBottom = r.fTop + 2 * size;
746
747 SkPath temp;
748
749 make_arb_round_rect(&temp, r, 0, 0);
750
robertphillips@google.com158618e2012-10-23 16:56:56 +0000751 SkRect result;
752 REPORTER_ASSERT(reporter, temp.isRect(&result));
753 REPORTER_ASSERT(reporter, r == result);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000754 }
755}
756
reed@google.com0bb18bb2012-07-26 15:20:36 +0000757static void test_rect_isfinite(skiatest::Reporter* reporter) {
758 const SkScalar inf = SK_ScalarInfinity;
caryclark@google.com7abfa492013-04-12 11:59:41 +0000759 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000760 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000761
reed@google.com0bb18bb2012-07-26 15:20:36 +0000762 SkRect r;
763 r.setEmpty();
764 REPORTER_ASSERT(reporter, r.isFinite());
caryclark@google.com7abfa492013-04-12 11:59:41 +0000765 r.set(0, 0, inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000766 REPORTER_ASSERT(reporter, !r.isFinite());
767 r.set(0, 0, nan, 0);
768 REPORTER_ASSERT(reporter, !r.isFinite());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000769
reed@google.com0bb18bb2012-07-26 15:20:36 +0000770 SkPoint pts[] = {
771 { 0, 0 },
772 { SK_Scalar1, 0 },
773 { 0, SK_Scalar1 },
774 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000775
reed@google.com0bb18bb2012-07-26 15:20:36 +0000776 bool isFine = r.setBoundsCheck(pts, 3);
777 REPORTER_ASSERT(reporter, isFine);
778 REPORTER_ASSERT(reporter, !r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000779
reed@google.com0bb18bb2012-07-26 15:20:36 +0000780 pts[1].set(inf, 0);
781 isFine = r.setBoundsCheck(pts, 3);
782 REPORTER_ASSERT(reporter, !isFine);
783 REPORTER_ASSERT(reporter, r.isEmpty());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000784
reed@google.com0bb18bb2012-07-26 15:20:36 +0000785 pts[1].set(nan, 0);
786 isFine = r.setBoundsCheck(pts, 3);
787 REPORTER_ASSERT(reporter, !isFine);
788 REPORTER_ASSERT(reporter, r.isEmpty());
789}
790
791static void test_path_isfinite(skiatest::Reporter* reporter) {
792 const SkScalar inf = SK_ScalarInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000793 const SkScalar negInf = SK_ScalarNegativeInfinity;
reed@google.com0bb18bb2012-07-26 15:20:36 +0000794 const SkScalar nan = SK_ScalarNaN;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000795
reed@google.com0bb18bb2012-07-26 15:20:36 +0000796 SkPath path;
797 REPORTER_ASSERT(reporter, path.isFinite());
798
799 path.reset();
800 REPORTER_ASSERT(reporter, path.isFinite());
801
802 path.reset();
803 path.moveTo(SK_Scalar1, 0);
804 REPORTER_ASSERT(reporter, path.isFinite());
805
806 path.reset();
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000807 path.moveTo(inf, negInf);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000808 REPORTER_ASSERT(reporter, !path.isFinite());
809
810 path.reset();
811 path.moveTo(nan, 0);
812 REPORTER_ASSERT(reporter, !path.isFinite());
813}
814
815static void test_isfinite(skiatest::Reporter* reporter) {
816 test_rect_isfinite(reporter);
817 test_path_isfinite(reporter);
818}
819
reed@google.com744faba2012-05-29 19:54:52 +0000820// assert that we always
821// start with a moveTo
822// only have 1 moveTo
823// only have Lines after that
824// end with a single close
825// only have (at most) 1 close
826//
827static void test_poly(skiatest::Reporter* reporter, const SkPath& path,
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000828 const SkPoint srcPts[], bool expectClose) {
reed@google.com744faba2012-05-29 19:54:52 +0000829 SkPath::RawIter iter(path);
830 SkPoint pts[4];
reed@google.com744faba2012-05-29 19:54:52 +0000831
832 bool firstTime = true;
833 bool foundClose = false;
834 for (;;) {
835 switch (iter.next(pts)) {
836 case SkPath::kMove_Verb:
837 REPORTER_ASSERT(reporter, firstTime);
838 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]);
839 srcPts++;
840 firstTime = false;
841 break;
842 case SkPath::kLine_Verb:
843 REPORTER_ASSERT(reporter, !firstTime);
844 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]);
845 srcPts++;
846 break;
847 case SkPath::kQuad_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000848 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb");
reed@google.com744faba2012-05-29 19:54:52 +0000849 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000850 case SkPath::kConic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000851 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb");
reed@google.com277c3f82013-05-31 15:17:50 +0000852 break;
reed@google.com744faba2012-05-29 19:54:52 +0000853 case SkPath::kCubic_Verb:
mtklein@google.com330313a2013-08-22 15:37:26 +0000854 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb");
reed@google.com744faba2012-05-29 19:54:52 +0000855 break;
856 case SkPath::kClose_Verb:
857 REPORTER_ASSERT(reporter, !firstTime);
858 REPORTER_ASSERT(reporter, !foundClose);
859 REPORTER_ASSERT(reporter, expectClose);
860 foundClose = true;
861 break;
862 case SkPath::kDone_Verb:
863 goto DONE;
864 }
865 }
866DONE:
867 REPORTER_ASSERT(reporter, foundClose == expectClose);
868}
869
870static void test_addPoly(skiatest::Reporter* reporter) {
871 SkPoint pts[32];
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000872 SkRandom rand;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000873
reed@google.com744faba2012-05-29 19:54:52 +0000874 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) {
875 pts[i].fX = rand.nextSScalar1();
876 pts[i].fY = rand.nextSScalar1();
877 }
878
879 for (int doClose = 0; doClose <= 1; ++doClose) {
880 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) {
881 SkPath path;
bsalomon98806072014-12-12 15:11:17 -0800882 path.addPoly(pts, SkToInt(count), SkToBool(doClose));
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000883 test_poly(reporter, path, pts, SkToBool(doClose));
reed@google.com744faba2012-05-29 19:54:52 +0000884 }
885 }
886}
887
reed@google.com8b06f1a2012-05-29 12:03:46 +0000888static void test_strokerec(skiatest::Reporter* reporter) {
889 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
890 REPORTER_ASSERT(reporter, rec.isFillStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891
reed@google.com8b06f1a2012-05-29 12:03:46 +0000892 rec.setHairlineStyle();
893 REPORTER_ASSERT(reporter, rec.isHairlineStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000894
reed@google.com8b06f1a2012-05-29 12:03:46 +0000895 rec.setStrokeStyle(SK_Scalar1, false);
896 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000897
reed@google.com8b06f1a2012-05-29 12:03:46 +0000898 rec.setStrokeStyle(SK_Scalar1, true);
899 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000900
reed@google.com8b06f1a2012-05-29 12:03:46 +0000901 rec.setStrokeStyle(0, false);
902 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000903
reed@google.com8b06f1a2012-05-29 12:03:46 +0000904 rec.setStrokeStyle(0, true);
905 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle());
906}
907
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000908// Set this for paths that don't have a consistent direction such as a bowtie.
909// (cheapComputeDirection is not expected to catch these.)
reed026beb52015-06-10 14:23:15 -0700910const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000911
912static void check_direction(skiatest::Reporter* reporter, const SkPath& path,
reed026beb52015-06-10 14:23:15 -0700913 SkPathPriv::FirstDirection expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000914 if (expected == kDontCheckDir) {
915 return;
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000916 }
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000917 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
918
reed026beb52015-06-10 14:23:15 -0700919 SkPathPriv::FirstDirection dir;
920 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000921 REPORTER_ASSERT(reporter, dir == expected);
922 } else {
reed026beb52015-06-10 14:23:15 -0700923 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000924 }
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000925}
926
reed@google.com3e71a882012-01-10 18:44:37 +0000927static void test_direction(skiatest::Reporter* reporter) {
928 size_t i;
929 SkPath path;
halcanary96fcdcc2015-08-27 07:41:13 -0700930 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed026beb52015-06-10 14:23:15 -0700931 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
932 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
933 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection));
reed@google.com3e71a882012-01-10 18:44:37 +0000934
935 static const char* gDegen[] = {
936 "M 10 10",
937 "M 10 10 M 20 20",
938 "M 10 10 L 20 20",
939 "M 10 10 L 10 10 L 10 10",
940 "M 10 10 Q 10 10 10 10",
941 "M 10 10 C 10 10 10 10 10 10",
942 };
943 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) {
944 path.reset();
945 bool valid = SkParsePath::FromSVGString(gDegen[i], &path);
946 REPORTER_ASSERT(reporter, valid);
halcanary96fcdcc2015-08-27 07:41:13 -0700947 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr));
reed@google.com3e71a882012-01-10 18:44:37 +0000948 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000949
reed@google.com3e71a882012-01-10 18:44:37 +0000950 static const char* gCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000951 "M 10 10 L 10 10 Q 20 10 20 20",
reed@google.com3e71a882012-01-10 18:44:37 +0000952 "M 10 10 C 20 10 20 20 20 20",
reed@google.comd4146662012-01-31 15:42:29 +0000953 "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 +0000954 // rect with top two corners replaced by cubics with identical middle
955 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000956 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10",
957 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000958 };
959 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) {
960 path.reset();
961 bool valid = SkParsePath::FromSVGString(gCW[i], &path);
962 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700963 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000964 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000965
reed@google.com3e71a882012-01-10 18:44:37 +0000966 static const char* gCCW[] = {
reed@google.comcabaf1d2012-01-11 21:03:05 +0000967 "M 10 10 L 10 10 Q 20 10 20 -20",
reed@google.com3e71a882012-01-10 18:44:37 +0000968 "M 10 10 C 20 10 20 -20 20 -20",
reed@google.comd4146662012-01-31 15:42:29 +0000969 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max
bsalomon@google.com4eefe612012-07-10 18:28:12 +0000970 // rect with top two corners replaced by cubics with identical middle
971 // control points
reed@google.com20fb0c72012-11-13 13:47:39 +0000972 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10",
973 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif
reed@google.com3e71a882012-01-10 18:44:37 +0000974 };
975 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) {
976 path.reset();
977 bool valid = SkParsePath::FromSVGString(gCCW[i], &path);
978 REPORTER_ASSERT(reporter, valid);
reed026beb52015-06-10 14:23:15 -0700979 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +0000980 }
reed@google.comac8543f2012-01-30 20:51:25 +0000981
982 // Test two donuts, each wound a different direction. Only the outer contour
983 // determines the cheap direction
984 path.reset();
985 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction);
986 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -0700987 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000988
reed@google.comac8543f2012-01-30 20:51:25 +0000989 path.reset();
990 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction);
991 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction);
reed026beb52015-06-10 14:23:15 -0700992 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
bsalomon@google.comf0ed80a2012-02-17 13:38:26 +0000993
994 // triangle with one point really far from the origin.
995 path.reset();
996 // the first point is roughly 1.05e10, 1.05e10
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000997 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652));
bsalomon@google.com53aab782012-02-23 14:54:49 +0000998 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1);
999 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1);
reed026beb52015-06-10 14:23:15 -07001000 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001001
1002 path.reset();
1003 path.conicTo(20, 0, 20, 20, 0.5f);
1004 path.close();
reed026beb52015-06-10 14:23:15 -07001005 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001006
1007 path.reset();
1008 path.lineTo(1, 1e7f);
1009 path.lineTo(1e7f, 2e7f);
1010 path.close();
1011 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity());
reed026beb52015-06-10 14:23:15 -07001012 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection);
reed@google.com3e71a882012-01-10 18:44:37 +00001013}
1014
reed@google.comffdb0182011-11-14 19:29:14 +00001015static void add_rect(SkPath* path, const SkRect& r) {
1016 path->moveTo(r.fLeft, r.fTop);
1017 path->lineTo(r.fRight, r.fTop);
1018 path->lineTo(r.fRight, r.fBottom);
1019 path->lineTo(r.fLeft, r.fBottom);
1020 path->close();
1021}
1022
1023static void test_bounds(skiatest::Reporter* reporter) {
1024 static const SkRect rects[] = {
reed@google.com3563c9e2011-11-14 19:34:57 +00001025 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) },
1026 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) },
1027 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) },
1028 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) },
reed@google.comffdb0182011-11-14 19:29:14 +00001029 };
1030
1031 SkPath path0, path1;
1032 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) {
1033 path0.addRect(rects[i]);
1034 add_rect(&path1, rects[i]);
1035 }
1036
1037 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds());
1038}
1039
reed@google.com55b5f4b2011-09-07 12:23:41 +00001040static void stroke_cubic(const SkPoint pts[4]) {
1041 SkPath path;
1042 path.moveTo(pts[0]);
1043 path.cubicTo(pts[1], pts[2], pts[3]);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001044
reed@google.com55b5f4b2011-09-07 12:23:41 +00001045 SkPaint paint;
1046 paint.setStyle(SkPaint::kStroke_Style);
1047 paint.setStrokeWidth(SK_Scalar1 * 2);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001048
reed@google.com55b5f4b2011-09-07 12:23:41 +00001049 SkPath fill;
1050 paint.getFillPath(path, &fill);
1051}
1052
1053// just ensure this can run w/o any SkASSERTS firing in the debug build
1054// we used to assert due to differences in how we determine a degenerate vector
1055// but that was fixed with the introduction of SkPoint::CanNormalize
1056static void stroke_tiny_cubic() {
1057 SkPoint p0[] = {
1058 { 372.0f, 92.0f },
1059 { 372.0f, 92.0f },
1060 { 372.0f, 92.0f },
1061 { 372.0f, 92.0f },
1062 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001063
reed@google.com55b5f4b2011-09-07 12:23:41 +00001064 stroke_cubic(p0);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001065
reed@google.com55b5f4b2011-09-07 12:23:41 +00001066 SkPoint p1[] = {
1067 { 372.0f, 92.0f },
1068 { 372.0007f, 92.000755f },
1069 { 371.99927f, 92.003922f },
1070 { 371.99826f, 92.003899f },
1071 };
rmistry@google.comd6176b02012-08-23 18:14:13 +00001072
reed@google.com55b5f4b2011-09-07 12:23:41 +00001073 stroke_cubic(p1);
1074}
1075
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001076static void check_close(skiatest::Reporter* reporter, const SkPath& path) {
1077 for (int i = 0; i < 2; ++i) {
robertphillips@google.com09042b82012-04-06 20:01:46 +00001078 SkPath::Iter iter(path, SkToBool(i));
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001079 SkPoint mv;
1080 SkPoint pts[4];
1081 SkPath::Verb v;
1082 int nMT = 0;
1083 int nCL = 0;
tomhudson@google.com221db3c2011-07-28 21:10:29 +00001084 mv.set(0, 0);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001085 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
1086 switch (v) {
1087 case SkPath::kMove_Verb:
1088 mv = pts[0];
1089 ++nMT;
1090 break;
1091 case SkPath::kClose_Verb:
1092 REPORTER_ASSERT(reporter, mv == pts[0]);
1093 ++nCL;
1094 break;
1095 default:
1096 break;
1097 }
1098 }
1099 // if we force a close on the interator we should have a close
1100 // for every moveTo
1101 REPORTER_ASSERT(reporter, !i || nMT == nCL);
1102 }
1103}
1104
1105static void test_close(skiatest::Reporter* reporter) {
1106 SkPath closePt;
1107 closePt.moveTo(0, 0);
1108 closePt.close();
1109 check_close(reporter, closePt);
1110
1111 SkPath openPt;
1112 openPt.moveTo(0, 0);
1113 check_close(reporter, openPt);
1114
1115 SkPath empty;
1116 check_close(reporter, empty);
1117 empty.close();
1118 check_close(reporter, empty);
1119
1120 SkPath rect;
1121 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1122 check_close(reporter, rect);
1123 rect.close();
1124 check_close(reporter, rect);
1125
1126 SkPath quad;
1127 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1128 check_close(reporter, quad);
1129 quad.close();
1130 check_close(reporter, quad);
1131
1132 SkPath cubic;
rmistry@google.comd6176b02012-08-23 18:14:13 +00001133 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1,
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001134 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1);
1135 check_close(reporter, cubic);
1136 cubic.close();
1137 check_close(reporter, cubic);
1138
1139 SkPath line;
1140 line.moveTo(SK_Scalar1, SK_Scalar1);
1141 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1);
1142 check_close(reporter, line);
1143 line.close();
1144 check_close(reporter, line);
1145
1146 SkPath rect2;
1147 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1148 rect2.close();
1149 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1);
1150 check_close(reporter, rect2);
1151 rect2.close();
1152 check_close(reporter, rect2);
1153
1154 SkPath oval3;
1155 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100));
1156 oval3.close();
1157 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200));
1158 check_close(reporter, oval3);
1159 oval3.close();
1160 check_close(reporter, oval3);
1161
1162 SkPath moves;
1163 moves.moveTo(SK_Scalar1, SK_Scalar1);
1164 moves.moveTo(5 * SK_Scalar1, SK_Scalar1);
1165 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1);
1166 moves.moveTo(10 *SK_Scalar1, SK_Scalar1);
1167 check_close(reporter, moves);
reed@google.com55b5f4b2011-09-07 12:23:41 +00001168
1169 stroke_tiny_cubic();
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00001170}
1171
reed@google.com7c424812011-05-15 04:38:34 +00001172static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
1173 SkPath::Convexity expected) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001174 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path.
1175 SkPath::Convexity c = copy.getConvexity();
reed@google.com7c424812011-05-15 04:38:34 +00001176 REPORTER_ASSERT(reporter, c == expected);
1177}
1178
caryclarkb4216502015-03-02 13:02:34 -08001179static void test_path_crbug389050(skiatest::Reporter* reporter) {
1180 SkPath tinyConvexPolygon;
1181 tinyConvexPolygon.moveTo(600.131559f, 800.112512f);
1182 tinyConvexPolygon.lineTo(600.161735f, 800.118627f);
1183 tinyConvexPolygon.lineTo(600.148962f, 800.142338f);
1184 tinyConvexPolygon.lineTo(600.134891f, 800.137724f);
1185 tinyConvexPolygon.close();
1186 tinyConvexPolygon.getConvexity();
1187 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001188 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001189
1190 SkPath platTriangle;
1191 platTriangle.moveTo(0, 0);
1192 platTriangle.lineTo(200, 0);
1193 platTriangle.lineTo(100, 0.04f);
1194 platTriangle.close();
1195 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001196 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001197
1198 platTriangle.reset();
1199 platTriangle.moveTo(0, 0);
1200 platTriangle.lineTo(200, 0);
1201 platTriangle.lineTo(100, 0.03f);
1202 platTriangle.close();
1203 platTriangle.getConvexity();
reed026beb52015-06-10 14:23:15 -07001204 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection);
caryclarkb4216502015-03-02 13:02:34 -08001205}
1206
reed@google.com7c424812011-05-15 04:38:34 +00001207static void test_convexity2(skiatest::Reporter* reporter) {
1208 SkPath pt;
1209 pt.moveTo(0, 0);
1210 pt.close();
reed@google.comb54455e2011-05-16 14:16:04 +00001211 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001212 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001213
reed@google.com7c424812011-05-15 04:38:34 +00001214 SkPath line;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001215 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1);
1216 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001217 line.close();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001218 check_convexity(reporter, line, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001219 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001220
reed@google.com7c424812011-05-15 04:38:34 +00001221 SkPath triLeft;
1222 triLeft.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001223 triLeft.lineTo(SK_Scalar1, 0);
1224 triLeft.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001225 triLeft.close();
1226 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001227 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001228
reed@google.com7c424812011-05-15 04:38:34 +00001229 SkPath triRight;
1230 triRight.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001231 triRight.lineTo(-SK_Scalar1, 0);
1232 triRight.lineTo(SK_Scalar1, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001233 triRight.close();
1234 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001235 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001236
reed@google.com7c424812011-05-15 04:38:34 +00001237 SkPath square;
1238 square.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001239 square.lineTo(SK_Scalar1, 0);
1240 square.lineTo(SK_Scalar1, SK_Scalar1);
1241 square.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001242 square.close();
1243 check_convexity(reporter, square, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001244 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001245
reed@google.com7c424812011-05-15 04:38:34 +00001246 SkPath redundantSquare;
1247 redundantSquare.moveTo(0, 0);
1248 redundantSquare.lineTo(0, 0);
1249 redundantSquare.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001250 redundantSquare.lineTo(SK_Scalar1, 0);
1251 redundantSquare.lineTo(SK_Scalar1, 0);
1252 redundantSquare.lineTo(SK_Scalar1, 0);
1253 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1254 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1255 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1);
1256 redundantSquare.lineTo(0, SK_Scalar1);
1257 redundantSquare.lineTo(0, SK_Scalar1);
1258 redundantSquare.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001259 redundantSquare.close();
1260 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001261 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001262
reed@google.com7c424812011-05-15 04:38:34 +00001263 SkPath bowTie;
1264 bowTie.moveTo(0, 0);
1265 bowTie.lineTo(0, 0);
1266 bowTie.lineTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001267 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1268 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1269 bowTie.lineTo(SK_Scalar1, SK_Scalar1);
1270 bowTie.lineTo(SK_Scalar1, 0);
1271 bowTie.lineTo(SK_Scalar1, 0);
1272 bowTie.lineTo(SK_Scalar1, 0);
1273 bowTie.lineTo(0, SK_Scalar1);
1274 bowTie.lineTo(0, SK_Scalar1);
1275 bowTie.lineTo(0, SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001276 bowTie.close();
1277 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001278 check_direction(reporter, bowTie, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001279
reed@google.com7c424812011-05-15 04:38:34 +00001280 SkPath spiral;
1281 spiral.moveTo(0, 0);
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001282 spiral.lineTo(100*SK_Scalar1, 0);
1283 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1284 spiral.lineTo(0, 100*SK_Scalar1);
1285 spiral.lineTo(0, 50*SK_Scalar1);
1286 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1);
1287 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001288 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +00001289 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001290 check_direction(reporter, spiral, kDontCheckDir);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001291
reed@google.com7c424812011-05-15 04:38:34 +00001292 SkPath dent;
schenney@chromium.org6c31d9d2011-12-20 16:33:30 +00001293 dent.moveTo(0, 0);
1294 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1);
1295 dent.lineTo(0, 100*SK_Scalar1);
1296 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1);
1297 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1);
reed@google.com7c424812011-05-15 04:38:34 +00001298 dent.close();
1299 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001300 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection);
commit-bot@chromium.org8be07bb2014-05-22 14:58:53 +00001301
1302 // http://skbug.com/2235
1303 SkPath strokedSin;
1304 for (int i = 0; i < 2000; i++) {
1305 SkScalar x = SkIntToScalar(i) / 2;
1306 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3;
1307 if (0 == i) {
1308 strokedSin.moveTo(x, y);
1309 } else {
1310 strokedSin.lineTo(x, y);
1311 }
1312 }
1313 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1314 stroke.setStrokeStyle(2 * SK_Scalar1);
1315 stroke.applyToPath(&strokedSin, strokedSin);
1316 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity);
1317 check_direction(reporter, strokedSin, kDontCheckDir);
robertphillipsc506e302014-09-16 09:43:31 -07001318
1319 // http://crbug.com/412640
1320 SkPath degenerateConcave;
1321 degenerateConcave.moveTo(148.67912f, 191.875f);
1322 degenerateConcave.lineTo(470.37695f, 7.5f);
1323 degenerateConcave.lineTo(148.67912f, 191.875f);
1324 degenerateConcave.lineTo(41.446522f, 376.25f);
1325 degenerateConcave.lineTo(-55.971577f, 460.0f);
1326 degenerateConcave.lineTo(41.446522f, 376.25f);
1327 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity);
reed026beb52015-06-10 14:23:15 -07001328 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection);
robertphillipsf52a0632014-11-17 12:11:42 -08001329
1330 // http://crbug.com/433683
1331 SkPath badFirstVector;
1332 badFirstVector.moveTo(501.087708f, 319.610352f);
1333 badFirstVector.lineTo(501.087708f, 319.610352f);
1334 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f);
1335 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f);
1336 badFirstVector.lineTo(301.557678f, 98.044601f);
1337 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f);
1338 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f);
1339 badFirstVector.lineTo(504.912292f, 316.389648f);
1340 badFirstVector.lineTo(501.087708f, 319.610352f);
1341 badFirstVector.close();
1342 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +00001343}
1344
reed@android.com6b82d1a2009-06-03 02:35:01 +00001345static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
1346 const SkRect& bounds) {
1347 REPORTER_ASSERT(reporter, p.isConvex());
1348 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +00001349
reed@android.com6b82d1a2009-06-03 02:35:01 +00001350 SkPath p2(p);
1351 REPORTER_ASSERT(reporter, p2.isConvex());
1352 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
1353
1354 SkPath other;
1355 other.swap(p2);
1356 REPORTER_ASSERT(reporter, other.isConvex());
1357 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
1358}
1359
reed@google.com04863fa2011-05-15 04:08:24 +00001360static void setFromString(SkPath* path, const char str[]) {
1361 bool first = true;
1362 while (str) {
1363 SkScalar x, y;
1364 str = SkParse::FindScalar(str, &x);
halcanary96fcdcc2015-08-27 07:41:13 -07001365 if (nullptr == str) {
reed@google.com04863fa2011-05-15 04:08:24 +00001366 break;
1367 }
1368 str = SkParse::FindScalar(str, &y);
1369 SkASSERT(str);
1370 if (first) {
1371 path->moveTo(x, y);
1372 first = false;
1373 } else {
1374 path->lineTo(x, y);
1375 }
1376 }
1377}
1378
1379static void test_convexity(skiatest::Reporter* reporter) {
reed@google.com04863fa2011-05-15 04:08:24 +00001380 SkPath path;
1381
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001382 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001383 path.addCircle(0, 0, SkIntToScalar(10));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001384 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed@google.come3543972012-01-10 18:59:22 +00001385 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001386 check_convexity(reporter, path, SkPath::kConcave_Convexity);
1387
reed@google.com04863fa2011-05-15 04:08:24 +00001388 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001389 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001390 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001391 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection));
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001392
reed@google.com04863fa2011-05-15 04:08:24 +00001393 path.reset();
reed@google.come3543972012-01-10 18:59:22 +00001394 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001395 check_convexity(reporter, path, SkPath::kConvex_Convexity);
reed026beb52015-06-10 14:23:15 -07001396 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001397
reed@google.com04863fa2011-05-15 04:08:24 +00001398 static const struct {
reed026beb52015-06-10 14:23:15 -07001399 const char* fPathStr;
1400 SkPath::Convexity fExpectedConvexity;
1401 SkPathPriv::FirstDirection fExpectedDirection;
reed@google.com04863fa2011-05-15 04:08:24 +00001402 } gRec[] = {
reed026beb52015-06-10 14:23:15 -07001403 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1404 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1405 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection },
1406 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection },
1407 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection },
1408 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection },
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001409 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir },
reed026beb52015-06-10 14:23:15 -07001410 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection },
reed@google.com04863fa2011-05-15 04:08:24 +00001411 };
1412
1413 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
1414 SkPath path;
1415 setFromString(&path, gRec[i].fPathStr);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00001416 check_convexity(reporter, path, gRec[i].fExpectedConvexity);
1417 check_direction(reporter, path, gRec[i].fExpectedDirection);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001418 // check after setting the initial convex and direction
1419 if (kDontCheckDir != gRec[i].fExpectedDirection) {
1420 SkPath copy(path);
reed026beb52015-06-10 14:23:15 -07001421 SkPathPriv::FirstDirection dir;
1422 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir);
1423 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection)
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001424 ^ foundDir);
1425 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir);
1426 check_convexity(reporter, copy, gRec[i].fExpectedConvexity);
1427 }
1428 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity());
1429 check_direction(reporter, path, gRec[i].fExpectedDirection);
reed@google.com04863fa2011-05-15 04:08:24 +00001430 }
caryclarkd3d1a982014-12-08 04:57:38 -08001431
1432 static const SkPoint nonFinitePts[] = {
1433 { SK_ScalarInfinity, 0 },
1434 { 0, SK_ScalarInfinity },
1435 { SK_ScalarInfinity, SK_ScalarInfinity },
1436 { SK_ScalarNegativeInfinity, 0},
1437 { 0, SK_ScalarNegativeInfinity },
1438 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity },
1439 { SK_ScalarNegativeInfinity, SK_ScalarInfinity },
1440 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
1441 { SK_ScalarNaN, 0 },
1442 { 0, SK_ScalarNaN },
1443 { SK_ScalarNaN, SK_ScalarNaN },
1444 };
1445
1446 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]);
1447
1448 static const SkPoint finitePts[] = {
1449 { SK_ScalarMax, 0 },
1450 { 0, SK_ScalarMax },
1451 { SK_ScalarMax, SK_ScalarMax },
1452 { SK_ScalarMin, 0 },
1453 { 0, SK_ScalarMin },
1454 { SK_ScalarMin, SK_ScalarMin },
1455 };
1456
1457 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]);
1458
1459 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) {
1460 int i = (int) (index % nonFinitePtsCount);
1461 int f = (int) (index % finitePtsCount);
1462 int g = (int) ((f + 1) % finitePtsCount);
1463 path.reset();
1464 switch (index % 13) {
1465 case 0: path.lineTo(nonFinitePts[i]); break;
1466 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break;
1467 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break;
1468 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break;
1469 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break;
1470 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break;
1471 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break;
1472 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break;
1473 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break;
1474 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break;
1475 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break;
1476 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break;
1477 case 12: path.moveTo(nonFinitePts[i]); break;
1478 }
1479 check_convexity(reporter, path, SkPath::kUnknown_Convexity);
1480 }
1481
1482 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) {
1483 int f = (int) (index % finitePtsCount);
1484 int g = (int) ((f + 1) % finitePtsCount);
1485 path.reset();
1486 int curveSelect = index % 11;
1487 switch (curveSelect) {
1488 case 0: path.moveTo(finitePts[f]); break;
1489 case 1: path.lineTo(finitePts[f]); break;
1490 case 2: path.quadTo(finitePts[f], finitePts[f]); break;
1491 case 3: path.quadTo(finitePts[f], finitePts[g]); break;
1492 case 4: path.quadTo(finitePts[g], finitePts[f]); break;
1493 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break;
1494 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break;
1495 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break;
1496 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break;
1497 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break;
1498 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break;
1499 }
1500 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity
1501 : SkPath::kUnknown_Convexity);
1502 }
1503
reed@google.com04863fa2011-05-15 04:08:24 +00001504}
1505
reed@google.com7e6c4d12012-05-10 14:05:43 +00001506static void test_isLine(skiatest::Reporter* reporter) {
1507 SkPath path;
1508 SkPoint pts[2];
1509 const SkScalar value = SkIntToScalar(5);
1510
halcanary96fcdcc2015-08-27 07:41:13 -07001511 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001512
reed@google.com7e6c4d12012-05-10 14:05:43 +00001513 // set some non-zero values
1514 pts[0].set(value, value);
1515 pts[1].set(value, value);
1516 REPORTER_ASSERT(reporter, !path.isLine(pts));
1517 // check that pts was untouched
1518 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1519 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1520
1521 const SkScalar moveX = SkIntToScalar(1);
1522 const SkScalar moveY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001523 REPORTER_ASSERT(reporter, value != moveX && value != moveY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001524
1525 path.moveTo(moveX, moveY);
halcanary96fcdcc2015-08-27 07:41:13 -07001526 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001527 REPORTER_ASSERT(reporter, !path.isLine(pts));
1528 // check that pts was untouched
1529 REPORTER_ASSERT(reporter, pts[0].equals(value, value));
1530 REPORTER_ASSERT(reporter, pts[1].equals(value, value));
1531
1532 const SkScalar lineX = SkIntToScalar(2);
1533 const SkScalar lineY = SkIntToScalar(2);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001534 REPORTER_ASSERT(reporter, value != lineX && value != lineY);
reed@google.com7e6c4d12012-05-10 14:05:43 +00001535
1536 path.lineTo(lineX, lineY);
halcanary96fcdcc2015-08-27 07:41:13 -07001537 REPORTER_ASSERT(reporter, path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001538
1539 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY));
1540 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY));
1541 REPORTER_ASSERT(reporter, path.isLine(pts));
1542 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1543 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
1544
1545 path.lineTo(0, 0); // too many points/verbs
halcanary96fcdcc2015-08-27 07:41:13 -07001546 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001547 REPORTER_ASSERT(reporter, !path.isLine(pts));
1548 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY));
1549 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001550
1551 path.reset();
1552 path.quadTo(1, 1, 2, 2);
halcanary96fcdcc2015-08-27 07:41:13 -07001553 REPORTER_ASSERT(reporter, !path.isLine(nullptr));
reed@google.com7e6c4d12012-05-10 14:05:43 +00001554}
1555
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001556static void test_conservativelyContains(skiatest::Reporter* reporter) {
1557 SkPath path;
1558
1559 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect.
1560 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1561
1562 // A circle that bounds kBaseRect (with a significant amount of slop)
1563 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height());
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001564 circleR = SkScalarMul(circleR, 1.75f) / 2;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001565 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()};
1566
1567 // round-rect radii
1568 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)};
skia.committer@gmail.comcec8de62012-11-14 02:01:22 +00001569
caryclark@google.com56f233a2012-11-19 13:06:06 +00001570 static const struct SUPPRESS_VISIBILITY_WARNING {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001571 SkRect fQueryRect;
1572 bool fInRect;
1573 bool fInCircle;
1574 bool fInRR;
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001575 bool fInCubicRR;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001576 } kQueries[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001577 {kBaseRect, true, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001578
1579 // rect well inside of kBaseRect
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001580 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(),
1581 kBaseRect.fTop + 0.25f*kBaseRect.height(),
1582 kBaseRect.fRight - 0.25f*kBaseRect.width(),
1583 kBaseRect.fBottom - 0.25f*kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001584 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001585
1586 // rects with edges off by one from kBaseRect's edges
1587 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1588 kBaseRect.width(), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001589 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001590 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1591 kBaseRect.width() + 1, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001592 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001593 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop,
1594 kBaseRect.width() + 1, kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001595 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001596 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1597 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001598 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001599 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1600 kBaseRect.width(), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001601 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001602 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop,
1603 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001604 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001605 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1,
1606 kBaseRect.width() + 2, kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001607 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001608
1609 // zero-w/h rects at each corner of kBaseRect
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001610 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false},
1611 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true},
1612 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true},
1613 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001614
1615 // far away rect
1616 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom,
1617 SkIntToScalar(10), SkIntToScalar(10)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001618 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001619
1620 // very large rect containing kBaseRect
1621 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(),
1622 kBaseRect.fTop - 5 * kBaseRect.height(),
1623 11 * kBaseRect.width(), 11 * kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001624 false, false, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001625
1626 // skinny rect that spans same y-range as kBaseRect
1627 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1628 SkIntToScalar(1), kBaseRect.height()),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001629 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001630
1631 // short rect that spans same x-range as kBaseRect
1632 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001633 true, true, true, true},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001634
1635 // skinny rect that spans slightly larger y-range than kBaseRect
1636 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop,
1637 SkIntToScalar(1), kBaseRect.height() + 1),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001638 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001639
1640 // short rect that spans slightly larger x-range than kBaseRect
1641 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(),
1642 kBaseRect.width() + 1, SkScalar(1)),
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001643 false, true, false, false},
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001644 };
1645
1646 for (int inv = 0; inv < 4; ++inv) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001647 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001648 SkRect qRect = kQueries[q].fQueryRect;
1649 if (inv & 0x1) {
1650 SkTSwap(qRect.fLeft, qRect.fRight);
1651 }
1652 if (inv & 0x2) {
1653 SkTSwap(qRect.fTop, qRect.fBottom);
1654 }
1655 for (int d = 0; d < 2; ++d) {
1656 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction;
1657 path.reset();
1658 path.addRect(kBaseRect, dir);
1659 REPORTER_ASSERT(reporter, kQueries[q].fInRect ==
1660 path.conservativelyContainsRect(qRect));
1661
1662 path.reset();
1663 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir);
1664 REPORTER_ASSERT(reporter, kQueries[q].fInCircle ==
1665 path.conservativelyContainsRect(qRect));
1666
1667 path.reset();
1668 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir);
1669 REPORTER_ASSERT(reporter, kQueries[q].fInRR ==
1670 path.conservativelyContainsRect(qRect));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001671
1672 path.reset();
1673 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop);
1674 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop,
1675 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2,
1676 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]);
1677 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom);
1678 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom);
1679 path.lineTo(kBaseRect.fRight, kBaseRect.fTop);
1680 path.close();
1681 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR ==
1682 path.conservativelyContainsRect(qRect));
1683
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001684 }
1685 // Slightly non-convex shape, shouldn't contain any rects.
1686 path.reset();
1687 path.moveTo(0, 0);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00001688 path.lineTo(SkIntToScalar(50), 0.05f);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001689 path.lineTo(SkIntToScalar(100), 0);
1690 path.lineTo(SkIntToScalar(100), SkIntToScalar(100));
1691 path.lineTo(0, SkIntToScalar(100));
1692 path.close();
1693 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect));
1694 }
1695 }
1696
1697 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes.
1698 path.reset();
1699 path.moveTo(0, 0);
1700 path.lineTo(SkIntToScalar(100), 0);
1701 path.lineTo(0, SkIntToScalar(100));
1702
1703 // inside, on along top edge
1704 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1705 SkIntToScalar(10),
1706 SkIntToScalar(10))));
1707 // above
1708 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1709 SkRect::MakeXYWH(SkIntToScalar(50),
1710 SkIntToScalar(-10),
1711 SkIntToScalar(10),
1712 SkIntToScalar(10))));
1713 // to the left
1714 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10),
1715 SkIntToScalar(5),
1716 SkIntToScalar(5),
1717 SkIntToScalar(5))));
1718
1719 // outside the diagonal edge
1720 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10),
1721 SkIntToScalar(200),
1722 SkIntToScalar(20),
1723 SkIntToScalar(5))));
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001724
bsalomonb17c1292014-08-28 14:04:55 -07001725
1726 // Test that multiple move commands do not cause asserts.
1727
1728 // At the time of writing, this would not modify cached convexity. This caused an assert while
1729 // checking conservative containment again. http://skbug.com/1460
1730 path.moveTo(SkIntToScalar(100), SkIntToScalar(100));
1731#if 0
1732 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1733 SkIntToScalar(10),
1734 SkIntToScalar(10))));
1735#endif
1736
1737 // Same as above path and first test but with an extra moveTo.
commit-bot@chromium.org62df5262013-08-01 15:35:06 +00001738 path.reset();
1739 path.moveTo(100, 100);
1740 path.moveTo(0, 0);
1741 path.lineTo(SkIntToScalar(100), 0);
1742 path.lineTo(0, SkIntToScalar(100));
1743
1744 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0,
1745 SkIntToScalar(10),
1746 SkIntToScalar(10))));
1747
bsalomonb17c1292014-08-28 14:04:55 -07001748 // Test that multiple move commands do not cause asserts and that the function
1749 // is not confused by the multiple moves.
1750 path.reset();
1751 path.moveTo(0, 0);
1752 path.lineTo(SkIntToScalar(100), 0);
1753 path.lineTo(0, SkIntToScalar(100));
1754 path.moveTo(0, SkIntToScalar(200));
1755 path.lineTo(SkIntToScalar(100), SkIntToScalar(200));
1756 path.lineTo(0, SkIntToScalar(300));
1757
1758 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(
1759 SkRect::MakeXYWH(SkIntToScalar(50), 0,
1760 SkIntToScalar(10),
1761 SkIntToScalar(10))));
1762
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00001763 path.reset();
1764 path.lineTo(100, 100);
1765 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1)));
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001766}
1767
reed@google.comf32322b2013-10-16 15:14:04 +00001768static void test_isRect_open_close(skiatest::Reporter* reporter) {
1769 SkPath path;
1770 bool isClosed;
1771
1772 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1);
reed@google.comf32322b2013-10-16 15:14:04 +00001773 path.close();
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001774
halcanary96fcdcc2015-08-27 07:41:13 -07001775 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr));
reed@google.comf32322b2013-10-16 15:14:04 +00001776 REPORTER_ASSERT(reporter, isClosed);
1777}
1778
caryclark@google.comf1316942011-07-26 19:54:45 +00001779// Simple isRect test is inline TestPath, below.
1780// test_isRect provides more extensive testing.
1781static void test_isRect(skiatest::Reporter* reporter) {
reed@google.comf32322b2013-10-16 15:14:04 +00001782 test_isRect_open_close(reporter);
1783
caryclark@google.comf1316942011-07-26 19:54:45 +00001784 // passing tests (all moveTo / lineTo...
1785 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1786 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1787 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1788 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
1789 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
1790 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1791 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1792 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1793 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001794 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}};
1795 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 +00001796 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}};
1797 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
1798 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}};
caryclark@google.combfe90372012-11-21 13:56:20 +00001799 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}};
rmistry@google.comd6176b02012-08-23 18:14:13 +00001800
caryclark@google.comf1316942011-07-26 19:54:45 +00001801 // failing tests
1802 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1803 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1804 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1805 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1806 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1807 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1808 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1809 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
caryclark@google.combfe90372012-11-21 13:56:20 +00001810 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps
1811 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap
1812 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short
rmistry@google.comd6176b02012-08-23 18:14:13 +00001813
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001814 // no close, but we should detect them as fillably the same as a rect
1815 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
1816 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}};
1817 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start
1818
1819 // like c2, but we double-back on ourselves
1820 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}};
1821 // like c2, but we overshoot the start point
1822 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}};
1823 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}};
caryclark@google.comf1316942011-07-26 19:54:45 +00001824
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001825 struct IsRectTest {
1826 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001827 int fPointCount;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001828 bool fClose;
1829 bool fIsRect;
1830 } tests[] = {
1831 { r1, SK_ARRAY_COUNT(r1), true, true },
1832 { r2, SK_ARRAY_COUNT(r2), true, true },
1833 { r3, SK_ARRAY_COUNT(r3), true, true },
1834 { r4, SK_ARRAY_COUNT(r4), true, true },
1835 { r5, SK_ARRAY_COUNT(r5), true, true },
1836 { r6, SK_ARRAY_COUNT(r6), true, true },
1837 { r7, SK_ARRAY_COUNT(r7), true, true },
1838 { r8, SK_ARRAY_COUNT(r8), true, true },
1839 { r9, SK_ARRAY_COUNT(r9), true, true },
1840 { ra, SK_ARRAY_COUNT(ra), true, true },
1841 { rb, SK_ARRAY_COUNT(rb), true, true },
1842 { rc, SK_ARRAY_COUNT(rc), true, true },
1843 { rd, SK_ARRAY_COUNT(rd), true, true },
1844 { re, SK_ARRAY_COUNT(re), true, true },
1845 { rf, SK_ARRAY_COUNT(rf), true, true },
1846
1847 { f1, SK_ARRAY_COUNT(f1), true, false },
1848 { f2, SK_ARRAY_COUNT(f2), true, false },
1849 { f3, SK_ARRAY_COUNT(f3), true, false },
1850 { f4, SK_ARRAY_COUNT(f4), true, false },
1851 { f5, SK_ARRAY_COUNT(f5), true, false },
1852 { f6, SK_ARRAY_COUNT(f6), true, false },
1853 { f7, SK_ARRAY_COUNT(f7), true, false },
1854 { f8, SK_ARRAY_COUNT(f8), true, false },
1855 { f9, SK_ARRAY_COUNT(f9), true, false },
1856 { fa, SK_ARRAY_COUNT(fa), true, false },
1857 { fb, SK_ARRAY_COUNT(fb), true, false },
1858
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00001859 { c1, SK_ARRAY_COUNT(c1), false, true },
1860 { c2, SK_ARRAY_COUNT(c2), false, true },
1861 { c3, SK_ARRAY_COUNT(c3), false, true },
1862
1863 { d1, SK_ARRAY_COUNT(d1), false, false },
1864 { d2, SK_ARRAY_COUNT(d2), false, false },
1865 { d3, SK_ARRAY_COUNT(d3), false, false },
caryclark@google.comf1316942011-07-26 19:54:45 +00001866 };
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001867
1868 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08001869 int index;
caryclark@google.comf1316942011-07-26 19:54:45 +00001870 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
1871 SkPath path;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001872 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
1873 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
1874 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.comf1316942011-07-26 19:54:45 +00001875 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001876 if (tests[testIndex].fClose) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001877 path.close();
1878 }
halcanary96fcdcc2015-08-27 07:41:13 -07001879 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001880
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001881 if (tests[testIndex].fIsRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001882 SkRect computed, expected;
caryclark@google.comf68154a2012-11-21 15:18:06 +00001883 bool isClosed;
reed026beb52015-06-10 14:23:15 -07001884 SkPath::Direction direction;
1885 SkPathPriv::FirstDirection cheapDirection;
robertphillips91b0a352015-01-05 10:13:46 -08001886 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
reed026beb52015-06-10 14:23:15 -07001887 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection));
robertphillips91b0a352015-01-05 10:13:46 -08001888 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction));
1889 REPORTER_ASSERT(reporter, expected == computed);
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001890 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose);
reed026beb52015-06-10 14:23:15 -07001891 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001892 } else {
1893 SkRect computed;
1894 computed.set(123, 456, 789, 1011);
robertphillips91b0a352015-01-05 10:13:46 -08001895 bool isClosed = (bool)-1;
1896 SkPath::Direction direction = (SkPath::Direction) - 1;
1897 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction));
caryclark@google.comf68154a2012-11-21 15:18:06 +00001898 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456);
1899 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011);
caryclark@google.comf68154a2012-11-21 15:18:06 +00001900 REPORTER_ASSERT(reporter, isClosed == (bool) -1);
1901 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1);
caryclark@google.com56f233a2012-11-19 13:06:06 +00001902 }
caryclark@google.comf1316942011-07-26 19:54:45 +00001903 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001904
caryclark@google.comf1316942011-07-26 19:54:45 +00001905 // fail, close then line
1906 SkPath path1;
1907 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001908 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001909 path1.lineTo(r1[index].fX, r1[index].fY);
1910 }
1911 path1.close();
1912 path1.lineTo(1, 0);
halcanary96fcdcc2015-08-27 07:41:13 -07001913 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001914
caryclark@google.comf1316942011-07-26 19:54:45 +00001915 // fail, move in the middle
1916 path1.reset();
1917 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001918 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001919 if (index == 2) {
1920 path1.moveTo(1, .5f);
1921 }
1922 path1.lineTo(r1[index].fX, r1[index].fY);
1923 }
1924 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001925 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001926
1927 // fail, move on the edge
1928 path1.reset();
bsalomon98806072014-12-12 15:11:17 -08001929 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001930 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
1931 path1.lineTo(r1[index].fX, r1[index].fY);
1932 }
1933 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001934 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001935
caryclark@google.comf1316942011-07-26 19:54:45 +00001936 // fail, quad
1937 path1.reset();
1938 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001939 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001940 if (index == 2) {
1941 path1.quadTo(1, .5f, 1, .5f);
1942 }
1943 path1.lineTo(r1[index].fX, r1[index].fY);
1944 }
1945 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001946 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001947
caryclark@google.comf1316942011-07-26 19:54:45 +00001948 // fail, cubic
1949 path1.reset();
1950 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08001951 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.comf1316942011-07-26 19:54:45 +00001952 if (index == 2) {
1953 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
1954 }
1955 path1.lineTo(r1[index].fX, r1[index].fY);
1956 }
1957 path1.close();
halcanary96fcdcc2015-08-27 07:41:13 -07001958 REPORTER_ASSERT(reporter, !path1.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00001959}
1960
caryclark95bc5f32015-04-08 08:34:15 -07001961static void test_isNestedFillRects(skiatest::Reporter* reporter) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00001962 // passing tests (all moveTo / lineTo...
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001963 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001964 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}};
1965 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}};
1966 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}};
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001967 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001968 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
1969 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}};
1970 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}};
1971 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}};
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001972 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW
1973 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 +00001974 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW
1975 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW
1976 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW
caryclark@google.com56f233a2012-11-19 13:06:06 +00001977
1978 // failing tests
1979 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points
1980 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal
1981 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps
1982 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up
1983 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots
1984 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots
1985 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots
1986 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L'
1987
caryclark95bc5f32015-04-08 08:34:15 -07001988 // success, no close is OK
caryclark@google.com56f233a2012-11-19 13:06:06 +00001989 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match
1990 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto
1991
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001992 struct IsNestedRectTest {
1993 SkPoint *fPoints;
bsalomon98806072014-12-12 15:11:17 -08001994 int fPointCount;
reed026beb52015-06-10 14:23:15 -07001995 SkPathPriv::FirstDirection fDirection;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00001996 bool fClose;
1997 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2);
1998 } tests[] = {
reed026beb52015-06-10 14:23:15 -07001999 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true },
2000 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true },
2001 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true },
2002 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true },
2003 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true },
2004 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true },
2005 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true },
2006 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true },
2007 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true },
2008 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true },
2009 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true },
2010 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true },
2011 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true },
2012 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true },
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002013
reed026beb52015-06-10 14:23:15 -07002014 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false },
2015 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false },
2016 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false },
2017 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false },
2018 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false },
2019 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false },
2020 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false },
2021 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002022
reed026beb52015-06-10 14:23:15 -07002023 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true },
2024 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true },
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002025 };
2026
2027 const size_t testCount = SK_ARRAY_COUNT(tests);
bsalomon98806072014-12-12 15:11:17 -08002028 int index;
caryclark@google.com56f233a2012-11-19 13:06:06 +00002029 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002030 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) {
2031 SkPath path;
2032 if (rectFirst) {
2033 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2034 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002035 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY);
2036 for (index = 1; index < tests[testIndex].fPointCount; ++index) {
2037 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002038 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002039 if (tests[testIndex].fClose) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002040 path.close();
2041 }
2042 if (!rectFirst) {
2043 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2044 }
caryclark95bc5f32015-04-08 08:34:15 -07002045 REPORTER_ASSERT(reporter,
halcanary96fcdcc2015-08-27 07:41:13 -07002046 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr));
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002047 if (tests[testIndex].fIsNestedRect) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002048 SkRect expected[2], computed[2];
reed026beb52015-06-10 14:23:15 -07002049 SkPathPriv::FirstDirection expectedDirs[2];
2050 SkPath::Direction computedDirs[2];
caryclark@google.com56f233a2012-11-19 13:06:06 +00002051 SkRect testBounds;
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002052 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount);
caryclark@google.com56f233a2012-11-19 13:06:06 +00002053 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2);
2054 expected[1] = testBounds;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002055 if (rectFirst) {
reed026beb52015-06-10 14:23:15 -07002056 expectedDirs[0] = SkPathPriv::kCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002057 } else {
reed026beb52015-06-10 14:23:15 -07002058 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00002059 }
bungeman@google.comb8d9d5b2013-09-25 18:21:39 +00002060 expectedDirs[1] = tests[testIndex].fDirection;
caryclark95bc5f32015-04-08 08:34:15 -07002061 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002062 REPORTER_ASSERT(reporter, expected[0] == computed[0]);
2063 REPORTER_ASSERT(reporter, expected[1] == computed[1]);
reed026beb52015-06-10 14:23:15 -07002064 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0]));
2065 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1]));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002066 }
caryclark@google.com56f233a2012-11-19 13:06:06 +00002067 }
2068
2069 // fail, close then line
2070 SkPath path1;
2071 if (rectFirst) {
2072 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2073 }
2074 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002075 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002076 path1.lineTo(r1[index].fX, r1[index].fY);
2077 }
2078 path1.close();
2079 path1.lineTo(1, 0);
2080 if (!rectFirst) {
2081 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2082 }
halcanary96fcdcc2015-08-27 07:41:13 -07002083 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002084
2085 // fail, move in the middle
2086 path1.reset();
2087 if (rectFirst) {
2088 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2089 }
2090 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002091 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002092 if (index == 2) {
2093 path1.moveTo(1, .5f);
2094 }
2095 path1.lineTo(r1[index].fX, r1[index].fY);
2096 }
2097 path1.close();
2098 if (!rectFirst) {
2099 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2100 }
halcanary96fcdcc2015-08-27 07:41:13 -07002101 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002102
2103 // fail, move on the edge
2104 path1.reset();
2105 if (rectFirst) {
2106 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2107 }
bsalomon98806072014-12-12 15:11:17 -08002108 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002109 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY);
2110 path1.lineTo(r1[index].fX, r1[index].fY);
2111 }
2112 path1.close();
2113 if (!rectFirst) {
2114 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2115 }
halcanary96fcdcc2015-08-27 07:41:13 -07002116 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002117
2118 // fail, quad
2119 path1.reset();
2120 if (rectFirst) {
2121 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2122 }
2123 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002124 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002125 if (index == 2) {
2126 path1.quadTo(1, .5f, 1, .5f);
2127 }
2128 path1.lineTo(r1[index].fX, r1[index].fY);
2129 }
2130 path1.close();
2131 if (!rectFirst) {
2132 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2133 }
halcanary96fcdcc2015-08-27 07:41:13 -07002134 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002135
2136 // fail, cubic
2137 path1.reset();
2138 if (rectFirst) {
2139 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction);
2140 }
2141 path1.moveTo(r1[0].fX, r1[0].fY);
bsalomon98806072014-12-12 15:11:17 -08002142 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) {
caryclark@google.com56f233a2012-11-19 13:06:06 +00002143 if (index == 2) {
2144 path1.cubicTo(1, .5f, 1, .5f, 1, .5f);
2145 }
2146 path1.lineTo(r1[index].fX, r1[index].fY);
2147 }
2148 path1.close();
2149 if (!rectFirst) {
2150 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction);
2151 }
halcanary96fcdcc2015-08-27 07:41:13 -07002152 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
skia.committer@gmail.com34587162012-11-20 02:01:23 +00002153
caryclark@google.com56f233a2012-11-19 13:06:06 +00002154 // fail, not nested
2155 path1.reset();
2156 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction);
2157 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction);
halcanary96fcdcc2015-08-27 07:41:13 -07002158 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002159 }
caryclark@google.combfe90372012-11-21 13:56:20 +00002160
caryclark95bc5f32015-04-08 08:34:15 -07002161 // pass, constructed explicitly from manually closed rects specified as moves/lines.
2162 SkPath path;
2163 path.moveTo(0, 0);
2164 path.lineTo(10, 0);
2165 path.lineTo(10, 10);
2166 path.lineTo(0, 10);
2167 path.lineTo(0, 0);
2168 path.moveTo(1, 1);
2169 path.lineTo(9, 1);
2170 path.lineTo(9, 9);
2171 path.lineTo(1, 9);
2172 path.lineTo(1, 1);
halcanary96fcdcc2015-08-27 07:41:13 -07002173 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr));
caryclark95bc5f32015-04-08 08:34:15 -07002174
caryclark@google.combfe90372012-11-21 13:56:20 +00002175 // pass, stroke rect
2176 SkPath src, dst;
2177 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction);
2178 SkPaint strokePaint;
2179 strokePaint.setStyle(SkPaint::kStroke_Style);
2180 strokePaint.setStrokeWidth(2);
2181 strokePaint.getFillPath(src, &dst);
halcanary96fcdcc2015-08-27 07:41:13 -07002182 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr));
caryclark@google.com56f233a2012-11-19 13:06:06 +00002183}
2184
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002185static void write_and_read_back(skiatest::Reporter* reporter,
2186 const SkPath& p) {
commit-bot@chromium.org19382422014-01-14 20:51:26 +00002187 SkWriter32 writer;
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002188 writer.writePath(p);
reed@google.com44699382013-10-31 17:28:30 +00002189 size_t size = writer.bytesWritten();
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002190 SkAutoMalloc storage(size);
2191 writer.flatten(storage.get());
2192 SkReader32 reader(storage.get(), size);
2193
2194 SkPath readBack;
2195 REPORTER_ASSERT(reporter, readBack != p);
2196 reader.readPath(&readBack);
2197 REPORTER_ASSERT(reporter, readBack == p);
2198
rmistry@google.comd6176b02012-08-23 18:14:13 +00002199 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() ==
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002200 p.getConvexityOrUnknown());
2201
halcanary96fcdcc2015-08-27 07:41:13 -07002202 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr));
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002203
2204 const SkRect& origBounds = p.getBounds();
2205 const SkRect& readBackBounds = readBack.getBounds();
2206
2207 REPORTER_ASSERT(reporter, origBounds == readBackBounds);
2208}
2209
reed@google.com53effc52011-09-21 19:05:12 +00002210static void test_flattening(skiatest::Reporter* reporter) {
2211 SkPath p;
2212
2213 static const SkPoint pts[] = {
2214 { 0, 0 },
2215 { SkIntToScalar(10), SkIntToScalar(10) },
2216 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 },
2217 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }
2218 };
2219 p.moveTo(pts[0]);
2220 p.lineTo(pts[1]);
2221 p.quadTo(pts[2], pts[3]);
2222 p.cubicTo(pts[4], pts[5], pts[6]);
2223
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002224 write_and_read_back(reporter, p);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002225
2226 // create a buffer that should be much larger than the path so we don't
2227 // kill our stack if writer goes too far.
2228 char buffer[1024];
halcanary96fcdcc2015-08-27 07:41:13 -07002229 size_t size1 = p.writeToMemory(nullptr);
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002230 size_t size2 = p.writeToMemory(buffer);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002231 REPORTER_ASSERT(reporter, size1 == size2);
2232
2233 SkPath p2;
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +00002234 size_t size3 = p2.readFromMemory(buffer, 1024);
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002235 REPORTER_ASSERT(reporter, size1 == size3);
2236 REPORTER_ASSERT(reporter, p == p2);
2237
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002238 size3 = p2.readFromMemory(buffer, 0);
2239 REPORTER_ASSERT(reporter, !size3);
2240
2241 SkPath tooShort;
2242 size3 = tooShort.readFromMemory(buffer, size1 - 1);
2243 REPORTER_ASSERT(reporter, tooShort.isEmpty());
2244
djsollen@google.com94e75ee2012-06-08 18:30:46 +00002245 char buffer2[1024];
2246 size3 = p2.writeToMemory(buffer2);
2247 REPORTER_ASSERT(reporter, size1 == size3);
2248 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
robertphillips@google.com2972bb52012-08-07 17:32:51 +00002249
2250 // test persistence of the oval flag & convexity
2251 {
2252 SkPath oval;
2253 SkRect rect = SkRect::MakeWH(10, 10);
2254 oval.addOval(rect);
2255
2256 write_and_read_back(reporter, oval);
2257 }
reed@google.com53effc52011-09-21 19:05:12 +00002258}
2259
2260static void test_transform(skiatest::Reporter* reporter) {
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002261 SkPath p;
rmistry@google.comd6176b02012-08-23 18:14:13 +00002262
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002263#define CONIC_PERSPECTIVE_BUG_FIXED 0
reed@google.com53effc52011-09-21 19:05:12 +00002264 static const SkPoint pts[] = {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002265 { 0, 0 }, // move
2266 { SkIntToScalar(10), SkIntToScalar(10) }, // line
2267 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad
2268 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic
2269#if CONIC_PERSPECTIVE_BUG_FIXED
2270 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic
2271#endif
reed@google.com53effc52011-09-21 19:05:12 +00002272 };
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002273 const int kPtCount = SK_ARRAY_COUNT(pts);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002274
reed@google.com53effc52011-09-21 19:05:12 +00002275 p.moveTo(pts[0]);
2276 p.lineTo(pts[1]);
2277 p.quadTo(pts[2], pts[3]);
2278 p.cubicTo(pts[4], pts[5], pts[6]);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002279#if CONIC_PERSPECTIVE_BUG_FIXED
2280 p.conicTo(pts[4], pts[5], 0.5f);
2281#endif
2282 p.close();
rmistry@google.comd6176b02012-08-23 18:14:13 +00002283
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002284 {
2285 SkMatrix matrix;
2286 matrix.reset();
2287 SkPath p1;
2288 p.transform(matrix, &p1);
2289 REPORTER_ASSERT(reporter, p == p1);
reed@google.com53effc52011-09-21 19:05:12 +00002290 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002291
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002292
2293 {
2294 SkMatrix matrix;
2295 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3);
2296
2297 SkPath p1; // Leave p1 non-unique (i.e., the empty path)
2298
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002299 p.transform(matrix, &p1);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002300 SkPoint pts1[kPtCount];
skia.committer@gmail.com6e515d62013-12-04 07:02:26 +00002301 int count = p1.getPoints(pts1, kPtCount);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002302 REPORTER_ASSERT(reporter, kPtCount == count);
2303 for (int i = 0; i < count; ++i) {
2304 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3);
2305 REPORTER_ASSERT(reporter, newPt == pts1[i]);
2306 }
2307 }
2308
2309 {
2310 SkMatrix matrix;
2311 matrix.reset();
reed3f43f8a2015-01-20 19:58:36 -08002312 matrix.setPerspX(4);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002313
2314 SkPath p1;
2315 p1.moveTo(SkPoint::Make(0, 0));
2316
2317 p.transform(matrix, &p1);
2318 REPORTER_ASSERT(reporter, matrix.invert(&matrix));
halcanary96fcdcc2015-08-27 07:41:13 -07002319 p1.transform(matrix, nullptr);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002320 SkRect pBounds = p.getBounds();
2321 SkRect p1Bounds = p1.getBounds();
2322 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft));
2323 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop));
2324 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight));
2325 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom));
2326 }
2327
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002328 p.reset();
2329 p.addCircle(0, 0, 1, SkPath::kCW_Direction);
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002330
2331 {
2332 SkMatrix matrix;
2333 matrix.reset();
2334 SkPath p1;
2335 p1.moveTo(SkPoint::Make(0, 0));
2336
2337 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002338 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002339 }
2340
2341
2342 {
2343 SkMatrix matrix;
2344 matrix.reset();
2345 matrix.setScaleX(-1);
2346 SkPath p1;
2347 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2348
2349 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002350 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002351 }
2352
2353 {
2354 SkMatrix matrix;
2355 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1);
2356 SkPath p1;
2357 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path)
2358
2359 p.transform(matrix, &p1);
reed026beb52015-06-10 14:23:15 -07002360 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection));
robertphillips@google.comb06e88d2013-12-03 17:15:36 +00002361 }
reed@google.com53effc52011-09-21 19:05:12 +00002362}
2363
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002364static void test_zero_length_paths(skiatest::Reporter* reporter) {
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002365 SkPath p;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002366 uint8_t verbs[32];
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002367
caryclark@google.com56f233a2012-11-19 13:06:06 +00002368 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002369 const char* testPath;
2370 const size_t numResultPts;
2371 const SkRect resultBound;
2372 const SkPath::Verb* resultVerbs;
2373 const size_t numResultVerbs;
2374 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002375
schenney@chromium.org7e963602012-06-13 17:05:43 +00002376 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb };
2377 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb };
2378 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb };
2379 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb };
2380 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb };
2381 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb };
2382 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb };
2383 static const SkPath::Verb resultVerbs8[] = {
2384 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb
2385 };
2386 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb };
2387 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb };
2388 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb };
2389 static const SkPath::Verb resultVerbs12[] = {
2390 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb
2391 };
2392 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb };
2393 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb };
2394 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb };
2395 static const SkPath::Verb resultVerbs16[] = {
2396 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb
2397 };
2398 static const struct zeroPathTestData gZeroLengthTests[] = {
reed91f283b2015-07-28 06:00:50 -07002399 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002400 { "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 -07002401 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002402 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2403 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) },
2404 { "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) },
2405 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) },
2406 { "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) },
2407 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) },
2408 { "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) },
2409 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) },
2410 { "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) },
2411 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) },
2412 { "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 +00002413 SK_ARRAY_COUNT(resultVerbs14)
2414 },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002415 { "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) },
2416 { "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 +00002417 SK_ARRAY_COUNT(resultVerbs16)
2418 }
2419 };
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002420
schenney@chromium.org7e963602012-06-13 17:05:43 +00002421 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) {
2422 p.reset();
2423 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p);
2424 REPORTER_ASSERT(reporter, valid);
2425 REPORTER_ASSERT(reporter, !p.isEmpty());
2426 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints());
2427 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds());
2428 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs)));
2429 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) {
2430 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]);
2431 }
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00002432 }
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00002433}
2434
2435struct SegmentInfo {
2436 SkPath fPath;
2437 int fPointCount;
2438};
2439
reed@google.com10296cc2011-09-21 12:29:05 +00002440#define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)
2441
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002442static void test_segment_masks(skiatest::Reporter* reporter) {
reed@google.comeef938c2012-08-01 20:01:49 +00002443 SkPath p, p2;
2444
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002445 p.moveTo(0, 0);
2446 p.quadTo(100, 100, 200, 200);
2447 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks());
2448 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002449 p2 = p;
2450 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002451 p.cubicTo(100, 100, 200, 200, 300, 300);
2452 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks());
2453 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.comeef938c2012-08-01 20:01:49 +00002454 p2 = p;
2455 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
2456
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002457 p.reset();
2458 p.moveTo(0, 0);
2459 p.cubicTo(100, 100, 200, 200, 300, 300);
2460 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks());
reed@google.comeef938c2012-08-01 20:01:49 +00002461 p2 = p;
2462 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks());
rmistry@google.comd6176b02012-08-23 18:14:13 +00002463
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002464 REPORTER_ASSERT(reporter, !p.isEmpty());
2465}
2466
2467static void test_iter(skiatest::Reporter* reporter) {
schenney@chromium.org7e963602012-06-13 17:05:43 +00002468 SkPath p;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002469 SkPoint pts[4];
2470
2471 // Test an iterator with no path
2472 SkPath::Iter noPathIter;
2473 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002474
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002475 // Test that setting an empty path works
2476 noPathIter.setPath(p, false);
2477 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002478
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002479 // Test that close path makes no difference for an empty path
2480 noPathIter.setPath(p, true);
2481 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
schenney@chromium.org7e963602012-06-13 17:05:43 +00002482
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002483 // Test an iterator with an initial empty path
2484 SkPath::Iter iter(p, false);
2485 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2486
2487 // Test that close path makes no difference
schenney@chromium.org7e963602012-06-13 17:05:43 +00002488 iter.setPath(p, true);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002489 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2490
rmistry@google.comd6176b02012-08-23 18:14:13 +00002491
schenney@chromium.org7e963602012-06-13 17:05:43 +00002492 struct iterTestData {
2493 const char* testPath;
2494 const bool forceClose;
2495 const bool consumeDegenerates;
2496 const size_t* numResultPtsPerVerb;
2497 const SkPoint* resultPts;
2498 const SkPath::Verb* resultVerbs;
2499 const size_t numResultVerbs;
2500 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002501
schenney@chromium.org7e963602012-06-13 17:05:43 +00002502 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb };
2503 static const SkPath::Verb resultVerbs2[] = {
2504 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb
2505 };
2506 static const SkPath::Verb resultVerbs3[] = {
2507 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2508 };
2509 static const SkPath::Verb resultVerbs4[] = {
2510 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2511 };
2512 static const SkPath::Verb resultVerbs5[] = {
2513 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb
2514 };
2515 static const size_t resultPtsSizes1[] = { 0 };
schenney@chromium.orgfedd09b2012-06-13 18:29:20 +00002516 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 };
2517 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 };
2518 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 };
2519 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 };
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002520 static const SkPoint* resultPts1 = 0;
schenney@chromium.org7e963602012-06-13 17:05:43 +00002521 static const SkPoint resultPts2[] = {
2522 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }
2523 };
2524 static const SkPoint resultPts3[] = {
2525 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 },
2526 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }
2527 };
2528 static const SkPoint resultPts4[] = {
2529 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2530 };
2531 static const SkPoint resultPts5[] = {
2532 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 }
2533 };
2534 static const struct iterTestData gIterTests[] = {
2535 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
schenney@chromium.orgaaf16882012-06-13 17:41:00 +00002536 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2537 { "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 +00002538 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2539 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2540 { "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) },
2541 { "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 +00002542 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) },
2543 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) },
2544 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2545 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) },
2546 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) },
2547 { "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 +00002548 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002549
schenney@chromium.org7e963602012-06-13 17:05:43 +00002550 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) {
2551 p.reset();
2552 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p);
2553 REPORTER_ASSERT(reporter, valid);
2554 iter.setPath(p, gIterTests[i].forceClose);
2555 int j = 0, l = 0;
2556 do {
2557 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]);
2558 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) {
2559 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]);
2560 }
2561 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb);
2562 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs);
2563 }
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002564
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00002565 p.reset();
2566 iter.setPath(p, false);
2567 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2568 p.lineTo(1, 1);
2569 p.close();
2570 iter.setPath(p, false);
2571 REPORTER_ASSERT(reporter, iter.isClosedContour());
2572 p.reset();
2573 iter.setPath(p, true);
2574 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2575 p.lineTo(1, 1);
2576 iter.setPath(p, true);
2577 REPORTER_ASSERT(reporter, iter.isClosedContour());
2578 p.moveTo(0, 0);
2579 p.lineTo(2, 2);
2580 iter.setPath(p, false);
2581 REPORTER_ASSERT(reporter, !iter.isClosedContour());
2582
2583 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not
2584 // check to see if the result is correct.
2585 for (int setNaN = 0; setNaN < 4; ++setNaN) {
2586 p.reset();
2587 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0);
2588 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1);
2589 iter.setPath(p, true);
2590 iter.next(pts, false);
2591 iter.next(pts, false);
2592 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false));
2593 }
2594
2595 p.reset();
2596 p.quadTo(0, 0, 0, 0);
2597 iter.setPath(p, false);
2598 iter.next(pts, false);
2599 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false));
2600 iter.setPath(p, false);
2601 iter.next(pts, false);
2602 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2603
2604 p.reset();
2605 p.conicTo(0, 0, 0, 0, 0.5f);
2606 iter.setPath(p, false);
2607 iter.next(pts, false);
2608 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false));
2609 iter.setPath(p, false);
2610 iter.next(pts, false);
2611 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2612
2613 p.reset();
2614 p.cubicTo(0, 0, 0, 0, 0, 0);
2615 iter.setPath(p, false);
2616 iter.next(pts, false);
2617 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2618 iter.setPath(p, false);
2619 iter.next(pts, false);
2620 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2621
2622 p.moveTo(1, 1); // add a trailing moveto
2623 iter.setPath(p, false);
2624 iter.next(pts, false);
2625 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false));
2626 iter.setPath(p, false);
2627 iter.next(pts, false);
2628 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true));
2629
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002630 // The GM degeneratesegments.cpp test is more extensive
robertphillipsb8de1f42015-02-23 11:17:01 -08002631
2632 // Test out mixed degenerate and non-degenerate geometry with Conics
2633 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
2634 SkRect r = SkRect::MakeWH(100, 100);
2635 SkRRect rr;
2636 rr.setRectRadii(r, radii);
2637 p.reset();
2638 p.addRRect(rr);
2639 iter.setPath(p, false);
2640 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts));
2641 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2642 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts));
2643 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts));
2644 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight());
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002645}
2646
2647static void test_raw_iter(skiatest::Reporter* reporter) {
2648 SkPath p;
2649 SkPoint pts[4];
2650
2651 // Test an iterator with no path
2652 SkPath::RawIter noPathIter;
2653 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
2654 // Test that setting an empty path works
2655 noPathIter.setPath(p);
2656 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb);
rmistry@google.comd6176b02012-08-23 18:14:13 +00002657
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002658 // Test an iterator with an initial empty path
2659 SkPath::RawIter iter(p);
2660 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2661
2662 // Test that a move-only path returns the move.
2663 p.moveTo(SK_Scalar1, 0);
2664 iter.setPath(p);
2665 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2666 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2667 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2668 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2669
2670 // No matter how many moves we add, we should get them all back
2671 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2672 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2673 iter.setPath(p);
2674 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2675 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2676 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2677 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2678 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2679 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2680 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2681 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2682 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2683 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2684
2685 // Initial close is never ever stored
2686 p.reset();
2687 p.close();
2688 iter.setPath(p);
2689 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2690
2691 // Move/close sequences
2692 p.reset();
2693 p.close(); // Not stored, no purpose
2694 p.moveTo(SK_Scalar1, 0);
2695 p.close();
2696 p.close(); // Not stored, no purpose
2697 p.moveTo(SK_Scalar1*2, SK_Scalar1);
2698 p.close();
2699 p.moveTo(SK_Scalar1*3, SK_Scalar1*2);
2700 p.moveTo(SK_Scalar1*4, SK_Scalar1*3);
2701 p.close();
2702 iter.setPath(p);
2703 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2704 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1);
2705 REPORTER_ASSERT(reporter, pts[0].fY == 0);
2706 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002707 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2708 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2);
2709 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1);
2710 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002711 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2712 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3);
2713 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2);
2714 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb);
2715 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4);
2716 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3);
2717 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002718 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb);
2719
2720 // Generate random paths and verify
2721 SkPoint randomPts[25];
2722 for (int i = 0; i < 5; ++i) {
2723 for (int j = 0; j < 5; ++j) {
2724 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j);
2725 }
2726 }
2727
2728 // Max of 10 segments, max 3 points per segment
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +00002729 SkRandom rand(9876543);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002730 SkPoint expectedPts[31]; // May have leading moveTo
reed@google.comd335d1d2012-01-12 18:17:11 +00002731 SkPath::Verb expectedVerbs[22]; // May have leading moveTo
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002732 SkPath::Verb nextVerb;
reed@google.comd335d1d2012-01-12 18:17:11 +00002733
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002734 for (int i = 0; i < 500; ++i) {
2735 p.reset();
2736 bool lastWasClose = true;
2737 bool haveMoveTo = false;
reed@google.comd335d1d2012-01-12 18:17:11 +00002738 SkPoint lastMoveToPt = { 0, 0 };
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002739 int numPoints = 0;
2740 int numVerbs = (rand.nextU() >> 16) % 10;
2741 int numIterVerbs = 0;
2742 for (int j = 0; j < numVerbs; ++j) {
2743 do {
2744 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb);
2745 } while (lastWasClose && nextVerb == SkPath::kClose_Verb);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002746 switch (nextVerb) {
2747 case SkPath::kMove_Verb:
2748 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2749 p.moveTo(expectedPts[numPoints]);
reed@google.comd335d1d2012-01-12 18:17:11 +00002750 lastMoveToPt = expectedPts[numPoints];
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002751 numPoints += 1;
2752 lastWasClose = false;
2753 haveMoveTo = true;
2754 break;
2755 case SkPath::kLine_Verb:
2756 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002757 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002758 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2759 haveMoveTo = true;
2760 }
2761 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2762 p.lineTo(expectedPts[numPoints]);
2763 numPoints += 1;
2764 lastWasClose = false;
2765 break;
2766 case SkPath::kQuad_Verb:
2767 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002768 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002769 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2770 haveMoveTo = true;
2771 }
2772 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2773 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2774 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]);
2775 numPoints += 2;
2776 lastWasClose = false;
2777 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002778 case SkPath::kConic_Verb:
2779 if (!haveMoveTo) {
2780 expectedPts[numPoints++] = lastMoveToPt;
2781 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2782 haveMoveTo = true;
2783 }
2784 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2785 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2786 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2787 rand.nextUScalar1() * 4);
2788 numPoints += 2;
2789 lastWasClose = false;
2790 break;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002791 case SkPath::kCubic_Verb:
2792 if (!haveMoveTo) {
reed@google.comd335d1d2012-01-12 18:17:11 +00002793 expectedPts[numPoints++] = lastMoveToPt;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002794 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb;
2795 haveMoveTo = true;
2796 }
2797 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25];
2798 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25];
2799 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25];
2800 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1],
2801 expectedPts[numPoints + 2]);
2802 numPoints += 3;
2803 lastWasClose = false;
2804 break;
2805 case SkPath::kClose_Verb:
2806 p.close();
reed@google.comd335d1d2012-01-12 18:17:11 +00002807 haveMoveTo = false;
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002808 lastWasClose = true;
2809 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002810 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002811 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002812 }
2813 expectedVerbs[numIterVerbs++] = nextVerb;
2814 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00002815
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002816 iter.setPath(p);
2817 numVerbs = numIterVerbs;
2818 numIterVerbs = 0;
2819 int numIterPts = 0;
2820 SkPoint lastMoveTo;
2821 SkPoint lastPt;
2822 lastMoveTo.set(0, 0);
2823 lastPt.set(0, 0);
2824 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) {
2825 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]);
2826 numIterVerbs++;
2827 switch (nextVerb) {
2828 case SkPath::kMove_Verb:
2829 REPORTER_ASSERT(reporter, numIterPts < numPoints);
2830 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]);
2831 lastPt = lastMoveTo = pts[0];
2832 numIterPts += 1;
2833 break;
2834 case SkPath::kLine_Verb:
2835 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1);
2836 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2837 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2838 lastPt = pts[1];
2839 numIterPts += 1;
2840 break;
2841 case SkPath::kQuad_Verb:
reed@google.com277c3f82013-05-31 15:17:50 +00002842 case SkPath::kConic_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002843 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2);
2844 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2845 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2846 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2847 lastPt = pts[2];
2848 numIterPts += 2;
2849 break;
2850 case SkPath::kCubic_Verb:
2851 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3);
2852 REPORTER_ASSERT(reporter, pts[0] == lastPt);
2853 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]);
2854 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]);
2855 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]);
2856 lastPt = pts[3];
2857 numIterPts += 3;
2858 break;
2859 case SkPath::kClose_Verb:
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002860 lastPt = lastMoveTo;
2861 break;
reed@google.com277c3f82013-05-31 15:17:50 +00002862 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00002863 SkDEBUGFAIL("unexpected verb");
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00002864 }
2865 }
2866 REPORTER_ASSERT(reporter, numIterPts == numPoints);
2867 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs);
2868 }
2869}
2870
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002871static void check_for_circle(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002872 const SkPath& path,
2873 bool expectedCircle,
reed026beb52015-06-10 14:23:15 -07002874 SkPathPriv::FirstDirection expectedDir) {
robertphillips@google.com466310d2013-12-03 16:43:54 +00002875 SkRect rect = SkRect::MakeEmpty();
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002876 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle);
reed026beb52015-06-10 14:23:15 -07002877 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir));
skia.committer@gmail.comfbb0ed92012-11-13 21:46:06 +00002878
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002879 if (expectedCircle) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002880 REPORTER_ASSERT(reporter, rect.height() == rect.width());
2881 }
2882}
2883
2884static void test_circle_skew(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002885 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002886 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002887 SkPath tmp;
2888
2889 SkMatrix m;
2890 m.setSkew(SkIntToScalar(3), SkIntToScalar(5));
2891 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002892 // this matrix reverses the direction.
reed026beb52015-06-10 14:23:15 -07002893 if (SkPathPriv::kCCW_FirstDirection == dir) {
2894 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002895 } else {
reed026beb52015-06-10 14:23:15 -07002896 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir);
2897 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002898 }
2899 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002900}
2901
2902static void test_circle_translate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002903 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002904 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002905 SkPath tmp;
2906
2907 // translate at small offset
2908 SkMatrix m;
2909 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15));
2910 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002911 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002912
2913 tmp.reset();
2914 m.reset();
2915
2916 // translate at a relatively big offset
2917 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000));
2918 path.transform(m, &tmp);
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002919 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002920}
2921
2922static void test_circle_rotate(skiatest::Reporter* reporter,
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002923 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002924 SkPathPriv::FirstDirection dir) {
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002925 for (int angle = 0; angle < 360; ++angle) {
2926 SkPath tmp;
2927 SkMatrix m;
2928 m.setRotate(SkIntToScalar(angle));
2929 path.transform(m, &tmp);
2930
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002931 // TODO: a rotated circle whose rotated angle is not a multiple of 90
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002932 // degrees is not an oval anymore, this can be improved. we made this
2933 // for the simplicity of our implementation.
2934 if (angle % 90 == 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002935 check_for_circle(reporter, tmp, true, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002936 } else {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002937 check_for_circle(reporter, tmp, false, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002938 }
2939 }
2940}
2941
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002942static void test_circle_mirror_x(skiatest::Reporter* reporter,
2943 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002944 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002945 SkPath tmp;
2946 SkMatrix m;
2947 m.reset();
2948 m.setScaleX(-SK_Scalar1);
2949 path.transform(m, &tmp);
2950
reed026beb52015-06-10 14:23:15 -07002951 if (SkPathPriv::kCW_FirstDirection == dir) {
2952 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002953 } else {
reed026beb52015-06-10 14:23:15 -07002954 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2955 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002956 }
2957
2958 check_for_circle(reporter, tmp, true, dir);
2959}
2960
2961static void test_circle_mirror_y(skiatest::Reporter* reporter,
2962 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002963 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002964 SkPath tmp;
2965 SkMatrix m;
2966 m.reset();
2967 m.setScaleY(-SK_Scalar1);
2968 path.transform(m, &tmp);
2969
reed026beb52015-06-10 14:23:15 -07002970 if (SkPathPriv::kCW_FirstDirection == dir) {
2971 dir = SkPathPriv::kCCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002972 } else {
reed026beb52015-06-10 14:23:15 -07002973 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir);
2974 dir = SkPathPriv::kCW_FirstDirection;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002975 }
2976
2977 check_for_circle(reporter, tmp, true, dir);
2978}
2979
2980static void test_circle_mirror_xy(skiatest::Reporter* reporter,
2981 const SkPath& path,
reed026beb52015-06-10 14:23:15 -07002982 SkPathPriv::FirstDirection dir) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +00002983 SkPath tmp;
2984 SkMatrix m;
2985 m.reset();
2986 m.setScaleX(-SK_Scalar1);
2987 m.setScaleY(-SK_Scalar1);
2988 path.transform(m, &tmp);
2989
2990 check_for_circle(reporter, tmp, true, dir);
2991}
2992
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002993static void test_circle_with_direction(skiatest::Reporter* reporter,
reed026beb52015-06-10 14:23:15 -07002994 SkPath::Direction inDir) {
2995 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00002996 SkPath path;
2997
2998 // circle at origin
reed026beb52015-06-10 14:23:15 -07002999 path.addCircle(0, 0, SkIntToScalar(20), inDir);
3000
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003001 check_for_circle(reporter, path, true, dir);
3002 test_circle_rotate(reporter, path, dir);
3003 test_circle_translate(reporter, path, dir);
3004 test_circle_skew(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003005
3006 // circle at an offset at (10, 10)
3007 path.reset();
3008 path.addCircle(SkIntToScalar(10), SkIntToScalar(10),
reed026beb52015-06-10 14:23:15 -07003009 SkIntToScalar(20), inDir);
3010
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003011 check_for_circle(reporter, path, true, dir);
3012 test_circle_rotate(reporter, path, dir);
3013 test_circle_translate(reporter, path, dir);
3014 test_circle_skew(reporter, path, dir);
3015 test_circle_mirror_x(reporter, path, dir);
3016 test_circle_mirror_y(reporter, path, dir);
3017 test_circle_mirror_xy(reporter, path, dir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003018}
3019
3020static void test_circle_with_add_paths(skiatest::Reporter* reporter) {
3021 SkPath path;
3022 SkPath circle;
3023 SkPath rect;
3024 SkPath empty;
3025
reed026beb52015-06-10 14:23:15 -07003026 const SkPath::Direction kCircleDir = SkPath::kCW_Direction;
3027 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction;
bsalomon@google.com30c174b2012-11-13 14:36:42 +00003028
3029 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003030 rect.addRect(SkIntToScalar(5), SkIntToScalar(5),
3031 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction);
3032
3033 SkMatrix translate;
3034 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12));
3035
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003036 // Although all the path concatenation related operations leave
3037 // the path a circle, most mark it as a non-circle for simplicity
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003038
3039 // empty + circle (translate)
3040 path = empty;
3041 path.addPath(circle, translate);
reed026beb52015-06-10 14:23:15 -07003042 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003043
3044 // circle + empty (translate)
3045 path = circle;
3046 path.addPath(empty, translate);
reed026beb52015-06-10 14:23:15 -07003047 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003048
3049 // test reverseAddPath
3050 path = circle;
3051 path.reverseAddPath(rect);
reed026beb52015-06-10 14:23:15 -07003052 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003053}
3054
3055static void test_circle(skiatest::Reporter* reporter) {
3056 test_circle_with_direction(reporter, SkPath::kCW_Direction);
3057 test_circle_with_direction(reporter, SkPath::kCCW_Direction);
3058
3059 // multiple addCircle()
3060 SkPath path;
3061 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3062 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction);
reed026beb52015-06-10 14:23:15 -07003063 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003064
3065 // some extra lineTo() would make isOval() fail
3066 path.reset();
3067 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3068 path.lineTo(0, 0);
reed026beb52015-06-10 14:23:15 -07003069 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003070
3071 // not back to the original point
3072 path.reset();
3073 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction);
3074 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5));
reed026beb52015-06-10 14:23:15 -07003075 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003076
3077 test_circle_with_add_paths(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003078
3079 // test negative radius
3080 path.reset();
3081 path.addCircle(0, 0, -1, SkPath::kCW_Direction);
3082 REPORTER_ASSERT(reporter, path.isEmpty());
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003083}
3084
3085static void test_oval(skiatest::Reporter* reporter) {
3086 SkRect rect;
3087 SkMatrix m;
3088 SkPath path;
3089
3090 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50));
3091 path.addOval(rect);
3092
halcanary96fcdcc2015-08-27 07:41:13 -07003093 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003094
3095 m.setRotate(SkIntToScalar(90));
3096 SkPath tmp;
3097 path.transform(m, &tmp);
3098 // an oval rotated 90 degrees is still an oval.
halcanary96fcdcc2015-08-27 07:41:13 -07003099 REPORTER_ASSERT(reporter, tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003100
3101 m.reset();
3102 m.setRotate(SkIntToScalar(30));
3103 tmp.reset();
3104 path.transform(m, &tmp);
3105 // an oval rotated 30 degrees is not an oval anymore.
halcanary96fcdcc2015-08-27 07:41:13 -07003106 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003107
3108 // since empty path being transformed.
3109 path.reset();
3110 tmp.reset();
3111 m.reset();
3112 path.transform(m, &tmp);
halcanary96fcdcc2015-08-27 07:41:13 -07003113 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003114
3115 // empty path is not an oval
3116 tmp.reset();
halcanary96fcdcc2015-08-27 07:41:13 -07003117 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003118
3119 // only has moveTo()s
3120 tmp.reset();
3121 tmp.moveTo(0, 0);
3122 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10));
halcanary96fcdcc2015-08-27 07:41:13 -07003123 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003124
3125 // mimic WebKit's calling convention,
3126 // call moveTo() first and then call addOval()
3127 path.reset();
3128 path.moveTo(0, 0);
3129 path.addOval(rect);
halcanary96fcdcc2015-08-27 07:41:13 -07003130 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003131
3132 // copy path
3133 path.reset();
3134 tmp.reset();
3135 tmp.addOval(rect);
3136 path = tmp;
halcanary96fcdcc2015-08-27 07:41:13 -07003137 REPORTER_ASSERT(reporter, path.isOval(nullptr));
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003138}
3139
bungeman@google.coma5809a32013-06-21 15:13:34 +00003140static void test_empty(skiatest::Reporter* reporter, const SkPath& p) {
3141 SkPath empty;
reed@android.com80e39a72009-04-02 16:59:40 +00003142
reed@android.com3abec1d2009-03-02 05:36:20 +00003143 REPORTER_ASSERT(reporter, p.isEmpty());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003144 REPORTER_ASSERT(reporter, 0 == p.countPoints());
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003145 REPORTER_ASSERT(reporter, 0 == p.countVerbs());
reed@google.com10296cc2011-09-21 12:29:05 +00003146 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks());
reed@google.comb54455e2011-05-16 14:16:04 +00003147 REPORTER_ASSERT(reporter, p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +00003148 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
3149 REPORTER_ASSERT(reporter, !p.isInverseFillType());
bungeman@google.coma5809a32013-06-21 15:13:34 +00003150 REPORTER_ASSERT(reporter, p == empty);
3151 REPORTER_ASSERT(reporter, !(p != empty));
3152}
3153
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003154static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path,
3155 SkPath::Direction dir) {
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003156 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003157 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003158 path->setConvexity(SkPath::kUnknown_Convexity);
3159 REPORTER_ASSERT(reporter, path->isConvex());
3160 path->reset();
3161}
3162
caryclarkd3d1a982014-12-08 04:57:38 -08003163static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path,
3164 SkPath::Direction dir) {
3165 REPORTER_ASSERT(reporter, path->isConvex());
reed026beb52015-06-10 14:23:15 -07003166 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir)));
caryclarkd3d1a982014-12-08 04:57:38 -08003167 path->setConvexity(SkPath::kUnknown_Convexity);
3168 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity);
3169 path->reset();
3170}
3171
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003172static void test_rrect(skiatest::Reporter* reporter) {
3173 SkPath p;
3174 SkRRect rr;
3175 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
3176 SkRect r = {10, 20, 30, 40};
3177 rr.setRectRadii(r, radii);
3178 p.addRRect(rr);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003179 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003180 p.addRRect(rr, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003181 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003182 p.addRoundRect(r, &radii[0].fX);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003183 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003184 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003185 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003186 p.addRoundRect(r, radii[1].fX, radii[1].fY);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003187 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003188 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003189 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction);
3190 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) {
3191 SkVector save = radii[i];
3192 radii[i].set(0, 0);
3193 rr.setRectRadii(r, radii);
3194 p.addRRect(rr);
3195 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3196 radii[i] = save;
3197 }
3198 p.addRoundRect(r, 0, 0);
3199 SkRect returnedRect;
3200 REPORTER_ASSERT(reporter, p.isRect(&returnedRect));
3201 REPORTER_ASSERT(reporter, returnedRect == r);
3202 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3203 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3204 rr.setRectRadii(r, zeroRadii);
3205 p.addRRect(rr);
3206 bool closed;
3207 SkPath::Direction dir;
halcanary96fcdcc2015-08-27 07:41:13 -07003208 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003209 REPORTER_ASSERT(reporter, closed);
3210 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir);
3211 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
3212 p.addRRect(rr, SkPath::kCW_Direction);
3213 p.addRRect(rr, SkPath::kCW_Direction);
3214 REPORTER_ASSERT(reporter, !p.isConvex());
3215 p.reset();
3216 p.addRRect(rr, SkPath::kCCW_Direction);
3217 p.addRRect(rr, SkPath::kCCW_Direction);
3218 REPORTER_ASSERT(reporter, !p.isConvex());
3219 p.reset();
3220 SkRect emptyR = {10, 20, 10, 30};
3221 rr.setRectRadii(emptyR, radii);
3222 p.addRRect(rr);
3223 REPORTER_ASSERT(reporter, p.isEmpty());
3224 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax};
3225 rr.setRectRadii(largeR, radii);
3226 p.addRRect(rr);
caryclarkd3d1a982014-12-08 04:57:38 -08003227 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction);
reed454fa712015-02-10 08:46:22 -08003228
3229 // we check for non-finites
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003230 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity};
3231 rr.setRectRadii(infR, radii);
reed454fa712015-02-10 08:46:22 -08003232 REPORTER_ASSERT(reporter, rr.isEmpty());
3233
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003234 SkRect tinyR = {0, 0, 1e-9f, 1e-9f};
3235 p.addRoundRect(tinyR, 5e-11f, 5e-11f);
3236 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003237}
3238
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003239static void test_arc(skiatest::Reporter* reporter) {
3240 SkPath p;
3241 SkRect emptyOval = {10, 20, 30, 20};
3242 REPORTER_ASSERT(reporter, emptyOval.isEmpty());
3243 p.addArc(emptyOval, 1, 2);
3244 REPORTER_ASSERT(reporter, p.isEmpty());
3245 p.reset();
3246 SkRect oval = {10, 20, 30, 40};
3247 p.addArc(oval, 1, 0);
3248 REPORTER_ASSERT(reporter, p.isEmpty());
3249 p.reset();
3250 SkPath cwOval;
3251 cwOval.addOval(oval);
3252 p.addArc(oval, 1, 360);
3253 REPORTER_ASSERT(reporter, p == cwOval);
3254 p.reset();
3255 SkPath ccwOval;
3256 ccwOval.addOval(oval, SkPath::kCCW_Direction);
3257 p.addArc(oval, 1, -360);
3258 REPORTER_ASSERT(reporter, p == ccwOval);
3259 p.reset();
3260 p.addArc(oval, 1, 180);
3261 REPORTER_ASSERT(reporter, p.isConvex());
reed026beb52015-06-10 14:23:15 -07003262 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003263 p.setConvexity(SkPath::kUnknown_Convexity);
3264 REPORTER_ASSERT(reporter, p.isConvex());
3265}
3266
3267static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3268 SkScalar x0, SkScalar y0) {
3269 SkPoint pts[4];
3270 SkPath::Verb v = iter->next(pts);
3271 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb);
3272 REPORTER_ASSERT(reporter, pts[0].fX == x0);
3273 REPORTER_ASSERT(reporter, pts[0].fY == y0);
3274}
3275
3276static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3277 SkScalar x1, SkScalar y1) {
3278 SkPoint pts[4];
3279 SkPath::Verb v = iter->next(pts);
3280 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb);
3281 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3282 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3283}
3284
3285static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter,
3286 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3287 SkPoint pts[4];
3288 SkPath::Verb v = iter->next(pts);
3289 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb);
3290 REPORTER_ASSERT(reporter, pts[1].fX == x1);
3291 REPORTER_ASSERT(reporter, pts[1].fY == y1);
3292 REPORTER_ASSERT(reporter, pts[2].fX == x2);
3293 REPORTER_ASSERT(reporter, pts[2].fY == y2);
3294}
3295
3296static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3297 SkPoint pts[4];
3298 SkPath::Verb v = iter->next(pts);
3299 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb);
3300}
3301
3302static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) {
3303 check_done(reporter, p, iter);
3304 p->reset();
3305}
3306
3307static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p,
3308 SkScalar x0, SkScalar y0) {
3309 SkPath::RawIter iter(*p);
3310 check_move(reporter, &iter, x0, y0);
3311 check_done_and_reset(reporter, p, &iter);
3312}
3313
3314static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p,
3315 SkScalar x1, SkScalar y1) {
3316 SkPath::RawIter iter(*p);
3317 check_move(reporter, &iter, 0, 0);
3318 check_line(reporter, &iter, x1, y1);
3319 check_done_and_reset(reporter, p, &iter);
3320}
3321
3322static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p,
3323 SkScalar x1, SkScalar y1) {
3324 SkPath::RawIter iter(*p);
3325 check_move(reporter, &iter, 0, 0);
3326 check_line(reporter, &iter, x1, y1);
3327 check_done(reporter, p, &iter);
3328}
3329
3330static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p,
3331 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3332 SkPath::RawIter iter(*p);
3333 check_move(reporter, &iter, 0, 0);
3334 check_line(reporter, &iter, x1, y1);
3335 check_line(reporter, &iter, x2, y2);
3336 check_done_and_reset(reporter, p, &iter);
3337}
3338
3339static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p,
3340 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
3341 SkPath::RawIter iter(*p);
3342 check_move(reporter, &iter, 0, 0);
3343 check_quad(reporter, &iter, x1, y1, x2, y2);
3344 check_done_and_reset(reporter, p, &iter);
3345}
3346
reedd5d27d92015-02-09 13:54:43 -08003347static bool nearly_equal(const SkRect& a, const SkRect& b) {
3348 return SkScalarNearlyEqual(a.fLeft, b.fLeft) &&
3349 SkScalarNearlyEqual(a.fTop, b.fTop) &&
3350 SkScalarNearlyEqual(a.fRight, b.fRight) &&
3351 SkScalarNearlyEqual(a.fBottom, b.fBottom);
3352}
3353
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003354static void test_arcTo(skiatest::Reporter* reporter) {
3355 SkPath p;
3356 p.arcTo(0, 0, 1, 2, 1);
3357 check_path_is_line_and_reset(reporter, &p, 0, 0);
3358 p.arcTo(1, 2, 1, 2, 1);
3359 check_path_is_line_and_reset(reporter, &p, 1, 2);
3360 p.arcTo(1, 2, 3, 4, 0);
3361 check_path_is_line_and_reset(reporter, &p, 1, 2);
3362 p.arcTo(1, 2, 0, 0, 1);
3363 check_path_is_line_and_reset(reporter, &p, 1, 2);
3364 p.arcTo(1, 0, 1, 1, 1);
3365 SkPoint pt;
3366 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1);
3367 p.reset();
3368 p.arcTo(1, 0, 1, -1, 1);
3369 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1);
3370 p.reset();
3371 SkRect oval = {1, 2, 3, 4};
3372 p.arcTo(oval, 0, 0, true);
3373 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3374 p.arcTo(oval, 0, 0, false);
3375 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3376 p.arcTo(oval, 360, 0, true);
3377 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
3378 p.arcTo(oval, 360, 0, false);
3379 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY());
reedd5d27d92015-02-09 13:54:43 -08003380
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003381 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003382 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003383 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003384 sweep += delta;
3385 delta /= 2;
3386 }
3387 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +00003388 p.arcTo(oval, 0, sweep, false);
reedd5d27d92015-02-09 13:54:43 -08003389 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003390 sweep -= delta;
3391 delta /= 2;
3392 }
3393 SkRect noOvalWidth = {1, 2, 0, 3};
3394 p.reset();
3395 p.arcTo(noOvalWidth, 0, 360, false);
3396 REPORTER_ASSERT(reporter, p.isEmpty());
3397
3398 SkRect noOvalHeight = {1, 2, 3, 1};
3399 p.reset();
3400 p.arcTo(noOvalHeight, 0, 360, false);
3401 REPORTER_ASSERT(reporter, p.isEmpty());
3402}
3403
3404static void test_addPath(skiatest::Reporter* reporter) {
3405 SkPath p, q;
3406 p.lineTo(1, 2);
3407 q.moveTo(4, 4);
3408 q.lineTo(7, 8);
3409 q.conicTo(8, 7, 6, 5, 0.5f);
3410 q.quadTo(6, 7, 8, 6);
3411 q.cubicTo(5, 6, 7, 8, 7, 5);
3412 q.close();
3413 p.addPath(q, -4, -4);
3414 SkRect expected = {0, 0, 4, 4};
3415 REPORTER_ASSERT(reporter, p.getBounds() == expected);
3416 p.reset();
3417 p.reverseAddPath(q);
3418 SkRect reverseExpected = {4, 4, 8, 8};
3419 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3420}
3421
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003422static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) {
3423 SkPath p, q;
3424 if (explicitMoveTo) {
3425 p.moveTo(1, 1);
3426 }
3427 p.lineTo(1, 2);
3428 if (explicitMoveTo) {
3429 q.moveTo(2, 1);
3430 }
3431 q.lineTo(2, 2);
3432 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode);
3433 uint8_t verbs[4];
3434 int verbcount = p.getVerbs(verbs, 4);
3435 REPORTER_ASSERT(reporter, verbcount == 4);
3436 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3437 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3438 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb));
3439 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb);
3440}
3441
3442static void test_extendClosedPath(skiatest::Reporter* reporter) {
3443 SkPath p, q;
3444 p.moveTo(1, 1);
3445 p.lineTo(1, 2);
3446 p.lineTo(2, 2);
3447 p.close();
3448 q.moveTo(2, 1);
3449 q.lineTo(2, 3);
3450 p.addPath(q, SkPath::kExtend_AddPathMode);
3451 uint8_t verbs[7];
3452 int verbcount = p.getVerbs(verbs, 7);
3453 REPORTER_ASSERT(reporter, verbcount == 7);
3454 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb);
3455 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb);
3456 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb);
3457 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb);
3458 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb);
3459 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb);
3460 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb);
3461
3462 SkPoint pt;
3463 REPORTER_ASSERT(reporter, p.getLastPt(&pt));
3464 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3));
3465 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1));
3466}
3467
3468static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) {
3469 SkPath p, q, r;
3470 // case 1: dst is empty
3471 p.moveTo(2, 1);
3472 p.lineTo(2, 3);
3473 q.addPath(p, mode);
3474 REPORTER_ASSERT(reporter, q == p);
3475 // case 2: src is empty
3476 p.addPath(r, mode);
3477 REPORTER_ASSERT(reporter, q == p);
3478 // case 3: src and dst are empty
3479 q.reset();
3480 q.addPath(r, mode);
3481 REPORTER_ASSERT(reporter, q.isEmpty());
skia.committer@gmail.com877c4492014-02-12 03:02:04 +00003482}
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003483
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003484static void test_conicTo_special_case(skiatest::Reporter* reporter) {
3485 SkPath p;
3486 p.conicTo(1, 2, 3, 4, -1);
3487 check_path_is_line_and_reset(reporter, &p, 3, 4);
3488 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity);
3489 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4);
3490 p.conicTo(1, 2, 3, 4, 1);
3491 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4);
3492}
3493
3494static void test_get_point(skiatest::Reporter* reporter) {
3495 SkPath p;
3496 SkPoint pt = p.getPoint(0);
3497 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0));
halcanary96fcdcc2015-08-27 07:41:13 -07003498 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003499 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0));
3500 p.setLastPt(10, 10);
3501 pt = p.getPoint(0);
3502 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10));
halcanary96fcdcc2015-08-27 07:41:13 -07003503 REPORTER_ASSERT(reporter, p.getLastPt(nullptr));
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003504 p.rMoveTo(10, 10);
3505 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20));
3506}
3507
3508static void test_contains(skiatest::Reporter* reporter) {
3509 SkPath p;
3510 p.setFillType(SkPath::kInverseWinding_FillType);
3511 REPORTER_ASSERT(reporter, p.contains(0, 0));
3512 p.setFillType(SkPath::kWinding_FillType);
3513 REPORTER_ASSERT(reporter, !p.contains(0, 0));
3514 p.moveTo(4, 4);
3515 p.lineTo(6, 8);
3516 p.lineTo(8, 4);
3517 // test quick reject
3518 REPORTER_ASSERT(reporter, !p.contains(4, 0));
3519 REPORTER_ASSERT(reporter, !p.contains(0, 4));
3520 REPORTER_ASSERT(reporter, !p.contains(4, 10));
3521 REPORTER_ASSERT(reporter, !p.contains(10, 4));
3522 // test various crossings in x
3523 REPORTER_ASSERT(reporter, !p.contains(5, 7));
3524 REPORTER_ASSERT(reporter, p.contains(6, 7));
3525 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3526 p.reset();
3527 p.moveTo(4, 4);
3528 p.lineTo(8, 6);
3529 p.lineTo(4, 8);
3530 // test various crossings in y
3531 REPORTER_ASSERT(reporter, !p.contains(7, 5));
3532 REPORTER_ASSERT(reporter, p.contains(7, 6));
3533 REPORTER_ASSERT(reporter, !p.contains(7, 7));
3534 // test quads
3535 p.reset();
3536 p.moveTo(4, 4);
3537 p.quadTo(6, 6, 8, 8);
3538 p.quadTo(6, 8, 4, 8);
3539 p.quadTo(4, 6, 4, 4);
3540 REPORTER_ASSERT(reporter, p.contains(5, 6));
3541 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3542
3543 p.reset();
3544 p.moveTo(6, 6);
3545 p.quadTo(8, 8, 6, 8);
3546 p.quadTo(4, 8, 4, 6);
3547 p.quadTo(4, 4, 6, 6);
3548 REPORTER_ASSERT(reporter, p.contains(5, 6));
3549 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3550
3551#define CONIC_CONTAINS_BUG_FIXED 0
3552#if CONIC_CONTAINS_BUG_FIXED
3553 p.reset();
3554 p.moveTo(4, 4);
3555 p.conicTo(6, 6, 8, 8, 0.5f);
3556 p.conicTo(6, 8, 4, 8, 0.5f);
3557 p.conicTo(4, 6, 4, 4, 0.5f);
3558 REPORTER_ASSERT(reporter, p.contains(5, 6));
3559 REPORTER_ASSERT(reporter, !p.contains(6, 5));
3560#endif
3561
3562 // test cubics
3563 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}};
3564 for (int i = 0; i < 3; ++i) {
3565 p.reset();
3566 p.setFillType(SkPath::kEvenOdd_FillType);
3567 p.moveTo(pts[i].fX, pts[i].fY);
3568 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);
3569 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);
3570 p.close();
3571 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f));
3572 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f));
3573 }
3574}
3575
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003576class PathRefTest_Private {
3577public:
3578 static void TestPathRef(skiatest::Reporter* reporter) {
3579 static const int kRepeatCnt = 10;
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003580
halcanary385fe4d2015-08-26 13:07:48 -07003581 SkAutoTUnref<SkPathRef> pathRef(new SkPathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003582
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003583 SkPathRef::Editor ed(&pathRef);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003584
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003585 {
3586 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt);
3587 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3588 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3589 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks());
3590 for (int i = 0; i < kRepeatCnt; ++i) {
3591 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i));
3592 }
3593 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003594 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003595
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003596 {
3597 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt);
3598 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3599 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints());
3600 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks());
3601 for (int i = 0; i < kRepeatCnt; ++i) {
3602 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i));
3603 }
3604 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003605 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003606
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003607 {
3608 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt);
3609 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3610 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3611 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks());
3612 for (int i = 0; i < kRepeatCnt; ++i) {
3613 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i));
3614 }
3615 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003616 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003617
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003618 {
halcanary96fcdcc2015-08-27 07:41:13 -07003619 SkScalar* weights = nullptr;
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003620 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights);
3621 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3622 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints());
3623 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights());
3624 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks());
bsalomon49f085d2014-09-05 13:34:00 -07003625 REPORTER_ASSERT(reporter, weights);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003626 for (int i = 0; i < kRepeatCnt; ++i) {
3627 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i));
3628 }
3629 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003630 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003631
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003632 {
3633 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt);
3634 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs());
3635 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints());
3636 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks());
3637 for (int i = 0; i < kRepeatCnt; ++i) {
3638 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i));
3639 }
3640 ed.resetToSize(0, 0, 0);
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003641 }
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003642 }
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003643};
robertphillips@google.com6b8dbb62013-12-12 23:03:51 +00003644
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003645static void test_operatorEqual(skiatest::Reporter* reporter) {
3646 SkPath a;
3647 SkPath b;
3648 REPORTER_ASSERT(reporter, a == a);
3649 REPORTER_ASSERT(reporter, a == b);
3650 a.setFillType(SkPath::kInverseWinding_FillType);
3651 REPORTER_ASSERT(reporter, a != b);
3652 a.reset();
3653 REPORTER_ASSERT(reporter, a == b);
3654 a.lineTo(1, 1);
3655 REPORTER_ASSERT(reporter, a != b);
3656 a.reset();
3657 REPORTER_ASSERT(reporter, a == b);
3658 a.lineTo(1, 1);
3659 b.lineTo(1, 2);
3660 REPORTER_ASSERT(reporter, a != b);
3661 a.reset();
3662 a.lineTo(1, 2);
3663 REPORTER_ASSERT(reporter, a == b);
3664}
3665
caryclark66a5d8b2014-06-24 08:30:15 -07003666static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force,
caryclarke9562592014-09-15 09:26:09 -07003667 bool dumpAsHex, const char* str) {
caryclark66a5d8b2014-06-24 08:30:15 -07003668 SkDynamicMemoryWStream wStream;
caryclarke9562592014-09-15 09:26:09 -07003669 path.dump(&wStream, force, dumpAsHex);
caryclark66a5d8b2014-06-24 08:30:15 -07003670 SkAutoDataUnref data(wStream.copyToData());
3671 REPORTER_ASSERT(reporter, data->size() == strlen(str));
mtkleind4897592014-11-14 09:22:40 -08003672 if (strlen(str) > 0) {
3673 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str)));
3674 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07003675 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str)));
mtkleind4897592014-11-14 09:22:40 -08003676 }
caryclark66a5d8b2014-06-24 08:30:15 -07003677}
3678
3679static void test_dump(skiatest::Reporter* reporter) {
3680 SkPath p;
caryclarke9562592014-09-15 09:26:09 -07003681 compare_dump(reporter, p, false, false, "");
3682 compare_dump(reporter, p, true, false, "");
caryclark66a5d8b2014-06-24 08:30:15 -07003683 p.moveTo(1, 2);
3684 p.lineTo(3, 4);
caryclarke9562592014-09-15 09:26:09 -07003685 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3686 "path.lineTo(3, 4);\n");
3687 compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n"
3688 "path.lineTo(3, 4);\n"
3689 "path.lineTo(1, 2);\n"
3690 "path.close();\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003691 p.reset();
3692 p.moveTo(1, 2);
3693 p.quadTo(3, 4, 5, 6);
caryclarke9562592014-09-15 09:26:09 -07003694 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3695 "path.quadTo(3, 4, 5, 6);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003696 p.reset();
3697 p.moveTo(1, 2);
3698 p.conicTo(3, 4, 5, 6, 0.5f);
caryclarke9562592014-09-15 09:26:09 -07003699 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3700 "path.conicTo(3, 4, 5, 6, 0.5f);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003701 p.reset();
3702 p.moveTo(1, 2);
3703 p.cubicTo(3, 4, 5, 6, 7, 8);
caryclarke9562592014-09-15 09:26:09 -07003704 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3705 "path.cubicTo(3, 4, 5, 6, 7, 8);\n");
3706 p.reset();
3707 p.moveTo(1, 2);
3708 p.lineTo(3, 4);
caryclark08fa28c2014-10-23 13:08:56 -07003709 compare_dump(reporter, p, false, true,
3710 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n"
3711 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n");
caryclarke9562592014-09-15 09:26:09 -07003712 p.reset();
3713 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000));
3714 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000));
3715 compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n"
3716 "path.lineTo(3, 4);\n");
caryclark66a5d8b2014-06-24 08:30:15 -07003717}
3718
senorblanco84cd6212015-08-04 10:01:58 -07003719namespace {
3720
3721class ChangeListener : public SkPathRef::GenIDChangeListener {
3722public:
3723 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; }
3724 virtual ~ChangeListener() {}
3725 void onChange() override {
3726 *fChanged = true;
3727 }
3728private:
3729 bool* fChanged;
3730};
3731
3732}
3733
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003734class PathTest_Private {
3735public:
3736 static void TestPathTo(skiatest::Reporter* reporter) {
3737 SkPath p, q;
3738 p.lineTo(4, 4);
3739 p.reversePathTo(q);
3740 check_path_is_line(reporter, &p, 4, 4);
3741 q.moveTo(-4, -4);
3742 p.reversePathTo(q);
3743 check_path_is_line(reporter, &p, 4, 4);
3744 q.lineTo(7, 8);
3745 q.conicTo(8, 7, 6, 5, 0.5f);
3746 q.quadTo(6, 7, 8, 6);
3747 q.cubicTo(5, 6, 7, 8, 7, 5);
3748 q.close();
3749 p.reversePathTo(q);
3750 SkRect reverseExpected = {-4, -4, 8, 8};
3751 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected);
3752 }
senorblanco84cd6212015-08-04 10:01:58 -07003753
3754 static void TestPathrefListeners(skiatest::Reporter* reporter) {
3755 SkPath p;
3756
3757 bool changed = false;
3758 p.moveTo(0, 0);
3759
3760 // Check that listener is notified on moveTo().
3761
halcanary385fe4d2015-08-26 13:07:48 -07003762 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003763 REPORTER_ASSERT(reporter, !changed);
3764 p.moveTo(10, 0);
3765 REPORTER_ASSERT(reporter, changed);
3766
3767 // Check that listener is notified on lineTo().
halcanary385fe4d2015-08-26 13:07:48 -07003768 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003769 REPORTER_ASSERT(reporter, !changed);
3770 p.lineTo(20, 0);
3771 REPORTER_ASSERT(reporter, changed);
3772
3773 // Check that listener is notified on reset().
halcanary385fe4d2015-08-26 13:07:48 -07003774 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003775 REPORTER_ASSERT(reporter, !changed);
3776 p.reset();
3777 REPORTER_ASSERT(reporter, changed);
3778
3779 p.moveTo(0, 0);
3780
3781 // Check that listener is notified on rewind().
halcanary385fe4d2015-08-26 13:07:48 -07003782 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003783 REPORTER_ASSERT(reporter, !changed);
3784 p.rewind();
3785 REPORTER_ASSERT(reporter, changed);
3786
3787 // Check that listener is notified when pathref is deleted.
3788 {
3789 SkPath q;
3790 q.moveTo(10, 10);
halcanary385fe4d2015-08-26 13:07:48 -07003791 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed));
senorblanco84cd6212015-08-04 10:01:58 -07003792 REPORTER_ASSERT(reporter, !changed);
3793 }
3794 // q went out of scope.
3795 REPORTER_ASSERT(reporter, changed);
3796 }
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003797};
3798
commit-bot@chromium.org05ec2232014-01-15 18:00:57 +00003799DEF_TEST(Paths, reporter) {
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +00003800 test_path_crbug364224();
3801
bungeman@google.coma5809a32013-06-21 15:13:34 +00003802 SkTSize<SkScalar>::Make(3,4);
3803
3804 SkPath p, empty;
3805 SkRect bounds, bounds2;
3806 test_empty(reporter, p);
reed@android.com3abec1d2009-03-02 05:36:20 +00003807
reed@android.comd252db02009-04-01 18:31:44 +00003808 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +00003809
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003810 // this triggers a code path in SkPath::operator= which is otherwise unexercised
3811 SkPath& self = p;
3812 p = self;
3813
3814 // this triggers a code path in SkPath::swap which is otherwise unexercised
3815 p.swap(self);
3816
reed@android.com3abec1d2009-03-02 05:36:20 +00003817 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003818
reed@android.com6b82d1a2009-06-03 02:35:01 +00003819 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
3820 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003821 // we have quads or cubics
reed220f9262014-12-17 08:21:04 -08003822 REPORTER_ASSERT(reporter,
3823 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask));
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003824 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003825
reed@android.com6b82d1a2009-06-03 02:35:01 +00003826 p.reset();
bungeman@google.coma5809a32013-06-21 15:13:34 +00003827 test_empty(reporter, p);
reed@google.com10296cc2011-09-21 12:29:05 +00003828
reed@android.com6b82d1a2009-06-03 02:35:01 +00003829 p.addOval(bounds);
3830 check_convex_bounds(reporter, p, bounds);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003831 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@google.com62047cf2011-02-07 19:39:09 +00003832
bungeman@google.coma5809a32013-06-21 15:13:34 +00003833 p.rewind();
3834 test_empty(reporter, p);
3835
reed@android.com3abec1d2009-03-02 05:36:20 +00003836 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +00003837 check_convex_bounds(reporter, p, bounds);
reed@google.com10296cc2011-09-21 12:29:05 +00003838 // we have only lines
3839 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks());
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003840 REPORTER_ASSERT(reporter, !p.isEmpty());
reed@android.com3abec1d2009-03-02 05:36:20 +00003841
bungeman@google.coma5809a32013-06-21 15:13:34 +00003842 REPORTER_ASSERT(reporter, p != empty);
3843 REPORTER_ASSERT(reporter, !(p == empty));
reed@android.com3abec1d2009-03-02 05:36:20 +00003844
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003845 // do getPoints and getVerbs return the right result
halcanary96fcdcc2015-08-27 07:41:13 -07003846 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4);
3847 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5);
reed@android.com3abec1d2009-03-02 05:36:20 +00003848 SkPoint pts[4];
3849 int count = p.getPoints(pts, 4);
3850 REPORTER_ASSERT(reporter, count == 4);
bsalomon@google.comdf9d6562012-06-07 21:43:15 +00003851 uint8_t verbs[6];
3852 verbs[5] = 0xff;
3853 p.getVerbs(verbs, 5);
3854 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]);
3855 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]);
3856 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]);
3857 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]);
3858 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]);
3859 REPORTER_ASSERT(reporter, 0xff == verbs[5]);
reed@android.com3abec1d2009-03-02 05:36:20 +00003860 bounds2.set(pts, 4);
3861 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003862
reed@android.com3abec1d2009-03-02 05:36:20 +00003863 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
3864 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +00003865 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +00003866
halcanary96fcdcc2015-08-27 07:41:13 -07003867 REPORTER_ASSERT(reporter, p.isRect(nullptr));
caryclark@google.comf1316942011-07-26 19:54:45 +00003868 bounds2.setEmpty();
reed@android.com3abec1d2009-03-02 05:36:20 +00003869 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
3870 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +00003871
reed@android.com3abec1d2009-03-02 05:36:20 +00003872 // now force p to not be a rect
3873 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
3874 p.addRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -07003875 REPORTER_ASSERT(reporter, !p.isRect(nullptr));
reed@android.com3abec1d2009-03-02 05:36:20 +00003876
robertphillipsfe7c4272014-12-29 11:36:39 -08003877 // Test an edge case w.r.t. the bound returned by isRect (i.e., the
3878 // path has a trailing moveTo. Please see crbug.com\445368)
3879 {
3880 SkRect r;
3881 p.reset();
3882 p.addRect(bounds);
3883 REPORTER_ASSERT(reporter, p.isRect(&r));
3884 REPORTER_ASSERT(reporter, r == bounds);
3885 // add a moveTo outside of our bounds
3886 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10);
3887 REPORTER_ASSERT(reporter, p.isRect(&r));
3888 REPORTER_ASSERT(reporter, r == bounds);
3889 }
3890
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003891 test_operatorEqual(reporter);
reed@google.com7e6c4d12012-05-10 14:05:43 +00003892 test_isLine(reporter);
3893 test_isRect(reporter);
caryclark95bc5f32015-04-08 08:34:15 -07003894 test_isNestedFillRects(reporter);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +00003895 test_zero_length_paths(reporter);
reed@google.comcabaf1d2012-01-11 21:03:05 +00003896 test_direction(reporter);
reed@google.com04863fa2011-05-15 04:08:24 +00003897 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +00003898 test_convexity2(reporter);
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00003899 test_conservativelyContains(reporter);
bsalomon@google.comb3b8dfa2011-07-13 17:44:36 +00003900 test_close(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003901 test_segment_masks(reporter);
reed@google.com53effc52011-09-21 19:05:12 +00003902 test_flattening(reporter);
3903 test_transform(reporter);
reed@google.com3563c9e2011-11-14 19:34:57 +00003904 test_bounds(reporter);
schenney@chromium.org6630d8d2012-01-04 21:05:51 +00003905 test_iter(reporter);
3906 test_raw_iter(reporter);
bsalomon@google.com6aa29652012-04-18 13:29:52 +00003907 test_circle(reporter);
3908 test_oval(reporter);
reed@google.com8b06f1a2012-05-29 12:03:46 +00003909 test_strokerec(reporter);
reed@google.com744faba2012-05-29 19:54:52 +00003910 test_addPoly(reporter);
reed@google.com0bb18bb2012-07-26 15:20:36 +00003911 test_isfinite(reporter);
tomhudson@google.comed02c4d2012-08-10 14:10:45 +00003912 test_isfinite_after_transform(reporter);
robertphillips@google.comb95eaa82012-10-18 15:26:12 +00003913 test_arb_round_rect_is_convex(reporter);
robertphillips@google.com158618e2012-10-23 16:56:56 +00003914 test_arb_zero_rad_round_rect_is_rect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003915 test_addrect(reporter);
reed@google.coma8790de2012-10-24 21:04:04 +00003916 test_addrect_isfinite(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +00003917 test_tricky_cubic();
3918 test_clipped_cubic();
3919 test_crbug_170666();
caryclarke8c56662015-07-14 11:19:26 -07003920 test_crbug_493450(reporter);
3921 test_crbug_495894(reporter);
reed@google.com7a90daf2013-04-10 18:44:00 +00003922 test_bad_cubic_crbug229478();
reed@google.com3eff3592013-05-08 21:08:21 +00003923 test_bad_cubic_crbug234190();
commit-bot@chromium.org1ab9f732013-10-30 18:57:55 +00003924 test_gen_id(reporter);
commit-bot@chromium.org9d54aeb2013-08-09 19:48:26 +00003925 test_path_close_issue1474(reporter);
reed@google.comb58ba892013-10-15 15:44:35 +00003926 test_path_to_region(reporter);
commit-bot@chromium.org42feaaf2013-11-08 15:51:12 +00003927 test_rrect(reporter);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003928 test_arc(reporter);
3929 test_arcTo(reporter);
3930 test_addPath(reporter);
commit-bot@chromium.org14747e52014-02-11 21:16:29 +00003931 test_addPathMode(reporter, false, false);
3932 test_addPathMode(reporter, true, false);
3933 test_addPathMode(reporter, false, true);
3934 test_addPathMode(reporter, true, true);
3935 test_extendClosedPath(reporter);
3936 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode);
3937 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode);
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +00003938 test_conicTo_special_case(reporter);
3939 test_get_point(reporter);
3940 test_contains(reporter);
3941 PathTest_Private::TestPathTo(reporter);
robertphillips@google.com0efb21b2013-12-13 19:36:25 +00003942 PathRefTest_Private::TestPathRef(reporter);
senorblanco84cd6212015-08-04 10:01:58 -07003943 PathTest_Private::TestPathrefListeners(reporter);
caryclark66a5d8b2014-06-24 08:30:15 -07003944 test_dump(reporter);
caryclarkb4216502015-03-02 13:02:34 -08003945 test_path_crbug389050(reporter);
piotaixrfac4e0e2014-08-26 11:59:04 -07003946 test_path_crbugskia2820(reporter);
caryclark5ccef572015-03-02 10:07:56 -08003947 test_skbug_3469(reporter);
reed5bcbe912014-12-15 12:28:33 -08003948 test_skbug_3239(reporter);
reed91f283b2015-07-28 06:00:50 -07003949 test_bounds_crbug_513799(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +00003950}