epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 7 | |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 9 | #include "SkPaint.h" |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 10 | #include "SkParse.h" |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 11 | #include "SkParsePath.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "SkPath.h" |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 13 | #include "SkPathEffect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 16 | #include "SkReader32.h" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 17 | #include "SkSize.h" |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 18 | #include "SkStream.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 19 | #include "SkSurface.h" |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
| 21 | #include "SkWriter32.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 22 | #include "Test.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 23 | |
reed | 5bcbe91 | 2014-12-15 12:28:33 -0800 | [diff] [blame] | 24 | static void set_radii(SkVector radii[4], int index, float rad) { |
| 25 | sk_bzero(radii, sizeof(SkVector) * 4); |
| 26 | radii[index].set(rad, rad); |
| 27 | } |
| 28 | |
| 29 | static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds, |
| 30 | const SkVector radii[4]) { |
| 31 | SkRRect rrect; |
| 32 | rrect.setRectRadii(bounds, radii); |
| 33 | REPORTER_ASSERT(reporter, bounds == rrect.rect()); |
| 34 | |
| 35 | SkPath path; |
| 36 | // this line should not assert in the debug build (from validate) |
| 37 | path.addRRect(rrect); |
| 38 | REPORTER_ASSERT(reporter, bounds == path.getBounds()); |
| 39 | } |
| 40 | |
| 41 | static void test_skbug_3239(skiatest::Reporter* reporter) { |
| 42 | const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */ |
| 43 | const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */ |
| 44 | const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */ |
| 45 | |
| 46 | const float rad = 33436320; |
| 47 | |
| 48 | const SkRect rectx = SkRect::MakeLTRB(min, min, max, big); |
| 49 | const SkRect recty = SkRect::MakeLTRB(min, min, big, max); |
| 50 | |
| 51 | SkVector radii[4]; |
| 52 | for (int i = 0; i < 4; ++i) { |
| 53 | set_radii(radii, i, rad); |
| 54 | test_add_rrect(reporter, rectx, radii); |
| 55 | test_add_rrect(reporter, recty, radii); |
| 56 | } |
| 57 | } |
| 58 | |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 59 | static void make_path_crbug364224(SkPath* path) { |
| 60 | path->reset(); |
| 61 | path->moveTo(3.747501373f, 2.724499941f); |
| 62 | path->lineTo(3.747501373f, 3.75f); |
| 63 | path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f); |
| 64 | path->lineTo(0.7475013733f, 4.0f); |
| 65 | path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f); |
| 66 | path->lineTo(0.4975013733f, 1.0f); |
| 67 | path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f); |
| 68 | path->lineTo(3.497501373f, 0.75f); |
| 69 | path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f); |
| 70 | path->lineTo(3.715001345f, 0.5512499809f); |
| 71 | path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f); |
| 72 | path->lineTo(0.7475013733f, 0.4999999702f); |
| 73 | path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f); |
| 74 | path->lineTo(0.2475013733f, 3.75f); |
| 75 | path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f); |
| 76 | path->lineTo(3.497501373f, 4.25f); |
| 77 | path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f); |
| 78 | path->lineTo(3.997501373f, 2.474750042f); |
| 79 | path->lineTo(3.747501373f, 2.724499941f); |
| 80 | path->close(); |
| 81 | } |
| 82 | |
| 83 | static void make_path_crbug364224_simplified(SkPath* path) { |
| 84 | path->moveTo(3.747501373f, 2.724499941f); |
| 85 | path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f); |
| 86 | path->close(); |
| 87 | } |
| 88 | |
| 89 | static void test_path_crbug364224() { |
| 90 | SkPath path; |
| 91 | SkPaint paint; |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 92 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88)); |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 93 | SkCanvas* canvas = surface->getCanvas(); |
| 94 | |
| 95 | make_path_crbug364224_simplified(&path); |
| 96 | canvas->drawPath(path, paint); |
| 97 | |
| 98 | make_path_crbug364224(&path); |
| 99 | canvas->drawPath(path, paint); |
| 100 | } |
| 101 | |
piotaixr | fac4e0e | 2014-08-26 11:59:04 -0700 | [diff] [blame] | 102 | /** |
| 103 | * In debug mode, this path was causing an assertion to fail in |
| 104 | * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value. |
| 105 | */ |
| 106 | static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) { |
| 107 | SkPoint orig, p1, p2, p3; |
| 108 | orig = SkPoint::Make(1.f, 1.f); |
| 109 | p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f); |
| 110 | p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero); |
| 111 | p3 = SkPoint::Make(2.f, 2.f); |
| 112 | |
| 113 | path->reset(); |
| 114 | path->moveTo(orig); |
| 115 | path->cubicTo(p1, p2, p3); |
| 116 | path->close(); |
| 117 | } |
| 118 | |
| 119 | static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) { |
| 120 | SkPath path; |
| 121 | make_path_crbugskia2820(&path, reporter); |
| 122 | |
| 123 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 124 | stroke.setStrokeStyle(2 * SK_Scalar1); |
| 125 | stroke.applyToPath(&path, path); |
| 126 | } |
| 127 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 128 | static void make_path0(SkPath* path) { |
| 129 | // from * https://code.google.com/p/skia/issues/detail?id=1706 |
| 130 | |
| 131 | path->moveTo(146.939f, 1012.84f); |
| 132 | path->lineTo(181.747f, 1009.18f); |
| 133 | path->lineTo(182.165f, 1013.16f); |
| 134 | path->lineTo(147.357f, 1016.82f); |
| 135 | path->lineTo(146.939f, 1012.84f); |
| 136 | path->close(); |
| 137 | } |
| 138 | |
| 139 | static void make_path1(SkPath* path) { |
| 140 | path->addRect(SkRect::MakeXYWH(10, 10, 10, 1)); |
| 141 | } |
| 142 | |
| 143 | typedef void (*PathProc)(SkPath*); |
| 144 | |
| 145 | /* |
| 146 | * Regression test: we used to crash (overwrite internal storage) during |
| 147 | * construction of the region when the path was INVERSE. That is now fixed, |
| 148 | * so test these regions (which used to assert/crash). |
| 149 | * |
| 150 | * https://code.google.com/p/skia/issues/detail?id=1706 |
| 151 | */ |
| 152 | static void test_path_to_region(skiatest::Reporter* reporter) { |
| 153 | PathProc procs[] = { |
| 154 | make_path0, |
| 155 | make_path1, |
| 156 | }; |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 157 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 158 | SkRegion clip; |
| 159 | clip.setRect(0, 0, 1255, 1925); |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 160 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 161 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
| 162 | SkPath path; |
| 163 | procs[i](&path); |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 164 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 165 | SkRegion rgn; |
| 166 | rgn.setPath(path, clip); |
| 167 | path.toggleInverseFillType(); |
| 168 | rgn.setPath(path, clip); |
| 169 | } |
| 170 | } |
| 171 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 172 | #if defined(WIN32) |
| 173 | #define SUPPRESS_VISIBILITY_WARNING |
| 174 | #else |
| 175 | #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden"))) |
| 176 | #endif |
| 177 | |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 178 | static void test_path_close_issue1474(skiatest::Reporter* reporter) { |
| 179 | // This test checks that r{Line,Quad,Conic,Cubic}To following a close() |
| 180 | // are relative to the point we close to, not relative to the point we close from. |
| 181 | SkPath path; |
| 182 | SkPoint last; |
| 183 | |
| 184 | // Test rLineTo(). |
| 185 | path.rLineTo(0, 100); |
| 186 | path.rLineTo(100, 0); |
| 187 | path.close(); // Returns us back to 0,0. |
| 188 | path.rLineTo(50, 50); // This should go to 50,50. |
| 189 | |
| 190 | path.getLastPt(&last); |
| 191 | REPORTER_ASSERT(reporter, 50 == last.fX); |
| 192 | REPORTER_ASSERT(reporter, 50 == last.fY); |
| 193 | |
| 194 | // Test rQuadTo(). |
| 195 | path.rewind(); |
| 196 | path.rLineTo(0, 100); |
| 197 | path.rLineTo(100, 0); |
| 198 | path.close(); |
| 199 | path.rQuadTo(50, 50, 75, 75); |
| 200 | |
| 201 | path.getLastPt(&last); |
| 202 | REPORTER_ASSERT(reporter, 75 == last.fX); |
| 203 | REPORTER_ASSERT(reporter, 75 == last.fY); |
| 204 | |
| 205 | // Test rConicTo(). |
| 206 | path.rewind(); |
| 207 | path.rLineTo(0, 100); |
| 208 | path.rLineTo(100, 0); |
| 209 | path.close(); |
| 210 | path.rConicTo(50, 50, 85, 85, 2); |
| 211 | |
| 212 | path.getLastPt(&last); |
| 213 | REPORTER_ASSERT(reporter, 85 == last.fX); |
| 214 | REPORTER_ASSERT(reporter, 85 == last.fY); |
| 215 | |
| 216 | // Test rCubicTo(). |
| 217 | path.rewind(); |
| 218 | path.rLineTo(0, 100); |
| 219 | path.rLineTo(100, 0); |
| 220 | path.close(); |
| 221 | path.rCubicTo(50, 50, 85, 85, 95, 95); |
| 222 | |
| 223 | path.getLastPt(&last); |
| 224 | REPORTER_ASSERT(reporter, 95 == last.fX); |
| 225 | REPORTER_ASSERT(reporter, 95 == last.fY); |
| 226 | } |
| 227 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 228 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 229 | SkPath a, b; |
| 230 | REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); |
| 231 | |
| 232 | a.moveTo(0, 0); |
| 233 | const uint32_t z = a.getGenerationID(); |
| 234 | REPORTER_ASSERT(reporter, z != b.getGenerationID()); |
| 235 | |
| 236 | a.reset(); |
| 237 | REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); |
| 238 | |
| 239 | a.moveTo(1, 1); |
| 240 | const uint32_t y = a.getGenerationID(); |
| 241 | REPORTER_ASSERT(reporter, z != y); |
| 242 | |
| 243 | b.moveTo(2, 2); |
| 244 | const uint32_t x = b.getGenerationID(); |
| 245 | REPORTER_ASSERT(reporter, x != y && x != z); |
| 246 | |
| 247 | a.swap(b); |
| 248 | REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x); |
| 249 | |
| 250 | b = a; |
| 251 | REPORTER_ASSERT(reporter, b.getGenerationID() == x); |
| 252 | |
| 253 | SkPath c(a); |
| 254 | REPORTER_ASSERT(reporter, c.getGenerationID() == x); |
| 255 | |
| 256 | c.lineTo(3, 3); |
| 257 | const uint32_t w = c.getGenerationID(); |
| 258 | REPORTER_ASSERT(reporter, b.getGenerationID() == x); |
| 259 | REPORTER_ASSERT(reporter, a.getGenerationID() == x); |
| 260 | REPORTER_ASSERT(reporter, w != x); |
| 261 | |
djsollen | 50da1d8 | 2015-02-17 09:09:53 -0800 | [diff] [blame] | 262 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 263 | static bool kExpectGenIDToIgnoreFill = false; |
| 264 | #else |
| 265 | static bool kExpectGenIDToIgnoreFill = true; |
| 266 | #endif |
| 267 | |
| 268 | c.toggleInverseFillType(); |
| 269 | const uint32_t v = c.getGenerationID(); |
| 270 | REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill); |
| 271 | |
| 272 | c.rewind(); |
| 273 | REPORTER_ASSERT(reporter, v != c.getGenerationID()); |
| 274 | } |
| 275 | |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 276 | // This used to assert in the debug build, as the edges did not all line-up. |
| 277 | static void test_bad_cubic_crbug234190() { |
| 278 | SkPath path; |
| 279 | path.moveTo(13.8509f, 3.16858f); |
| 280 | path.cubicTo(-2.35893e+08f, -4.21044e+08f, |
| 281 | -2.38991e+08f, -4.26573e+08f, |
| 282 | -2.41016e+08f, -4.30188e+08f); |
| 283 | |
| 284 | SkPaint paint; |
| 285 | paint.setAntiAlias(true); |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 286 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(84, 88)); |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 287 | surface->getCanvas()->drawPath(path, paint); |
| 288 | } |
| 289 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 290 | static void test_bad_cubic_crbug229478() { |
| 291 | const SkPoint pts[] = { |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 292 | { 4595.91064f, -11596.9873f }, |
| 293 | { 4597.2168f, -11595.9414f }, |
| 294 | { 4598.52344f, -11594.8955f }, |
| 295 | { 4599.83008f, -11593.8496f }, |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 296 | }; |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 297 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 298 | SkPath path; |
| 299 | path.moveTo(pts[0]); |
| 300 | path.cubicTo(pts[1], pts[2], pts[3]); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 301 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 302 | SkPaint paint; |
| 303 | paint.setStyle(SkPaint::kStroke_Style); |
| 304 | paint.setStrokeWidth(20); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 305 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 306 | SkPath dst; |
| 307 | // Before the fix, this would infinite-recurse, and run out of stack |
| 308 | // because we would keep trying to subdivide a degenerate cubic segment. |
| 309 | paint.getFillPath(path, &dst, NULL); |
| 310 | } |
| 311 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 312 | static void build_path_170666(SkPath& path) { |
| 313 | path.moveTo(17.9459f, 21.6344f); |
| 314 | path.lineTo(139.545f, -47.8105f); |
| 315 | path.lineTo(139.545f, -47.8105f); |
| 316 | path.lineTo(131.07f, -47.3888f); |
| 317 | path.lineTo(131.07f, -47.3888f); |
| 318 | path.lineTo(122.586f, -46.9532f); |
| 319 | path.lineTo(122.586f, -46.9532f); |
| 320 | path.lineTo(18076.6f, 31390.9f); |
| 321 | path.lineTo(18076.6f, 31390.9f); |
| 322 | path.lineTo(18085.1f, 31390.5f); |
| 323 | path.lineTo(18085.1f, 31390.5f); |
| 324 | path.lineTo(18076.6f, 31390.9f); |
| 325 | path.lineTo(18076.6f, 31390.9f); |
| 326 | path.lineTo(17955, 31460.3f); |
| 327 | path.lineTo(17955, 31460.3f); |
| 328 | path.lineTo(17963.5f, 31459.9f); |
| 329 | path.lineTo(17963.5f, 31459.9f); |
| 330 | path.lineTo(17971.9f, 31459.5f); |
| 331 | path.lineTo(17971.9f, 31459.5f); |
| 332 | path.lineTo(17.9551f, 21.6205f); |
| 333 | path.lineTo(17.9551f, 21.6205f); |
| 334 | path.lineTo(9.47091f, 22.0561f); |
| 335 | path.lineTo(9.47091f, 22.0561f); |
| 336 | path.lineTo(17.9459f, 21.6344f); |
| 337 | path.lineTo(17.9459f, 21.6344f); |
| 338 | path.close();path.moveTo(0.995934f, 22.4779f); |
| 339 | path.lineTo(0.986725f, 22.4918f); |
| 340 | path.lineTo(0.986725f, 22.4918f); |
| 341 | path.lineTo(17955, 31460.4f); |
| 342 | path.lineTo(17955, 31460.4f); |
| 343 | path.lineTo(17971.9f, 31459.5f); |
| 344 | path.lineTo(17971.9f, 31459.5f); |
| 345 | path.lineTo(18093.6f, 31390.1f); |
| 346 | path.lineTo(18093.6f, 31390.1f); |
| 347 | path.lineTo(18093.6f, 31390); |
| 348 | path.lineTo(18093.6f, 31390); |
| 349 | path.lineTo(139.555f, -47.8244f); |
| 350 | path.lineTo(139.555f, -47.8244f); |
| 351 | path.lineTo(122.595f, -46.9671f); |
| 352 | path.lineTo(122.595f, -46.9671f); |
| 353 | path.lineTo(0.995934f, 22.4779f); |
| 354 | path.lineTo(0.995934f, 22.4779f); |
| 355 | path.close(); |
| 356 | path.moveTo(5.43941f, 25.5223f); |
| 357 | path.lineTo(798267, -28871.1f); |
| 358 | path.lineTo(798267, -28871.1f); |
| 359 | path.lineTo(3.12512e+06f, -113102); |
| 360 | path.lineTo(3.12512e+06f, -113102); |
| 361 | path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813); |
| 362 | path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f); |
| 363 | path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f); |
| 364 | path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f); |
| 365 | path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f); |
| 366 | path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f); |
| 367 | path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f); |
| 368 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 369 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 370 | path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f); |
| 371 | path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f); |
| 372 | path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f); |
| 373 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 374 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 375 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 376 | path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f); |
| 377 | path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f); |
| 378 | path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f); |
| 379 | path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f); |
| 380 | path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f); |
| 381 | path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814); |
| 382 | path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103); |
| 383 | path.lineTo(798284, -28872); |
| 384 | path.lineTo(798284, -28872); |
| 385 | path.lineTo(22.4044f, 24.6677f); |
| 386 | path.lineTo(22.4044f, 24.6677f); |
| 387 | path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f); |
| 388 | path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f); |
| 389 | path.close(); |
| 390 | } |
| 391 | |
| 392 | static void build_path_simple_170666(SkPath& path) { |
| 393 | path.moveTo(126.677f, 24.1591f); |
| 394 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 395 | } |
| 396 | |
| 397 | // This used to assert in the SK_DEBUG build, as the clip step would fail with |
| 398 | // too-few interations in our cubic-line intersection code. That code now runs |
| 399 | // 24 interations (instead of 16). |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 400 | static void test_crbug_170666() { |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 401 | SkPath path; |
| 402 | SkPaint paint; |
| 403 | paint.setAntiAlias(true); |
| 404 | |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 405 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(1000, 1000)); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 406 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 407 | build_path_simple_170666(path); |
| 408 | surface->getCanvas()->drawPath(path, paint); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 409 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 410 | build_path_170666(path); |
| 411 | surface->getCanvas()->drawPath(path, paint); |
| 412 | } |
| 413 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 414 | static void test_addrect(skiatest::Reporter* reporter) { |
| 415 | SkPath path; |
| 416 | path.lineTo(0, 0); |
| 417 | path.addRect(SkRect::MakeWH(50, 100)); |
| 418 | REPORTER_ASSERT(reporter, path.isRect(NULL)); |
| 419 | |
| 420 | path.reset(); |
| 421 | path.lineTo(FLT_EPSILON, FLT_EPSILON); |
| 422 | path.addRect(SkRect::MakeWH(50, 100)); |
| 423 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 424 | |
| 425 | path.reset(); |
| 426 | path.quadTo(0, 0, 0, 0); |
| 427 | path.addRect(SkRect::MakeWH(50, 100)); |
| 428 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 429 | |
| 430 | path.reset(); |
| 431 | path.conicTo(0, 0, 0, 0, 0.5f); |
| 432 | path.addRect(SkRect::MakeWH(50, 100)); |
| 433 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 434 | |
| 435 | path.reset(); |
| 436 | path.cubicTo(0, 0, 0, 0, 0, 0); |
| 437 | path.addRect(SkRect::MakeWH(50, 100)); |
| 438 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 439 | } |
| 440 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 441 | // Make sure we stay non-finite once we get there (unless we reset or rewind). |
| 442 | static void test_addrect_isfinite(skiatest::Reporter* reporter) { |
| 443 | SkPath path; |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 444 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 445 | path.addRect(SkRect::MakeWH(50, 100)); |
| 446 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 447 | |
| 448 | path.moveTo(0, 0); |
| 449 | path.lineTo(SK_ScalarInfinity, 42); |
| 450 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 451 | |
| 452 | path.addRect(SkRect::MakeWH(50, 100)); |
| 453 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 454 | |
| 455 | path.reset(); |
| 456 | REPORTER_ASSERT(reporter, path.isFinite()); |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 457 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 458 | path.addRect(SkRect::MakeWH(50, 100)); |
| 459 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 460 | } |
| 461 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 462 | static void build_big_path(SkPath* path, bool reducedCase) { |
| 463 | if (reducedCase) { |
| 464 | path->moveTo(577330, 1971.72f); |
| 465 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 466 | } else { |
| 467 | path->moveTo(60.1631f, 7.70567f); |
| 468 | path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f); |
| 469 | path->lineTo(577379, 1977.77f); |
| 470 | path->quadTo(577364, 1979.57f, 577325, 1980.26f); |
| 471 | path->quadTo(577286, 1980.95f, 577245, 1980.13f); |
| 472 | path->quadTo(577205, 1979.3f, 577187, 1977.45f); |
| 473 | path->quadTo(577168, 1975.6f, 577183, 1973.8f); |
| 474 | path->quadTo(577198, 1972, 577238, 1971.31f); |
| 475 | path->quadTo(577277, 1970.62f, 577317, 1971.45f); |
| 476 | path->quadTo(577330, 1971.72f, 577341, 1972.11f); |
| 477 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 478 | path->moveTo(306.718f, -32.912f); |
| 479 | path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f); |
| 480 | } |
| 481 | } |
| 482 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 483 | static void test_clipped_cubic() { |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 484 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(640, 480)); |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 485 | |
| 486 | // This path used to assert, because our cubic-chopping code incorrectly |
| 487 | // moved control points after the chop. This test should be run in SK_DEBUG |
| 488 | // mode to ensure that we no long assert. |
| 489 | SkPath path; |
| 490 | for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) { |
| 491 | build_big_path(&path, SkToBool(doReducedCase)); |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 492 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 493 | SkPaint paint; |
| 494 | for (int doAA = 0; doAA <= 1; ++doAA) { |
| 495 | paint.setAntiAlias(SkToBool(doAA)); |
| 496 | surface->getCanvas()->drawPath(path, paint); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 501 | // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/ |
| 502 | // which triggered an assert, from a tricky cubic. This test replicates that |
| 503 | // example, so we can ensure that we handle it (in SkEdge.cpp), and don't |
| 504 | // assert in the SK_DEBUG build. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 505 | static void test_tricky_cubic() { |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 506 | const SkPoint pts[] = { |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 507 | { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) }, |
| 508 | { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) }, |
| 509 | { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) }, |
| 510 | { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) }, |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | SkPath path; |
| 514 | path.moveTo(pts[0]); |
| 515 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 516 | |
| 517 | SkPaint paint; |
| 518 | paint.setAntiAlias(true); |
| 519 | |
reed | 3054be1 | 2014-12-10 07:24:28 -0800 | [diff] [blame] | 520 | SkSurface* surface = SkSurface::NewRasterN32Premul(19, 130); |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 521 | surface->getCanvas()->drawPath(path, paint); |
| 522 | surface->unref(); |
| 523 | } |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 524 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 525 | // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651 |
| 526 | // |
| 527 | static void test_isfinite_after_transform(skiatest::Reporter* reporter) { |
| 528 | SkPath path; |
| 529 | path.quadTo(157, 366, 286, 208); |
| 530 | path.arcTo(37, 442, 315, 163, 957494590897113.0f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 531 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 532 | SkMatrix matrix; |
| 533 | matrix.setScale(1000*1000, 1000*1000); |
| 534 | |
| 535 | // Be sure that path::transform correctly updates isFinite and the bounds |
| 536 | // if the transformation overflows. The previous bug was that isFinite was |
| 537 | // set to true in this case, but the bounds were not set to empty (which |
| 538 | // they should be). |
| 539 | while (path.isFinite()) { |
| 540 | REPORTER_ASSERT(reporter, path.getBounds().isFinite()); |
| 541 | REPORTER_ASSERT(reporter, !path.getBounds().isEmpty()); |
| 542 | path.transform(matrix); |
| 543 | } |
| 544 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 545 | |
| 546 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 547 | path.transform(matrix); |
| 548 | // we need to still be non-finite |
| 549 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 550 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 551 | } |
| 552 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 553 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 554 | SkScalar xIn, SkScalar yIn, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 555 | int startAngle) |
| 556 | { |
| 557 | |
| 558 | SkScalar rx = SkMinScalar(rect.width(), xIn); |
| 559 | SkScalar ry = SkMinScalar(rect.height(), yIn); |
| 560 | |
| 561 | SkRect arcRect; |
| 562 | arcRect.set(-rx, -ry, rx, ry); |
| 563 | switch (startAngle) { |
| 564 | case 0: |
| 565 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); |
| 566 | break; |
| 567 | case 90: |
| 568 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); |
| 569 | break; |
| 570 | case 180: |
| 571 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); |
| 572 | break; |
| 573 | case 270: |
| 574 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); |
| 575 | break; |
| 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); |
| 581 | } |
| 582 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 583 | static void make_arb_round_rect(SkPath* path, const SkRect& r, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 584 | SkScalar xCorner, SkScalar yCorner) { |
| 585 | // we are lazy here and use the same x & y for each corner |
| 586 | add_corner_arc(path, r, xCorner, yCorner, 270); |
| 587 | add_corner_arc(path, r, xCorner, yCorner, 0); |
| 588 | add_corner_arc(path, r, xCorner, yCorner, 90); |
| 589 | add_corner_arc(path, r, xCorner, yCorner, 180); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 590 | path->close(); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // Chrome creates its own round rects with each corner possibly being different. |
| 594 | // Performance will suffer if they are not convex. |
| 595 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 596 | // the same test (but with drawing) |
| 597 | static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 598 | SkRandom rand; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 599 | SkRect r; |
| 600 | |
| 601 | for (int i = 0; i < 5000; ++i) { |
| 602 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 603 | SkScalar size = rand.nextUScalar1() * 30; |
| 604 | if (size < SK_Scalar1) { |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 605 | continue; |
| 606 | } |
| 607 | r.fLeft = rand.nextUScalar1() * 300; |
| 608 | r.fTop = rand.nextUScalar1() * 300; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 609 | r.fRight = r.fLeft + 2 * size; |
| 610 | r.fBottom = r.fTop + 2 * size; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 611 | |
| 612 | SkPath temp; |
| 613 | |
| 614 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 615 | |
| 616 | REPORTER_ASSERT(reporter, temp.isConvex()); |
| 617 | } |
| 618 | } |
| 619 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 620 | // Chrome will sometimes create a 0 radius round rect. The degenerate |
| 621 | // quads prevent the path from being converted to a rect |
| 622 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 623 | // the same test (but with drawing) |
| 624 | static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 625 | SkRandom rand; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 626 | SkRect r; |
| 627 | |
| 628 | for (int i = 0; i < 5000; ++i) { |
| 629 | |
| 630 | SkScalar size = rand.nextUScalar1() * 30; |
| 631 | if (size < SK_Scalar1) { |
| 632 | continue; |
| 633 | } |
| 634 | r.fLeft = rand.nextUScalar1() * 300; |
| 635 | r.fTop = rand.nextUScalar1() * 300; |
| 636 | r.fRight = r.fLeft + 2 * size; |
| 637 | r.fBottom = r.fTop + 2 * size; |
| 638 | |
| 639 | SkPath temp; |
| 640 | |
| 641 | make_arb_round_rect(&temp, r, 0, 0); |
| 642 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 643 | SkRect result; |
| 644 | REPORTER_ASSERT(reporter, temp.isRect(&result)); |
| 645 | REPORTER_ASSERT(reporter, r == result); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 649 | static void test_rect_isfinite(skiatest::Reporter* reporter) { |
| 650 | const SkScalar inf = SK_ScalarInfinity; |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 651 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 652 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 653 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 654 | SkRect r; |
| 655 | r.setEmpty(); |
| 656 | REPORTER_ASSERT(reporter, r.isFinite()); |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 657 | r.set(0, 0, inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 658 | REPORTER_ASSERT(reporter, !r.isFinite()); |
| 659 | r.set(0, 0, nan, 0); |
| 660 | REPORTER_ASSERT(reporter, !r.isFinite()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 661 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 662 | SkPoint pts[] = { |
| 663 | { 0, 0 }, |
| 664 | { SK_Scalar1, 0 }, |
| 665 | { 0, SK_Scalar1 }, |
| 666 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 667 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 668 | bool isFine = r.setBoundsCheck(pts, 3); |
| 669 | REPORTER_ASSERT(reporter, isFine); |
| 670 | REPORTER_ASSERT(reporter, !r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 671 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 672 | pts[1].set(inf, 0); |
| 673 | isFine = r.setBoundsCheck(pts, 3); |
| 674 | REPORTER_ASSERT(reporter, !isFine); |
| 675 | REPORTER_ASSERT(reporter, r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 676 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 677 | pts[1].set(nan, 0); |
| 678 | isFine = r.setBoundsCheck(pts, 3); |
| 679 | REPORTER_ASSERT(reporter, !isFine); |
| 680 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 681 | } |
| 682 | |
| 683 | static void test_path_isfinite(skiatest::Reporter* reporter) { |
| 684 | const SkScalar inf = SK_ScalarInfinity; |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 685 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 686 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 687 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 688 | SkPath path; |
| 689 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 690 | |
| 691 | path.reset(); |
| 692 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 693 | |
| 694 | path.reset(); |
| 695 | path.moveTo(SK_Scalar1, 0); |
| 696 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 697 | |
| 698 | path.reset(); |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 699 | path.moveTo(inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 700 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 701 | |
| 702 | path.reset(); |
| 703 | path.moveTo(nan, 0); |
| 704 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 705 | } |
| 706 | |
| 707 | static void test_isfinite(skiatest::Reporter* reporter) { |
| 708 | test_rect_isfinite(reporter); |
| 709 | test_path_isfinite(reporter); |
| 710 | } |
| 711 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 712 | // assert that we always |
| 713 | // start with a moveTo |
| 714 | // only have 1 moveTo |
| 715 | // only have Lines after that |
| 716 | // end with a single close |
| 717 | // only have (at most) 1 close |
| 718 | // |
| 719 | static void test_poly(skiatest::Reporter* reporter, const SkPath& path, |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 720 | const SkPoint srcPts[], bool expectClose) { |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 721 | SkPath::RawIter iter(path); |
| 722 | SkPoint pts[4]; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 723 | |
| 724 | bool firstTime = true; |
| 725 | bool foundClose = false; |
| 726 | for (;;) { |
| 727 | switch (iter.next(pts)) { |
| 728 | case SkPath::kMove_Verb: |
| 729 | REPORTER_ASSERT(reporter, firstTime); |
| 730 | REPORTER_ASSERT(reporter, pts[0] == srcPts[0]); |
| 731 | srcPts++; |
| 732 | firstTime = false; |
| 733 | break; |
| 734 | case SkPath::kLine_Verb: |
| 735 | REPORTER_ASSERT(reporter, !firstTime); |
| 736 | REPORTER_ASSERT(reporter, pts[1] == srcPts[0]); |
| 737 | srcPts++; |
| 738 | break; |
| 739 | case SkPath::kQuad_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 740 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 741 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 742 | case SkPath::kConic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 743 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 744 | break; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 745 | case SkPath::kCubic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 746 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 747 | break; |
| 748 | case SkPath::kClose_Verb: |
| 749 | REPORTER_ASSERT(reporter, !firstTime); |
| 750 | REPORTER_ASSERT(reporter, !foundClose); |
| 751 | REPORTER_ASSERT(reporter, expectClose); |
| 752 | foundClose = true; |
| 753 | break; |
| 754 | case SkPath::kDone_Verb: |
| 755 | goto DONE; |
| 756 | } |
| 757 | } |
| 758 | DONE: |
| 759 | REPORTER_ASSERT(reporter, foundClose == expectClose); |
| 760 | } |
| 761 | |
| 762 | static void test_addPoly(skiatest::Reporter* reporter) { |
| 763 | SkPoint pts[32]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 764 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 765 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 766 | for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) { |
| 767 | pts[i].fX = rand.nextSScalar1(); |
| 768 | pts[i].fY = rand.nextSScalar1(); |
| 769 | } |
| 770 | |
| 771 | for (int doClose = 0; doClose <= 1; ++doClose) { |
| 772 | for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) { |
| 773 | SkPath path; |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 774 | path.addPoly(pts, SkToInt(count), SkToBool(doClose)); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 775 | test_poly(reporter, path, pts, SkToBool(doClose)); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 780 | static void test_strokerec(skiatest::Reporter* reporter) { |
| 781 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 782 | REPORTER_ASSERT(reporter, rec.isFillStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 783 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 784 | rec.setHairlineStyle(); |
| 785 | REPORTER_ASSERT(reporter, rec.isHairlineStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 786 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 787 | rec.setStrokeStyle(SK_Scalar1, false); |
| 788 | REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 789 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 790 | rec.setStrokeStyle(SK_Scalar1, true); |
| 791 | REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 792 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 793 | rec.setStrokeStyle(0, false); |
| 794 | REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 795 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 796 | rec.setStrokeStyle(0, true); |
| 797 | REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle()); |
| 798 | } |
| 799 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 800 | // Set this for paths that don't have a consistent direction such as a bowtie. |
| 801 | // (cheapComputeDirection is not expected to catch these.) |
| 802 | static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1); |
| 803 | |
| 804 | static void check_direction(skiatest::Reporter* reporter, const SkPath& path, |
| 805 | SkPath::Direction expected) { |
| 806 | if (expected == kDontCheckDir) { |
| 807 | return; |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 808 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 809 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 810 | |
| 811 | SkPath::Direction dir; |
| 812 | if (copy.cheapComputeDirection(&dir)) { |
| 813 | REPORTER_ASSERT(reporter, dir == expected); |
| 814 | } else { |
| 815 | REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected); |
| 816 | } |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 817 | } |
| 818 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 819 | static void test_direction(skiatest::Reporter* reporter) { |
| 820 | size_t i; |
| 821 | SkPath path; |
| 822 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 823 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction)); |
| 824 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction)); |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 825 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction)); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 826 | |
| 827 | static const char* gDegen[] = { |
| 828 | "M 10 10", |
| 829 | "M 10 10 M 20 20", |
| 830 | "M 10 10 L 20 20", |
| 831 | "M 10 10 L 10 10 L 10 10", |
| 832 | "M 10 10 Q 10 10 10 10", |
| 833 | "M 10 10 C 10 10 10 10 10 10", |
| 834 | }; |
| 835 | for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) { |
| 836 | path.reset(); |
| 837 | bool valid = SkParsePath::FromSVGString(gDegen[i], &path); |
| 838 | REPORTER_ASSERT(reporter, valid); |
| 839 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 840 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 841 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 842 | static const char* gCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 843 | "M 10 10 L 10 10 Q 20 10 20 20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 844 | "M 10 10 C 20 10 20 20 20 20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 845 | "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 846 | // rect with top two corners replaced by cubics with identical middle |
| 847 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 848 | "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10", |
| 849 | "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 850 | }; |
| 851 | for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) { |
| 852 | path.reset(); |
| 853 | bool valid = SkParsePath::FromSVGString(gCW[i], &path); |
| 854 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 855 | check_direction(reporter, path, SkPath::kCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 856 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 857 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 858 | static const char* gCCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 859 | "M 10 10 L 10 10 Q 20 10 20 -20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 860 | "M 10 10 C 20 10 20 -20 20 -20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 861 | "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 862 | // rect with top two corners replaced by cubics with identical middle |
| 863 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 864 | "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10", |
| 865 | "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 866 | }; |
| 867 | for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) { |
| 868 | path.reset(); |
| 869 | bool valid = SkParsePath::FromSVGString(gCCW[i], &path); |
| 870 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 871 | check_direction(reporter, path, SkPath::kCCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 872 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 873 | |
| 874 | // Test two donuts, each wound a different direction. Only the outer contour |
| 875 | // determines the cheap direction |
| 876 | path.reset(); |
| 877 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction); |
| 878 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 879 | check_direction(reporter, path, SkPath::kCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 880 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 881 | path.reset(); |
| 882 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction); |
| 883 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 884 | check_direction(reporter, path, SkPath::kCCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 885 | |
| 886 | // triangle with one point really far from the origin. |
| 887 | path.reset(); |
| 888 | // the first point is roughly 1.05e10, 1.05e10 |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 889 | path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652)); |
bsalomon@google.com | 53aab78 | 2012-02-23 14:54:49 +0000 | [diff] [blame] | 890 | path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1); |
| 891 | path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 892 | check_direction(reporter, path, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 893 | |
| 894 | path.reset(); |
| 895 | path.conicTo(20, 0, 20, 20, 0.5f); |
| 896 | path.close(); |
| 897 | check_direction(reporter, path, SkPath::kCW_Direction); |
| 898 | |
| 899 | path.reset(); |
| 900 | path.lineTo(1, 1e7f); |
| 901 | path.lineTo(1e7f, 2e7f); |
| 902 | path.close(); |
| 903 | REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity()); |
| 904 | check_direction(reporter, path, SkPath::kCCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 905 | } |
| 906 | |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 907 | static void add_rect(SkPath* path, const SkRect& r) { |
| 908 | path->moveTo(r.fLeft, r.fTop); |
| 909 | path->lineTo(r.fRight, r.fTop); |
| 910 | path->lineTo(r.fRight, r.fBottom); |
| 911 | path->lineTo(r.fLeft, r.fBottom); |
| 912 | path->close(); |
| 913 | } |
| 914 | |
| 915 | static void test_bounds(skiatest::Reporter* reporter) { |
| 916 | static const SkRect rects[] = { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 917 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, |
| 918 | { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, |
| 919 | { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, |
| 920 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 921 | }; |
| 922 | |
| 923 | SkPath path0, path1; |
| 924 | for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { |
| 925 | path0.addRect(rects[i]); |
| 926 | add_rect(&path1, rects[i]); |
| 927 | } |
| 928 | |
| 929 | REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); |
| 930 | } |
| 931 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 932 | static void stroke_cubic(const SkPoint pts[4]) { |
| 933 | SkPath path; |
| 934 | path.moveTo(pts[0]); |
| 935 | path.cubicTo(pts[1], pts[2], pts[3]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 936 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 937 | SkPaint paint; |
| 938 | paint.setStyle(SkPaint::kStroke_Style); |
| 939 | paint.setStrokeWidth(SK_Scalar1 * 2); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 940 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 941 | SkPath fill; |
| 942 | paint.getFillPath(path, &fill); |
| 943 | } |
| 944 | |
| 945 | // just ensure this can run w/o any SkASSERTS firing in the debug build |
| 946 | // we used to assert due to differences in how we determine a degenerate vector |
| 947 | // but that was fixed with the introduction of SkPoint::CanNormalize |
| 948 | static void stroke_tiny_cubic() { |
| 949 | SkPoint p0[] = { |
| 950 | { 372.0f, 92.0f }, |
| 951 | { 372.0f, 92.0f }, |
| 952 | { 372.0f, 92.0f }, |
| 953 | { 372.0f, 92.0f }, |
| 954 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 955 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 956 | stroke_cubic(p0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 957 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 958 | SkPoint p1[] = { |
| 959 | { 372.0f, 92.0f }, |
| 960 | { 372.0007f, 92.000755f }, |
| 961 | { 371.99927f, 92.003922f }, |
| 962 | { 371.99826f, 92.003899f }, |
| 963 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 964 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 965 | stroke_cubic(p1); |
| 966 | } |
| 967 | |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 968 | static void check_close(skiatest::Reporter* reporter, const SkPath& path) { |
| 969 | for (int i = 0; i < 2; ++i) { |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 970 | SkPath::Iter iter(path, SkToBool(i)); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 971 | SkPoint mv; |
| 972 | SkPoint pts[4]; |
| 973 | SkPath::Verb v; |
| 974 | int nMT = 0; |
| 975 | int nCL = 0; |
tomhudson@google.com | 221db3c | 2011-07-28 21:10:29 +0000 | [diff] [blame] | 976 | mv.set(0, 0); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 977 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 978 | switch (v) { |
| 979 | case SkPath::kMove_Verb: |
| 980 | mv = pts[0]; |
| 981 | ++nMT; |
| 982 | break; |
| 983 | case SkPath::kClose_Verb: |
| 984 | REPORTER_ASSERT(reporter, mv == pts[0]); |
| 985 | ++nCL; |
| 986 | break; |
| 987 | default: |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | // if we force a close on the interator we should have a close |
| 992 | // for every moveTo |
| 993 | REPORTER_ASSERT(reporter, !i || nMT == nCL); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | static void test_close(skiatest::Reporter* reporter) { |
| 998 | SkPath closePt; |
| 999 | closePt.moveTo(0, 0); |
| 1000 | closePt.close(); |
| 1001 | check_close(reporter, closePt); |
| 1002 | |
| 1003 | SkPath openPt; |
| 1004 | openPt.moveTo(0, 0); |
| 1005 | check_close(reporter, openPt); |
| 1006 | |
| 1007 | SkPath empty; |
| 1008 | check_close(reporter, empty); |
| 1009 | empty.close(); |
| 1010 | check_close(reporter, empty); |
| 1011 | |
| 1012 | SkPath rect; |
| 1013 | rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 1014 | check_close(reporter, rect); |
| 1015 | rect.close(); |
| 1016 | check_close(reporter, rect); |
| 1017 | |
| 1018 | SkPath quad; |
| 1019 | quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 1020 | check_close(reporter, quad); |
| 1021 | quad.close(); |
| 1022 | check_close(reporter, quad); |
| 1023 | |
| 1024 | SkPath cubic; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1025 | quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1026 | 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1); |
| 1027 | check_close(reporter, cubic); |
| 1028 | cubic.close(); |
| 1029 | check_close(reporter, cubic); |
| 1030 | |
| 1031 | SkPath line; |
| 1032 | line.moveTo(SK_Scalar1, SK_Scalar1); |
| 1033 | line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1); |
| 1034 | check_close(reporter, line); |
| 1035 | line.close(); |
| 1036 | check_close(reporter, line); |
| 1037 | |
| 1038 | SkPath rect2; |
| 1039 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 1040 | rect2.close(); |
| 1041 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 1042 | check_close(reporter, rect2); |
| 1043 | rect2.close(); |
| 1044 | check_close(reporter, rect2); |
| 1045 | |
| 1046 | SkPath oval3; |
| 1047 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100)); |
| 1048 | oval3.close(); |
| 1049 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200)); |
| 1050 | check_close(reporter, oval3); |
| 1051 | oval3.close(); |
| 1052 | check_close(reporter, oval3); |
| 1053 | |
| 1054 | SkPath moves; |
| 1055 | moves.moveTo(SK_Scalar1, SK_Scalar1); |
| 1056 | moves.moveTo(5 * SK_Scalar1, SK_Scalar1); |
| 1057 | moves.moveTo(SK_Scalar1, 10 * SK_Scalar1); |
| 1058 | moves.moveTo(10 *SK_Scalar1, SK_Scalar1); |
| 1059 | check_close(reporter, moves); |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 1060 | |
| 1061 | stroke_tiny_cubic(); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1064 | static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, |
| 1065 | SkPath::Convexity expected) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1066 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 1067 | SkPath::Convexity c = copy.getConvexity(); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1068 | REPORTER_ASSERT(reporter, c == expected); |
| 1069 | } |
| 1070 | |
| 1071 | static void test_convexity2(skiatest::Reporter* reporter) { |
| 1072 | SkPath pt; |
| 1073 | pt.moveTo(0, 0); |
| 1074 | pt.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 1075 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1076 | check_direction(reporter, pt, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1077 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1078 | SkPath line; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1079 | line.moveTo(12*SK_Scalar1, 20*SK_Scalar1); |
| 1080 | line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1081 | line.close(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1082 | check_convexity(reporter, line, SkPath::kConvex_Convexity); |
| 1083 | check_direction(reporter, line, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1084 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1085 | SkPath triLeft; |
| 1086 | triLeft.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1087 | triLeft.lineTo(SK_Scalar1, 0); |
| 1088 | triLeft.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1089 | triLeft.close(); |
| 1090 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1091 | check_direction(reporter, triLeft, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1092 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1093 | SkPath triRight; |
| 1094 | triRight.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1095 | triRight.lineTo(-SK_Scalar1, 0); |
| 1096 | triRight.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1097 | triRight.close(); |
| 1098 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1099 | check_direction(reporter, triRight, SkPath::kCCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1100 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1101 | SkPath square; |
| 1102 | square.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1103 | square.lineTo(SK_Scalar1, 0); |
| 1104 | square.lineTo(SK_Scalar1, SK_Scalar1); |
| 1105 | square.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1106 | square.close(); |
| 1107 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1108 | check_direction(reporter, square, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1109 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1110 | SkPath redundantSquare; |
| 1111 | redundantSquare.moveTo(0, 0); |
| 1112 | redundantSquare.lineTo(0, 0); |
| 1113 | redundantSquare.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1114 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1115 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1116 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1117 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1118 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1119 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1120 | redundantSquare.lineTo(0, SK_Scalar1); |
| 1121 | redundantSquare.lineTo(0, SK_Scalar1); |
| 1122 | redundantSquare.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1123 | redundantSquare.close(); |
| 1124 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1125 | check_direction(reporter, redundantSquare, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1126 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1127 | SkPath bowTie; |
| 1128 | bowTie.moveTo(0, 0); |
| 1129 | bowTie.lineTo(0, 0); |
| 1130 | bowTie.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1131 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1132 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1133 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1134 | bowTie.lineTo(SK_Scalar1, 0); |
| 1135 | bowTie.lineTo(SK_Scalar1, 0); |
| 1136 | bowTie.lineTo(SK_Scalar1, 0); |
| 1137 | bowTie.lineTo(0, SK_Scalar1); |
| 1138 | bowTie.lineTo(0, SK_Scalar1); |
| 1139 | bowTie.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1140 | bowTie.close(); |
| 1141 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1142 | check_direction(reporter, bowTie, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1143 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1144 | SkPath spiral; |
| 1145 | spiral.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1146 | spiral.lineTo(100*SK_Scalar1, 0); |
| 1147 | spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 1148 | spiral.lineTo(0, 100*SK_Scalar1); |
| 1149 | spiral.lineTo(0, 50*SK_Scalar1); |
| 1150 | spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1); |
| 1151 | spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1152 | spiral.close(); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 1153 | check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1154 | check_direction(reporter, spiral, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1155 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1156 | SkPath dent; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1157 | dent.moveTo(0, 0); |
| 1158 | dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 1159 | dent.lineTo(0, 100*SK_Scalar1); |
| 1160 | dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1); |
| 1161 | dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1162 | dent.close(); |
| 1163 | check_convexity(reporter, dent, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1164 | check_direction(reporter, dent, SkPath::kCW_Direction); |
commit-bot@chromium.org | 8be07bb | 2014-05-22 14:58:53 +0000 | [diff] [blame] | 1165 | |
| 1166 | // http://skbug.com/2235 |
| 1167 | SkPath strokedSin; |
| 1168 | for (int i = 0; i < 2000; i++) { |
| 1169 | SkScalar x = SkIntToScalar(i) / 2; |
| 1170 | SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3; |
| 1171 | if (0 == i) { |
| 1172 | strokedSin.moveTo(x, y); |
| 1173 | } else { |
| 1174 | strokedSin.lineTo(x, y); |
| 1175 | } |
| 1176 | } |
| 1177 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 1178 | stroke.setStrokeStyle(2 * SK_Scalar1); |
| 1179 | stroke.applyToPath(&strokedSin, strokedSin); |
| 1180 | check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity); |
| 1181 | check_direction(reporter, strokedSin, kDontCheckDir); |
robertphillips | c506e30 | 2014-09-16 09:43:31 -0700 | [diff] [blame] | 1182 | |
| 1183 | // http://crbug.com/412640 |
| 1184 | SkPath degenerateConcave; |
| 1185 | degenerateConcave.moveTo(148.67912f, 191.875f); |
| 1186 | degenerateConcave.lineTo(470.37695f, 7.5f); |
| 1187 | degenerateConcave.lineTo(148.67912f, 191.875f); |
| 1188 | degenerateConcave.lineTo(41.446522f, 376.25f); |
| 1189 | degenerateConcave.lineTo(-55.971577f, 460.0f); |
| 1190 | degenerateConcave.lineTo(41.446522f, 376.25f); |
| 1191 | check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity); |
| 1192 | check_direction(reporter, degenerateConcave, SkPath::kUnknown_Direction); |
robertphillips | f52a063 | 2014-11-17 12:11:42 -0800 | [diff] [blame] | 1193 | |
| 1194 | // http://crbug.com/433683 |
| 1195 | SkPath badFirstVector; |
| 1196 | badFirstVector.moveTo(501.087708f, 319.610352f); |
| 1197 | badFirstVector.lineTo(501.087708f, 319.610352f); |
| 1198 | badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f); |
| 1199 | badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f); |
| 1200 | badFirstVector.lineTo(301.557678f, 98.044601f); |
| 1201 | badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f); |
| 1202 | badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f); |
| 1203 | badFirstVector.lineTo(504.912292f, 316.389648f); |
| 1204 | badFirstVector.lineTo(501.087708f, 319.610352f); |
| 1205 | badFirstVector.close(); |
| 1206 | check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1209 | static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, |
| 1210 | const SkRect& bounds) { |
| 1211 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 1212 | REPORTER_ASSERT(reporter, p.getBounds() == bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 1213 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1214 | SkPath p2(p); |
| 1215 | REPORTER_ASSERT(reporter, p2.isConvex()); |
| 1216 | REPORTER_ASSERT(reporter, p2.getBounds() == bounds); |
| 1217 | |
| 1218 | SkPath other; |
| 1219 | other.swap(p2); |
| 1220 | REPORTER_ASSERT(reporter, other.isConvex()); |
| 1221 | REPORTER_ASSERT(reporter, other.getBounds() == bounds); |
| 1222 | } |
| 1223 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1224 | static void setFromString(SkPath* path, const char str[]) { |
| 1225 | bool first = true; |
| 1226 | while (str) { |
| 1227 | SkScalar x, y; |
| 1228 | str = SkParse::FindScalar(str, &x); |
| 1229 | if (NULL == str) { |
| 1230 | break; |
| 1231 | } |
| 1232 | str = SkParse::FindScalar(str, &y); |
| 1233 | SkASSERT(str); |
| 1234 | if (first) { |
| 1235 | path->moveTo(x, y); |
| 1236 | first = false; |
| 1237 | } else { |
| 1238 | path->lineTo(x, y); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | static void test_convexity(skiatest::Reporter* reporter) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1244 | SkPath path; |
| 1245 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1246 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1247 | path.addCircle(0, 0, SkIntToScalar(10)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1248 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1249 | path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1250 | check_convexity(reporter, path, SkPath::kConcave_Convexity); |
| 1251 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1252 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1253 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1254 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1255 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1256 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1257 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1258 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1259 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1260 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1261 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1262 | static const struct { |
| 1263 | const char* fPathStr; |
| 1264 | SkPath::Convexity fExpectedConvexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1265 | SkPath::Direction fExpectedDirection; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1266 | } gRec[] = { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1267 | { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1268 | { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1269 | { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1270 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction }, |
| 1271 | { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction }, |
| 1272 | { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction }, |
| 1273 | { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir }, |
| 1274 | { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction }, |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1275 | }; |
| 1276 | |
| 1277 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 1278 | SkPath path; |
| 1279 | setFromString(&path, gRec[i].fPathStr); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1280 | check_convexity(reporter, path, gRec[i].fExpectedConvexity); |
| 1281 | check_direction(reporter, path, gRec[i].fExpectedDirection); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1282 | // check after setting the initial convex and direction |
| 1283 | if (kDontCheckDir != gRec[i].fExpectedDirection) { |
| 1284 | SkPath copy(path); |
| 1285 | SkPath::Direction dir; |
| 1286 | bool foundDir = copy.cheapComputeDirection(&dir); |
| 1287 | REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction) |
| 1288 | ^ foundDir); |
| 1289 | REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir); |
| 1290 | check_convexity(reporter, copy, gRec[i].fExpectedConvexity); |
| 1291 | } |
| 1292 | REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity()); |
| 1293 | check_direction(reporter, path, gRec[i].fExpectedDirection); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1294 | } |
caryclark | d3d1a98 | 2014-12-08 04:57:38 -0800 | [diff] [blame] | 1295 | |
| 1296 | static const SkPoint nonFinitePts[] = { |
| 1297 | { SK_ScalarInfinity, 0 }, |
| 1298 | { 0, SK_ScalarInfinity }, |
| 1299 | { SK_ScalarInfinity, SK_ScalarInfinity }, |
| 1300 | { SK_ScalarNegativeInfinity, 0}, |
| 1301 | { 0, SK_ScalarNegativeInfinity }, |
| 1302 | { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity }, |
| 1303 | { SK_ScalarNegativeInfinity, SK_ScalarInfinity }, |
| 1304 | { SK_ScalarInfinity, SK_ScalarNegativeInfinity }, |
| 1305 | { SK_ScalarNaN, 0 }, |
| 1306 | { 0, SK_ScalarNaN }, |
| 1307 | { SK_ScalarNaN, SK_ScalarNaN }, |
| 1308 | }; |
| 1309 | |
| 1310 | const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]); |
| 1311 | |
| 1312 | static const SkPoint finitePts[] = { |
| 1313 | { SK_ScalarMax, 0 }, |
| 1314 | { 0, SK_ScalarMax }, |
| 1315 | { SK_ScalarMax, SK_ScalarMax }, |
| 1316 | { SK_ScalarMin, 0 }, |
| 1317 | { 0, SK_ScalarMin }, |
| 1318 | { SK_ScalarMin, SK_ScalarMin }, |
| 1319 | }; |
| 1320 | |
| 1321 | const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]); |
| 1322 | |
| 1323 | for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) { |
| 1324 | int i = (int) (index % nonFinitePtsCount); |
| 1325 | int f = (int) (index % finitePtsCount); |
| 1326 | int g = (int) ((f + 1) % finitePtsCount); |
| 1327 | path.reset(); |
| 1328 | switch (index % 13) { |
| 1329 | case 0: path.lineTo(nonFinitePts[i]); break; |
| 1330 | case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break; |
| 1331 | case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break; |
| 1332 | case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break; |
| 1333 | case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break; |
| 1334 | case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break; |
| 1335 | case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break; |
| 1336 | case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break; |
| 1337 | case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break; |
| 1338 | case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break; |
| 1339 | case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break; |
| 1340 | case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break; |
| 1341 | case 12: path.moveTo(nonFinitePts[i]); break; |
| 1342 | } |
| 1343 | check_convexity(reporter, path, SkPath::kUnknown_Convexity); |
| 1344 | } |
| 1345 | |
| 1346 | for (int index = 0; index < (int) (11 * finitePtsCount); ++index) { |
| 1347 | int f = (int) (index % finitePtsCount); |
| 1348 | int g = (int) ((f + 1) % finitePtsCount); |
| 1349 | path.reset(); |
| 1350 | int curveSelect = index % 11; |
| 1351 | switch (curveSelect) { |
| 1352 | case 0: path.moveTo(finitePts[f]); break; |
| 1353 | case 1: path.lineTo(finitePts[f]); break; |
| 1354 | case 2: path.quadTo(finitePts[f], finitePts[f]); break; |
| 1355 | case 3: path.quadTo(finitePts[f], finitePts[g]); break; |
| 1356 | case 4: path.quadTo(finitePts[g], finitePts[f]); break; |
| 1357 | case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break; |
| 1358 | case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break; |
| 1359 | case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break; |
| 1360 | case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break; |
| 1361 | case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break; |
| 1362 | case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break; |
| 1363 | } |
| 1364 | check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity |
| 1365 | : SkPath::kUnknown_Convexity); |
| 1366 | } |
| 1367 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1370 | static void test_isLine(skiatest::Reporter* reporter) { |
| 1371 | SkPath path; |
| 1372 | SkPoint pts[2]; |
| 1373 | const SkScalar value = SkIntToScalar(5); |
| 1374 | |
| 1375 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1376 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1377 | // set some non-zero values |
| 1378 | pts[0].set(value, value); |
| 1379 | pts[1].set(value, value); |
| 1380 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1381 | // check that pts was untouched |
| 1382 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1383 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1384 | |
| 1385 | const SkScalar moveX = SkIntToScalar(1); |
| 1386 | const SkScalar moveY = SkIntToScalar(2); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1387 | REPORTER_ASSERT(reporter, value != moveX && value != moveY); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1388 | |
| 1389 | path.moveTo(moveX, moveY); |
| 1390 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1391 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1392 | // check that pts was untouched |
| 1393 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1394 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1395 | |
| 1396 | const SkScalar lineX = SkIntToScalar(2); |
| 1397 | const SkScalar lineY = SkIntToScalar(2); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1398 | REPORTER_ASSERT(reporter, value != lineX && value != lineY); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1399 | |
| 1400 | path.lineTo(lineX, lineY); |
| 1401 | REPORTER_ASSERT(reporter, path.isLine(NULL)); |
| 1402 | |
| 1403 | REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY)); |
| 1404 | REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY)); |
| 1405 | REPORTER_ASSERT(reporter, path.isLine(pts)); |
| 1406 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1407 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 1408 | |
| 1409 | path.lineTo(0, 0); // too many points/verbs |
| 1410 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1411 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1412 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1413 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1414 | |
| 1415 | path.reset(); |
| 1416 | path.quadTo(1, 1, 2, 2); |
| 1417 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1420 | static void test_conservativelyContains(skiatest::Reporter* reporter) { |
| 1421 | SkPath path; |
| 1422 | |
| 1423 | // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect. |
| 1424 | static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 1425 | |
| 1426 | // A circle that bounds kBaseRect (with a significant amount of slop) |
| 1427 | SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height()); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1428 | circleR = SkScalarMul(circleR, 1.75f) / 2; |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1429 | static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()}; |
| 1430 | |
| 1431 | // round-rect radii |
| 1432 | static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)}; |
skia.committer@gmail.com | cec8de6 | 2012-11-14 02:01:22 +0000 | [diff] [blame] | 1433 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1434 | static const struct SUPPRESS_VISIBILITY_WARNING { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1435 | SkRect fQueryRect; |
| 1436 | bool fInRect; |
| 1437 | bool fInCircle; |
| 1438 | bool fInRR; |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1439 | bool fInCubicRR; |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1440 | } kQueries[] = { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1441 | {kBaseRect, true, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1442 | |
| 1443 | // rect well inside of kBaseRect |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1444 | {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(), |
| 1445 | kBaseRect.fTop + 0.25f*kBaseRect.height(), |
| 1446 | kBaseRect.fRight - 0.25f*kBaseRect.width(), |
| 1447 | kBaseRect.fBottom - 0.25f*kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1448 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1449 | |
| 1450 | // rects with edges off by one from kBaseRect's edges |
| 1451 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1452 | kBaseRect.width(), kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1453 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1454 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1455 | kBaseRect.width() + 1, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1456 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1457 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1458 | kBaseRect.width() + 1, kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1459 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1460 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1461 | kBaseRect.width(), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1462 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1463 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1464 | kBaseRect.width(), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1465 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1466 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1467 | kBaseRect.width() + 2, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1468 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1469 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1470 | kBaseRect.width() + 2, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1471 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1472 | |
| 1473 | // zero-w/h rects at each corner of kBaseRect |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1474 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false}, |
| 1475 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true}, |
| 1476 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true}, |
| 1477 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1478 | |
| 1479 | // far away rect |
| 1480 | {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom, |
| 1481 | SkIntToScalar(10), SkIntToScalar(10)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1482 | false, false, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1483 | |
| 1484 | // very large rect containing kBaseRect |
| 1485 | {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(), |
| 1486 | kBaseRect.fTop - 5 * kBaseRect.height(), |
| 1487 | 11 * kBaseRect.width(), 11 * kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1488 | false, false, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1489 | |
| 1490 | // skinny rect that spans same y-range as kBaseRect |
| 1491 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1492 | SkIntToScalar(1), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1493 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1494 | |
| 1495 | // short rect that spans same x-range as kBaseRect |
| 1496 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1497 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1498 | |
| 1499 | // skinny rect that spans slightly larger y-range than kBaseRect |
| 1500 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1501 | SkIntToScalar(1), kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1502 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1503 | |
| 1504 | // short rect that spans slightly larger x-range than kBaseRect |
| 1505 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), |
| 1506 | kBaseRect.width() + 1, SkScalar(1)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1507 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1508 | }; |
| 1509 | |
| 1510 | for (int inv = 0; inv < 4; ++inv) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1511 | for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1512 | SkRect qRect = kQueries[q].fQueryRect; |
| 1513 | if (inv & 0x1) { |
| 1514 | SkTSwap(qRect.fLeft, qRect.fRight); |
| 1515 | } |
| 1516 | if (inv & 0x2) { |
| 1517 | SkTSwap(qRect.fTop, qRect.fBottom); |
| 1518 | } |
| 1519 | for (int d = 0; d < 2; ++d) { |
| 1520 | SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction; |
| 1521 | path.reset(); |
| 1522 | path.addRect(kBaseRect, dir); |
| 1523 | REPORTER_ASSERT(reporter, kQueries[q].fInRect == |
| 1524 | path.conservativelyContainsRect(qRect)); |
| 1525 | |
| 1526 | path.reset(); |
| 1527 | path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir); |
| 1528 | REPORTER_ASSERT(reporter, kQueries[q].fInCircle == |
| 1529 | path.conservativelyContainsRect(qRect)); |
| 1530 | |
| 1531 | path.reset(); |
| 1532 | path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir); |
| 1533 | REPORTER_ASSERT(reporter, kQueries[q].fInRR == |
| 1534 | path.conservativelyContainsRect(qRect)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1535 | |
| 1536 | path.reset(); |
| 1537 | path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop); |
| 1538 | path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop, |
| 1539 | kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2, |
| 1540 | kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]); |
| 1541 | path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom); |
| 1542 | path.lineTo(kBaseRect.fRight, kBaseRect.fBottom); |
| 1543 | path.lineTo(kBaseRect.fRight, kBaseRect.fTop); |
| 1544 | path.close(); |
| 1545 | REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR == |
| 1546 | path.conservativelyContainsRect(qRect)); |
| 1547 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1548 | } |
| 1549 | // Slightly non-convex shape, shouldn't contain any rects. |
| 1550 | path.reset(); |
| 1551 | path.moveTo(0, 0); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1552 | path.lineTo(SkIntToScalar(50), 0.05f); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1553 | path.lineTo(SkIntToScalar(100), 0); |
| 1554 | path.lineTo(SkIntToScalar(100), SkIntToScalar(100)); |
| 1555 | path.lineTo(0, SkIntToScalar(100)); |
| 1556 | path.close(); |
| 1557 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect)); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | // make sure a minimal convex shape works, a right tri with edges along pos x and y axes. |
| 1562 | path.reset(); |
| 1563 | path.moveTo(0, 0); |
| 1564 | path.lineTo(SkIntToScalar(100), 0); |
| 1565 | path.lineTo(0, SkIntToScalar(100)); |
| 1566 | |
| 1567 | // inside, on along top edge |
| 1568 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1569 | SkIntToScalar(10), |
| 1570 | SkIntToScalar(10)))); |
| 1571 | // above |
| 1572 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( |
| 1573 | SkRect::MakeXYWH(SkIntToScalar(50), |
| 1574 | SkIntToScalar(-10), |
| 1575 | SkIntToScalar(10), |
| 1576 | SkIntToScalar(10)))); |
| 1577 | // to the left |
| 1578 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10), |
| 1579 | SkIntToScalar(5), |
| 1580 | SkIntToScalar(5), |
| 1581 | SkIntToScalar(5)))); |
| 1582 | |
| 1583 | // outside the diagonal edge |
| 1584 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10), |
| 1585 | SkIntToScalar(200), |
| 1586 | SkIntToScalar(20), |
| 1587 | SkIntToScalar(5)))); |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 1588 | |
bsalomon | b17c129 | 2014-08-28 14:04:55 -0700 | [diff] [blame] | 1589 | |
| 1590 | // Test that multiple move commands do not cause asserts. |
| 1591 | |
| 1592 | // At the time of writing, this would not modify cached convexity. This caused an assert while |
| 1593 | // checking conservative containment again. http://skbug.com/1460 |
| 1594 | path.moveTo(SkIntToScalar(100), SkIntToScalar(100)); |
| 1595 | #if 0 |
| 1596 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1597 | SkIntToScalar(10), |
| 1598 | SkIntToScalar(10)))); |
| 1599 | #endif |
| 1600 | |
| 1601 | // Same as above path and first test but with an extra moveTo. |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 1602 | path.reset(); |
| 1603 | path.moveTo(100, 100); |
| 1604 | path.moveTo(0, 0); |
| 1605 | path.lineTo(SkIntToScalar(100), 0); |
| 1606 | path.lineTo(0, SkIntToScalar(100)); |
| 1607 | |
| 1608 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1609 | SkIntToScalar(10), |
| 1610 | SkIntToScalar(10)))); |
| 1611 | |
bsalomon | b17c129 | 2014-08-28 14:04:55 -0700 | [diff] [blame] | 1612 | // Test that multiple move commands do not cause asserts and that the function |
| 1613 | // is not confused by the multiple moves. |
| 1614 | path.reset(); |
| 1615 | path.moveTo(0, 0); |
| 1616 | path.lineTo(SkIntToScalar(100), 0); |
| 1617 | path.lineTo(0, SkIntToScalar(100)); |
| 1618 | path.moveTo(0, SkIntToScalar(200)); |
| 1619 | path.lineTo(SkIntToScalar(100), SkIntToScalar(200)); |
| 1620 | path.lineTo(0, SkIntToScalar(300)); |
| 1621 | |
| 1622 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( |
| 1623 | SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1624 | SkIntToScalar(10), |
| 1625 | SkIntToScalar(10)))); |
| 1626 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1627 | path.reset(); |
| 1628 | path.lineTo(100, 100); |
| 1629 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1))); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1632 | static void test_isRect_open_close(skiatest::Reporter* reporter) { |
| 1633 | SkPath path; |
| 1634 | bool isClosed; |
| 1635 | |
| 1636 | path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1); |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1637 | path.close(); |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 1638 | |
robertphillips | 91b0a35 | 2015-01-05 10:13:46 -0800 | [diff] [blame] | 1639 | REPORTER_ASSERT(reporter, path.isRect(NULL, &isClosed, NULL)); |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1640 | REPORTER_ASSERT(reporter, isClosed); |
| 1641 | } |
| 1642 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1643 | // Simple isRect test is inline TestPath, below. |
| 1644 | // test_isRect provides more extensive testing. |
| 1645 | static void test_isRect(skiatest::Reporter* reporter) { |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1646 | test_isRect_open_close(reporter); |
| 1647 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1648 | // passing tests (all moveTo / lineTo... |
| 1649 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; |
| 1650 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1651 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1652 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
| 1653 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; |
| 1654 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1655 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1656 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1657 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1658 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; |
| 1659 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1660 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1661 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1662 | SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1663 | SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}}; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1664 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1665 | // failing tests |
| 1666 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1667 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1668 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1669 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1670 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1671 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1672 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1673 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1674 | SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps |
| 1675 | SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap |
| 1676 | SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1677 | |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 1678 | // no close, but we should detect them as fillably the same as a rect |
| 1679 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; |
| 1680 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; |
| 1681 | SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start |
| 1682 | |
| 1683 | // like c2, but we double-back on ourselves |
| 1684 | SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}}; |
| 1685 | // like c2, but we overshoot the start point |
| 1686 | SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}}; |
| 1687 | SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}}; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1688 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1689 | struct IsRectTest { |
| 1690 | SkPoint *fPoints; |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1691 | int fPointCount; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1692 | bool fClose; |
| 1693 | bool fIsRect; |
| 1694 | } tests[] = { |
| 1695 | { r1, SK_ARRAY_COUNT(r1), true, true }, |
| 1696 | { r2, SK_ARRAY_COUNT(r2), true, true }, |
| 1697 | { r3, SK_ARRAY_COUNT(r3), true, true }, |
| 1698 | { r4, SK_ARRAY_COUNT(r4), true, true }, |
| 1699 | { r5, SK_ARRAY_COUNT(r5), true, true }, |
| 1700 | { r6, SK_ARRAY_COUNT(r6), true, true }, |
| 1701 | { r7, SK_ARRAY_COUNT(r7), true, true }, |
| 1702 | { r8, SK_ARRAY_COUNT(r8), true, true }, |
| 1703 | { r9, SK_ARRAY_COUNT(r9), true, true }, |
| 1704 | { ra, SK_ARRAY_COUNT(ra), true, true }, |
| 1705 | { rb, SK_ARRAY_COUNT(rb), true, true }, |
| 1706 | { rc, SK_ARRAY_COUNT(rc), true, true }, |
| 1707 | { rd, SK_ARRAY_COUNT(rd), true, true }, |
| 1708 | { re, SK_ARRAY_COUNT(re), true, true }, |
| 1709 | { rf, SK_ARRAY_COUNT(rf), true, true }, |
| 1710 | |
| 1711 | { f1, SK_ARRAY_COUNT(f1), true, false }, |
| 1712 | { f2, SK_ARRAY_COUNT(f2), true, false }, |
| 1713 | { f3, SK_ARRAY_COUNT(f3), true, false }, |
| 1714 | { f4, SK_ARRAY_COUNT(f4), true, false }, |
| 1715 | { f5, SK_ARRAY_COUNT(f5), true, false }, |
| 1716 | { f6, SK_ARRAY_COUNT(f6), true, false }, |
| 1717 | { f7, SK_ARRAY_COUNT(f7), true, false }, |
| 1718 | { f8, SK_ARRAY_COUNT(f8), true, false }, |
| 1719 | { f9, SK_ARRAY_COUNT(f9), true, false }, |
| 1720 | { fa, SK_ARRAY_COUNT(fa), true, false }, |
| 1721 | { fb, SK_ARRAY_COUNT(fb), true, false }, |
| 1722 | |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 1723 | { c1, SK_ARRAY_COUNT(c1), false, true }, |
| 1724 | { c2, SK_ARRAY_COUNT(c2), false, true }, |
| 1725 | { c3, SK_ARRAY_COUNT(c3), false, true }, |
| 1726 | |
| 1727 | { d1, SK_ARRAY_COUNT(d1), false, false }, |
| 1728 | { d2, SK_ARRAY_COUNT(d2), false, false }, |
| 1729 | { d3, SK_ARRAY_COUNT(d3), false, false }, |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1730 | }; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1731 | |
| 1732 | const size_t testCount = SK_ARRAY_COUNT(tests); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1733 | int index; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1734 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1735 | SkPath path; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1736 | path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); |
| 1737 | for (index = 1; index < tests[testIndex].fPointCount; ++index) { |
| 1738 | path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1739 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1740 | if (tests[testIndex].fClose) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1741 | path.close(); |
| 1742 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1743 | REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1744 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1745 | if (tests[testIndex].fIsRect) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1746 | SkRect computed, expected; |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1747 | bool isClosed; |
| 1748 | SkPath::Direction direction, cheapDirection; |
robertphillips | 91b0a35 | 2015-01-05 10:13:46 -0800 | [diff] [blame] | 1749 | expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1750 | REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection)); |
robertphillips | 91b0a35 | 2015-01-05 10:13:46 -0800 | [diff] [blame] | 1751 | REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction)); |
| 1752 | REPORTER_ASSERT(reporter, expected == computed); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1753 | REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1754 | REPORTER_ASSERT(reporter, direction == cheapDirection); |
| 1755 | } else { |
| 1756 | SkRect computed; |
| 1757 | computed.set(123, 456, 789, 1011); |
robertphillips | 91b0a35 | 2015-01-05 10:13:46 -0800 | [diff] [blame] | 1758 | bool isClosed = (bool)-1; |
| 1759 | SkPath::Direction direction = (SkPath::Direction) - 1; |
| 1760 | REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1761 | REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456); |
| 1762 | REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1763 | REPORTER_ASSERT(reporter, isClosed == (bool) -1); |
| 1764 | REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1765 | } |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1766 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1767 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1768 | // fail, close then line |
| 1769 | SkPath path1; |
| 1770 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1771 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1772 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1773 | } |
| 1774 | path1.close(); |
| 1775 | path1.lineTo(1, 0); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1776 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1777 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1778 | // fail, move in the middle |
| 1779 | path1.reset(); |
| 1780 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1781 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1782 | if (index == 2) { |
| 1783 | path1.moveTo(1, .5f); |
| 1784 | } |
| 1785 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1786 | } |
| 1787 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1788 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1789 | |
| 1790 | // fail, move on the edge |
| 1791 | path1.reset(); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1792 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1793 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1794 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1795 | } |
| 1796 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1797 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1798 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1799 | // fail, quad |
| 1800 | path1.reset(); |
| 1801 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1802 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1803 | if (index == 2) { |
| 1804 | path1.quadTo(1, .5f, 1, .5f); |
| 1805 | } |
| 1806 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1807 | } |
| 1808 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1809 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1810 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1811 | // fail, cubic |
| 1812 | path1.reset(); |
| 1813 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1814 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1815 | if (index == 2) { |
| 1816 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 1817 | } |
| 1818 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1819 | } |
| 1820 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1821 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1824 | static void test_isNestedRects(skiatest::Reporter* reporter) { |
| 1825 | // passing tests (all moveTo / lineTo... |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1826 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1827 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1828 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1829 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1830 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1831 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1832 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1833 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1834 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1835 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW |
| 1836 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1837 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW |
| 1838 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW |
| 1839 | SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1840 | |
| 1841 | // failing tests |
| 1842 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1843 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1844 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1845 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1846 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1847 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1848 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1849 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
| 1850 | |
| 1851 | // failing, no close |
| 1852 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 1853 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 1854 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1855 | struct IsNestedRectTest { |
| 1856 | SkPoint *fPoints; |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1857 | int fPointCount; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1858 | SkPath::Direction fDirection; |
| 1859 | bool fClose; |
| 1860 | bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2); |
| 1861 | } tests[] = { |
| 1862 | { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true }, |
| 1863 | { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true }, |
| 1864 | { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true }, |
| 1865 | { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true }, |
| 1866 | { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true }, |
| 1867 | { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true }, |
| 1868 | { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true }, |
| 1869 | { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true }, |
| 1870 | { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true }, |
| 1871 | { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true }, |
| 1872 | { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true }, |
| 1873 | { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true }, |
| 1874 | { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true }, |
| 1875 | { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true }, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1876 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1877 | { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false }, |
| 1878 | { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false }, |
| 1879 | { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false }, |
| 1880 | { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false }, |
| 1881 | { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false }, |
| 1882 | { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false }, |
| 1883 | { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false }, |
| 1884 | { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false }, |
| 1885 | |
| 1886 | { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false }, |
| 1887 | { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false }, |
| 1888 | }; |
| 1889 | |
| 1890 | const size_t testCount = SK_ARRAY_COUNT(tests); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1891 | int index; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1892 | for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1893 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1894 | SkPath path; |
| 1895 | if (rectFirst) { |
| 1896 | path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1897 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1898 | path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); |
| 1899 | for (index = 1; index < tests[testIndex].fPointCount; ++index) { |
| 1900 | path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1901 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1902 | if (tests[testIndex].fClose) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1903 | path.close(); |
| 1904 | } |
| 1905 | if (!rectFirst) { |
| 1906 | path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1907 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1908 | REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL)); |
| 1909 | if (tests[testIndex].fIsNestedRect) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1910 | SkRect expected[2], computed[2]; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1911 | SkPath::Direction expectedDirs[2], computedDirs[2]; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1912 | SkRect testBounds; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1913 | testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1914 | expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2); |
| 1915 | expected[1] = testBounds; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1916 | if (rectFirst) { |
| 1917 | expectedDirs[0] = SkPath::kCW_Direction; |
| 1918 | } else { |
| 1919 | expectedDirs[0] = SkPath::kCCW_Direction; |
| 1920 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1921 | expectedDirs[1] = tests[testIndex].fDirection; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1922 | REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1923 | REPORTER_ASSERT(reporter, expected[0] == computed[0]); |
| 1924 | REPORTER_ASSERT(reporter, expected[1] == computed[1]); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1925 | REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]); |
| 1926 | REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1927 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | // fail, close then line |
| 1931 | SkPath path1; |
| 1932 | if (rectFirst) { |
| 1933 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1934 | } |
| 1935 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1936 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1937 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1938 | } |
| 1939 | path1.close(); |
| 1940 | path1.lineTo(1, 0); |
| 1941 | if (!rectFirst) { |
| 1942 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1943 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1944 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1945 | |
| 1946 | // fail, move in the middle |
| 1947 | path1.reset(); |
| 1948 | if (rectFirst) { |
| 1949 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1950 | } |
| 1951 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1952 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1953 | if (index == 2) { |
| 1954 | path1.moveTo(1, .5f); |
| 1955 | } |
| 1956 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1957 | } |
| 1958 | path1.close(); |
| 1959 | if (!rectFirst) { |
| 1960 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1961 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1962 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1963 | |
| 1964 | // fail, move on the edge |
| 1965 | path1.reset(); |
| 1966 | if (rectFirst) { |
| 1967 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1968 | } |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1969 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1970 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1971 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1972 | } |
| 1973 | path1.close(); |
| 1974 | if (!rectFirst) { |
| 1975 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1976 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1977 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1978 | |
| 1979 | // fail, quad |
| 1980 | path1.reset(); |
| 1981 | if (rectFirst) { |
| 1982 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1983 | } |
| 1984 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 1985 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1986 | if (index == 2) { |
| 1987 | path1.quadTo(1, .5f, 1, .5f); |
| 1988 | } |
| 1989 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1990 | } |
| 1991 | path1.close(); |
| 1992 | if (!rectFirst) { |
| 1993 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1994 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1995 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1996 | |
| 1997 | // fail, cubic |
| 1998 | path1.reset(); |
| 1999 | if (rectFirst) { |
| 2000 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 2001 | } |
| 2002 | path1.moveTo(r1[0].fX, r1[0].fY); |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 2003 | for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2004 | if (index == 2) { |
| 2005 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 2006 | } |
| 2007 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 2008 | } |
| 2009 | path1.close(); |
| 2010 | if (!rectFirst) { |
| 2011 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 2012 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 2013 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 2014 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2015 | // fail, not nested |
| 2016 | path1.reset(); |
| 2017 | path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction); |
| 2018 | path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 2019 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2020 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 2021 | |
| 2022 | // pass, stroke rect |
| 2023 | SkPath src, dst; |
| 2024 | src.addRect(1, 1, 7, 7, SkPath::kCW_Direction); |
| 2025 | SkPaint strokePaint; |
| 2026 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 2027 | strokePaint.setStrokeWidth(2); |
| 2028 | strokePaint.getFillPath(src, &dst); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 2029 | REPORTER_ASSERT(reporter, dst.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2032 | static void write_and_read_back(skiatest::Reporter* reporter, |
| 2033 | const SkPath& p) { |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 2034 | SkWriter32 writer; |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2035 | writer.writePath(p); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 2036 | size_t size = writer.bytesWritten(); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2037 | SkAutoMalloc storage(size); |
| 2038 | writer.flatten(storage.get()); |
| 2039 | SkReader32 reader(storage.get(), size); |
| 2040 | |
| 2041 | SkPath readBack; |
| 2042 | REPORTER_ASSERT(reporter, readBack != p); |
| 2043 | reader.readPath(&readBack); |
| 2044 | REPORTER_ASSERT(reporter, readBack == p); |
| 2045 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2046 | REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() == |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2047 | p.getConvexityOrUnknown()); |
| 2048 | |
| 2049 | REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL)); |
| 2050 | |
| 2051 | const SkRect& origBounds = p.getBounds(); |
| 2052 | const SkRect& readBackBounds = readBack.getBounds(); |
| 2053 | |
| 2054 | REPORTER_ASSERT(reporter, origBounds == readBackBounds); |
| 2055 | } |
| 2056 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2057 | static void test_flattening(skiatest::Reporter* reporter) { |
| 2058 | SkPath p; |
| 2059 | |
| 2060 | static const SkPoint pts[] = { |
| 2061 | { 0, 0 }, |
| 2062 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 2063 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 2064 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 2065 | }; |
| 2066 | p.moveTo(pts[0]); |
| 2067 | p.lineTo(pts[1]); |
| 2068 | p.quadTo(pts[2], pts[3]); |
| 2069 | p.cubicTo(pts[4], pts[5], pts[6]); |
| 2070 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2071 | write_and_read_back(reporter, p); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2072 | |
| 2073 | // create a buffer that should be much larger than the path so we don't |
| 2074 | // kill our stack if writer goes too far. |
| 2075 | char buffer[1024]; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2076 | size_t size1 = p.writeToMemory(NULL); |
| 2077 | size_t size2 = p.writeToMemory(buffer); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2078 | REPORTER_ASSERT(reporter, size1 == size2); |
| 2079 | |
| 2080 | SkPath p2; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 2081 | size_t size3 = p2.readFromMemory(buffer, 1024); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2082 | REPORTER_ASSERT(reporter, size1 == size3); |
| 2083 | REPORTER_ASSERT(reporter, p == p2); |
| 2084 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2085 | size3 = p2.readFromMemory(buffer, 0); |
| 2086 | REPORTER_ASSERT(reporter, !size3); |
| 2087 | |
| 2088 | SkPath tooShort; |
| 2089 | size3 = tooShort.readFromMemory(buffer, size1 - 1); |
| 2090 | REPORTER_ASSERT(reporter, tooShort.isEmpty()); |
| 2091 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 2092 | char buffer2[1024]; |
| 2093 | size3 = p2.writeToMemory(buffer2); |
| 2094 | REPORTER_ASSERT(reporter, size1 == size3); |
| 2095 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 2096 | |
| 2097 | // test persistence of the oval flag & convexity |
| 2098 | { |
| 2099 | SkPath oval; |
| 2100 | SkRect rect = SkRect::MakeWH(10, 10); |
| 2101 | oval.addOval(rect); |
| 2102 | |
| 2103 | write_and_read_back(reporter, oval); |
| 2104 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | static void test_transform(skiatest::Reporter* reporter) { |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2108 | SkPath p; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2109 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2110 | #define CONIC_PERSPECTIVE_BUG_FIXED 0 |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2111 | static const SkPoint pts[] = { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2112 | { 0, 0 }, // move |
| 2113 | { SkIntToScalar(10), SkIntToScalar(10) }, // line |
| 2114 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad |
| 2115 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic |
| 2116 | #if CONIC_PERSPECTIVE_BUG_FIXED |
| 2117 | { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic |
| 2118 | #endif |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2119 | }; |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2120 | const int kPtCount = SK_ARRAY_COUNT(pts); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2121 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2122 | p.moveTo(pts[0]); |
| 2123 | p.lineTo(pts[1]); |
| 2124 | p.quadTo(pts[2], pts[3]); |
| 2125 | p.cubicTo(pts[4], pts[5], pts[6]); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2126 | #if CONIC_PERSPECTIVE_BUG_FIXED |
| 2127 | p.conicTo(pts[4], pts[5], 0.5f); |
| 2128 | #endif |
| 2129 | p.close(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2130 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2131 | { |
| 2132 | SkMatrix matrix; |
| 2133 | matrix.reset(); |
| 2134 | SkPath p1; |
| 2135 | p.transform(matrix, &p1); |
| 2136 | REPORTER_ASSERT(reporter, p == p1); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2137 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2138 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2139 | |
| 2140 | { |
| 2141 | SkMatrix matrix; |
| 2142 | matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3); |
| 2143 | |
| 2144 | SkPath p1; // Leave p1 non-unique (i.e., the empty path) |
| 2145 | |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 2146 | p.transform(matrix, &p1); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2147 | SkPoint pts1[kPtCount]; |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 2148 | int count = p1.getPoints(pts1, kPtCount); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2149 | REPORTER_ASSERT(reporter, kPtCount == count); |
| 2150 | for (int i = 0; i < count; ++i) { |
| 2151 | SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3); |
| 2152 | REPORTER_ASSERT(reporter, newPt == pts1[i]); |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | { |
| 2157 | SkMatrix matrix; |
| 2158 | matrix.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 2159 | matrix.setPerspX(4); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2160 | |
| 2161 | SkPath p1; |
| 2162 | p1.moveTo(SkPoint::Make(0, 0)); |
| 2163 | |
| 2164 | p.transform(matrix, &p1); |
| 2165 | REPORTER_ASSERT(reporter, matrix.invert(&matrix)); |
| 2166 | p1.transform(matrix, NULL); |
| 2167 | SkRect pBounds = p.getBounds(); |
| 2168 | SkRect p1Bounds = p1.getBounds(); |
| 2169 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft)); |
| 2170 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop)); |
| 2171 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight)); |
| 2172 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom)); |
| 2173 | } |
| 2174 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2175 | p.reset(); |
| 2176 | p.addCircle(0, 0, 1, SkPath::kCW_Direction); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 2177 | |
| 2178 | { |
| 2179 | SkMatrix matrix; |
| 2180 | matrix.reset(); |
| 2181 | SkPath p1; |
| 2182 | p1.moveTo(SkPoint::Make(0, 0)); |
| 2183 | |
| 2184 | p.transform(matrix, &p1); |
| 2185 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction)); |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | { |
| 2190 | SkMatrix matrix; |
| 2191 | matrix.reset(); |
| 2192 | matrix.setScaleX(-1); |
| 2193 | SkPath p1; |
| 2194 | p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) |
| 2195 | |
| 2196 | p.transform(matrix, &p1); |
| 2197 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction)); |
| 2198 | } |
| 2199 | |
| 2200 | { |
| 2201 | SkMatrix matrix; |
| 2202 | matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1); |
| 2203 | SkPath p1; |
| 2204 | p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) |
| 2205 | |
| 2206 | p.transform(matrix, &p1); |
| 2207 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction)); |
| 2208 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2211 | static void test_zero_length_paths(skiatest::Reporter* reporter) { |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2212 | SkPath p; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2213 | uint8_t verbs[32]; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2214 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2215 | struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2216 | const char* testPath; |
| 2217 | const size_t numResultPts; |
| 2218 | const SkRect resultBound; |
| 2219 | const SkPath::Verb* resultVerbs; |
| 2220 | const size_t numResultVerbs; |
| 2221 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2222 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2223 | static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb }; |
| 2224 | static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb }; |
| 2225 | static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 2226 | static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 2227 | static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 2228 | static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 2229 | static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb }; |
| 2230 | static const SkPath::Verb resultVerbs8[] = { |
| 2231 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb |
| 2232 | }; |
| 2233 | static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 2234 | static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 2235 | static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb }; |
| 2236 | static const SkPath::Verb resultVerbs12[] = { |
| 2237 | SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb |
| 2238 | }; |
| 2239 | static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 2240 | static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 2241 | static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb }; |
| 2242 | static const SkPath::Verb resultVerbs16[] = { |
| 2243 | SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb |
| 2244 | }; |
| 2245 | static const struct zeroPathTestData gZeroLengthTests[] = { |
| 2246 | { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2247 | { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2248 | { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2249 | { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 2250 | { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }, |
| 2251 | { "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) }, |
| 2252 | { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) }, |
| 2253 | { "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) }, |
| 2254 | { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) }, |
| 2255 | { "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) }, |
| 2256 | { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) }, |
| 2257 | { "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) }, |
| 2258 | { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) }, |
| 2259 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2260 | SK_ARRAY_COUNT(resultVerbs14) |
| 2261 | }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2262 | { "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) }, |
| 2263 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2264 | SK_ARRAY_COUNT(resultVerbs16) |
| 2265 | } |
| 2266 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2267 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2268 | for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) { |
| 2269 | p.reset(); |
| 2270 | bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p); |
| 2271 | REPORTER_ASSERT(reporter, valid); |
| 2272 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 2273 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints()); |
| 2274 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds()); |
| 2275 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs))); |
| 2276 | for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) { |
| 2277 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]); |
| 2278 | } |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2279 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | struct SegmentInfo { |
| 2283 | SkPath fPath; |
| 2284 | int fPointCount; |
| 2285 | }; |
| 2286 | |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2287 | #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask) |
| 2288 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2289 | static void test_segment_masks(skiatest::Reporter* reporter) { |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2290 | SkPath p, p2; |
| 2291 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2292 | p.moveTo(0, 0); |
| 2293 | p.quadTo(100, 100, 200, 200); |
| 2294 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); |
| 2295 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2296 | p2 = p; |
| 2297 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2298 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 2299 | REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); |
| 2300 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2301 | p2 = p; |
| 2302 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
| 2303 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2304 | p.reset(); |
| 2305 | p.moveTo(0, 0); |
| 2306 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 2307 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2308 | p2 = p; |
| 2309 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2310 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2311 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 2312 | } |
| 2313 | |
| 2314 | static void test_iter(skiatest::Reporter* reporter) { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2315 | SkPath p; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2316 | SkPoint pts[4]; |
| 2317 | |
| 2318 | // Test an iterator with no path |
| 2319 | SkPath::Iter noPathIter; |
| 2320 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2321 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2322 | // Test that setting an empty path works |
| 2323 | noPathIter.setPath(p, false); |
| 2324 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2325 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2326 | // Test that close path makes no difference for an empty path |
| 2327 | noPathIter.setPath(p, true); |
| 2328 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2329 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2330 | // Test an iterator with an initial empty path |
| 2331 | SkPath::Iter iter(p, false); |
| 2332 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2333 | |
| 2334 | // Test that close path makes no difference |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2335 | iter.setPath(p, true); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2336 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2337 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2338 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2339 | struct iterTestData { |
| 2340 | const char* testPath; |
| 2341 | const bool forceClose; |
| 2342 | const bool consumeDegenerates; |
| 2343 | const size_t* numResultPtsPerVerb; |
| 2344 | const SkPoint* resultPts; |
| 2345 | const SkPath::Verb* resultVerbs; |
| 2346 | const size_t numResultVerbs; |
| 2347 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2348 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2349 | static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb }; |
| 2350 | static const SkPath::Verb resultVerbs2[] = { |
| 2351 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb |
| 2352 | }; |
| 2353 | static const SkPath::Verb resultVerbs3[] = { |
| 2354 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2355 | }; |
| 2356 | static const SkPath::Verb resultVerbs4[] = { |
| 2357 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2358 | }; |
| 2359 | static const SkPath::Verb resultVerbs5[] = { |
| 2360 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2361 | }; |
| 2362 | static const size_t resultPtsSizes1[] = { 0 }; |
schenney@chromium.org | fedd09b | 2012-06-13 18:29:20 +0000 | [diff] [blame] | 2363 | static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 }; |
| 2364 | static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 }; |
| 2365 | static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 }; |
| 2366 | static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 }; |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2367 | static const SkPoint* resultPts1 = 0; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2368 | static const SkPoint resultPts2[] = { |
| 2369 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 } |
| 2370 | }; |
| 2371 | static const SkPoint resultPts3[] = { |
| 2372 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }, |
| 2373 | { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 } |
| 2374 | }; |
| 2375 | static const SkPoint resultPts4[] = { |
| 2376 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 2377 | }; |
| 2378 | static const SkPoint resultPts5[] = { |
| 2379 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 2380 | }; |
| 2381 | static const struct iterTestData gIterTests[] = { |
| 2382 | { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2383 | { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2384 | { "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.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2385 | { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2386 | { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2387 | { "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) }, |
| 2388 | { "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.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2389 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
| 2390 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
| 2391 | { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2392 | { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2393 | { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 2394 | { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) } |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2395 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2396 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2397 | for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) { |
| 2398 | p.reset(); |
| 2399 | bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p); |
| 2400 | REPORTER_ASSERT(reporter, valid); |
| 2401 | iter.setPath(p, gIterTests[i].forceClose); |
| 2402 | int j = 0, l = 0; |
| 2403 | do { |
| 2404 | REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]); |
| 2405 | for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) { |
| 2406 | REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]); |
| 2407 | } |
| 2408 | } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb); |
| 2409 | REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs); |
| 2410 | } |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2411 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2412 | p.reset(); |
| 2413 | iter.setPath(p, false); |
| 2414 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2415 | p.lineTo(1, 1); |
| 2416 | p.close(); |
| 2417 | iter.setPath(p, false); |
| 2418 | REPORTER_ASSERT(reporter, iter.isClosedContour()); |
| 2419 | p.reset(); |
| 2420 | iter.setPath(p, true); |
| 2421 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2422 | p.lineTo(1, 1); |
| 2423 | iter.setPath(p, true); |
| 2424 | REPORTER_ASSERT(reporter, iter.isClosedContour()); |
| 2425 | p.moveTo(0, 0); |
| 2426 | p.lineTo(2, 2); |
| 2427 | iter.setPath(p, false); |
| 2428 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2429 | |
| 2430 | // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not |
| 2431 | // check to see if the result is correct. |
| 2432 | for (int setNaN = 0; setNaN < 4; ++setNaN) { |
| 2433 | p.reset(); |
| 2434 | p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0); |
| 2435 | p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1); |
| 2436 | iter.setPath(p, true); |
| 2437 | iter.next(pts, false); |
| 2438 | iter.next(pts, false); |
| 2439 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false)); |
| 2440 | } |
| 2441 | |
| 2442 | p.reset(); |
| 2443 | p.quadTo(0, 0, 0, 0); |
| 2444 | iter.setPath(p, false); |
| 2445 | iter.next(pts, false); |
| 2446 | REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false)); |
| 2447 | iter.setPath(p, false); |
| 2448 | iter.next(pts, false); |
| 2449 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2450 | |
| 2451 | p.reset(); |
| 2452 | p.conicTo(0, 0, 0, 0, 0.5f); |
| 2453 | iter.setPath(p, false); |
| 2454 | iter.next(pts, false); |
| 2455 | REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false)); |
| 2456 | iter.setPath(p, false); |
| 2457 | iter.next(pts, false); |
| 2458 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2459 | |
| 2460 | p.reset(); |
| 2461 | p.cubicTo(0, 0, 0, 0, 0, 0); |
| 2462 | iter.setPath(p, false); |
| 2463 | iter.next(pts, false); |
| 2464 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); |
| 2465 | iter.setPath(p, false); |
| 2466 | iter.next(pts, false); |
| 2467 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2468 | |
| 2469 | p.moveTo(1, 1); // add a trailing moveto |
| 2470 | iter.setPath(p, false); |
| 2471 | iter.next(pts, false); |
| 2472 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); |
| 2473 | iter.setPath(p, false); |
| 2474 | iter.next(pts, false); |
| 2475 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2476 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2477 | // The GM degeneratesegments.cpp test is more extensive |
| 2478 | } |
| 2479 | |
| 2480 | static void test_raw_iter(skiatest::Reporter* reporter) { |
| 2481 | SkPath p; |
| 2482 | SkPoint pts[4]; |
| 2483 | |
| 2484 | // Test an iterator with no path |
| 2485 | SkPath::RawIter noPathIter; |
| 2486 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
| 2487 | // Test that setting an empty path works |
| 2488 | noPathIter.setPath(p); |
| 2489 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2490 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2491 | // Test an iterator with an initial empty path |
| 2492 | SkPath::RawIter iter(p); |
| 2493 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2494 | |
| 2495 | // Test that a move-only path returns the move. |
| 2496 | p.moveTo(SK_Scalar1, 0); |
| 2497 | iter.setPath(p); |
| 2498 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2499 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2500 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2501 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2502 | |
| 2503 | // No matter how many moves we add, we should get them all back |
| 2504 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 2505 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 2506 | iter.setPath(p); |
| 2507 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2508 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2509 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2510 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2511 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2512 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2513 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2514 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 2515 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 2516 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2517 | |
| 2518 | // Initial close is never ever stored |
| 2519 | p.reset(); |
| 2520 | p.close(); |
| 2521 | iter.setPath(p); |
| 2522 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2523 | |
| 2524 | // Move/close sequences |
| 2525 | p.reset(); |
| 2526 | p.close(); // Not stored, no purpose |
| 2527 | p.moveTo(SK_Scalar1, 0); |
| 2528 | p.close(); |
| 2529 | p.close(); // Not stored, no purpose |
| 2530 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 2531 | p.close(); |
| 2532 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 2533 | p.moveTo(SK_Scalar1*4, SK_Scalar1*3); |
| 2534 | p.close(); |
| 2535 | iter.setPath(p); |
| 2536 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2537 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2538 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2539 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2540 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2541 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2542 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2543 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2544 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2545 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2546 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2547 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2548 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2549 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 2550 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 2551 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2552 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2553 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2554 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2555 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2556 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2557 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2558 | |
| 2559 | // Generate random paths and verify |
| 2560 | SkPoint randomPts[25]; |
| 2561 | for (int i = 0; i < 5; ++i) { |
| 2562 | for (int j = 0; j < 5; ++j) { |
| 2563 | randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | // Max of 10 segments, max 3 points per segment |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 2568 | SkRandom rand(9876543); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2569 | SkPoint expectedPts[31]; // May have leading moveTo |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2570 | SkPath::Verb expectedVerbs[22]; // May have leading moveTo |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2571 | SkPath::Verb nextVerb; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2572 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2573 | for (int i = 0; i < 500; ++i) { |
| 2574 | p.reset(); |
| 2575 | bool lastWasClose = true; |
| 2576 | bool haveMoveTo = false; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2577 | SkPoint lastMoveToPt = { 0, 0 }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2578 | int numPoints = 0; |
| 2579 | int numVerbs = (rand.nextU() >> 16) % 10; |
| 2580 | int numIterVerbs = 0; |
| 2581 | for (int j = 0; j < numVerbs; ++j) { |
| 2582 | do { |
| 2583 | nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb); |
| 2584 | } while (lastWasClose && nextVerb == SkPath::kClose_Verb); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2585 | switch (nextVerb) { |
| 2586 | case SkPath::kMove_Verb: |
| 2587 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2588 | p.moveTo(expectedPts[numPoints]); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2589 | lastMoveToPt = expectedPts[numPoints]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2590 | numPoints += 1; |
| 2591 | lastWasClose = false; |
| 2592 | haveMoveTo = true; |
| 2593 | break; |
| 2594 | case SkPath::kLine_Verb: |
| 2595 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2596 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2597 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2598 | haveMoveTo = true; |
| 2599 | } |
| 2600 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2601 | p.lineTo(expectedPts[numPoints]); |
| 2602 | numPoints += 1; |
| 2603 | lastWasClose = false; |
| 2604 | break; |
| 2605 | case SkPath::kQuad_Verb: |
| 2606 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2607 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2608 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2609 | haveMoveTo = true; |
| 2610 | } |
| 2611 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2612 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2613 | p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]); |
| 2614 | numPoints += 2; |
| 2615 | lastWasClose = false; |
| 2616 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2617 | case SkPath::kConic_Verb: |
| 2618 | if (!haveMoveTo) { |
| 2619 | expectedPts[numPoints++] = lastMoveToPt; |
| 2620 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2621 | haveMoveTo = true; |
| 2622 | } |
| 2623 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2624 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2625 | p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2626 | rand.nextUScalar1() * 4); |
| 2627 | numPoints += 2; |
| 2628 | lastWasClose = false; |
| 2629 | break; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2630 | case SkPath::kCubic_Verb: |
| 2631 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2632 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2633 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2634 | haveMoveTo = true; |
| 2635 | } |
| 2636 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2637 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2638 | expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2639 | p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2640 | expectedPts[numPoints + 2]); |
| 2641 | numPoints += 3; |
| 2642 | lastWasClose = false; |
| 2643 | break; |
| 2644 | case SkPath::kClose_Verb: |
| 2645 | p.close(); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2646 | haveMoveTo = false; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2647 | lastWasClose = true; |
| 2648 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2649 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2650 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2651 | } |
| 2652 | expectedVerbs[numIterVerbs++] = nextVerb; |
| 2653 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2654 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2655 | iter.setPath(p); |
| 2656 | numVerbs = numIterVerbs; |
| 2657 | numIterVerbs = 0; |
| 2658 | int numIterPts = 0; |
| 2659 | SkPoint lastMoveTo; |
| 2660 | SkPoint lastPt; |
| 2661 | lastMoveTo.set(0, 0); |
| 2662 | lastPt.set(0, 0); |
| 2663 | while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2664 | REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]); |
| 2665 | numIterVerbs++; |
| 2666 | switch (nextVerb) { |
| 2667 | case SkPath::kMove_Verb: |
| 2668 | REPORTER_ASSERT(reporter, numIterPts < numPoints); |
| 2669 | REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]); |
| 2670 | lastPt = lastMoveTo = pts[0]; |
| 2671 | numIterPts += 1; |
| 2672 | break; |
| 2673 | case SkPath::kLine_Verb: |
| 2674 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 1); |
| 2675 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2676 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2677 | lastPt = pts[1]; |
| 2678 | numIterPts += 1; |
| 2679 | break; |
| 2680 | case SkPath::kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2681 | case SkPath::kConic_Verb: |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2682 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 2); |
| 2683 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2684 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2685 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2686 | lastPt = pts[2]; |
| 2687 | numIterPts += 2; |
| 2688 | break; |
| 2689 | case SkPath::kCubic_Verb: |
| 2690 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 3); |
| 2691 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2692 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2693 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2694 | REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]); |
| 2695 | lastPt = pts[3]; |
| 2696 | numIterPts += 3; |
| 2697 | break; |
| 2698 | case SkPath::kClose_Verb: |
| 2699 | REPORTER_ASSERT(reporter, pts[0] == lastMoveTo); |
| 2700 | lastPt = lastMoveTo; |
| 2701 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2702 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2703 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | REPORTER_ASSERT(reporter, numIterPts == numPoints); |
| 2707 | REPORTER_ASSERT(reporter, numIterVerbs == numVerbs); |
| 2708 | } |
| 2709 | } |
| 2710 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2711 | static void check_for_circle(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2712 | const SkPath& path, |
| 2713 | bool expectedCircle, |
| 2714 | SkPath::Direction expectedDir) { |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 2715 | SkRect rect = SkRect::MakeEmpty(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2716 | REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle); |
| 2717 | REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir)); |
skia.committer@gmail.com | fbb0ed9 | 2012-11-13 21:46:06 +0000 | [diff] [blame] | 2718 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2719 | if (expectedCircle) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2720 | REPORTER_ASSERT(reporter, rect.height() == rect.width()); |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | static void test_circle_skew(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2725 | const SkPath& path, |
| 2726 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2727 | SkPath tmp; |
| 2728 | |
| 2729 | SkMatrix m; |
| 2730 | m.setSkew(SkIntToScalar(3), SkIntToScalar(5)); |
| 2731 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2732 | // this matrix reverses the direction. |
| 2733 | if (SkPath::kCCW_Direction == dir) { |
| 2734 | dir = SkPath::kCW_Direction; |
| 2735 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2736 | REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2737 | dir = SkPath::kCCW_Direction; |
| 2738 | } |
| 2739 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static void test_circle_translate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2743 | const SkPath& path, |
| 2744 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2745 | SkPath tmp; |
| 2746 | |
| 2747 | // translate at small offset |
| 2748 | SkMatrix m; |
| 2749 | m.setTranslate(SkIntToScalar(15), SkIntToScalar(15)); |
| 2750 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2751 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2752 | |
| 2753 | tmp.reset(); |
| 2754 | m.reset(); |
| 2755 | |
| 2756 | // translate at a relatively big offset |
| 2757 | m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000)); |
| 2758 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2759 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | static void test_circle_rotate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2763 | const SkPath& path, |
| 2764 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2765 | for (int angle = 0; angle < 360; ++angle) { |
| 2766 | SkPath tmp; |
| 2767 | SkMatrix m; |
| 2768 | m.setRotate(SkIntToScalar(angle)); |
| 2769 | path.transform(m, &tmp); |
| 2770 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2771 | // TODO: a rotated circle whose rotated angle is not a multiple of 90 |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2772 | // degrees is not an oval anymore, this can be improved. we made this |
| 2773 | // for the simplicity of our implementation. |
| 2774 | if (angle % 90 == 0) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2775 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2776 | } else { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2777 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2782 | static void test_circle_mirror_x(skiatest::Reporter* reporter, |
| 2783 | const SkPath& path, |
| 2784 | SkPath::Direction dir) { |
| 2785 | SkPath tmp; |
| 2786 | SkMatrix m; |
| 2787 | m.reset(); |
| 2788 | m.setScaleX(-SK_Scalar1); |
| 2789 | path.transform(m, &tmp); |
| 2790 | |
| 2791 | if (SkPath::kCW_Direction == dir) { |
| 2792 | dir = SkPath::kCCW_Direction; |
| 2793 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2794 | REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2795 | dir = SkPath::kCW_Direction; |
| 2796 | } |
| 2797 | |
| 2798 | check_for_circle(reporter, tmp, true, dir); |
| 2799 | } |
| 2800 | |
| 2801 | static void test_circle_mirror_y(skiatest::Reporter* reporter, |
| 2802 | const SkPath& path, |
| 2803 | SkPath::Direction dir) { |
| 2804 | SkPath tmp; |
| 2805 | SkMatrix m; |
| 2806 | m.reset(); |
| 2807 | m.setScaleY(-SK_Scalar1); |
| 2808 | path.transform(m, &tmp); |
| 2809 | |
| 2810 | if (SkPath::kCW_Direction == dir) { |
| 2811 | dir = SkPath::kCCW_Direction; |
| 2812 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2813 | REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2814 | dir = SkPath::kCW_Direction; |
| 2815 | } |
| 2816 | |
| 2817 | check_for_circle(reporter, tmp, true, dir); |
| 2818 | } |
| 2819 | |
| 2820 | static void test_circle_mirror_xy(skiatest::Reporter* reporter, |
| 2821 | const SkPath& path, |
| 2822 | SkPath::Direction dir) { |
| 2823 | SkPath tmp; |
| 2824 | SkMatrix m; |
| 2825 | m.reset(); |
| 2826 | m.setScaleX(-SK_Scalar1); |
| 2827 | m.setScaleY(-SK_Scalar1); |
| 2828 | path.transform(m, &tmp); |
| 2829 | |
| 2830 | check_for_circle(reporter, tmp, true, dir); |
| 2831 | } |
| 2832 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2833 | static void test_circle_with_direction(skiatest::Reporter* reporter, |
| 2834 | SkPath::Direction dir) { |
| 2835 | SkPath path; |
| 2836 | |
| 2837 | // circle at origin |
| 2838 | path.addCircle(0, 0, SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2839 | check_for_circle(reporter, path, true, dir); |
| 2840 | test_circle_rotate(reporter, path, dir); |
| 2841 | test_circle_translate(reporter, path, dir); |
| 2842 | test_circle_skew(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2843 | |
| 2844 | // circle at an offset at (10, 10) |
| 2845 | path.reset(); |
| 2846 | path.addCircle(SkIntToScalar(10), SkIntToScalar(10), |
| 2847 | SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2848 | check_for_circle(reporter, path, true, dir); |
| 2849 | test_circle_rotate(reporter, path, dir); |
| 2850 | test_circle_translate(reporter, path, dir); |
| 2851 | test_circle_skew(reporter, path, dir); |
| 2852 | test_circle_mirror_x(reporter, path, dir); |
| 2853 | test_circle_mirror_y(reporter, path, dir); |
| 2854 | test_circle_mirror_xy(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | static void test_circle_with_add_paths(skiatest::Reporter* reporter) { |
| 2858 | SkPath path; |
| 2859 | SkPath circle; |
| 2860 | SkPath rect; |
| 2861 | SkPath empty; |
| 2862 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2863 | static const SkPath::Direction kCircleDir = SkPath::kCW_Direction; |
| 2864 | static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction; |
| 2865 | |
| 2866 | circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2867 | rect.addRect(SkIntToScalar(5), SkIntToScalar(5), |
| 2868 | SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction); |
| 2869 | |
| 2870 | SkMatrix translate; |
| 2871 | translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12)); |
| 2872 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 2873 | // Although all the path concatenation related operations leave |
| 2874 | // the path a circle, most mark it as a non-circle for simplicity |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2875 | |
| 2876 | // empty + circle (translate) |
| 2877 | path = empty; |
| 2878 | path.addPath(circle, translate); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2879 | check_for_circle(reporter, path, false, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2880 | |
| 2881 | // circle + empty (translate) |
| 2882 | path = circle; |
| 2883 | path.addPath(empty, translate); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 2884 | check_for_circle(reporter, path, true, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2885 | |
| 2886 | // test reverseAddPath |
| 2887 | path = circle; |
| 2888 | path.reverseAddPath(rect); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2889 | check_for_circle(reporter, path, false, kCircleDirOpposite); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | static void test_circle(skiatest::Reporter* reporter) { |
| 2893 | test_circle_with_direction(reporter, SkPath::kCW_Direction); |
| 2894 | test_circle_with_direction(reporter, SkPath::kCCW_Direction); |
| 2895 | |
| 2896 | // multiple addCircle() |
| 2897 | SkPath path; |
| 2898 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2899 | path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2900 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2901 | |
| 2902 | // some extra lineTo() would make isOval() fail |
| 2903 | path.reset(); |
| 2904 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2905 | path.lineTo(0, 0); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2906 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2907 | |
| 2908 | // not back to the original point |
| 2909 | path.reset(); |
| 2910 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2911 | path.setLastPt(SkIntToScalar(5), SkIntToScalar(5)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2912 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2913 | |
| 2914 | test_circle_with_add_paths(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2915 | |
| 2916 | // test negative radius |
| 2917 | path.reset(); |
| 2918 | path.addCircle(0, 0, -1, SkPath::kCW_Direction); |
| 2919 | REPORTER_ASSERT(reporter, path.isEmpty()); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | static void test_oval(skiatest::Reporter* reporter) { |
| 2923 | SkRect rect; |
| 2924 | SkMatrix m; |
| 2925 | SkPath path; |
| 2926 | |
| 2927 | rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50)); |
| 2928 | path.addOval(rect); |
| 2929 | |
| 2930 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2931 | |
| 2932 | m.setRotate(SkIntToScalar(90)); |
| 2933 | SkPath tmp; |
| 2934 | path.transform(m, &tmp); |
| 2935 | // an oval rotated 90 degrees is still an oval. |
| 2936 | REPORTER_ASSERT(reporter, tmp.isOval(NULL)); |
| 2937 | |
| 2938 | m.reset(); |
| 2939 | m.setRotate(SkIntToScalar(30)); |
| 2940 | tmp.reset(); |
| 2941 | path.transform(m, &tmp); |
| 2942 | // an oval rotated 30 degrees is not an oval anymore. |
| 2943 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2944 | |
| 2945 | // since empty path being transformed. |
| 2946 | path.reset(); |
| 2947 | tmp.reset(); |
| 2948 | m.reset(); |
| 2949 | path.transform(m, &tmp); |
| 2950 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2951 | |
| 2952 | // empty path is not an oval |
| 2953 | tmp.reset(); |
| 2954 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2955 | |
| 2956 | // only has moveTo()s |
| 2957 | tmp.reset(); |
| 2958 | tmp.moveTo(0, 0); |
| 2959 | tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 2960 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2961 | |
| 2962 | // mimic WebKit's calling convention, |
| 2963 | // call moveTo() first and then call addOval() |
| 2964 | path.reset(); |
| 2965 | path.moveTo(0, 0); |
| 2966 | path.addOval(rect); |
| 2967 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2968 | |
| 2969 | // copy path |
| 2970 | path.reset(); |
| 2971 | tmp.reset(); |
| 2972 | tmp.addOval(rect); |
| 2973 | path = tmp; |
| 2974 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2975 | } |
| 2976 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2977 | static void test_empty(skiatest::Reporter* reporter, const SkPath& p) { |
| 2978 | SkPath empty; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2979 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2980 | REPORTER_ASSERT(reporter, p.isEmpty()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2981 | REPORTER_ASSERT(reporter, 0 == p.countPoints()); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2982 | REPORTER_ASSERT(reporter, 0 == p.countVerbs()); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2983 | REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2984 | REPORTER_ASSERT(reporter, p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2985 | REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); |
| 2986 | REPORTER_ASSERT(reporter, !p.isInverseFillType()); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2987 | REPORTER_ASSERT(reporter, p == empty); |
| 2988 | REPORTER_ASSERT(reporter, !(p != empty)); |
| 2989 | } |
| 2990 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2991 | static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path, |
| 2992 | SkPath::Direction dir) { |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2993 | REPORTER_ASSERT(reporter, path->isConvex()); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2994 | REPORTER_ASSERT(reporter, path->cheapIsDirection(dir)); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2995 | path->setConvexity(SkPath::kUnknown_Convexity); |
| 2996 | REPORTER_ASSERT(reporter, path->isConvex()); |
| 2997 | path->reset(); |
| 2998 | } |
| 2999 | |
caryclark | d3d1a98 | 2014-12-08 04:57:38 -0800 | [diff] [blame] | 3000 | static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path, |
| 3001 | SkPath::Direction dir) { |
| 3002 | REPORTER_ASSERT(reporter, path->isConvex()); |
| 3003 | REPORTER_ASSERT(reporter, path->cheapIsDirection(dir)); |
| 3004 | path->setConvexity(SkPath::kUnknown_Convexity); |
| 3005 | REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity); |
| 3006 | path->reset(); |
| 3007 | } |
| 3008 | |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3009 | static void test_rrect(skiatest::Reporter* reporter) { |
| 3010 | SkPath p; |
| 3011 | SkRRect rr; |
| 3012 | SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; |
| 3013 | SkRect r = {10, 20, 30, 40}; |
| 3014 | rr.setRectRadii(r, radii); |
| 3015 | p.addRRect(rr); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3016 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3017 | p.addRRect(rr, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3018 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3019 | p.addRoundRect(r, &radii[0].fX); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3020 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3021 | p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3022 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3023 | p.addRoundRect(r, radii[1].fX, radii[1].fY); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3024 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3025 | p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3026 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
| 3027 | for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) { |
| 3028 | SkVector save = radii[i]; |
| 3029 | radii[i].set(0, 0); |
| 3030 | rr.setRectRadii(r, radii); |
| 3031 | p.addRRect(rr); |
| 3032 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 3033 | radii[i] = save; |
| 3034 | } |
| 3035 | p.addRoundRect(r, 0, 0); |
| 3036 | SkRect returnedRect; |
| 3037 | REPORTER_ASSERT(reporter, p.isRect(&returnedRect)); |
| 3038 | REPORTER_ASSERT(reporter, returnedRect == r); |
| 3039 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 3040 | SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 3041 | rr.setRectRadii(r, zeroRadii); |
| 3042 | p.addRRect(rr); |
| 3043 | bool closed; |
| 3044 | SkPath::Direction dir; |
robertphillips | 91b0a35 | 2015-01-05 10:13:46 -0800 | [diff] [blame] | 3045 | REPORTER_ASSERT(reporter, p.isRect(NULL, &closed, &dir)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3046 | REPORTER_ASSERT(reporter, closed); |
| 3047 | REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); |
| 3048 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 3049 | p.addRRect(rr, SkPath::kCW_Direction); |
| 3050 | p.addRRect(rr, SkPath::kCW_Direction); |
| 3051 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 3052 | p.reset(); |
| 3053 | p.addRRect(rr, SkPath::kCCW_Direction); |
| 3054 | p.addRRect(rr, SkPath::kCCW_Direction); |
| 3055 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 3056 | p.reset(); |
| 3057 | SkRect emptyR = {10, 20, 10, 30}; |
| 3058 | rr.setRectRadii(emptyR, radii); |
| 3059 | p.addRRect(rr); |
| 3060 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3061 | SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax}; |
| 3062 | rr.setRectRadii(largeR, radii); |
| 3063 | p.addRRect(rr); |
caryclark | d3d1a98 | 2014-12-08 04:57:38 -0800 | [diff] [blame] | 3064 | test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction); |
reed | 454fa71 | 2015-02-10 08:46:22 -0800 | [diff] [blame] | 3065 | |
| 3066 | // we check for non-finites |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3067 | SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity}; |
| 3068 | rr.setRectRadii(infR, radii); |
reed | 454fa71 | 2015-02-10 08:46:22 -0800 | [diff] [blame] | 3069 | REPORTER_ASSERT(reporter, rr.isEmpty()); |
| 3070 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3071 | SkRect tinyR = {0, 0, 1e-9f, 1e-9f}; |
| 3072 | p.addRoundRect(tinyR, 5e-11f, 5e-11f); |
| 3073 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3076 | static void test_arc(skiatest::Reporter* reporter) { |
| 3077 | SkPath p; |
| 3078 | SkRect emptyOval = {10, 20, 30, 20}; |
| 3079 | REPORTER_ASSERT(reporter, emptyOval.isEmpty()); |
| 3080 | p.addArc(emptyOval, 1, 2); |
| 3081 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3082 | p.reset(); |
| 3083 | SkRect oval = {10, 20, 30, 40}; |
| 3084 | p.addArc(oval, 1, 0); |
| 3085 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3086 | p.reset(); |
| 3087 | SkPath cwOval; |
| 3088 | cwOval.addOval(oval); |
| 3089 | p.addArc(oval, 1, 360); |
| 3090 | REPORTER_ASSERT(reporter, p == cwOval); |
| 3091 | p.reset(); |
| 3092 | SkPath ccwOval; |
| 3093 | ccwOval.addOval(oval, SkPath::kCCW_Direction); |
| 3094 | p.addArc(oval, 1, -360); |
| 3095 | REPORTER_ASSERT(reporter, p == ccwOval); |
| 3096 | p.reset(); |
| 3097 | p.addArc(oval, 1, 180); |
| 3098 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 3099 | REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction)); |
| 3100 | p.setConvexity(SkPath::kUnknown_Convexity); |
| 3101 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 3102 | } |
| 3103 | |
| 3104 | static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 3105 | SkScalar x0, SkScalar y0) { |
| 3106 | SkPoint pts[4]; |
| 3107 | SkPath::Verb v = iter->next(pts); |
| 3108 | REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb); |
| 3109 | REPORTER_ASSERT(reporter, pts[0].fX == x0); |
| 3110 | REPORTER_ASSERT(reporter, pts[0].fY == y0); |
| 3111 | } |
| 3112 | |
| 3113 | static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 3114 | SkScalar x1, SkScalar y1) { |
| 3115 | SkPoint pts[4]; |
| 3116 | SkPath::Verb v = iter->next(pts); |
| 3117 | REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb); |
| 3118 | REPORTER_ASSERT(reporter, pts[1].fX == x1); |
| 3119 | REPORTER_ASSERT(reporter, pts[1].fY == y1); |
| 3120 | } |
| 3121 | |
| 3122 | static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 3123 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 3124 | SkPoint pts[4]; |
| 3125 | SkPath::Verb v = iter->next(pts); |
| 3126 | REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb); |
| 3127 | REPORTER_ASSERT(reporter, pts[1].fX == x1); |
| 3128 | REPORTER_ASSERT(reporter, pts[1].fY == y1); |
| 3129 | REPORTER_ASSERT(reporter, pts[2].fX == x2); |
| 3130 | REPORTER_ASSERT(reporter, pts[2].fY == y2); |
| 3131 | } |
| 3132 | |
| 3133 | static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { |
| 3134 | SkPoint pts[4]; |
| 3135 | SkPath::Verb v = iter->next(pts); |
| 3136 | REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb); |
| 3137 | } |
| 3138 | |
| 3139 | static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { |
| 3140 | check_done(reporter, p, iter); |
| 3141 | p->reset(); |
| 3142 | } |
| 3143 | |
| 3144 | static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 3145 | SkScalar x0, SkScalar y0) { |
| 3146 | SkPath::RawIter iter(*p); |
| 3147 | check_move(reporter, &iter, x0, y0); |
| 3148 | check_done_and_reset(reporter, p, &iter); |
| 3149 | } |
| 3150 | |
| 3151 | static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 3152 | SkScalar x1, SkScalar y1) { |
| 3153 | SkPath::RawIter iter(*p); |
| 3154 | check_move(reporter, &iter, 0, 0); |
| 3155 | check_line(reporter, &iter, x1, y1); |
| 3156 | check_done_and_reset(reporter, p, &iter); |
| 3157 | } |
| 3158 | |
| 3159 | static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p, |
| 3160 | SkScalar x1, SkScalar y1) { |
| 3161 | SkPath::RawIter iter(*p); |
| 3162 | check_move(reporter, &iter, 0, 0); |
| 3163 | check_line(reporter, &iter, x1, y1); |
| 3164 | check_done(reporter, p, &iter); |
| 3165 | } |
| 3166 | |
| 3167 | static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 3168 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 3169 | SkPath::RawIter iter(*p); |
| 3170 | check_move(reporter, &iter, 0, 0); |
| 3171 | check_line(reporter, &iter, x1, y1); |
| 3172 | check_line(reporter, &iter, x2, y2); |
| 3173 | check_done_and_reset(reporter, p, &iter); |
| 3174 | } |
| 3175 | |
| 3176 | static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 3177 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 3178 | SkPath::RawIter iter(*p); |
| 3179 | check_move(reporter, &iter, 0, 0); |
| 3180 | check_quad(reporter, &iter, x1, y1, x2, y2); |
| 3181 | check_done_and_reset(reporter, p, &iter); |
| 3182 | } |
| 3183 | |
reed | d5d27d9 | 2015-02-09 13:54:43 -0800 | [diff] [blame] | 3184 | static bool nearly_equal(const SkRect& a, const SkRect& b) { |
| 3185 | return SkScalarNearlyEqual(a.fLeft, b.fLeft) && |
| 3186 | SkScalarNearlyEqual(a.fTop, b.fTop) && |
| 3187 | SkScalarNearlyEqual(a.fRight, b.fRight) && |
| 3188 | SkScalarNearlyEqual(a.fBottom, b.fBottom); |
| 3189 | } |
| 3190 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3191 | static void test_arcTo(skiatest::Reporter* reporter) { |
| 3192 | SkPath p; |
| 3193 | p.arcTo(0, 0, 1, 2, 1); |
| 3194 | check_path_is_line_and_reset(reporter, &p, 0, 0); |
| 3195 | p.arcTo(1, 2, 1, 2, 1); |
| 3196 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 3197 | p.arcTo(1, 2, 3, 4, 0); |
| 3198 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 3199 | p.arcTo(1, 2, 0, 0, 1); |
| 3200 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 3201 | p.arcTo(1, 0, 1, 1, 1); |
| 3202 | SkPoint pt; |
| 3203 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1); |
| 3204 | p.reset(); |
| 3205 | p.arcTo(1, 0, 1, -1, 1); |
| 3206 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1); |
| 3207 | p.reset(); |
| 3208 | SkRect oval = {1, 2, 3, 4}; |
| 3209 | p.arcTo(oval, 0, 0, true); |
| 3210 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3211 | p.arcTo(oval, 0, 0, false); |
| 3212 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3213 | p.arcTo(oval, 360, 0, true); |
| 3214 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3215 | p.arcTo(oval, 360, 0, false); |
| 3216 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
reed | d5d27d9 | 2015-02-09 13:54:43 -0800 | [diff] [blame] | 3217 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3218 | for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 3219 | p.arcTo(oval, 0, sweep, false); |
reed | d5d27d9 | 2015-02-09 13:54:43 -0800 | [diff] [blame] | 3220 | REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3221 | sweep += delta; |
| 3222 | delta /= 2; |
| 3223 | } |
| 3224 | for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 3225 | p.arcTo(oval, 0, sweep, false); |
reed | d5d27d9 | 2015-02-09 13:54:43 -0800 | [diff] [blame] | 3226 | REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3227 | sweep -= delta; |
| 3228 | delta /= 2; |
| 3229 | } |
| 3230 | SkRect noOvalWidth = {1, 2, 0, 3}; |
| 3231 | p.reset(); |
| 3232 | p.arcTo(noOvalWidth, 0, 360, false); |
| 3233 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3234 | |
| 3235 | SkRect noOvalHeight = {1, 2, 3, 1}; |
| 3236 | p.reset(); |
| 3237 | p.arcTo(noOvalHeight, 0, 360, false); |
| 3238 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3239 | } |
| 3240 | |
| 3241 | static void test_addPath(skiatest::Reporter* reporter) { |
| 3242 | SkPath p, q; |
| 3243 | p.lineTo(1, 2); |
| 3244 | q.moveTo(4, 4); |
| 3245 | q.lineTo(7, 8); |
| 3246 | q.conicTo(8, 7, 6, 5, 0.5f); |
| 3247 | q.quadTo(6, 7, 8, 6); |
| 3248 | q.cubicTo(5, 6, 7, 8, 7, 5); |
| 3249 | q.close(); |
| 3250 | p.addPath(q, -4, -4); |
| 3251 | SkRect expected = {0, 0, 4, 4}; |
| 3252 | REPORTER_ASSERT(reporter, p.getBounds() == expected); |
| 3253 | p.reset(); |
| 3254 | p.reverseAddPath(q); |
| 3255 | SkRect reverseExpected = {4, 4, 8, 8}; |
| 3256 | REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); |
| 3257 | } |
| 3258 | |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 3259 | static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) { |
| 3260 | SkPath p, q; |
| 3261 | if (explicitMoveTo) { |
| 3262 | p.moveTo(1, 1); |
| 3263 | } |
| 3264 | p.lineTo(1, 2); |
| 3265 | if (explicitMoveTo) { |
| 3266 | q.moveTo(2, 1); |
| 3267 | } |
| 3268 | q.lineTo(2, 2); |
| 3269 | p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode); |
| 3270 | uint8_t verbs[4]; |
| 3271 | int verbcount = p.getVerbs(verbs, 4); |
| 3272 | REPORTER_ASSERT(reporter, verbcount == 4); |
| 3273 | REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb); |
| 3274 | REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb); |
| 3275 | REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb)); |
| 3276 | REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb); |
| 3277 | } |
| 3278 | |
| 3279 | static void test_extendClosedPath(skiatest::Reporter* reporter) { |
| 3280 | SkPath p, q; |
| 3281 | p.moveTo(1, 1); |
| 3282 | p.lineTo(1, 2); |
| 3283 | p.lineTo(2, 2); |
| 3284 | p.close(); |
| 3285 | q.moveTo(2, 1); |
| 3286 | q.lineTo(2, 3); |
| 3287 | p.addPath(q, SkPath::kExtend_AddPathMode); |
| 3288 | uint8_t verbs[7]; |
| 3289 | int verbcount = p.getVerbs(verbs, 7); |
| 3290 | REPORTER_ASSERT(reporter, verbcount == 7); |
| 3291 | REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb); |
| 3292 | REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb); |
| 3293 | REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb); |
| 3294 | REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb); |
| 3295 | REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb); |
| 3296 | REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb); |
| 3297 | REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb); |
| 3298 | |
| 3299 | SkPoint pt; |
| 3300 | REPORTER_ASSERT(reporter, p.getLastPt(&pt)); |
| 3301 | REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3)); |
| 3302 | REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1)); |
| 3303 | } |
| 3304 | |
| 3305 | static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) { |
| 3306 | SkPath p, q, r; |
| 3307 | // case 1: dst is empty |
| 3308 | p.moveTo(2, 1); |
| 3309 | p.lineTo(2, 3); |
| 3310 | q.addPath(p, mode); |
| 3311 | REPORTER_ASSERT(reporter, q == p); |
| 3312 | // case 2: src is empty |
| 3313 | p.addPath(r, mode); |
| 3314 | REPORTER_ASSERT(reporter, q == p); |
| 3315 | // case 3: src and dst are empty |
| 3316 | q.reset(); |
| 3317 | q.addPath(r, mode); |
| 3318 | REPORTER_ASSERT(reporter, q.isEmpty()); |
skia.committer@gmail.com | 877c449 | 2014-02-12 03:02:04 +0000 | [diff] [blame] | 3319 | } |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 3320 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3321 | static void test_conicTo_special_case(skiatest::Reporter* reporter) { |
| 3322 | SkPath p; |
| 3323 | p.conicTo(1, 2, 3, 4, -1); |
| 3324 | check_path_is_line_and_reset(reporter, &p, 3, 4); |
| 3325 | p.conicTo(1, 2, 3, 4, SK_ScalarInfinity); |
| 3326 | check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4); |
| 3327 | p.conicTo(1, 2, 3, 4, 1); |
| 3328 | check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4); |
| 3329 | } |
| 3330 | |
| 3331 | static void test_get_point(skiatest::Reporter* reporter) { |
| 3332 | SkPath p; |
| 3333 | SkPoint pt = p.getPoint(0); |
| 3334 | REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0)); |
| 3335 | REPORTER_ASSERT(reporter, !p.getLastPt(NULL)); |
| 3336 | REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0)); |
| 3337 | p.setLastPt(10, 10); |
| 3338 | pt = p.getPoint(0); |
| 3339 | REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10)); |
| 3340 | REPORTER_ASSERT(reporter, p.getLastPt(NULL)); |
| 3341 | p.rMoveTo(10, 10); |
| 3342 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20)); |
| 3343 | } |
| 3344 | |
| 3345 | static void test_contains(skiatest::Reporter* reporter) { |
| 3346 | SkPath p; |
| 3347 | p.setFillType(SkPath::kInverseWinding_FillType); |
| 3348 | REPORTER_ASSERT(reporter, p.contains(0, 0)); |
| 3349 | p.setFillType(SkPath::kWinding_FillType); |
| 3350 | REPORTER_ASSERT(reporter, !p.contains(0, 0)); |
| 3351 | p.moveTo(4, 4); |
| 3352 | p.lineTo(6, 8); |
| 3353 | p.lineTo(8, 4); |
| 3354 | // test quick reject |
| 3355 | REPORTER_ASSERT(reporter, !p.contains(4, 0)); |
| 3356 | REPORTER_ASSERT(reporter, !p.contains(0, 4)); |
| 3357 | REPORTER_ASSERT(reporter, !p.contains(4, 10)); |
| 3358 | REPORTER_ASSERT(reporter, !p.contains(10, 4)); |
| 3359 | // test various crossings in x |
| 3360 | REPORTER_ASSERT(reporter, !p.contains(5, 7)); |
| 3361 | REPORTER_ASSERT(reporter, p.contains(6, 7)); |
| 3362 | REPORTER_ASSERT(reporter, !p.contains(7, 7)); |
| 3363 | p.reset(); |
| 3364 | p.moveTo(4, 4); |
| 3365 | p.lineTo(8, 6); |
| 3366 | p.lineTo(4, 8); |
| 3367 | // test various crossings in y |
| 3368 | REPORTER_ASSERT(reporter, !p.contains(7, 5)); |
| 3369 | REPORTER_ASSERT(reporter, p.contains(7, 6)); |
| 3370 | REPORTER_ASSERT(reporter, !p.contains(7, 7)); |
| 3371 | // test quads |
| 3372 | p.reset(); |
| 3373 | p.moveTo(4, 4); |
| 3374 | p.quadTo(6, 6, 8, 8); |
| 3375 | p.quadTo(6, 8, 4, 8); |
| 3376 | p.quadTo(4, 6, 4, 4); |
| 3377 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3378 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3379 | |
| 3380 | p.reset(); |
| 3381 | p.moveTo(6, 6); |
| 3382 | p.quadTo(8, 8, 6, 8); |
| 3383 | p.quadTo(4, 8, 4, 6); |
| 3384 | p.quadTo(4, 4, 6, 6); |
| 3385 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3386 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3387 | |
| 3388 | #define CONIC_CONTAINS_BUG_FIXED 0 |
| 3389 | #if CONIC_CONTAINS_BUG_FIXED |
| 3390 | p.reset(); |
| 3391 | p.moveTo(4, 4); |
| 3392 | p.conicTo(6, 6, 8, 8, 0.5f); |
| 3393 | p.conicTo(6, 8, 4, 8, 0.5f); |
| 3394 | p.conicTo(4, 6, 4, 4, 0.5f); |
| 3395 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3396 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3397 | #endif |
| 3398 | |
| 3399 | // test cubics |
| 3400 | SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}}; |
| 3401 | for (int i = 0; i < 3; ++i) { |
| 3402 | p.reset(); |
| 3403 | p.setFillType(SkPath::kEvenOdd_FillType); |
| 3404 | p.moveTo(pts[i].fX, pts[i].fY); |
| 3405 | 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); |
| 3406 | 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); |
| 3407 | p.close(); |
| 3408 | REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f)); |
| 3409 | REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f)); |
| 3410 | } |
| 3411 | } |
| 3412 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3413 | class PathRefTest_Private { |
| 3414 | public: |
| 3415 | static void TestPathRef(skiatest::Reporter* reporter) { |
| 3416 | static const int kRepeatCnt = 10; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3417 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3418 | SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef)); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3419 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3420 | SkPathRef::Editor ed(&pathRef); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3421 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3422 | { |
| 3423 | ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt); |
| 3424 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3425 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); |
| 3426 | REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks()); |
| 3427 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3428 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i)); |
| 3429 | } |
| 3430 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3431 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3432 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3433 | { |
| 3434 | ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt); |
| 3435 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3436 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); |
| 3437 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks()); |
| 3438 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3439 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i)); |
| 3440 | } |
| 3441 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3442 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3443 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3444 | { |
| 3445 | ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt); |
| 3446 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3447 | REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); |
| 3448 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks()); |
| 3449 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3450 | REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i)); |
| 3451 | } |
| 3452 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3453 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3454 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3455 | { |
| 3456 | SkScalar* weights = NULL; |
| 3457 | ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights); |
| 3458 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3459 | REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); |
| 3460 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights()); |
| 3461 | REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3462 | REPORTER_ASSERT(reporter, weights); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3463 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3464 | REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i)); |
| 3465 | } |
| 3466 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3467 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3468 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3469 | { |
| 3470 | ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt); |
| 3471 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3472 | REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints()); |
| 3473 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks()); |
| 3474 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3475 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i)); |
| 3476 | } |
| 3477 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3478 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3479 | } |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3480 | }; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3481 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3482 | static void test_operatorEqual(skiatest::Reporter* reporter) { |
| 3483 | SkPath a; |
| 3484 | SkPath b; |
| 3485 | REPORTER_ASSERT(reporter, a == a); |
| 3486 | REPORTER_ASSERT(reporter, a == b); |
| 3487 | a.setFillType(SkPath::kInverseWinding_FillType); |
| 3488 | REPORTER_ASSERT(reporter, a != b); |
| 3489 | a.reset(); |
| 3490 | REPORTER_ASSERT(reporter, a == b); |
| 3491 | a.lineTo(1, 1); |
| 3492 | REPORTER_ASSERT(reporter, a != b); |
| 3493 | a.reset(); |
| 3494 | REPORTER_ASSERT(reporter, a == b); |
| 3495 | a.lineTo(1, 1); |
| 3496 | b.lineTo(1, 2); |
| 3497 | REPORTER_ASSERT(reporter, a != b); |
| 3498 | a.reset(); |
| 3499 | a.lineTo(1, 2); |
| 3500 | REPORTER_ASSERT(reporter, a == b); |
| 3501 | } |
| 3502 | |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3503 | static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force, |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3504 | bool dumpAsHex, const char* str) { |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3505 | SkDynamicMemoryWStream wStream; |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3506 | path.dump(&wStream, force, dumpAsHex); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3507 | SkAutoDataUnref data(wStream.copyToData()); |
| 3508 | REPORTER_ASSERT(reporter, data->size() == strlen(str)); |
mtklein | d489759 | 2014-11-14 09:22:40 -0800 | [diff] [blame] | 3509 | if (strlen(str) > 0) { |
| 3510 | REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str))); |
| 3511 | } else { |
| 3512 | REPORTER_ASSERT(reporter, data->data() == NULL || !memcmp(data->data(), str, strlen(str))); |
| 3513 | } |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | static void test_dump(skiatest::Reporter* reporter) { |
| 3517 | SkPath p; |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3518 | compare_dump(reporter, p, false, false, ""); |
| 3519 | compare_dump(reporter, p, true, false, ""); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3520 | p.moveTo(1, 2); |
| 3521 | p.lineTo(3, 4); |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3522 | compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n" |
| 3523 | "path.lineTo(3, 4);\n"); |
| 3524 | compare_dump(reporter, p, true, false, "path.moveTo(1, 2);\n" |
| 3525 | "path.lineTo(3, 4);\n" |
| 3526 | "path.lineTo(1, 2);\n" |
| 3527 | "path.close();\n"); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3528 | p.reset(); |
| 3529 | p.moveTo(1, 2); |
| 3530 | p.quadTo(3, 4, 5, 6); |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3531 | compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n" |
| 3532 | "path.quadTo(3, 4, 5, 6);\n"); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3533 | p.reset(); |
| 3534 | p.moveTo(1, 2); |
| 3535 | p.conicTo(3, 4, 5, 6, 0.5f); |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3536 | compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n" |
| 3537 | "path.conicTo(3, 4, 5, 6, 0.5f);\n"); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3538 | p.reset(); |
| 3539 | p.moveTo(1, 2); |
| 3540 | p.cubicTo(3, 4, 5, 6, 7, 8); |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3541 | compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n" |
| 3542 | "path.cubicTo(3, 4, 5, 6, 7, 8);\n"); |
| 3543 | p.reset(); |
| 3544 | p.moveTo(1, 2); |
| 3545 | p.lineTo(3, 4); |
caryclark | 08fa28c | 2014-10-23 13:08:56 -0700 | [diff] [blame] | 3546 | compare_dump(reporter, p, false, true, |
| 3547 | "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n" |
| 3548 | "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n"); |
caryclark | e956259 | 2014-09-15 09:26:09 -0700 | [diff] [blame] | 3549 | p.reset(); |
| 3550 | p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); |
| 3551 | p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); |
| 3552 | compare_dump(reporter, p, false, false, "path.moveTo(1, 2);\n" |
| 3553 | "path.lineTo(3, 4);\n"); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3556 | class PathTest_Private { |
| 3557 | public: |
| 3558 | static void TestPathTo(skiatest::Reporter* reporter) { |
| 3559 | SkPath p, q; |
| 3560 | p.lineTo(4, 4); |
| 3561 | p.reversePathTo(q); |
| 3562 | check_path_is_line(reporter, &p, 4, 4); |
| 3563 | q.moveTo(-4, -4); |
| 3564 | p.reversePathTo(q); |
| 3565 | check_path_is_line(reporter, &p, 4, 4); |
| 3566 | q.lineTo(7, 8); |
| 3567 | q.conicTo(8, 7, 6, 5, 0.5f); |
| 3568 | q.quadTo(6, 7, 8, 6); |
| 3569 | q.cubicTo(5, 6, 7, 8, 7, 5); |
| 3570 | q.close(); |
| 3571 | p.reversePathTo(q); |
| 3572 | SkRect reverseExpected = {-4, -4, 8, 8}; |
| 3573 | REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); |
| 3574 | } |
| 3575 | }; |
| 3576 | |
commit-bot@chromium.org | 05ec223 | 2014-01-15 18:00:57 +0000 | [diff] [blame] | 3577 | DEF_TEST(Paths, reporter) { |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 3578 | test_path_crbug364224(); |
| 3579 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3580 | SkTSize<SkScalar>::Make(3,4); |
| 3581 | |
| 3582 | SkPath p, empty; |
| 3583 | SkRect bounds, bounds2; |
| 3584 | test_empty(reporter, p); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3585 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 3586 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3587 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3588 | // this triggers a code path in SkPath::operator= which is otherwise unexercised |
| 3589 | SkPath& self = p; |
| 3590 | p = self; |
| 3591 | |
| 3592 | // this triggers a code path in SkPath::swap which is otherwise unexercised |
| 3593 | p.swap(self); |
| 3594 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3595 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3596 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3597 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 3598 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3599 | // we have quads or cubics |
reed | 220f926 | 2014-12-17 08:21:04 -0800 | [diff] [blame] | 3600 | REPORTER_ASSERT(reporter, |
| 3601 | p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask)); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3602 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 3603 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3604 | p.reset(); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3605 | test_empty(reporter, p); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3606 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3607 | p.addOval(bounds); |
| 3608 | check_convex_bounds(reporter, p, bounds); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3609 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 3610 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3611 | p.rewind(); |
| 3612 | test_empty(reporter, p); |
| 3613 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3614 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3615 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3616 | // we have only lines |
| 3617 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3618 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3619 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3620 | REPORTER_ASSERT(reporter, p != empty); |
| 3621 | REPORTER_ASSERT(reporter, !(p == empty)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3622 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 3623 | // do getPoints and getVerbs return the right result |
| 3624 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4); |
| 3625 | REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3626 | SkPoint pts[4]; |
| 3627 | int count = p.getPoints(pts, 4); |
| 3628 | REPORTER_ASSERT(reporter, count == 4); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 3629 | uint8_t verbs[6]; |
| 3630 | verbs[5] = 0xff; |
| 3631 | p.getVerbs(verbs, 5); |
| 3632 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]); |
| 3633 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]); |
| 3634 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]); |
| 3635 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]); |
| 3636 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]); |
| 3637 | REPORTER_ASSERT(reporter, 0xff == verbs[5]); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3638 | bounds2.set(pts, 4); |
| 3639 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3640 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3641 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 3642 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 3643 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3644 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3645 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 3646 | bounds2.setEmpty(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3647 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 3648 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3649 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3650 | // now force p to not be a rect |
| 3651 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 3652 | p.addRect(bounds); |
| 3653 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3654 | |
robertphillips | fe7c427 | 2014-12-29 11:36:39 -0800 | [diff] [blame] | 3655 | // Test an edge case w.r.t. the bound returned by isRect (i.e., the |
| 3656 | // path has a trailing moveTo. Please see crbug.com\445368) |
| 3657 | { |
| 3658 | SkRect r; |
| 3659 | p.reset(); |
| 3660 | p.addRect(bounds); |
| 3661 | REPORTER_ASSERT(reporter, p.isRect(&r)); |
| 3662 | REPORTER_ASSERT(reporter, r == bounds); |
| 3663 | // add a moveTo outside of our bounds |
| 3664 | p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10); |
| 3665 | REPORTER_ASSERT(reporter, p.isRect(&r)); |
| 3666 | REPORTER_ASSERT(reporter, r == bounds); |
| 3667 | } |
| 3668 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3669 | test_operatorEqual(reporter); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 3670 | test_isLine(reporter); |
| 3671 | test_isRect(reporter); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 3672 | test_isNestedRects(reporter); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3673 | test_zero_length_paths(reporter); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 3674 | test_direction(reporter); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 3675 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 3676 | test_convexity2(reporter); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 3677 | test_conservativelyContains(reporter); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 3678 | test_close(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 3679 | test_segment_masks(reporter); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 3680 | test_flattening(reporter); |
| 3681 | test_transform(reporter); |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 3682 | test_bounds(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 3683 | test_iter(reporter); |
| 3684 | test_raw_iter(reporter); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 3685 | test_circle(reporter); |
| 3686 | test_oval(reporter); |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 3687 | test_strokerec(reporter); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 3688 | test_addPoly(reporter); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 3689 | test_isfinite(reporter); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 3690 | test_isfinite_after_transform(reporter); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 3691 | test_arb_round_rect_is_convex(reporter); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 3692 | test_arb_zero_rad_round_rect_is_rect(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3693 | test_addrect(reporter); |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 3694 | test_addrect_isfinite(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 3695 | test_tricky_cubic(); |
| 3696 | test_clipped_cubic(); |
| 3697 | test_crbug_170666(); |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 3698 | test_bad_cubic_crbug229478(); |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 3699 | test_bad_cubic_crbug234190(); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 3700 | test_gen_id(reporter); |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 3701 | test_path_close_issue1474(reporter); |
reed@google.com | b58ba89 | 2013-10-15 15:44:35 +0000 | [diff] [blame] | 3702 | test_path_to_region(reporter); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3703 | test_rrect(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3704 | test_arc(reporter); |
| 3705 | test_arcTo(reporter); |
| 3706 | test_addPath(reporter); |
commit-bot@chromium.org | 14747e5 | 2014-02-11 21:16:29 +0000 | [diff] [blame] | 3707 | test_addPathMode(reporter, false, false); |
| 3708 | test_addPathMode(reporter, true, false); |
| 3709 | test_addPathMode(reporter, false, true); |
| 3710 | test_addPathMode(reporter, true, true); |
| 3711 | test_extendClosedPath(reporter); |
| 3712 | test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode); |
| 3713 | test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3714 | test_conicTo_special_case(reporter); |
| 3715 | test_get_point(reporter); |
| 3716 | test_contains(reporter); |
| 3717 | PathTest_Private::TestPathTo(reporter); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3718 | PathRefTest_Private::TestPathRef(reporter); |
caryclark | 66a5d8b | 2014-06-24 08:30:15 -0700 | [diff] [blame] | 3719 | test_dump(reporter); |
piotaixr | fac4e0e | 2014-08-26 11:59:04 -0700 | [diff] [blame] | 3720 | test_path_crbugskia2820(reporter); |
reed | 5bcbe91 | 2014-12-15 12:28:33 -0800 | [diff] [blame] | 3721 | test_skbug_3239(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3722 | } |