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 */ 7 8 #include "SkAutoMalloc.h" 9 #include "SkCanvas.h" 10 #include "SkGeometry.h" 11 #include "SkPaint.h" 12 #include "SkParse.h" 13 #include "SkParsePath.h" 14 #include "SkPathEffect.h" 15 #include "SkPathPriv.h" 16 #include "SkRRect.h" 17 #include "SkRandom.h" 18 #include "SkReader32.h" 19 #include "SkSize.h" 20 #include "SkStream.h" 21 #include "SkStrokeRec.h" 22 #include "SkSurface.h" 23 #include "SkWriter32.h" 24 #include "Test.h" 25 #include <cmath> 26 27 static void set_radii(SkVector radii[4], int index, float rad) { 28 sk_bzero(radii, sizeof(SkVector) * 4); 29 radii[index].set(rad, rad); 30 } 31 32 static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds, 33 const SkVector radii[4]) { 34 SkRRect rrect; 35 rrect.setRectRadii(bounds, radii); 36 REPORTER_ASSERT(reporter, bounds == rrect.rect()); 37 38 SkPath path; 39 // this line should not assert in the debug build (from validate) 40 path.addRRect(rrect); 41 REPORTER_ASSERT(reporter, bounds == path.getBounds()); 42 } 43 44 static void test_skbug_3469(skiatest::Reporter* reporter) { 45 SkPath path; 46 path.moveTo(20, 20); 47 path.quadTo(20, 50, 80, 50); 48 path.quadTo(20, 50, 20, 80); 49 REPORTER_ASSERT(reporter, !path.isConvex()); 50 } 51 52 static void test_skbug_3239(skiatest::Reporter* reporter) { 53 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */ 54 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */ 55 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */ 56 57 const float rad = 33436320; 58 59 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big); 60 const SkRect recty = SkRect::MakeLTRB(min, min, big, max); 61 62 SkVector radii[4]; 63 for (int i = 0; i < 4; ++i) { 64 set_radii(radii, i, rad); 65 test_add_rrect(reporter, rectx, radii); 66 test_add_rrect(reporter, recty, radii); 67 } 68 } 69 70 static void make_path_crbug364224(SkPath* path) { 71 path->reset(); 72 path->moveTo(3.747501373f, 2.724499941f); 73 path->lineTo(3.747501373f, 3.75f); 74 path->cubicTo(3.747501373f, 3.88774991f, 3.635501385f, 4.0f, 3.497501373f, 4.0f); 75 path->lineTo(0.7475013733f, 4.0f); 76 path->cubicTo(0.6095013618f, 4.0f, 0.4975013733f, 3.88774991f, 0.4975013733f, 3.75f); 77 path->lineTo(0.4975013733f, 1.0f); 78 path->cubicTo(0.4975013733f, 0.8622499704f, 0.6095013618f, 0.75f, 0.7475013733f,0.75f); 79 path->lineTo(3.497501373f, 0.75f); 80 path->cubicTo(3.50275135f, 0.75f, 3.5070014f, 0.7527500391f, 3.513001442f, 0.753000021f); 81 path->lineTo(3.715001345f, 0.5512499809f); 82 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f); 83 path->lineTo(0.7475013733f, 0.4999999702f); 84 path->cubicTo(0.4715013802f, 0.4999999702f, 0.2475013733f, 0.7239999771f, 0.2475013733f, 1.0f); 85 path->lineTo(0.2475013733f, 3.75f); 86 path->cubicTo(0.2475013733f, 4.026000023f, 0.4715013504f, 4.25f, 0.7475013733f, 4.25f); 87 path->lineTo(3.497501373f, 4.25f); 88 path->cubicTo(3.773501396f, 4.25f, 3.997501373f, 4.026000023f, 3.997501373f, 3.75f); 89 path->lineTo(3.997501373f, 2.474750042f); 90 path->lineTo(3.747501373f, 2.724499941f); 91 path->close(); 92 } 93 94 static void make_path_crbug364224_simplified(SkPath* path) { 95 path->moveTo(3.747501373f, 2.724499941f); 96 path->cubicTo(3.648251295f, 0.5194999576f, 3.575501442f, 0.4999999702f, 3.497501373f, 0.4999999702f); 97 path->close(); 98 } 99 100 static void test_sect_with_horizontal_needs_pinning() { 101 // Test that sect_with_horizontal in SkLineClipper.cpp needs to pin after computing the 102 // intersection. 103 SkPath path; 104 path.reset(); 105 path.moveTo(-540000, -720000); 106 path.lineTo(-9.10000017e-05f, 9.99999996e-13f); 107 path.lineTo(1, 1); 108 109 // Without the pinning code in sect_with_horizontal(), this would assert in the lineclipper 110 SkPaint paint; 111 SkSurface::MakeRasterN32Premul(10, 10)->getCanvas()->drawPath(path, paint); 112 } 113 114 static void test_path_crbug364224() { 115 SkPath path; 116 SkPaint paint; 117 auto surface(SkSurface::MakeRasterN32Premul(84, 88)); 118 SkCanvas* canvas = surface->getCanvas(); 119 120 make_path_crbug364224_simplified(&path); 121 canvas->drawPath(path, paint); 122 123 make_path_crbug364224(&path); 124 canvas->drawPath(path, paint); 125 } 126 127 // this is a unit test instead of a GM because it doesn't draw anything 128 static void test_fuzz_crbug_638223() { 129 auto surface(SkSurface::MakeRasterN32Premul(250, 250)); 130 SkCanvas* canvas = surface->getCanvas(); 131 SkPath path; 132 path.moveTo(SkBits2Float(0x47452a00), SkBits2Float(0x43211d01)); // 50474, 161.113f 133 path.conicTo(SkBits2Float(0x401c0000), SkBits2Float(0x40680000), 134 SkBits2Float(0x02c25a81), SkBits2Float(0x981a1fa0), 135 SkBits2Float(0x6bf9abea)); // 2.4375f, 3.625f, 2.85577e-37f, -1.992e-24f, 6.03669e+26f 136 SkPaint paint; 137 paint.setAntiAlias(true); 138 canvas->drawPath(path, paint); 139 } 140 141 static void test_fuzz_crbug_643933() { 142 auto surface(SkSurface::MakeRasterN32Premul(250, 250)); 143 SkCanvas* canvas = surface->getCanvas(); 144 SkPaint paint; 145 paint.setAntiAlias(true); 146 SkPath path; 147 path.moveTo(0, 0); 148 path.conicTo(SkBits2Float(0x002001f2), SkBits2Float(0x4161ffff), // 2.93943e-39f, 14.125f 149 SkBits2Float(0x49f7224d), SkBits2Float(0x45eec8df), // 2.02452e+06f, 7641.11f 150 SkBits2Float(0x721aee0c)); // 3.0687e+30f 151 canvas->drawPath(path, paint); 152 path.reset(); 153 path.moveTo(0, 0); 154 path.conicTo(SkBits2Float(0x00007ff2), SkBits2Float(0x4169ffff), // 4.58981e-41f, 14.625f 155 SkBits2Float(0x43ff2261), SkBits2Float(0x41eeea04), // 510.269f, 29.8643f 156 SkBits2Float(0x5d06eff8)); // 6.07704e+17f 157 canvas->drawPath(path, paint); 158 } 159 160 static void test_fuzz_crbug_647922() { 161 auto surface(SkSurface::MakeRasterN32Premul(250, 250)); 162 SkCanvas* canvas = surface->getCanvas(); 163 SkPaint paint; 164 paint.setAntiAlias(true); 165 SkPath path; 166 path.moveTo(0, 0); 167 path.conicTo(SkBits2Float(0x00003939), SkBits2Float(0x42487fff), // 2.05276e-41f, 50.125f 168 SkBits2Float(0x48082361), SkBits2Float(0x4408e8e9), // 139406, 547.639f 169 SkBits2Float(0x4d1ade0f)); // 1.6239e+08f 170 canvas->drawPath(path, paint); 171 } 172 173 static void test_fuzz_crbug_662780() { 174 auto surface(SkSurface::MakeRasterN32Premul(250, 250)); 175 SkCanvas* canvas = surface->getCanvas(); 176 SkPaint paint; 177 paint.setAntiAlias(true); 178 SkPath path; 179 path.moveTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158 180 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x42f00000)); // 8, 120 181 // 8, 8, 8.00002f, 8, 0.707107f 182 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x41000000), 183 SkBits2Float(0x41000010), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3)); 184 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000)); // 308, 8 185 // 308, 8, 308, 8, 0.707107f 186 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x41000000), 187 SkBits2Float(0x439a0000), SkBits2Float(0x41000000), SkBits2Float(0x3f3504f3)); 188 path.lineTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000)); // 308, 158 189 // 308, 158, 308, 158, 0.707107f 190 path.conicTo(SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), 191 SkBits2Float(0x439a0000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3)); 192 path.lineTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000)); // 8, 158 193 // 8, 158, 8, 158, 0.707107f 194 path.conicTo(SkBits2Float(0x41000000), SkBits2Float(0x431e0000), 195 SkBits2Float(0x41000000), SkBits2Float(0x431e0000), SkBits2Float(0x3f3504f3)); 196 path.close(); 197 canvas->clipPath(path, true); 198 canvas->drawRect(SkRect::MakeWH(250, 250), paint); 199 } 200 201 static void test_mask_overflow() { 202 auto surface(SkSurface::MakeRasterN32Premul(500, 500)); 203 SkCanvas* canvas = surface->getCanvas(); 204 SkPaint paint; 205 paint.setAntiAlias(true); 206 SkPath path; 207 path.moveTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341 208 path.lineTo(SkBits2Float(0x43de6000), SkBits2Float(0x43aa8000)); // 444.75f, 341 209 // 440.47f, 341, 437, 344.47f, 437, 348.75f 210 path.cubicTo(SkBits2Float(0x43dc3c29), SkBits2Float(0x43aa8000), 211 SkBits2Float(0x43da8000), SkBits2Float(0x43ac3c29), 212 SkBits2Float(0x43da8000), SkBits2Float(0x43ae6000)); 213 path.lineTo(SkBits2Float(0x43da8000), SkBits2Float(0x43b18000)); // 437, 355 214 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43b18000)); // 453, 355 215 path.lineTo(SkBits2Float(0x43e28000), SkBits2Float(0x43aa8000)); // 453, 341 216 canvas->drawPath(path, paint); 217 } 218 219 static void test_fuzz_crbug_668907() { 220 auto surface(SkSurface::MakeRasterN32Premul(400, 500)); 221 SkCanvas* canvas = surface->getCanvas(); 222 SkPaint paint; 223 paint.setAntiAlias(true); 224 SkPath path; 225 path.moveTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f 226 path.quadTo(SkBits2Float(0x41410041), SkBits2Float(0xc1414141), SkBits2Float(0x41414141), 227 SkBits2Float(0x414100ff)); // 12.0626f, -12.0784f, 12.0784f, 12.0627f 228 path.lineTo(SkBits2Float(0x46313741), SkBits2Float(0x3b00e540)); // 11341.8f, 0.00196679f 229 path.close(); 230 canvas->drawPath(path, paint); 231 } 232 233 /** 234 * In debug mode, this path was causing an assertion to fail in 235 * SkPathStroker::preJoinTo() and, in Release, the use of an unitialized value. 236 */ 237 static void make_path_crbugskia2820(SkPath* path, skiatest::Reporter* reporter) { 238 SkPoint orig, p1, p2, p3; 239 orig = SkPoint::Make(1.f, 1.f); 240 p1 = SkPoint::Make(1.f - SK_ScalarNearlyZero, 1.f); 241 p2 = SkPoint::Make(1.f, 1.f + SK_ScalarNearlyZero); 242 p3 = SkPoint::Make(2.f, 2.f); 243 244 path->reset(); 245 path->moveTo(orig); 246 path->cubicTo(p1, p2, p3); 247 path->close(); 248 } 249 250 static void test_path_crbugskia2820(skiatest::Reporter* reporter) {//GrContext* context) { 251 SkPath path; 252 make_path_crbugskia2820(&path, reporter); 253 254 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); 255 stroke.setStrokeStyle(2 * SK_Scalar1); 256 stroke.applyToPath(&path, path); 257 } 258 259 static void test_path_crbugskia5995() { 260 auto surface(SkSurface::MakeRasterN32Premul(500, 500)); 261 SkCanvas* canvas = surface->getCanvas(); 262 SkPaint paint; 263 paint.setAntiAlias(true); 264 SkPath path; 265 path.moveTo(SkBits2Float(0x40303030), SkBits2Float(0x3e303030)); // 2.75294f, 0.172059f 266 path.quadTo(SkBits2Float(0x41d63030), SkBits2Float(0x30303030), SkBits2Float(0x41013030), 267 SkBits2Float(0x00000000)); // 26.7735f, 6.40969e-10f, 8.07426f, 0 268 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0 269 canvas->drawPath(path, paint); 270 } 271 272 static void make_path0(SkPath* path) { 273 // from * https://code.google.com/p/skia/issues/detail?id=1706 274 275 path->moveTo(146.939f, 1012.84f); 276 path->lineTo(181.747f, 1009.18f); 277 path->lineTo(182.165f, 1013.16f); 278 path->lineTo(147.357f, 1016.82f); 279 path->lineTo(146.939f, 1012.84f); 280 path->close(); 281 } 282 283 static void make_path1(SkPath* path) { 284 path->addRect(SkRect::MakeXYWH(10, 10, 10, 1)); 285 } 286 287 typedef void (*PathProc)(SkPath*); 288 289 /* 290 * Regression test: we used to crash (overwrite internal storage) during 291 * construction of the region when the path was INVERSE. That is now fixed, 292 * so test these regions (which used to assert/crash). 293 * 294 * https://code.google.com/p/skia/issues/detail?id=1706 295 */ 296 static void test_path_to_region(skiatest::Reporter* reporter) { 297 PathProc procs[] = { 298 make_path0, 299 make_path1, 300 }; 301 302 SkRegion clip; 303 clip.setRect(0, 0, 1255, 1925); 304 305 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { 306 SkPath path; 307 procs[i](&path); 308 309 SkRegion rgn; 310 rgn.setPath(path, clip); 311 path.toggleInverseFillType(); 312 rgn.setPath(path, clip); 313 } 314 } 315 316 #ifdef SK_BUILD_FOR_WIN 317 #define SUPPRESS_VISIBILITY_WARNING 318 #else 319 #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden"))) 320 #endif 321 322 static void test_path_close_issue1474(skiatest::Reporter* reporter) { 323 // This test checks that r{Line,Quad,Conic,Cubic}To following a close() 324 // are relative to the point we close to, not relative to the point we close from. 325 SkPath path; 326 SkPoint last; 327 328 // Test rLineTo(). 329 path.rLineTo(0, 100); 330 path.rLineTo(100, 0); 331 path.close(); // Returns us back to 0,0. 332 path.rLineTo(50, 50); // This should go to 50,50. 333 334 path.getLastPt(&last); 335 REPORTER_ASSERT(reporter, 50 == last.fX); 336 REPORTER_ASSERT(reporter, 50 == last.fY); 337 338 // Test rQuadTo(). 339 path.rewind(); 340 path.rLineTo(0, 100); 341 path.rLineTo(100, 0); 342 path.close(); 343 path.rQuadTo(50, 50, 75, 75); 344 345 path.getLastPt(&last); 346 REPORTER_ASSERT(reporter, 75 == last.fX); 347 REPORTER_ASSERT(reporter, 75 == last.fY); 348 349 // Test rConicTo(). 350 path.rewind(); 351 path.rLineTo(0, 100); 352 path.rLineTo(100, 0); 353 path.close(); 354 path.rConicTo(50, 50, 85, 85, 2); 355 356 path.getLastPt(&last); 357 REPORTER_ASSERT(reporter, 85 == last.fX); 358 REPORTER_ASSERT(reporter, 85 == last.fY); 359 360 // Test rCubicTo(). 361 path.rewind(); 362 path.rLineTo(0, 100); 363 path.rLineTo(100, 0); 364 path.close(); 365 path.rCubicTo(50, 50, 85, 85, 95, 95); 366 367 path.getLastPt(&last); 368 REPORTER_ASSERT(reporter, 95 == last.fX); 369 REPORTER_ASSERT(reporter, 95 == last.fY); 370 } 371 372 static void test_gen_id(skiatest::Reporter* reporter) { 373 SkPath a, b; 374 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); 375 376 a.moveTo(0, 0); 377 const uint32_t z = a.getGenerationID(); 378 REPORTER_ASSERT(reporter, z != b.getGenerationID()); 379 380 a.reset(); 381 REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); 382 383 a.moveTo(1, 1); 384 const uint32_t y = a.getGenerationID(); 385 REPORTER_ASSERT(reporter, z != y); 386 387 b.moveTo(2, 2); 388 const uint32_t x = b.getGenerationID(); 389 REPORTER_ASSERT(reporter, x != y && x != z); 390 391 a.swap(b); 392 REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x); 393 394 b = a; 395 REPORTER_ASSERT(reporter, b.getGenerationID() == x); 396 397 SkPath c(a); 398 REPORTER_ASSERT(reporter, c.getGenerationID() == x); 399 400 c.lineTo(3, 3); 401 const uint32_t w = c.getGenerationID(); 402 REPORTER_ASSERT(reporter, b.getGenerationID() == x); 403 REPORTER_ASSERT(reporter, a.getGenerationID() == x); 404 REPORTER_ASSERT(reporter, w != x); 405 406 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 407 static bool kExpectGenIDToIgnoreFill = false; 408 #else 409 static bool kExpectGenIDToIgnoreFill = true; 410 #endif 411 412 c.toggleInverseFillType(); 413 const uint32_t v = c.getGenerationID(); 414 REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill); 415 416 c.rewind(); 417 REPORTER_ASSERT(reporter, v != c.getGenerationID()); 418 } 419 420 // This used to assert in the debug build, as the edges did not all line-up. 421 static void test_bad_cubic_crbug234190() { 422 SkPath path; 423 path.moveTo(13.8509f, 3.16858f); 424 path.cubicTo(-2.35893e+08f, -4.21044e+08f, 425 -2.38991e+08f, -4.26573e+08f, 426 -2.41016e+08f, -4.30188e+08f); 427 428 SkPaint paint; 429 paint.setAntiAlias(true); 430 auto surface(SkSurface::MakeRasterN32Premul(84, 88)); 431 surface->getCanvas()->drawPath(path, paint); 432 } 433 434 static void test_bad_cubic_crbug229478() { 435 const SkPoint pts[] = { 436 { 4595.91064f, -11596.9873f }, 437 { 4597.2168f, -11595.9414f }, 438 { 4598.52344f, -11594.8955f }, 439 { 4599.83008f, -11593.8496f }, 440 }; 441 442 SkPath path; 443 path.moveTo(pts[0]); 444 path.cubicTo(pts[1], pts[2], pts[3]); 445 446 SkPaint paint; 447 paint.setStyle(SkPaint::kStroke_Style); 448 paint.setStrokeWidth(20); 449 450 SkPath dst; 451 // Before the fix, this would infinite-recurse, and run out of stack 452 // because we would keep trying to subdivide a degenerate cubic segment. 453 paint.getFillPath(path, &dst, nullptr); 454 } 455 456 static void build_path_170666(SkPath& path) { 457 path.moveTo(17.9459f, 21.6344f); 458 path.lineTo(139.545f, -47.8105f); 459 path.lineTo(139.545f, -47.8105f); 460 path.lineTo(131.07f, -47.3888f); 461 path.lineTo(131.07f, -47.3888f); 462 path.lineTo(122.586f, -46.9532f); 463 path.lineTo(122.586f, -46.9532f); 464 path.lineTo(18076.6f, 31390.9f); 465 path.lineTo(18076.6f, 31390.9f); 466 path.lineTo(18085.1f, 31390.5f); 467 path.lineTo(18085.1f, 31390.5f); 468 path.lineTo(18076.6f, 31390.9f); 469 path.lineTo(18076.6f, 31390.9f); 470 path.lineTo(17955, 31460.3f); 471 path.lineTo(17955, 31460.3f); 472 path.lineTo(17963.5f, 31459.9f); 473 path.lineTo(17963.5f, 31459.9f); 474 path.lineTo(17971.9f, 31459.5f); 475 path.lineTo(17971.9f, 31459.5f); 476 path.lineTo(17.9551f, 21.6205f); 477 path.lineTo(17.9551f, 21.6205f); 478 path.lineTo(9.47091f, 22.0561f); 479 path.lineTo(9.47091f, 22.0561f); 480 path.lineTo(17.9459f, 21.6344f); 481 path.lineTo(17.9459f, 21.6344f); 482 path.close();path.moveTo(0.995934f, 22.4779f); 483 path.lineTo(0.986725f, 22.4918f); 484 path.lineTo(0.986725f, 22.4918f); 485 path.lineTo(17955, 31460.4f); 486 path.lineTo(17955, 31460.4f); 487 path.lineTo(17971.9f, 31459.5f); 488 path.lineTo(17971.9f, 31459.5f); 489 path.lineTo(18093.6f, 31390.1f); 490 path.lineTo(18093.6f, 31390.1f); 491 path.lineTo(18093.6f, 31390); 492 path.lineTo(18093.6f, 31390); 493 path.lineTo(139.555f, -47.8244f); 494 path.lineTo(139.555f, -47.8244f); 495 path.lineTo(122.595f, -46.9671f); 496 path.lineTo(122.595f, -46.9671f); 497 path.lineTo(0.995934f, 22.4779f); 498 path.lineTo(0.995934f, 22.4779f); 499 path.close(); 500 path.moveTo(5.43941f, 25.5223f); 501 path.lineTo(798267, -28871.1f); 502 path.lineTo(798267, -28871.1f); 503 path.lineTo(3.12512e+06f, -113102); 504 path.lineTo(3.12512e+06f, -113102); 505 path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813); 506 path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f); 507 path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f); 508 path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f); 509 path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f); 510 path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f); 511 path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f); 512 path.lineTo(2.78271e+08f, -1.00733e+07f); 513 path.lineTo(2.78271e+08f, -1.00733e+07f); 514 path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f); 515 path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f); 516 path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f); 517 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); 518 path.lineTo(2.77473e+08f, -1.00444e+07f); 519 path.lineTo(2.77473e+08f, -1.00444e+07f); 520 path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f); 521 path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f); 522 path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f); 523 path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f); 524 path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f); 525 path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814); 526 path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103); 527 path.lineTo(798284, -28872); 528 path.lineTo(798284, -28872); 529 path.lineTo(22.4044f, 24.6677f); 530 path.lineTo(22.4044f, 24.6677f); 531 path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f); 532 path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f); 533 path.close(); 534 } 535 536 static void build_path_simple_170666(SkPath& path) { 537 path.moveTo(126.677f, 24.1591f); 538 path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); 539 } 540 541 // This used to assert in the SK_DEBUG build, as the clip step would fail with 542 // too-few interations in our cubic-line intersection code. That code now runs 543 // 24 interations (instead of 16). 544 static void test_crbug_170666() { 545 SkPath path; 546 SkPaint paint; 547 paint.setAntiAlias(true); 548 549 auto surface(SkSurface::MakeRasterN32Premul(1000, 1000)); 550 551 build_path_simple_170666(path); 552 surface->getCanvas()->drawPath(path, paint); 553 554 build_path_170666(path); 555 surface->getCanvas()->drawPath(path, paint); 556 } 557 558 559 static void test_tiny_path_convexity(skiatest::Reporter* reporter, const char* pathBug, 560 SkScalar tx, SkScalar ty, SkScalar scale) { 561 SkPath smallPath; 562 SkAssertResult(SkParsePath::FromSVGString(pathBug, &smallPath)); 563 bool smallConvex = smallPath.isConvex(); 564 SkPath largePath; 565 SkAssertResult(SkParsePath::FromSVGString(pathBug, &largePath)); 566 SkMatrix matrix; 567 matrix.reset(); 568 matrix.preTranslate(100, 100); 569 matrix.preScale(scale, scale); 570 largePath.transform(matrix); 571 bool largeConvex = largePath.isConvex(); 572 REPORTER_ASSERT(reporter, smallConvex == largeConvex); 573 } 574 575 static void test_crbug_493450(skiatest::Reporter* reporter) { 576 const char reducedCase[] = 577 "M0,0" 578 "L0.0002, 0" 579 "L0.0002, 0.0002" 580 "L0.0001, 0.0001" 581 "L0,0.0002" 582 "Z"; 583 test_tiny_path_convexity(reporter, reducedCase, 100, 100, 100000); 584 const char originalFiddleData[] = 585 "M-0.3383152268862998,-0.11217565719203619L-0.33846085183212765,-0.11212264406895281" 586 "L-0.338509393480737,-0.11210607966681395L-0.33857792286700894,-0.1121889121487573" 587 "L-0.3383866116636664,-0.11228834570924921L-0.33842087635680235,-0.11246078673250548" 588 "L-0.33809536177201055,-0.11245415228342878L-0.33797257995493996,-0.11216571641452182" 589 "L-0.33802112160354925,-0.11201996164188659L-0.33819815585141844,-0.11218559834671019Z"; 590 test_tiny_path_convexity(reporter, originalFiddleData, 280081.4116670522f, 93268.04618493588f, 591 826357.3384828606f); 592 } 593 594 static void test_crbug_495894(skiatest::Reporter* reporter) { 595 const char originalFiddleData[] = 596 "M-0.34004273849857214,-0.11332803232216355L-0.34008271397389744,-0.11324483772714951" 597 "L-0.3401940742265893,-0.11324483772714951L-0.34017694188002134,-0.11329807920275889" 598 "L-0.3402026403998733,-0.11333468903941245L-0.34029972369709194,-0.11334134592705701" 599 "L-0.3403054344792813,-0.11344121970007795L-0.3403140006525653,-0.11351115418399343" 600 "L-0.34024261587519866,-0.11353446986281181L-0.3402197727464413,-0.11360442946144192" 601 "L-0.34013696640469604,-0.11359110237029302L-0.34009128014718143,-0.1135877707043939" 602 "L-0.3400598708451401,-0.11360776134112742L-0.34004273849857214,-0.11355112520064405" 603 "L-0.3400113291965308,-0.11355112520064405L-0.3399970522410575,-0.11359110237029302" 604 "L-0.33997135372120546,-0.11355112520064405L-0.3399627875479215,-0.11353780084493197" 605 "L-0.3399485105924481,-0.11350782354357004L-0.3400027630232468,-0.11346452910331437" 606 "L-0.3399485105924481,-0.11340126558629839L-0.33993994441916414,-0.11340126558629839" 607 "L-0.33988283659727087,-0.11331804756574679L-0.33989140277055485,-0.11324483772714951" 608 "L-0.33997991989448945,-0.11324483772714951L-0.3399856306766788,-0.11324483772714951" 609 "L-0.34002560615200417,-0.11334467443478255ZM-0.3400684370184241,-0.11338461985124307" 610 "L-0.340154098751264,-0.11341791238732665L-0.340162664924548,-0.1134378899559977" 611 "L-0.34017979727111597,-0.11340126558629839L-0.3401655203156427,-0.11338129083212668" 612 "L-0.34012268944922275,-0.11332137577529414L-0.34007414780061346,-0.11334467443478255Z" 613 "M-0.3400027630232468,-0.11290567901106024L-0.3400113291965308,-0.11298876531245433" 614 "L-0.33997991989448945,-0.11301535852306784L-0.33990282433493346,-0.11296217481488612" 615 "L-0.33993994441916414,-0.11288906492739594Z"; 616 test_tiny_path_convexity(reporter, originalFiddleData, 22682.240000000005f,7819.72220766405f, 617 65536); 618 } 619 620 static void test_crbug_613918() { 621 SkPath path; 622 path.conicTo(-6.62478e-08f, 4.13885e-08f, -6.36935e-08f, 3.97927e-08f, 0.729058f); 623 path.quadTo(2.28206e-09f, -1.42572e-09f, 3.91919e-09f, -2.44852e-09f); 624 path.cubicTo(-16752.2f, -26792.9f, -21.4673f, 10.9347f, -8.57322f, -7.22739f); 625 626 // This call could lead to an assert or uninitialized read due to a failure 627 // to check the return value from SkCubicClipper::ChopMonoAtY. 628 path.contains(-1.84817e-08f, 1.15465e-08f); 629 } 630 631 static void test_addrect(skiatest::Reporter* reporter) { 632 SkPath path; 633 path.lineTo(0, 0); 634 path.addRect(SkRect::MakeWH(50, 100)); 635 REPORTER_ASSERT(reporter, path.isRect(nullptr)); 636 637 path.reset(); 638 path.lineTo(FLT_EPSILON, FLT_EPSILON); 639 path.addRect(SkRect::MakeWH(50, 100)); 640 REPORTER_ASSERT(reporter, !path.isRect(nullptr)); 641 642 path.reset(); 643 path.quadTo(0, 0, 0, 0); 644 path.addRect(SkRect::MakeWH(50, 100)); 645 REPORTER_ASSERT(reporter, !path.isRect(nullptr)); 646 647 path.reset(); 648 path.conicTo(0, 0, 0, 0, 0.5f); 649 path.addRect(SkRect::MakeWH(50, 100)); 650 REPORTER_ASSERT(reporter, !path.isRect(nullptr)); 651 652 path.reset(); 653 path.cubicTo(0, 0, 0, 0, 0, 0); 654 path.addRect(SkRect::MakeWH(50, 100)); 655 REPORTER_ASSERT(reporter, !path.isRect(nullptr)); 656 } 657 658 // Make sure we stay non-finite once we get there (unless we reset or rewind). 659 static void test_addrect_isfinite(skiatest::Reporter* reporter) { 660 SkPath path; 661 662 path.addRect(SkRect::MakeWH(50, 100)); 663 REPORTER_ASSERT(reporter, path.isFinite()); 664 665 path.moveTo(0, 0); 666 path.lineTo(SK_ScalarInfinity, 42); 667 REPORTER_ASSERT(reporter, !path.isFinite()); 668 669 path.addRect(SkRect::MakeWH(50, 100)); 670 REPORTER_ASSERT(reporter, !path.isFinite()); 671 672 path.reset(); 673 REPORTER_ASSERT(reporter, path.isFinite()); 674 675 path.addRect(SkRect::MakeWH(50, 100)); 676 REPORTER_ASSERT(reporter, path.isFinite()); 677 } 678 679 static void build_big_path(SkPath* path, bool reducedCase) { 680 if (reducedCase) { 681 path->moveTo(577330, 1971.72f); 682 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); 683 } else { 684 path->moveTo(60.1631f, 7.70567f); 685 path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f); 686 path->lineTo(577379, 1977.77f); 687 path->quadTo(577364, 1979.57f, 577325, 1980.26f); 688 path->quadTo(577286, 1980.95f, 577245, 1980.13f); 689 path->quadTo(577205, 1979.3f, 577187, 1977.45f); 690 path->quadTo(577168, 1975.6f, 577183, 1973.8f); 691 path->quadTo(577198, 1972, 577238, 1971.31f); 692 path->quadTo(577277, 1970.62f, 577317, 1971.45f); 693 path->quadTo(577330, 1971.72f, 577341, 1972.11f); 694 path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); 695 path->moveTo(306.718f, -32.912f); 696 path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f); 697 } 698 } 699 700 static void test_clipped_cubic() { 701 auto surface(SkSurface::MakeRasterN32Premul(640, 480)); 702 703 // This path used to assert, because our cubic-chopping code incorrectly 704 // moved control points after the chop. This test should be run in SK_DEBUG 705 // mode to ensure that we no long assert. 706 SkPath path; 707 for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) { 708 build_big_path(&path, SkToBool(doReducedCase)); 709 710 SkPaint paint; 711 for (int doAA = 0; doAA <= 1; ++doAA) { 712 paint.setAntiAlias(SkToBool(doAA)); 713 surface->getCanvas()->drawPath(path, paint); 714 } 715 } 716 } 717 718 static void dump_if_ne(skiatest::Reporter* reporter, const SkRect& expected, const SkRect& bounds) { 719 if (expected != bounds) { 720 ERRORF(reporter, "path.getBounds() returned [%g %g %g %g], but expected [%g %g %g %g]", 721 bounds.left(), bounds.top(), bounds.right(), bounds.bottom(), 722 expected.left(), expected.top(), expected.right(), expected.bottom()); 723 } 724 } 725 726 static void test_bounds_crbug_513799(skiatest::Reporter* reporter) { 727 SkPath path; 728 #if 0 729 // As written these tests were failing on LLVM 4.2 MacMini Release mysteriously, so we've 730 // rewritten them to avoid this (compiler-bug?). 731 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(0, 0, 0, 0) == path.getBounds()); 732 733 path.moveTo(-5, -8); 734 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, -5, -8) == path.getBounds()); 735 736 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); 737 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds()); 738 739 path.moveTo(1, 2); 740 REPORTER_ASSERT(reporter, SkRect::MakeLTRB(-5, -8, 3, 4) == path.getBounds()); 741 #else 742 dump_if_ne(reporter, SkRect::MakeLTRB(0, 0, 0, 0), path.getBounds()); 743 744 path.moveTo(-5, -8); // should set the bounds 745 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, -5, -8), path.getBounds()); 746 747 path.addRect(SkRect::MakeLTRB(1, 2, 3, 4)); // should extend the bounds 748 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds()); 749 750 path.moveTo(1, 2); // don't expect this to have changed the bounds 751 dump_if_ne(reporter, SkRect::MakeLTRB(-5, -8, 3, 4), path.getBounds()); 752 #endif 753 } 754 755 #include "SkSurface.h" 756 static void test_fuzz_crbug_627414(skiatest::Reporter* reporter) { 757 SkPath path; 758 path.moveTo(0, 0); 759 path.conicTo(3.58732e-43f, 2.72084f, 3.00392f, 3.00392f, 8.46e+37f); 760 761 SkPaint paint; 762 paint.setAntiAlias(true); 763 764 auto surf = SkSurface::MakeRasterN32Premul(100, 100); 765 surf->getCanvas()->drawPath(path, paint); 766 } 767 768 // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/ 769 // which triggered an assert, from a tricky cubic. This test replicates that 770 // example, so we can ensure that we handle it (in SkEdge.cpp), and don't 771 // assert in the SK_DEBUG build. 772 static void test_tricky_cubic() { 773 const SkPoint pts[] = { 774 { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) }, 775 { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) }, 776 { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) }, 777 { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) }, 778 }; 779 780 SkPath path; 781 path.moveTo(pts[0]); 782 path.cubicTo(pts[1], pts[2], pts[3]); 783 784 SkPaint paint; 785 paint.setAntiAlias(true); 786 787 SkSurface::MakeRasterN32Premul(19, 130)->getCanvas()->drawPath(path, paint); 788 } 789 790 // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651 791 // 792 static void test_isfinite_after_transform(skiatest::Reporter* reporter) { 793 SkPath path; 794 path.quadTo(157, 366, 286, 208); 795 path.arcTo(37, 442, 315, 163, 957494590897113.0f); 796 797 SkMatrix matrix; 798 matrix.setScale(1000*1000, 1000*1000); 799 800 // Be sure that path::transform correctly updates isFinite and the bounds 801 // if the transformation overflows. The previous bug was that isFinite was 802 // set to true in this case, but the bounds were not set to empty (which 803 // they should be). 804 while (path.isFinite()) { 805 REPORTER_ASSERT(reporter, path.getBounds().isFinite()); 806 REPORTER_ASSERT(reporter, !path.getBounds().isEmpty()); 807 path.transform(matrix); 808 } 809 REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); 810 811 matrix.setTranslate(SK_Scalar1, SK_Scalar1); 812 path.transform(matrix); 813 // we need to still be non-finite 814 REPORTER_ASSERT(reporter, !path.isFinite()); 815 REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); 816 } 817 818 static void add_corner_arc(SkPath* path, const SkRect& rect, 819 SkScalar xIn, SkScalar yIn, 820 int startAngle) 821 { 822 823 SkScalar rx = SkMinScalar(rect.width(), xIn); 824 SkScalar ry = SkMinScalar(rect.height(), yIn); 825 826 SkRect arcRect; 827 arcRect.set(-rx, -ry, rx, ry); 828 switch (startAngle) { 829 case 0: 830 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); 831 break; 832 case 90: 833 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); 834 break; 835 case 180: 836 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); 837 break; 838 case 270: 839 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); 840 break; 841 default: 842 break; 843 } 844 845 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); 846 } 847 848 static void make_arb_round_rect(SkPath* path, const SkRect& r, 849 SkScalar xCorner, SkScalar yCorner) { 850 // we are lazy here and use the same x & y for each corner 851 add_corner_arc(path, r, xCorner, yCorner, 270); 852 add_corner_arc(path, r, xCorner, yCorner, 0); 853 add_corner_arc(path, r, xCorner, yCorner, 90); 854 add_corner_arc(path, r, xCorner, yCorner, 180); 855 path->close(); 856 } 857 858 // Chrome creates its own round rects with each corner possibly being different. 859 // Performance will suffer if they are not convex. 860 // Note: PathBench::ArbRoundRectBench performs almost exactly 861 // the same test (but with drawing) 862 static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) { 863 SkRandom rand; 864 SkRect r; 865 866 for (int i = 0; i < 5000; ++i) { 867 868 SkScalar size = rand.nextUScalar1() * 30; 869 if (size < SK_Scalar1) { 870 continue; 871 } 872 r.fLeft = rand.nextUScalar1() * 300; 873 r.fTop = rand.nextUScalar1() * 300; 874 r.fRight = r.fLeft + 2 * size; 875 r.fBottom = r.fTop + 2 * size; 876 877 SkPath temp; 878 879 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); 880 881 REPORTER_ASSERT(reporter, temp.isConvex()); 882 } 883 } 884 885 // Chrome will sometimes create a 0 radius round rect. The degenerate 886 // quads prevent the path from being converted to a rect 887 // Note: PathBench::ArbRoundRectBench performs almost exactly 888 // the same test (but with drawing) 889 static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) { 890 SkRandom rand; 891 SkRect r; 892 893 for (int i = 0; i < 5000; ++i) { 894 895 SkScalar size = rand.nextUScalar1() * 30; 896 if (size < SK_Scalar1) { 897 continue; 898 } 899 r.fLeft = rand.nextUScalar1() * 300; 900 r.fTop = rand.nextUScalar1() * 300; 901 r.fRight = r.fLeft + 2 * size; 902 r.fBottom = r.fTop + 2 * size; 903 904 SkPath temp; 905 906 make_arb_round_rect(&temp, r, 0, 0); 907 908 SkRect result; 909 REPORTER_ASSERT(reporter, temp.isRect(&result)); 910 REPORTER_ASSERT(reporter, r == result); 911 } 912 } 913 914 static void test_rect_isfinite(skiatest::Reporter* reporter) { 915 const SkScalar inf = SK_ScalarInfinity; 916 const SkScalar negInf = SK_ScalarNegativeInfinity; 917 const SkScalar nan = SK_ScalarNaN; 918 919 SkRect r; 920 r.setEmpty(); 921 REPORTER_ASSERT(reporter, r.isFinite()); 922 r.set(0, 0, inf, negInf); 923 REPORTER_ASSERT(reporter, !r.isFinite()); 924 r.set(0, 0, nan, 0); 925 REPORTER_ASSERT(reporter, !r.isFinite()); 926 927 SkPoint pts[] = { 928 { 0, 0 }, 929 { SK_Scalar1, 0 }, 930 { 0, SK_Scalar1 }, 931 }; 932 933 bool isFine = r.setBoundsCheck(pts, 3); 934 REPORTER_ASSERT(reporter, isFine); 935 REPORTER_ASSERT(reporter, !r.isEmpty()); 936 937 pts[1].set(inf, 0); 938 isFine = r.setBoundsCheck(pts, 3); 939 REPORTER_ASSERT(reporter, !isFine); 940 REPORTER_ASSERT(reporter, r.isEmpty()); 941 942 pts[1].set(nan, 0); 943 isFine = r.setBoundsCheck(pts, 3); 944 REPORTER_ASSERT(reporter, !isFine); 945 REPORTER_ASSERT(reporter, r.isEmpty()); 946 } 947 948 static void test_path_isfinite(skiatest::Reporter* reporter) { 949 const SkScalar inf = SK_ScalarInfinity; 950 const SkScalar negInf = SK_ScalarNegativeInfinity; 951 const SkScalar nan = SK_ScalarNaN; 952 953 SkPath path; 954 REPORTER_ASSERT(reporter, path.isFinite()); 955 956 path.reset(); 957 REPORTER_ASSERT(reporter, path.isFinite()); 958 959 path.reset(); 960 path.moveTo(SK_Scalar1, 0); 961 REPORTER_ASSERT(reporter, path.isFinite()); 962 963 path.reset(); 964 path.moveTo(inf, negInf); 965 REPORTER_ASSERT(reporter, !path.isFinite()); 966 967 path.reset(); 968 path.moveTo(nan, 0); 969 REPORTER_ASSERT(reporter, !path.isFinite()); 970 } 971 972 static void test_isfinite(skiatest::Reporter* reporter) { 973 test_rect_isfinite(reporter); 974 test_path_isfinite(reporter); 975 } 976 977 static void test_islastcontourclosed(skiatest::Reporter* reporter) { 978 SkPath path; 979 REPORTER_ASSERT(reporter, !path.isLastContourClosed()); 980 path.moveTo(0, 0); 981 REPORTER_ASSERT(reporter, !path.isLastContourClosed()); 982 path.close(); 983 REPORTER_ASSERT(reporter, path.isLastContourClosed()); 984 path.lineTo(100, 100); 985 REPORTER_ASSERT(reporter, !path.isLastContourClosed()); 986 path.moveTo(200, 200); 987 REPORTER_ASSERT(reporter, !path.isLastContourClosed()); 988 path.close(); 989 REPORTER_ASSERT(reporter, path.isLastContourClosed()); 990 path.moveTo(0, 0); 991 REPORTER_ASSERT(reporter, !path.isLastContourClosed()); 992 } 993 994 // assert that we always 995 // start with a moveTo 996 // only have 1 moveTo 997 // only have Lines after that 998 // end with a single close 999 // only have (at most) 1 close 1000 // 1001 static void test_poly(skiatest::Reporter* reporter, const SkPath& path, 1002 const SkPoint srcPts[], bool expectClose) { 1003 SkPath::RawIter iter(path); 1004 SkPoint pts[4]; 1005 1006 bool firstTime = true; 1007 bool foundClose = false; 1008 for (;;) { 1009 switch (iter.next(pts)) { 1010 case SkPath::kMove_Verb: 1011 REPORTER_ASSERT(reporter, firstTime); 1012 REPORTER_ASSERT(reporter, pts[0] == srcPts[0]); 1013 srcPts++; 1014 firstTime = false; 1015 break; 1016 case SkPath::kLine_Verb: 1017 REPORTER_ASSERT(reporter, !firstTime); 1018 REPORTER_ASSERT(reporter, pts[1] == srcPts[0]); 1019 srcPts++; 1020 break; 1021 case SkPath::kQuad_Verb: 1022 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb"); 1023 break; 1024 case SkPath::kConic_Verb: 1025 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb"); 1026 break; 1027 case SkPath::kCubic_Verb: 1028 REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb"); 1029 break; 1030 case SkPath::kClose_Verb: 1031 REPORTER_ASSERT(reporter, !firstTime); 1032 REPORTER_ASSERT(reporter, !foundClose); 1033 REPORTER_ASSERT(reporter, expectClose); 1034 foundClose = true; 1035 break; 1036 case SkPath::kDone_Verb: 1037 goto DONE; 1038 } 1039 } 1040 DONE: 1041 REPORTER_ASSERT(reporter, foundClose == expectClose); 1042 } 1043 1044 static void test_addPoly(skiatest::Reporter* reporter) { 1045 SkPoint pts[32]; 1046 SkRandom rand; 1047 1048 for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) { 1049 pts[i].fX = rand.nextSScalar1(); 1050 pts[i].fY = rand.nextSScalar1(); 1051 } 1052 1053 for (int doClose = 0; doClose <= 1; ++doClose) { 1054 for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) { 1055 SkPath path; 1056 path.addPoly(pts, SkToInt(count), SkToBool(doClose)); 1057 test_poly(reporter, path, pts, SkToBool(doClose)); 1058 } 1059 } 1060 } 1061 1062 static void test_strokerec(skiatest::Reporter* reporter) { 1063 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); 1064 REPORTER_ASSERT(reporter, rec.isFillStyle()); 1065 1066 rec.setHairlineStyle(); 1067 REPORTER_ASSERT(reporter, rec.isHairlineStyle()); 1068 1069 rec.setStrokeStyle(SK_Scalar1, false); 1070 REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle()); 1071 1072 rec.setStrokeStyle(SK_Scalar1, true); 1073 REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle()); 1074 1075 rec.setStrokeStyle(0, false); 1076 REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle()); 1077 1078 rec.setStrokeStyle(0, true); 1079 REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle()); 1080 } 1081 1082 // Set this for paths that don't have a consistent direction such as a bowtie. 1083 // (cheapComputeDirection is not expected to catch these.) 1084 const SkPathPriv::FirstDirection kDontCheckDir = static_cast<SkPathPriv::FirstDirection>(-1); 1085 1086 static void check_direction(skiatest::Reporter* reporter, const SkPath& path, 1087 SkPathPriv::FirstDirection expected) { 1088 if (expected == kDontCheckDir) { 1089 return; 1090 } 1091 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. 1092 1093 SkPathPriv::FirstDirection dir; 1094 if (SkPathPriv::CheapComputeFirstDirection(copy, &dir)) { 1095 REPORTER_ASSERT(reporter, dir == expected); 1096 } else { 1097 REPORTER_ASSERT(reporter, SkPathPriv::kUnknown_FirstDirection == expected); 1098 } 1099 } 1100 1101 static void test_direction(skiatest::Reporter* reporter) { 1102 size_t i; 1103 SkPath path; 1104 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr)); 1105 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection)); 1106 REPORTER_ASSERT(reporter, !SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection)); 1107 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kUnknown_FirstDirection)); 1108 1109 static const char* gDegen[] = { 1110 "M 10 10", 1111 "M 10 10 M 20 20", 1112 "M 10 10 L 20 20", 1113 "M 10 10 L 10 10 L 10 10", 1114 "M 10 10 Q 10 10 10 10", 1115 "M 10 10 C 10 10 10 10 10 10", 1116 }; 1117 for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) { 1118 path.reset(); 1119 bool valid = SkParsePath::FromSVGString(gDegen[i], &path); 1120 REPORTER_ASSERT(reporter, valid); 1121 REPORTER_ASSERT(reporter, !SkPathPriv::CheapComputeFirstDirection(path, nullptr)); 1122 } 1123 1124 static const char* gCW[] = { 1125 "M 10 10 L 10 10 Q 20 10 20 20", 1126 "M 10 10 C 20 10 20 20 20 20", 1127 "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max 1128 // rect with top two corners replaced by cubics with identical middle 1129 // control points 1130 "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10", 1131 "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif 1132 }; 1133 for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) { 1134 path.reset(); 1135 bool valid = SkParsePath::FromSVGString(gCW[i], &path); 1136 REPORTER_ASSERT(reporter, valid); 1137 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection); 1138 } 1139 1140 static const char* gCCW[] = { 1141 "M 10 10 L 10 10 Q 20 10 20 -20", 1142 "M 10 10 C 20 10 20 -20 20 -20", 1143 "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max 1144 // rect with top two corners replaced by cubics with identical middle 1145 // control points 1146 "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10", 1147 "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif 1148 }; 1149 for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) { 1150 path.reset(); 1151 bool valid = SkParsePath::FromSVGString(gCCW[i], &path); 1152 REPORTER_ASSERT(reporter, valid); 1153 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection); 1154 } 1155 1156 // Test two donuts, each wound a different direction. Only the outer contour 1157 // determines the cheap direction 1158 path.reset(); 1159 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction); 1160 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction); 1161 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection); 1162 1163 path.reset(); 1164 path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction); 1165 path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction); 1166 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection); 1167 1168 // triangle with one point really far from the origin. 1169 path.reset(); 1170 // the first point is roughly 1.05e10, 1.05e10 1171 path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652)); 1172 path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1); 1173 path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1); 1174 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection); 1175 1176 path.reset(); 1177 path.conicTo(20, 0, 20, 20, 0.5f); 1178 path.close(); 1179 check_direction(reporter, path, SkPathPriv::kCW_FirstDirection); 1180 1181 path.reset(); 1182 path.lineTo(1, 1e7f); 1183 path.lineTo(1e7f, 2e7f); 1184 path.close(); 1185 REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity()); 1186 check_direction(reporter, path, SkPathPriv::kCCW_FirstDirection); 1187 } 1188 1189 static void add_rect(SkPath* path, const SkRect& r) { 1190 path->moveTo(r.fLeft, r.fTop); 1191 path->lineTo(r.fRight, r.fTop); 1192 path->lineTo(r.fRight, r.fBottom); 1193 path->lineTo(r.fLeft, r.fBottom); 1194 path->close(); 1195 } 1196 1197 static void test_bounds(skiatest::Reporter* reporter) { 1198 static const SkRect rects[] = { 1199 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, 1200 { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, 1201 { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, 1202 { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, 1203 }; 1204 1205 SkPath path0, path1; 1206 for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { 1207 path0.addRect(rects[i]); 1208 add_rect(&path1, rects[i]); 1209 } 1210 1211 REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); 1212 } 1213 1214 static void stroke_cubic(const SkPoint pts[4]) { 1215 SkPath path; 1216 path.moveTo(pts[0]); 1217 path.cubicTo(pts[1], pts[2], pts[3]); 1218 1219 SkPaint paint; 1220 paint.setStyle(SkPaint::kStroke_Style); 1221 paint.setStrokeWidth(SK_Scalar1 * 2); 1222 1223 SkPath fill; 1224 paint.getFillPath(path, &fill); 1225 } 1226 1227 // just ensure this can run w/o any SkASSERTS firing in the debug build 1228 // we used to assert due to differences in how we determine a degenerate vector 1229 // but that was fixed with the introduction of SkPoint::CanNormalize 1230 static void stroke_tiny_cubic() { 1231 SkPoint p0[] = { 1232 { 372.0f, 92.0f }, 1233 { 372.0f, 92.0f }, 1234 { 372.0f, 92.0f }, 1235 { 372.0f, 92.0f }, 1236 }; 1237 1238 stroke_cubic(p0); 1239 1240 SkPoint p1[] = { 1241 { 372.0f, 92.0f }, 1242 { 372.0007f, 92.000755f }, 1243 { 371.99927f, 92.003922f }, 1244 { 371.99826f, 92.003899f }, 1245 }; 1246 1247 stroke_cubic(p1); 1248 } 1249 1250 static void check_close(skiatest::Reporter* reporter, const SkPath& path) { 1251 for (int i = 0; i < 2; ++i) { 1252 SkPath::Iter iter(path, SkToBool(i)); 1253 SkPoint mv; 1254 SkPoint pts[4]; 1255 SkPath::Verb v; 1256 int nMT = 0; 1257 int nCL = 0; 1258 mv.set(0, 0); 1259 while (SkPath::kDone_Verb != (v = iter.next(pts))) { 1260 switch (v) { 1261 case SkPath::kMove_Verb: 1262 mv = pts[0]; 1263 ++nMT; 1264 break; 1265 case SkPath::kClose_Verb: 1266 REPORTER_ASSERT(reporter, mv == pts[0]); 1267 ++nCL; 1268 break; 1269 default: 1270 break; 1271 } 1272 } 1273 // if we force a close on the interator we should have a close 1274 // for every moveTo 1275 REPORTER_ASSERT(reporter, !i || nMT == nCL); 1276 } 1277 } 1278 1279 static void test_close(skiatest::Reporter* reporter) { 1280 SkPath closePt; 1281 closePt.moveTo(0, 0); 1282 closePt.close(); 1283 check_close(reporter, closePt); 1284 1285 SkPath openPt; 1286 openPt.moveTo(0, 0); 1287 check_close(reporter, openPt); 1288 1289 SkPath empty; 1290 check_close(reporter, empty); 1291 empty.close(); 1292 check_close(reporter, empty); 1293 1294 SkPath rect; 1295 rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); 1296 check_close(reporter, rect); 1297 rect.close(); 1298 check_close(reporter, rect); 1299 1300 SkPath quad; 1301 quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); 1302 check_close(reporter, quad); 1303 quad.close(); 1304 check_close(reporter, quad); 1305 1306 SkPath cubic; 1307 quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 1308 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1); 1309 check_close(reporter, cubic); 1310 cubic.close(); 1311 check_close(reporter, cubic); 1312 1313 SkPath line; 1314 line.moveTo(SK_Scalar1, SK_Scalar1); 1315 line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1); 1316 check_close(reporter, line); 1317 line.close(); 1318 check_close(reporter, line); 1319 1320 SkPath rect2; 1321 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); 1322 rect2.close(); 1323 rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); 1324 check_close(reporter, rect2); 1325 rect2.close(); 1326 check_close(reporter, rect2); 1327 1328 SkPath oval3; 1329 oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100)); 1330 oval3.close(); 1331 oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200)); 1332 check_close(reporter, oval3); 1333 oval3.close(); 1334 check_close(reporter, oval3); 1335 1336 SkPath moves; 1337 moves.moveTo(SK_Scalar1, SK_Scalar1); 1338 moves.moveTo(5 * SK_Scalar1, SK_Scalar1); 1339 moves.moveTo(SK_Scalar1, 10 * SK_Scalar1); 1340 moves.moveTo(10 *SK_Scalar1, SK_Scalar1); 1341 check_close(reporter, moves); 1342 1343 stroke_tiny_cubic(); 1344 } 1345 1346 static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, 1347 SkPath::Convexity expected) { 1348 SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. 1349 SkPath::Convexity c = copy.getConvexity(); 1350 REPORTER_ASSERT(reporter, c == expected); 1351 } 1352 1353 static void test_path_crbug389050(skiatest::Reporter* reporter) { 1354 SkPath tinyConvexPolygon; 1355 tinyConvexPolygon.moveTo(600.131559f, 800.112512f); 1356 tinyConvexPolygon.lineTo(600.161735f, 800.118627f); 1357 tinyConvexPolygon.lineTo(600.148962f, 800.142338f); 1358 tinyConvexPolygon.lineTo(600.134891f, 800.137724f); 1359 tinyConvexPolygon.close(); 1360 tinyConvexPolygon.getConvexity(); 1361 check_convexity(reporter, tinyConvexPolygon, SkPath::kConvex_Convexity); 1362 check_direction(reporter, tinyConvexPolygon, SkPathPriv::kCW_FirstDirection); 1363 1364 SkPath platTriangle; 1365 platTriangle.moveTo(0, 0); 1366 platTriangle.lineTo(200, 0); 1367 platTriangle.lineTo(100, 0.04f); 1368 platTriangle.close(); 1369 platTriangle.getConvexity(); 1370 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection); 1371 1372 platTriangle.reset(); 1373 platTriangle.moveTo(0, 0); 1374 platTriangle.lineTo(200, 0); 1375 platTriangle.lineTo(100, 0.03f); 1376 platTriangle.close(); 1377 platTriangle.getConvexity(); 1378 check_direction(reporter, platTriangle, SkPathPriv::kCW_FirstDirection); 1379 } 1380 1381 static void test_convexity2(skiatest::Reporter* reporter) { 1382 SkPath pt; 1383 pt.moveTo(0, 0); 1384 pt.close(); 1385 check_convexity(reporter, pt, SkPath::kConvex_Convexity); 1386 check_direction(reporter, pt, SkPathPriv::kUnknown_FirstDirection); 1387 1388 SkPath line; 1389 line.moveTo(12*SK_Scalar1, 20*SK_Scalar1); 1390 line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1); 1391 line.close(); 1392 check_convexity(reporter, line, SkPath::kConvex_Convexity); 1393 check_direction(reporter, line, SkPathPriv::kUnknown_FirstDirection); 1394 1395 SkPath triLeft; 1396 triLeft.moveTo(0, 0); 1397 triLeft.lineTo(SK_Scalar1, 0); 1398 triLeft.lineTo(SK_Scalar1, SK_Scalar1); 1399 triLeft.close(); 1400 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); 1401 check_direction(reporter, triLeft, SkPathPriv::kCW_FirstDirection); 1402 1403 SkPath triRight; 1404 triRight.moveTo(0, 0); 1405 triRight.lineTo(-SK_Scalar1, 0); 1406 triRight.lineTo(SK_Scalar1, SK_Scalar1); 1407 triRight.close(); 1408 check_convexity(reporter, triRight, SkPath::kConvex_Convexity); 1409 check_direction(reporter, triRight, SkPathPriv::kCCW_FirstDirection); 1410 1411 SkPath square; 1412 square.moveTo(0, 0); 1413 square.lineTo(SK_Scalar1, 0); 1414 square.lineTo(SK_Scalar1, SK_Scalar1); 1415 square.lineTo(0, SK_Scalar1); 1416 square.close(); 1417 check_convexity(reporter, square, SkPath::kConvex_Convexity); 1418 check_direction(reporter, square, SkPathPriv::kCW_FirstDirection); 1419 1420 SkPath redundantSquare; 1421 redundantSquare.moveTo(0, 0); 1422 redundantSquare.lineTo(0, 0); 1423 redundantSquare.lineTo(0, 0); 1424 redundantSquare.lineTo(SK_Scalar1, 0); 1425 redundantSquare.lineTo(SK_Scalar1, 0); 1426 redundantSquare.lineTo(SK_Scalar1, 0); 1427 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); 1428 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); 1429 redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); 1430 redundantSquare.lineTo(0, SK_Scalar1); 1431 redundantSquare.lineTo(0, SK_Scalar1); 1432 redundantSquare.lineTo(0, SK_Scalar1); 1433 redundantSquare.close(); 1434 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); 1435 check_direction(reporter, redundantSquare, SkPathPriv::kCW_FirstDirection); 1436 1437 SkPath bowTie; 1438 bowTie.moveTo(0, 0); 1439 bowTie.lineTo(0, 0); 1440 bowTie.lineTo(0, 0); 1441 bowTie.lineTo(SK_Scalar1, SK_Scalar1); 1442 bowTie.lineTo(SK_Scalar1, SK_Scalar1); 1443 bowTie.lineTo(SK_Scalar1, SK_Scalar1); 1444 bowTie.lineTo(SK_Scalar1, 0); 1445 bowTie.lineTo(SK_Scalar1, 0); 1446 bowTie.lineTo(SK_Scalar1, 0); 1447 bowTie.lineTo(0, SK_Scalar1); 1448 bowTie.lineTo(0, SK_Scalar1); 1449 bowTie.lineTo(0, SK_Scalar1); 1450 bowTie.close(); 1451 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); 1452 check_direction(reporter, bowTie, kDontCheckDir); 1453 1454 SkPath spiral; 1455 spiral.moveTo(0, 0); 1456 spiral.lineTo(100*SK_Scalar1, 0); 1457 spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1); 1458 spiral.lineTo(0, 100*SK_Scalar1); 1459 spiral.lineTo(0, 50*SK_Scalar1); 1460 spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1); 1461 spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1); 1462 spiral.close(); 1463 check_convexity(reporter, spiral, SkPath::kConcave_Convexity); 1464 check_direction(reporter, spiral, kDontCheckDir); 1465 1466 SkPath dent; 1467 dent.moveTo(0, 0); 1468 dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1); 1469 dent.lineTo(0, 100*SK_Scalar1); 1470 dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1); 1471 dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1); 1472 dent.close(); 1473 check_convexity(reporter, dent, SkPath::kConcave_Convexity); 1474 check_direction(reporter, dent, SkPathPriv::kCW_FirstDirection); 1475 1476 // https://bug.skia.org/2235 1477 SkPath strokedSin; 1478 for (int i = 0; i < 2000; i++) { 1479 SkScalar x = SkIntToScalar(i) / 2; 1480 SkScalar y = 500 - (x + SkScalarSin(x / 100) * 40) / 3; 1481 if (0 == i) { 1482 strokedSin.moveTo(x, y); 1483 } else { 1484 strokedSin.lineTo(x, y); 1485 } 1486 } 1487 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); 1488 stroke.setStrokeStyle(2 * SK_Scalar1); 1489 stroke.applyToPath(&strokedSin, strokedSin); 1490 check_convexity(reporter, strokedSin, SkPath::kConcave_Convexity); 1491 check_direction(reporter, strokedSin, kDontCheckDir); 1492 1493 // http://crbug.com/412640 1494 SkPath degenerateConcave; 1495 degenerateConcave.moveTo(148.67912f, 191.875f); 1496 degenerateConcave.lineTo(470.37695f, 7.5f); 1497 degenerateConcave.lineTo(148.67912f, 191.875f); 1498 degenerateConcave.lineTo(41.446522f, 376.25f); 1499 degenerateConcave.lineTo(-55.971577f, 460.0f); 1500 degenerateConcave.lineTo(41.446522f, 376.25f); 1501 check_convexity(reporter, degenerateConcave, SkPath::kConcave_Convexity); 1502 check_direction(reporter, degenerateConcave, SkPathPriv::kUnknown_FirstDirection); 1503 1504 // http://crbug.com/433683 1505 SkPath badFirstVector; 1506 badFirstVector.moveTo(501.087708f, 319.610352f); 1507 badFirstVector.lineTo(501.087708f, 319.610352f); 1508 badFirstVector.cubicTo(501.087677f, 319.610321f, 449.271606f, 258.078674f, 395.084564f, 198.711182f); 1509 badFirstVector.cubicTo(358.967072f, 159.140717f, 321.910553f, 120.650436f, 298.442322f, 101.955399f); 1510 badFirstVector.lineTo(301.557678f, 98.044601f); 1511 badFirstVector.cubicTo(325.283844f, 116.945084f, 362.615204f, 155.720825f, 398.777557f, 195.340454f); 1512 badFirstVector.cubicTo(453.031860f, 254.781662f, 504.912262f, 316.389618f, 504.912292f, 316.389648f); 1513 badFirstVector.lineTo(504.912292f, 316.389648f); 1514 badFirstVector.lineTo(501.087708f, 319.610352f); 1515 badFirstVector.close(); 1516 check_convexity(reporter, badFirstVector, SkPath::kConcave_Convexity); 1517 } 1518 1519 static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, 1520 const SkRect& bounds) { 1521 REPORTER_ASSERT(reporter, p.isConvex()); 1522 REPORTER_ASSERT(reporter, p.getBounds() == bounds); 1523 1524 SkPath p2(p); 1525 REPORTER_ASSERT(reporter, p2.isConvex()); 1526 REPORTER_ASSERT(reporter, p2.getBounds() == bounds); 1527 1528 SkPath other; 1529 other.swap(p2); 1530 REPORTER_ASSERT(reporter, other.isConvex()); 1531 REPORTER_ASSERT(reporter, other.getBounds() == bounds); 1532 } 1533 1534 static void setFromString(SkPath* path, const char str[]) { 1535 bool first = true; 1536 while (str) { 1537 SkScalar x, y; 1538 str = SkParse::FindScalar(str, &x); 1539 if (nullptr == str) { 1540 break; 1541 } 1542 str = SkParse::FindScalar(str, &y); 1543 SkASSERT(str); 1544 if (first) { 1545 path->moveTo(x, y); 1546 first = false; 1547 } else { 1548 path->lineTo(x, y); 1549 } 1550 } 1551 } 1552 1553 static void test_convexity(skiatest::Reporter* reporter) { 1554 SkPath path; 1555 1556 check_convexity(reporter, path, SkPath::kConvex_Convexity); 1557 path.addCircle(0, 0, SkIntToScalar(10)); 1558 check_convexity(reporter, path, SkPath::kConvex_Convexity); 1559 path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle 1560 check_convexity(reporter, path, SkPath::kConcave_Convexity); 1561 1562 path.reset(); 1563 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction); 1564 check_convexity(reporter, path, SkPath::kConvex_Convexity); 1565 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCCW_FirstDirection)); 1566 1567 path.reset(); 1568 path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction); 1569 check_convexity(reporter, path, SkPath::kConvex_Convexity); 1570 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, SkPathPriv::kCW_FirstDirection)); 1571 1572 path.reset(); 1573 path.quadTo(100, 100, 50, 50); // This is a convex path from GM:convexpaths 1574 check_convexity(reporter, path, SkPath::kConvex_Convexity); 1575 1576 static const struct { 1577 const char* fPathStr; 1578 SkPath::Convexity fExpectedConvexity; 1579 SkPathPriv::FirstDirection fExpectedDirection; 1580 } gRec[] = { 1581 { "", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection }, 1582 { "0 0", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection }, 1583 { "0 0 10 10", SkPath::kConvex_Convexity, SkPathPriv::kUnknown_FirstDirection }, 1584 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPathPriv::kUnknown_FirstDirection }, 1585 { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPathPriv::kCW_FirstDirection }, 1586 { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPathPriv::kCCW_FirstDirection }, 1587 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir }, 1588 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPathPriv::kCW_FirstDirection }, 1589 }; 1590 1591 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { 1592 SkPath path; 1593 setFromString(&path, gRec[i].fPathStr); 1594 check_convexity(reporter, path, gRec[i].fExpectedConvexity); 1595 check_direction(reporter, path, gRec[i].fExpectedDirection); 1596 // check after setting the initial convex and direction 1597 if (kDontCheckDir != gRec[i].fExpectedDirection) { 1598 SkPath copy(path); 1599 SkPathPriv::FirstDirection dir; 1600 bool foundDir = SkPathPriv::CheapComputeFirstDirection(copy, &dir); 1601 REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPathPriv::kUnknown_FirstDirection) 1602 ^ foundDir); 1603 REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir); 1604 check_convexity(reporter, copy, gRec[i].fExpectedConvexity); 1605 } 1606 REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity()); 1607 check_direction(reporter, path, gRec[i].fExpectedDirection); 1608 } 1609 1610 static const SkPoint nonFinitePts[] = { 1611 { SK_ScalarInfinity, 0 }, 1612 { 0, SK_ScalarInfinity }, 1613 { SK_ScalarInfinity, SK_ScalarInfinity }, 1614 { SK_ScalarNegativeInfinity, 0}, 1615 { 0, SK_ScalarNegativeInfinity }, 1616 { SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity }, 1617 { SK_ScalarNegativeInfinity, SK_ScalarInfinity }, 1618 { SK_ScalarInfinity, SK_ScalarNegativeInfinity }, 1619 { SK_ScalarNaN, 0 }, 1620 { 0, SK_ScalarNaN }, 1621 { SK_ScalarNaN, SK_ScalarNaN }, 1622 }; 1623 1624 const size_t nonFinitePtsCount = sizeof(nonFinitePts) / sizeof(nonFinitePts[0]); 1625 1626 static const SkPoint finitePts[] = { 1627 { SK_ScalarMax, 0 }, 1628 { 0, SK_ScalarMax }, 1629 { SK_ScalarMax, SK_ScalarMax }, 1630 { SK_ScalarMin, 0 }, 1631 { 0, SK_ScalarMin }, 1632 { SK_ScalarMin, SK_ScalarMin }, 1633 }; 1634 1635 const size_t finitePtsCount = sizeof(finitePts) / sizeof(finitePts[0]); 1636 1637 for (int index = 0; index < (int) (13 * nonFinitePtsCount * finitePtsCount); ++index) { 1638 int i = (int) (index % nonFinitePtsCount); 1639 int f = (int) (index % finitePtsCount); 1640 int g = (int) ((f + 1) % finitePtsCount); 1641 path.reset(); 1642 switch (index % 13) { 1643 case 0: path.lineTo(nonFinitePts[i]); break; 1644 case 1: path.quadTo(nonFinitePts[i], nonFinitePts[i]); break; 1645 case 2: path.quadTo(nonFinitePts[i], finitePts[f]); break; 1646 case 3: path.quadTo(finitePts[f], nonFinitePts[i]); break; 1647 case 4: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[f]); break; 1648 case 5: path.cubicTo(finitePts[f], nonFinitePts[i], finitePts[f]); break; 1649 case 6: path.cubicTo(finitePts[f], finitePts[f], nonFinitePts[i]); break; 1650 case 7: path.cubicTo(nonFinitePts[i], nonFinitePts[i], finitePts[f]); break; 1651 case 8: path.cubicTo(nonFinitePts[i], finitePts[f], nonFinitePts[i]); break; 1652 case 9: path.cubicTo(finitePts[f], nonFinitePts[i], nonFinitePts[i]); break; 1653 case 10: path.cubicTo(nonFinitePts[i], nonFinitePts[i], nonFinitePts[i]); break; 1654 case 11: path.cubicTo(nonFinitePts[i], finitePts[f], finitePts[g]); break; 1655 case 12: path.moveTo(nonFinitePts[i]); break; 1656 } 1657 check_convexity(reporter, path, SkPath::kUnknown_Convexity); 1658 } 1659 1660 for (int index = 0; index < (int) (11 * finitePtsCount); ++index) { 1661 int f = (int) (index % finitePtsCount); 1662 int g = (int) ((f + 1) % finitePtsCount); 1663 path.reset(); 1664 int curveSelect = index % 11; 1665 switch (curveSelect) { 1666 case 0: path.moveTo(finitePts[f]); break; 1667 case 1: path.lineTo(finitePts[f]); break; 1668 case 2: path.quadTo(finitePts[f], finitePts[f]); break; 1669 case 3: path.quadTo(finitePts[f], finitePts[g]); break; 1670 case 4: path.quadTo(finitePts[g], finitePts[f]); break; 1671 case 5: path.cubicTo(finitePts[f], finitePts[f], finitePts[f]); break; 1672 case 6: path.cubicTo(finitePts[f], finitePts[f], finitePts[g]); break; 1673 case 7: path.cubicTo(finitePts[f], finitePts[g], finitePts[f]); break; 1674 case 8: path.cubicTo(finitePts[f], finitePts[g], finitePts[g]); break; 1675 case 9: path.cubicTo(finitePts[g], finitePts[f], finitePts[f]); break; 1676 case 10: path.cubicTo(finitePts[g], finitePts[f], finitePts[g]); break; 1677 } 1678 check_convexity(reporter, path, curveSelect == 0 ? SkPath::kConvex_Convexity 1679 : SkPath::kUnknown_Convexity); 1680 } 1681 1682 } 1683 1684 static void test_isLine(skiatest::Reporter* reporter) { 1685 SkPath path; 1686 SkPoint pts[2]; 1687 const SkScalar value = SkIntToScalar(5); 1688 1689 REPORTER_ASSERT(reporter, !path.isLine(nullptr)); 1690 1691 // set some non-zero values 1692 pts[0].set(value, value); 1693 pts[1].set(value, value); 1694 REPORTER_ASSERT(reporter, !path.isLine(pts)); 1695 // check that pts was untouched 1696 REPORTER_ASSERT(reporter, pts[0].equals(value, value)); 1697 REPORTER_ASSERT(reporter, pts[1].equals(value, value)); 1698 1699 const SkScalar moveX = SkIntToScalar(1); 1700 const SkScalar moveY = SkIntToScalar(2); 1701 REPORTER_ASSERT(reporter, value != moveX && value != moveY); 1702 1703 path.moveTo(moveX, moveY); 1704 REPORTER_ASSERT(reporter, !path.isLine(nullptr)); 1705 REPORTER_ASSERT(reporter, !path.isLine(pts)); 1706 // check that pts was untouched 1707 REPORTER_ASSERT(reporter, pts[0].equals(value, value)); 1708 REPORTER_ASSERT(reporter, pts[1].equals(value, value)); 1709 1710 const SkScalar lineX = SkIntToScalar(2); 1711 const SkScalar lineY = SkIntToScalar(2); 1712 REPORTER_ASSERT(reporter, value != lineX && value != lineY); 1713 1714 path.lineTo(lineX, lineY); 1715 REPORTER_ASSERT(reporter, path.isLine(nullptr)); 1716 1717 REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY)); 1718 REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY)); 1719 REPORTER_ASSERT(reporter, path.isLine(pts)); 1720 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); 1721 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); 1722 1723 path.lineTo(0, 0); // too many points/verbs 1724 REPORTER_ASSERT(reporter, !path.isLine(nullptr)); 1725 REPORTER_ASSERT(reporter, !path.isLine(pts)); 1726 REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); 1727 REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); 1728 1729 path.reset(); 1730 path.quadTo(1, 1, 2, 2); 1731 REPORTER_ASSERT(reporter, !path.isLine(nullptr)); 1732 } 1733 1734 static void test_conservativelyContains(skiatest::Reporter* reporter) { 1735 SkPath path; 1736 1737 // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect. 1738 static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); 1739 1740 // A circle that bounds kBaseRect (with a significant amount of slop) 1741 SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height()); 1742 circleR *= 1.75f / 2; 1743 static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()}; 1744 1745 // round-rect radii 1746 static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)}; 1747 1748 static const struct SUPPRESS_VISIBILITY_WARNING { 1749 SkRect fQueryRect; 1750 bool fInRect; 1751 bool fInCircle; 1752 bool fInRR; 1753 bool fInCubicRR; 1754 } kQueries[] = { 1755 {kBaseRect, true, true, false, false}, 1756 1757 // rect well inside of kBaseRect 1758 {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(), 1759 kBaseRect.fTop + 0.25f*kBaseRect.height(), 1760 kBaseRect.fRight - 0.25f*kBaseRect.width(), 1761 kBaseRect.fBottom - 0.25f*kBaseRect.height()), 1762 true, true, true, true}, 1763 1764 // rects with edges off by one from kBaseRect's edges 1765 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 1766 kBaseRect.width(), kBaseRect.height() + 1), 1767 false, true, false, false}, 1768 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 1769 kBaseRect.width() + 1, kBaseRect.height()), 1770 false, true, false, false}, 1771 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 1772 kBaseRect.width() + 1, kBaseRect.height() + 1), 1773 false, true, false, false}, 1774 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, 1775 kBaseRect.width(), kBaseRect.height()), 1776 false, true, false, false}, 1777 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, 1778 kBaseRect.width(), kBaseRect.height()), 1779 false, true, false, false}, 1780 {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, 1781 kBaseRect.width() + 2, kBaseRect.height()), 1782 false, true, false, false}, 1783 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, 1784 kBaseRect.width() + 2, kBaseRect.height()), 1785 false, true, false, false}, 1786 1787 // zero-w/h rects at each corner of kBaseRect 1788 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false}, 1789 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true}, 1790 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true}, 1791 {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true}, 1792 1793 // far away rect 1794 {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom, 1795 SkIntToScalar(10), SkIntToScalar(10)), 1796 false, false, false, false}, 1797 1798 // very large rect containing kBaseRect 1799 {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(), 1800 kBaseRect.fTop - 5 * kBaseRect.height(), 1801 11 * kBaseRect.width(), 11 * kBaseRect.height()), 1802 false, false, false, false}, 1803 1804 // skinny rect that spans same y-range as kBaseRect 1805 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, 1806 SkIntToScalar(1), kBaseRect.height()), 1807 true, true, true, true}, 1808 1809 // short rect that spans same x-range as kBaseRect 1810 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)), 1811 true, true, true, true}, 1812 1813 // skinny rect that spans slightly larger y-range than kBaseRect 1814 {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, 1815 SkIntToScalar(1), kBaseRect.height() + 1), 1816 false, true, false, false}, 1817 1818 // short rect that spans slightly larger x-range than kBaseRect 1819 {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), 1820 kBaseRect.width() + 1, SkScalar(1)), 1821 false, true, false, false}, 1822 }; 1823 1824 for (int inv = 0; inv < 4; ++inv) { 1825 for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) { 1826 SkRect qRect = kQueries[q].fQueryRect; 1827 if (inv & 0x1) { 1828 SkTSwap(qRect.fLeft, qRect.fRight); 1829 } 1830 if (inv & 0x2) { 1831 SkTSwap(qRect.fTop, qRect.fBottom); 1832 } 1833 for (int d = 0; d < 2; ++d) { 1834 SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction; 1835 path.reset(); 1836 path.addRect(kBaseRect, dir); 1837 REPORTER_ASSERT(reporter, kQueries[q].fInRect == 1838 path.conservativelyContainsRect(qRect)); 1839 1840 path.reset(); 1841 path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir); 1842 REPORTER_ASSERT(reporter, kQueries[q].fInCircle == 1843 path.conservativelyContainsRect(qRect)); 1844 1845 path.reset(); 1846 path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir); 1847 REPORTER_ASSERT(reporter, kQueries[q].fInRR == 1848 path.conservativelyContainsRect(qRect)); 1849 1850 path.reset(); 1851 path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop); 1852 path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop, 1853 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2, 1854 kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]); 1855 path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom); 1856 path.lineTo(kBaseRect.fRight, kBaseRect.fBottom); 1857 path.lineTo(kBaseRect.fRight, kBaseRect.fTop); 1858 path.close(); 1859 REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR == 1860 path.conservativelyContainsRect(qRect)); 1861 1862 } 1863 // Slightly non-convex shape, shouldn't contain any rects. 1864 path.reset(); 1865 path.moveTo(0, 0); 1866 path.lineTo(SkIntToScalar(50), 0.05f); 1867 path.lineTo(SkIntToScalar(100), 0); 1868 path.lineTo(SkIntToScalar(100), SkIntToScalar(100)); 1869 path.lineTo(0, SkIntToScalar(100)); 1870 path.close(); 1871 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect)); 1872 } 1873 } 1874 1875 // make sure a minimal convex shape works, a right tri with edges along pos x and y axes. 1876 path.reset(); 1877 path.moveTo(0, 0); 1878 path.lineTo(SkIntToScalar(100), 0); 1879 path.lineTo(0, SkIntToScalar(100)); 1880 1881 // inside, on along top edge 1882 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, 1883 SkIntToScalar(10), 1884 SkIntToScalar(10)))); 1885 // above 1886 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( 1887 SkRect::MakeXYWH(SkIntToScalar(50), 1888 SkIntToScalar(-10), 1889 SkIntToScalar(10), 1890 SkIntToScalar(10)))); 1891 // to the left 1892 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10), 1893 SkIntToScalar(5), 1894 SkIntToScalar(5), 1895 SkIntToScalar(5)))); 1896 1897 // outside the diagonal edge 1898 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10), 1899 SkIntToScalar(200), 1900 SkIntToScalar(20), 1901 SkIntToScalar(5)))); 1902 1903 1904 // Test that multiple move commands do not cause asserts. 1905 1906 // At the time of writing, this would not modify cached convexity. This caused an assert while 1907 // checking conservative containment again. https://bug.skia.org/1460 1908 path.moveTo(SkIntToScalar(100), SkIntToScalar(100)); 1909 #if 0 1910 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, 1911 SkIntToScalar(10), 1912 SkIntToScalar(10)))); 1913 #endif 1914 1915 // Same as above path and first test but with an extra moveTo. 1916 path.reset(); 1917 path.moveTo(100, 100); 1918 path.moveTo(0, 0); 1919 path.lineTo(SkIntToScalar(100), 0); 1920 path.lineTo(0, SkIntToScalar(100)); 1921 1922 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, 1923 SkIntToScalar(10), 1924 SkIntToScalar(10)))); 1925 1926 // Same as above path and first test but with the extra moveTo making a degenerate sub-path 1927 // following the non-empty sub-path. Verifies that this does not trigger assertions. 1928 path.reset(); 1929 path.moveTo(0, 0); 1930 path.lineTo(SkIntToScalar(100), 0); 1931 path.lineTo(0, SkIntToScalar(100)); 1932 path.moveTo(100, 100); 1933 1934 REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, 1935 SkIntToScalar(10), 1936 SkIntToScalar(10)))); 1937 1938 // Test that multiple move commands do not cause asserts and that the function 1939 // is not confused by the multiple moves. 1940 path.reset(); 1941 path.moveTo(0, 0); 1942 path.lineTo(SkIntToScalar(100), 0); 1943 path.lineTo(0, SkIntToScalar(100)); 1944 path.moveTo(0, SkIntToScalar(200)); 1945 path.lineTo(SkIntToScalar(100), SkIntToScalar(200)); 1946 path.lineTo(0, SkIntToScalar(300)); 1947 1948 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( 1949 SkRect::MakeXYWH(SkIntToScalar(50), 0, 1950 SkIntToScalar(10), 1951 SkIntToScalar(10)))); 1952 1953 path.reset(); 1954 path.lineTo(100, 100); 1955 REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1))); 1956 } 1957 1958 static void test_isRect_open_close(skiatest::Reporter* reporter) { 1959 SkPath path; 1960 bool isClosed; 1961 1962 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1); 1963 path.close(); 1964 1965 REPORTER_ASSERT(reporter, path.isRect(nullptr, &isClosed, nullptr)); 1966 REPORTER_ASSERT(reporter, isClosed); 1967 } 1968 1969 // Simple isRect test is inline TestPath, below. 1970 // test_isRect provides more extensive testing. 1971 static void test_isRect(skiatest::Reporter* reporter) { 1972 test_isRect_open_close(reporter); 1973 1974 // passing tests (all moveTo / lineTo... 1975 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 1976 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; 1977 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; 1978 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; 1979 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; 1980 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; 1981 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; 1982 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; 1983 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; 1984 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; 1985 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; 1986 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; 1987 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; 1988 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; 1989 SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}}; 1990 1991 // failing tests 1992 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points 1993 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal 1994 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps 1995 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up 1996 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots 1997 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots 1998 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots 1999 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' 2000 SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps 2001 SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap 2002 SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short 2003 2004 // no close, but we should detect them as fillably the same as a rect 2005 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 2006 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; 2007 SkPoint c3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 0}}; // hit the start 2008 2009 // like c2, but we double-back on ourselves 2010 SkPoint d1[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}, {0, 2}}; 2011 // like c2, but we overshoot the start point 2012 SkPoint d2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}}; 2013 SkPoint d3[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, -1}, {0, 0}}; 2014 2015 struct IsRectTest { 2016 SkPoint *fPoints; 2017 int fPointCount; 2018 bool fClose; 2019 bool fIsRect; 2020 } tests[] = { 2021 { r1, SK_ARRAY_COUNT(r1), true, true }, 2022 { r2, SK_ARRAY_COUNT(r2), true, true }, 2023 { r3, SK_ARRAY_COUNT(r3), true, true }, 2024 { r4, SK_ARRAY_COUNT(r4), true, true }, 2025 { r5, SK_ARRAY_COUNT(r5), true, true }, 2026 { r6, SK_ARRAY_COUNT(r6), true, true }, 2027 { r7, SK_ARRAY_COUNT(r7), true, true }, 2028 { r8, SK_ARRAY_COUNT(r8), true, true }, 2029 { r9, SK_ARRAY_COUNT(r9), true, true }, 2030 { ra, SK_ARRAY_COUNT(ra), true, true }, 2031 { rb, SK_ARRAY_COUNT(rb), true, true }, 2032 { rc, SK_ARRAY_COUNT(rc), true, true }, 2033 { rd, SK_ARRAY_COUNT(rd), true, true }, 2034 { re, SK_ARRAY_COUNT(re), true, true }, 2035 { rf, SK_ARRAY_COUNT(rf), true, true }, 2036 2037 { f1, SK_ARRAY_COUNT(f1), true, false }, 2038 { f2, SK_ARRAY_COUNT(f2), true, false }, 2039 { f3, SK_ARRAY_COUNT(f3), true, false }, 2040 { f4, SK_ARRAY_COUNT(f4), true, false }, 2041 { f5, SK_ARRAY_COUNT(f5), true, false }, 2042 { f6, SK_ARRAY_COUNT(f6), true, false }, 2043 { f7, SK_ARRAY_COUNT(f7), true, false }, 2044 { f8, SK_ARRAY_COUNT(f8), true, false }, 2045 { f9, SK_ARRAY_COUNT(f9), true, false }, 2046 { fa, SK_ARRAY_COUNT(fa), true, false }, 2047 { fb, SK_ARRAY_COUNT(fb), true, false }, 2048 2049 { c1, SK_ARRAY_COUNT(c1), false, true }, 2050 { c2, SK_ARRAY_COUNT(c2), false, true }, 2051 { c3, SK_ARRAY_COUNT(c3), false, true }, 2052 2053 { d1, SK_ARRAY_COUNT(d1), false, false }, 2054 { d2, SK_ARRAY_COUNT(d2), false, false }, 2055 { d3, SK_ARRAY_COUNT(d3), false, false }, 2056 }; 2057 2058 const size_t testCount = SK_ARRAY_COUNT(tests); 2059 int index; 2060 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { 2061 SkPath path; 2062 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); 2063 for (index = 1; index < tests[testIndex].fPointCount; ++index) { 2064 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); 2065 } 2066 if (tests[testIndex].fClose) { 2067 path.close(); 2068 } 2069 REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(nullptr)); 2070 2071 if (tests[testIndex].fIsRect) { 2072 SkRect computed, expected; 2073 bool isClosed; 2074 SkPath::Direction direction; 2075 SkPathPriv::FirstDirection cheapDirection; 2076 expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); 2077 REPORTER_ASSERT(reporter, SkPathPriv::CheapComputeFirstDirection(path, &cheapDirection)); 2078 REPORTER_ASSERT(reporter, path.isRect(&computed, &isClosed, &direction)); 2079 REPORTER_ASSERT(reporter, expected == computed); 2080 REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose); 2081 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(direction) == cheapDirection); 2082 } else { 2083 SkRect computed; 2084 computed.set(123, 456, 789, 1011); 2085 bool isClosed = (bool)-1; 2086 SkPath::Direction direction = (SkPath::Direction) - 1; 2087 REPORTER_ASSERT(reporter, !path.isRect(&computed, &isClosed, &direction)); 2088 REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456); 2089 REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011); 2090 REPORTER_ASSERT(reporter, isClosed == (bool) -1); 2091 REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1); 2092 } 2093 } 2094 2095 // fail, close then line 2096 SkPath path1; 2097 path1.moveTo(r1[0].fX, r1[0].fY); 2098 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2099 path1.lineTo(r1[index].fX, r1[index].fY); 2100 } 2101 path1.close(); 2102 path1.lineTo(1, 0); 2103 REPORTER_ASSERT(reporter, !path1.isRect(nullptr)); 2104 2105 // fail, move in the middle 2106 path1.reset(); 2107 path1.moveTo(r1[0].fX, r1[0].fY); 2108 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2109 if (index == 2) { 2110 path1.moveTo(1, .5f); 2111 } 2112 path1.lineTo(r1[index].fX, r1[index].fY); 2113 } 2114 path1.close(); 2115 REPORTER_ASSERT(reporter, !path1.isRect(nullptr)); 2116 2117 // fail, move on the edge 2118 path1.reset(); 2119 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2120 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); 2121 path1.lineTo(r1[index].fX, r1[index].fY); 2122 } 2123 path1.close(); 2124 REPORTER_ASSERT(reporter, !path1.isRect(nullptr)); 2125 2126 // fail, quad 2127 path1.reset(); 2128 path1.moveTo(r1[0].fX, r1[0].fY); 2129 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2130 if (index == 2) { 2131 path1.quadTo(1, .5f, 1, .5f); 2132 } 2133 path1.lineTo(r1[index].fX, r1[index].fY); 2134 } 2135 path1.close(); 2136 REPORTER_ASSERT(reporter, !path1.isRect(nullptr)); 2137 2138 // fail, cubic 2139 path1.reset(); 2140 path1.moveTo(r1[0].fX, r1[0].fY); 2141 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2142 if (index == 2) { 2143 path1.cubicTo(1, .5f, 1, .5f, 1, .5f); 2144 } 2145 path1.lineTo(r1[index].fX, r1[index].fY); 2146 } 2147 path1.close(); 2148 REPORTER_ASSERT(reporter, !path1.isRect(nullptr)); 2149 } 2150 2151 static void check_simple_closed_rect(skiatest::Reporter* reporter, const SkPath& path, 2152 const SkRect& rect, SkPath::Direction dir, unsigned start) { 2153 SkRect r = SkRect::MakeEmpty(); 2154 SkPath::Direction d = SkPath::kCCW_Direction; 2155 unsigned s = ~0U; 2156 2157 REPORTER_ASSERT(reporter, SkPathPriv::IsSimpleClosedRect(path, &r, &d, &s)); 2158 REPORTER_ASSERT(reporter, r == rect); 2159 REPORTER_ASSERT(reporter, d == dir); 2160 REPORTER_ASSERT(reporter, s == start); 2161 } 2162 2163 static void test_is_simple_closed_rect(skiatest::Reporter* reporter) { 2164 SkRect r = SkRect::MakeEmpty(); 2165 SkPath::Direction d = SkPath::kCCW_Direction; 2166 unsigned s = ~0U; 2167 2168 const SkRect testRect = SkRect::MakeXYWH(10, 10, 50, 70); 2169 const SkRect emptyRect = SkRect::MakeEmpty(); 2170 SkPath path; 2171 for (int start = 0; start < 4; ++start) { 2172 for (auto dir : {SkPath::kCCW_Direction, SkPath::kCW_Direction}) { 2173 SkPath path; 2174 path.addRect(testRect, dir, start); 2175 check_simple_closed_rect(reporter, path, testRect, dir, start); 2176 path.close(); 2177 check_simple_closed_rect(reporter, path, testRect, dir, start); 2178 SkPath path2 = path; 2179 path2.lineTo(10, 10); 2180 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2181 path2 = path; 2182 path2.moveTo(10, 10); 2183 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2184 path2 = path; 2185 path2.addRect(testRect, dir, start); 2186 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2187 // Make the path by hand, manually closing it. 2188 path2.reset(); 2189 SkPath::RawIter iter(path); 2190 SkPath::Verb v; 2191 SkPoint verbPts[4]; 2192 SkPoint firstPt = {0.f, 0.f}; 2193 while ((v = iter.next(verbPts)) != SkPath::kDone_Verb) { 2194 switch(v) { 2195 case SkPath::kMove_Verb: 2196 firstPt = verbPts[0]; 2197 path2.moveTo(verbPts[0]); 2198 break; 2199 case SkPath::kLine_Verb: 2200 path2.lineTo(verbPts[1]); 2201 break; 2202 default: 2203 break; 2204 } 2205 } 2206 // We haven't closed it yet... 2207 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2208 // ... now we do and test again. 2209 path2.lineTo(firstPt); 2210 check_simple_closed_rect(reporter, path2, testRect, dir, start); 2211 // A redundant close shouldn't cause a failure. 2212 path2.close(); 2213 check_simple_closed_rect(reporter, path2, testRect, dir, start); 2214 // Degenerate point and line rects are not allowed 2215 path2.reset(); 2216 path2.addRect(emptyRect, dir, start); 2217 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2218 SkRect degenRect = testRect; 2219 degenRect.fLeft = degenRect.fRight; 2220 path2.reset(); 2221 path2.addRect(degenRect, dir, start); 2222 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2223 degenRect = testRect; 2224 degenRect.fTop = degenRect.fBottom; 2225 path2.reset(); 2226 path2.addRect(degenRect, dir, start); 2227 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path2, &r, &d, &s)); 2228 // An inverted rect makes a rect path, but changes the winding dir and start point. 2229 SkPath::Direction swapDir = (dir == SkPath::kCW_Direction) 2230 ? SkPath::kCCW_Direction 2231 : SkPath::kCW_Direction; 2232 static constexpr unsigned kXSwapStarts[] = { 1, 0, 3, 2 }; 2233 static constexpr unsigned kYSwapStarts[] = { 3, 2, 1, 0 }; 2234 SkRect swapRect = testRect; 2235 SkTSwap(swapRect.fLeft, swapRect.fRight); 2236 path2.reset(); 2237 path2.addRect(swapRect, dir, start); 2238 check_simple_closed_rect(reporter, path2, testRect, swapDir, kXSwapStarts[start]); 2239 swapRect = testRect; 2240 SkTSwap(swapRect.fTop, swapRect.fBottom); 2241 path2.reset(); 2242 path2.addRect(swapRect, dir, start); 2243 check_simple_closed_rect(reporter, path2, testRect, swapDir, kYSwapStarts[start]); 2244 } 2245 } 2246 // down, up, left, close 2247 path.reset(); 2248 path.moveTo(1, 1); 2249 path.lineTo(1, 2); 2250 path.lineTo(1, 1); 2251 path.lineTo(0, 1); 2252 SkRect rect; 2253 SkPath::Direction dir; 2254 unsigned start; 2255 path.close(); 2256 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start)); 2257 // right, left, up, close 2258 path.reset(); 2259 path.moveTo(1, 1); 2260 path.lineTo(2, 1); 2261 path.lineTo(1, 1); 2262 path.lineTo(1, 0); 2263 path.close(); 2264 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start)); 2265 // parallelogram with horizontal edges 2266 path.reset(); 2267 path.moveTo(1, 0); 2268 path.lineTo(3, 0); 2269 path.lineTo(2, 1); 2270 path.lineTo(0, 1); 2271 path.close(); 2272 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start)); 2273 // parallelogram with vertical edges 2274 path.reset(); 2275 path.moveTo(0, 1); 2276 path.lineTo(0, 3); 2277 path.lineTo(1, 2); 2278 path.lineTo(1, 0); 2279 path.close(); 2280 REPORTER_ASSERT(reporter, !SkPathPriv::IsSimpleClosedRect(path, &rect, &dir, &start)); 2281 2282 } 2283 2284 static void test_isNestedFillRects(skiatest::Reporter* reporter) { 2285 // passing tests (all moveTo / lineTo... 2286 SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW 2287 SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; 2288 SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; 2289 SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; 2290 SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW 2291 SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; 2292 SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; 2293 SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; 2294 SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; 2295 SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW 2296 SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW 2297 SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW 2298 SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW 2299 SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW 2300 2301 // failing tests 2302 SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points 2303 SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal 2304 SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps 2305 SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up 2306 SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots 2307 SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots 2308 SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots 2309 SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' 2310 2311 // success, no close is OK 2312 SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match 2313 SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto 2314 2315 struct IsNestedRectTest { 2316 SkPoint *fPoints; 2317 int fPointCount; 2318 SkPathPriv::FirstDirection fDirection; 2319 bool fClose; 2320 bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2); 2321 } tests[] = { 2322 { r1, SK_ARRAY_COUNT(r1), SkPathPriv::kCW_FirstDirection , true, true }, 2323 { r2, SK_ARRAY_COUNT(r2), SkPathPriv::kCW_FirstDirection , true, true }, 2324 { r3, SK_ARRAY_COUNT(r3), SkPathPriv::kCW_FirstDirection , true, true }, 2325 { r4, SK_ARRAY_COUNT(r4), SkPathPriv::kCW_FirstDirection , true, true }, 2326 { r5, SK_ARRAY_COUNT(r5), SkPathPriv::kCCW_FirstDirection, true, true }, 2327 { r6, SK_ARRAY_COUNT(r6), SkPathPriv::kCCW_FirstDirection, true, true }, 2328 { r7, SK_ARRAY_COUNT(r7), SkPathPriv::kCCW_FirstDirection, true, true }, 2329 { r8, SK_ARRAY_COUNT(r8), SkPathPriv::kCCW_FirstDirection, true, true }, 2330 { r9, SK_ARRAY_COUNT(r9), SkPathPriv::kCCW_FirstDirection, true, true }, 2331 { ra, SK_ARRAY_COUNT(ra), SkPathPriv::kCCW_FirstDirection, true, true }, 2332 { rb, SK_ARRAY_COUNT(rb), SkPathPriv::kCW_FirstDirection, true, true }, 2333 { rc, SK_ARRAY_COUNT(rc), SkPathPriv::kCW_FirstDirection, true, true }, 2334 { rd, SK_ARRAY_COUNT(rd), SkPathPriv::kCCW_FirstDirection, true, true }, 2335 { re, SK_ARRAY_COUNT(re), SkPathPriv::kCW_FirstDirection, true, true }, 2336 2337 { f1, SK_ARRAY_COUNT(f1), SkPathPriv::kUnknown_FirstDirection, true, false }, 2338 { f2, SK_ARRAY_COUNT(f2), SkPathPriv::kUnknown_FirstDirection, true, false }, 2339 { f3, SK_ARRAY_COUNT(f3), SkPathPriv::kUnknown_FirstDirection, true, false }, 2340 { f4, SK_ARRAY_COUNT(f4), SkPathPriv::kUnknown_FirstDirection, true, false }, 2341 { f5, SK_ARRAY_COUNT(f5), SkPathPriv::kUnknown_FirstDirection, true, false }, 2342 { f6, SK_ARRAY_COUNT(f6), SkPathPriv::kUnknown_FirstDirection, true, false }, 2343 { f7, SK_ARRAY_COUNT(f7), SkPathPriv::kUnknown_FirstDirection, true, false }, 2344 { f8, SK_ARRAY_COUNT(f8), SkPathPriv::kUnknown_FirstDirection, true, false }, 2345 2346 { c1, SK_ARRAY_COUNT(c1), SkPathPriv::kCW_FirstDirection, false, true }, 2347 { c2, SK_ARRAY_COUNT(c2), SkPathPriv::kCW_FirstDirection, false, true }, 2348 }; 2349 2350 const size_t testCount = SK_ARRAY_COUNT(tests); 2351 int index; 2352 for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) { 2353 for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { 2354 SkPath path; 2355 if (rectFirst) { 2356 path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2357 } 2358 path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); 2359 for (index = 1; index < tests[testIndex].fPointCount; ++index) { 2360 path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); 2361 } 2362 if (tests[testIndex].fClose) { 2363 path.close(); 2364 } 2365 if (!rectFirst) { 2366 path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2367 } 2368 REPORTER_ASSERT(reporter, 2369 tests[testIndex].fIsNestedRect == path.isNestedFillRects(nullptr)); 2370 if (tests[testIndex].fIsNestedRect) { 2371 SkRect expected[2], computed[2]; 2372 SkPathPriv::FirstDirection expectedDirs[2]; 2373 SkPath::Direction computedDirs[2]; 2374 SkRect testBounds; 2375 testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); 2376 expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2); 2377 expected[1] = testBounds; 2378 if (rectFirst) { 2379 expectedDirs[0] = SkPathPriv::kCW_FirstDirection; 2380 } else { 2381 expectedDirs[0] = SkPathPriv::kCCW_FirstDirection; 2382 } 2383 expectedDirs[1] = tests[testIndex].fDirection; 2384 REPORTER_ASSERT(reporter, path.isNestedFillRects(computed, computedDirs)); 2385 REPORTER_ASSERT(reporter, expected[0] == computed[0]); 2386 REPORTER_ASSERT(reporter, expected[1] == computed[1]); 2387 REPORTER_ASSERT(reporter, expectedDirs[0] == SkPathPriv::AsFirstDirection(computedDirs[0])); 2388 REPORTER_ASSERT(reporter, expectedDirs[1] == SkPathPriv::AsFirstDirection(computedDirs[1])); 2389 } 2390 } 2391 2392 // fail, close then line 2393 SkPath path1; 2394 if (rectFirst) { 2395 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2396 } 2397 path1.moveTo(r1[0].fX, r1[0].fY); 2398 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2399 path1.lineTo(r1[index].fX, r1[index].fY); 2400 } 2401 path1.close(); 2402 path1.lineTo(1, 0); 2403 if (!rectFirst) { 2404 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2405 } 2406 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2407 2408 // fail, move in the middle 2409 path1.reset(); 2410 if (rectFirst) { 2411 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2412 } 2413 path1.moveTo(r1[0].fX, r1[0].fY); 2414 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2415 if (index == 2) { 2416 path1.moveTo(1, .5f); 2417 } 2418 path1.lineTo(r1[index].fX, r1[index].fY); 2419 } 2420 path1.close(); 2421 if (!rectFirst) { 2422 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2423 } 2424 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2425 2426 // fail, move on the edge 2427 path1.reset(); 2428 if (rectFirst) { 2429 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2430 } 2431 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2432 path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); 2433 path1.lineTo(r1[index].fX, r1[index].fY); 2434 } 2435 path1.close(); 2436 if (!rectFirst) { 2437 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2438 } 2439 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2440 2441 // fail, quad 2442 path1.reset(); 2443 if (rectFirst) { 2444 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2445 } 2446 path1.moveTo(r1[0].fX, r1[0].fY); 2447 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2448 if (index == 2) { 2449 path1.quadTo(1, .5f, 1, .5f); 2450 } 2451 path1.lineTo(r1[index].fX, r1[index].fY); 2452 } 2453 path1.close(); 2454 if (!rectFirst) { 2455 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2456 } 2457 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2458 2459 // fail, cubic 2460 path1.reset(); 2461 if (rectFirst) { 2462 path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); 2463 } 2464 path1.moveTo(r1[0].fX, r1[0].fY); 2465 for (index = 1; index < SkToInt(SK_ARRAY_COUNT(r1)); ++index) { 2466 if (index == 2) { 2467 path1.cubicTo(1, .5f, 1, .5f, 1, .5f); 2468 } 2469 path1.lineTo(r1[index].fX, r1[index].fY); 2470 } 2471 path1.close(); 2472 if (!rectFirst) { 2473 path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); 2474 } 2475 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2476 2477 // fail, not nested 2478 path1.reset(); 2479 path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction); 2480 path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction); 2481 REPORTER_ASSERT(reporter, !path1.isNestedFillRects(nullptr)); 2482 } 2483 2484 // pass, constructed explicitly from manually closed rects specified as moves/lines. 2485 SkPath path; 2486 path.moveTo(0, 0); 2487 path.lineTo(10, 0); 2488 path.lineTo(10, 10); 2489 path.lineTo(0, 10); 2490 path.lineTo(0, 0); 2491 path.moveTo(1, 1); 2492 path.lineTo(9, 1); 2493 path.lineTo(9, 9); 2494 path.lineTo(1, 9); 2495 path.lineTo(1, 1); 2496 REPORTER_ASSERT(reporter, path.isNestedFillRects(nullptr)); 2497 2498 // pass, stroke rect 2499 SkPath src, dst; 2500 src.addRect(1, 1, 7, 7, SkPath::kCW_Direction); 2501 SkPaint strokePaint; 2502 strokePaint.setStyle(SkPaint::kStroke_Style); 2503 strokePaint.setStrokeWidth(2); 2504 strokePaint.getFillPath(src, &dst); 2505 REPORTER_ASSERT(reporter, dst.isNestedFillRects(nullptr)); 2506 } 2507 2508 static void write_and_read_back(skiatest::Reporter* reporter, 2509 const SkPath& p) { 2510 SkWriter32 writer; 2511 writer.writePath(p); 2512 size_t size = writer.bytesWritten(); 2513 SkAutoMalloc storage(size); 2514 writer.flatten(storage.get()); 2515 SkReader32 reader(storage.get(), size); 2516 2517 SkPath readBack; 2518 REPORTER_ASSERT(reporter, readBack != p); 2519 reader.readPath(&readBack); 2520 REPORTER_ASSERT(reporter, readBack == p); 2521 2522 REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() == 2523 p.getConvexityOrUnknown()); 2524 2525 SkRect oval0, oval1; 2526 SkPath::Direction dir0, dir1; 2527 unsigned start0, start1; 2528 REPORTER_ASSERT(reporter, readBack.isOval(nullptr) == p.isOval(nullptr)); 2529 if (p.isOval(&oval0, &dir0, &start0) && readBack.isOval(&oval1, &dir1, &start1)) { 2530 REPORTER_ASSERT(reporter, oval0 == oval1); 2531 REPORTER_ASSERT(reporter, dir0 == dir1); 2532 REPORTER_ASSERT(reporter, start0 == start1); 2533 } 2534 REPORTER_ASSERT(reporter, readBack.isRRect(nullptr) == p.isRRect(nullptr)); 2535 SkRRect rrect0, rrect1; 2536 if (p.isRRect(&rrect0, &dir0, &start0) && readBack.isRRect(&rrect1, &dir1, &start1)) { 2537 REPORTER_ASSERT(reporter, rrect0 == rrect1); 2538 REPORTER_ASSERT(reporter, dir0 == dir1); 2539 REPORTER_ASSERT(reporter, start0 == start1); 2540 } 2541 const SkRect& origBounds = p.getBounds(); 2542 const SkRect& readBackBounds = readBack.getBounds(); 2543 2544 REPORTER_ASSERT(reporter, origBounds == readBackBounds); 2545 } 2546 2547 static void test_corrupt_flattening(skiatest::Reporter* reporter) { 2548 SkPath path; 2549 path.moveTo(1, 2); 2550 path.lineTo(1, 2); 2551 path.quadTo(1, 2, 3, 4); 2552 path.conicTo(1, 2, 3, 4, 0.5f); 2553 path.cubicTo(1, 2, 3, 4, 5, 6); 2554 uint8_t buffer[1024]; 2555 SkDEBUGCODE(size_t size =) path.writeToMemory(buffer); 2556 SkASSERT(size <= sizeof(buffer)); 2557 2558 // find where the counts and verbs are stored : from the impl in SkPathRef.cpp 2559 int32_t* vCount = (int32_t*)&buffer[16]; 2560 SkASSERT(*vCount == 5); 2561 int32_t* pCount = (int32_t*)&buffer[20]; 2562 SkASSERT(*pCount == 9); 2563 int32_t* cCount = (int32_t*)&buffer[24]; 2564 SkASSERT(*cCount == 1); 2565 uint8_t* verbs = &buffer[28]; 2566 2567 REPORTER_ASSERT(reporter, path.readFromMemory(buffer, sizeof(buffer))); 2568 2569 // check that we detect under/over-flow of counts 2570 2571 *vCount += 1; 2572 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2573 *vCount -= 1; // restore 2574 2575 *pCount += 1; 2576 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2577 *pCount -= 2; 2578 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2579 *pCount += 1; // restore 2580 2581 *cCount += 1; 2582 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2583 *cCount -= 2; 2584 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2585 *cCount += 1; // restore 2586 2587 // Check that we detect when the verbs indicate more or fewer pts/conics 2588 2589 uint8_t save = verbs[0]; 2590 SkASSERT(save == SkPath::kCubic_Verb); 2591 verbs[0] = SkPath::kQuad_Verb; 2592 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2593 verbs[0] = save; 2594 2595 save = verbs[1]; 2596 SkASSERT(save == SkPath::kConic_Verb); 2597 verbs[1] = SkPath::kQuad_Verb; 2598 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2599 verbs[1] = SkPath::kCubic_Verb; 2600 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2601 verbs[1] = save; 2602 2603 // Check that we detect invalid verbs 2604 save = verbs[1]; 2605 verbs[1] = 17; 2606 REPORTER_ASSERT(reporter, !path.readFromMemory(buffer, sizeof(buffer))); 2607 verbs[1] = save; 2608 } 2609 2610 static void test_flattening(skiatest::Reporter* reporter) { 2611 SkPath p; 2612 2613 static const SkPoint pts[] = { 2614 { 0, 0 }, 2615 { SkIntToScalar(10), SkIntToScalar(10) }, 2616 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, 2617 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } 2618 }; 2619 p.moveTo(pts[0]); 2620 p.lineTo(pts[1]); 2621 p.quadTo(pts[2], pts[3]); 2622 p.cubicTo(pts[4], pts[5], pts[6]); 2623 2624 write_and_read_back(reporter, p); 2625 2626 // create a buffer that should be much larger than the path so we don't 2627 // kill our stack if writer goes too far. 2628 char buffer[1024]; 2629 size_t size1 = p.writeToMemory(nullptr); 2630 size_t size2 = p.writeToMemory(buffer); 2631 REPORTER_ASSERT(reporter, size1 == size2); 2632 2633 SkPath p2; 2634 size_t size3 = p2.readFromMemory(buffer, 1024); 2635 REPORTER_ASSERT(reporter, size1 == size3); 2636 REPORTER_ASSERT(reporter, p == p2); 2637 2638 size3 = p2.readFromMemory(buffer, 0); 2639 REPORTER_ASSERT(reporter, !size3); 2640 2641 SkPath tooShort; 2642 size3 = tooShort.readFromMemory(buffer, size1 - 1); 2643 REPORTER_ASSERT(reporter, tooShort.isEmpty()); 2644 2645 char buffer2[1024]; 2646 size3 = p2.writeToMemory(buffer2); 2647 REPORTER_ASSERT(reporter, size1 == size3); 2648 REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); 2649 2650 // test persistence of the oval flag & convexity 2651 { 2652 SkPath oval; 2653 SkRect rect = SkRect::MakeWH(10, 10); 2654 oval.addOval(rect); 2655 2656 write_and_read_back(reporter, oval); 2657 } 2658 2659 test_corrupt_flattening(reporter); 2660 } 2661 2662 static void test_transform(skiatest::Reporter* reporter) { 2663 SkPath p; 2664 2665 #define CONIC_PERSPECTIVE_BUG_FIXED 0 2666 static const SkPoint pts[] = { 2667 { 0, 0 }, // move 2668 { SkIntToScalar(10), SkIntToScalar(10) }, // line 2669 { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad 2670 { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic 2671 #if CONIC_PERSPECTIVE_BUG_FIXED 2672 { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic 2673 #endif 2674 }; 2675 const int kPtCount = SK_ARRAY_COUNT(pts); 2676 2677 p.moveTo(pts[0]); 2678 p.lineTo(pts[1]); 2679 p.quadTo(pts[2], pts[3]); 2680 p.cubicTo(pts[4], pts[5], pts[6]); 2681 #if CONIC_PERSPECTIVE_BUG_FIXED 2682 p.conicTo(pts[4], pts[5], 0.5f); 2683 #endif 2684 p.close(); 2685 2686 { 2687 SkMatrix matrix; 2688 matrix.reset(); 2689 SkPath p1; 2690 p.transform(matrix, &p1); 2691 REPORTER_ASSERT(reporter, p == p1); 2692 } 2693 2694 2695 { 2696 SkMatrix matrix; 2697 matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3); 2698 2699 SkPath p1; // Leave p1 non-unique (i.e., the empty path) 2700 2701 p.transform(matrix, &p1); 2702 SkPoint pts1[kPtCount]; 2703 int count = p1.getPoints(pts1, kPtCount); 2704 REPORTER_ASSERT(reporter, kPtCount == count); 2705 for (int i = 0; i < count; ++i) { 2706 SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3); 2707 REPORTER_ASSERT(reporter, newPt == pts1[i]); 2708 } 2709 } 2710 2711 { 2712 SkMatrix matrix; 2713 matrix.reset(); 2714 matrix.setPerspX(4); 2715 2716 SkPath p1; 2717 p1.moveTo(SkPoint::Make(0, 0)); 2718 2719 p.transform(matrix, &p1); 2720 REPORTER_ASSERT(reporter, matrix.invert(&matrix)); 2721 p1.transform(matrix, nullptr); 2722 SkRect pBounds = p.getBounds(); 2723 SkRect p1Bounds = p1.getBounds(); 2724 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft)); 2725 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop)); 2726 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight)); 2727 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom)); 2728 } 2729 2730 p.reset(); 2731 p.addCircle(0, 0, 1, SkPath::kCW_Direction); 2732 2733 { 2734 SkMatrix matrix; 2735 matrix.reset(); 2736 SkPath p1; 2737 p1.moveTo(SkPoint::Make(0, 0)); 2738 2739 p.transform(matrix, &p1); 2740 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCW_FirstDirection)); 2741 } 2742 2743 2744 { 2745 SkMatrix matrix; 2746 matrix.reset(); 2747 matrix.setScaleX(-1); 2748 SkPath p1; 2749 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) 2750 2751 p.transform(matrix, &p1); 2752 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kCCW_FirstDirection)); 2753 } 2754 2755 { 2756 SkMatrix matrix; 2757 matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1); 2758 SkPath p1; 2759 p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) 2760 2761 p.transform(matrix, &p1); 2762 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p1, SkPathPriv::kUnknown_FirstDirection)); 2763 } 2764 } 2765 2766 static void test_zero_length_paths(skiatest::Reporter* reporter) { 2767 SkPath p; 2768 uint8_t verbs[32]; 2769 2770 struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData { 2771 const char* testPath; 2772 const size_t numResultPts; 2773 const SkRect resultBound; 2774 const SkPath::Verb* resultVerbs; 2775 const size_t numResultVerbs; 2776 }; 2777 2778 static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb }; 2779 static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb }; 2780 static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb }; 2781 static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb }; 2782 static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb }; 2783 static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb }; 2784 static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb }; 2785 static const SkPath::Verb resultVerbs8[] = { 2786 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb 2787 }; 2788 static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb }; 2789 static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb }; 2790 static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb }; 2791 static const SkPath::Verb resultVerbs12[] = { 2792 SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb 2793 }; 2794 static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb }; 2795 static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb }; 2796 static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb }; 2797 static const SkPath::Verb resultVerbs16[] = { 2798 SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb 2799 }; 2800 static const struct zeroPathTestData gZeroLengthTests[] = { 2801 { "M 1 1", 1, {1, 1, 1, 1}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2802 { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, 2803 { "M 1 1 z", 1, {1, 1, 1, 1}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, 2804 { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, 2805 { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }, 2806 { "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) }, 2807 { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) }, 2808 { "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) }, 2809 { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) }, 2810 { "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) }, 2811 { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) }, 2812 { "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) }, 2813 { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) }, 2814 { "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, 2815 SK_ARRAY_COUNT(resultVerbs14) 2816 }, 2817 { "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) }, 2818 { "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, 2819 SK_ARRAY_COUNT(resultVerbs16) 2820 } 2821 }; 2822 2823 for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) { 2824 p.reset(); 2825 bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p); 2826 REPORTER_ASSERT(reporter, valid); 2827 REPORTER_ASSERT(reporter, !p.isEmpty()); 2828 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints()); 2829 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds()); 2830 REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs))); 2831 for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) { 2832 REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]); 2833 } 2834 } 2835 } 2836 2837 struct SegmentInfo { 2838 SkPath fPath; 2839 int fPointCount; 2840 }; 2841 2842 #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask) 2843 2844 static void test_segment_masks(skiatest::Reporter* reporter) { 2845 SkPath p, p2; 2846 2847 p.moveTo(0, 0); 2848 p.quadTo(100, 100, 200, 200); 2849 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); 2850 REPORTER_ASSERT(reporter, !p.isEmpty()); 2851 p2 = p; 2852 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); 2853 p.cubicTo(100, 100, 200, 200, 300, 300); 2854 REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); 2855 REPORTER_ASSERT(reporter, !p.isEmpty()); 2856 p2 = p; 2857 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); 2858 2859 p.reset(); 2860 p.moveTo(0, 0); 2861 p.cubicTo(100, 100, 200, 200, 300, 300); 2862 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); 2863 p2 = p; 2864 REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); 2865 2866 REPORTER_ASSERT(reporter, !p.isEmpty()); 2867 } 2868 2869 static void test_iter(skiatest::Reporter* reporter) { 2870 SkPath p; 2871 SkPoint pts[4]; 2872 2873 // Test an iterator with no path 2874 SkPath::Iter noPathIter; 2875 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); 2876 2877 // Test that setting an empty path works 2878 noPathIter.setPath(p, false); 2879 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); 2880 2881 // Test that close path makes no difference for an empty path 2882 noPathIter.setPath(p, true); 2883 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); 2884 2885 // Test an iterator with an initial empty path 2886 SkPath::Iter iter(p, false); 2887 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 2888 2889 // Test that close path makes no difference 2890 iter.setPath(p, true); 2891 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 2892 2893 2894 struct iterTestData { 2895 const char* testPath; 2896 const bool forceClose; 2897 const bool consumeDegenerates; 2898 const size_t* numResultPtsPerVerb; 2899 const SkPoint* resultPts; 2900 const SkPath::Verb* resultVerbs; 2901 const size_t numResultVerbs; 2902 }; 2903 2904 static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb }; 2905 static const SkPath::Verb resultVerbs2[] = { 2906 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb 2907 }; 2908 static const SkPath::Verb resultVerbs3[] = { 2909 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb 2910 }; 2911 static const SkPath::Verb resultVerbs4[] = { 2912 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb 2913 }; 2914 static const SkPath::Verb resultVerbs5[] = { 2915 SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb 2916 }; 2917 static const size_t resultPtsSizes1[] = { 0 }; 2918 static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 }; 2919 static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 }; 2920 static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 }; 2921 static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 }; 2922 static const SkPoint* resultPts1 = 0; 2923 static const SkPoint resultPts2[] = { 2924 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 } 2925 }; 2926 static const SkPoint resultPts3[] = { 2927 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }, 2928 { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 } 2929 }; 2930 static const SkPoint resultPts4[] = { 2931 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } 2932 }; 2933 static const SkPoint resultPts5[] = { 2934 { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } 2935 }; 2936 static const struct iterTestData gIterTests[] = { 2937 { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2938 { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2939 { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2940 { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2941 { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2942 { "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) }, 2943 { "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) }, 2944 { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, 2945 { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, 2946 { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2947 { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, 2948 { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, 2949 { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) } 2950 }; 2951 2952 for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) { 2953 p.reset(); 2954 bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p); 2955 REPORTER_ASSERT(reporter, valid); 2956 iter.setPath(p, gIterTests[i].forceClose); 2957 int j = 0, l = 0; 2958 do { 2959 REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]); 2960 for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) { 2961 REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]); 2962 } 2963 } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb); 2964 REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs); 2965 } 2966 2967 p.reset(); 2968 iter.setPath(p, false); 2969 REPORTER_ASSERT(reporter, !iter.isClosedContour()); 2970 p.lineTo(1, 1); 2971 p.close(); 2972 iter.setPath(p, false); 2973 REPORTER_ASSERT(reporter, iter.isClosedContour()); 2974 p.reset(); 2975 iter.setPath(p, true); 2976 REPORTER_ASSERT(reporter, !iter.isClosedContour()); 2977 p.lineTo(1, 1); 2978 iter.setPath(p, true); 2979 REPORTER_ASSERT(reporter, iter.isClosedContour()); 2980 p.moveTo(0, 0); 2981 p.lineTo(2, 2); 2982 iter.setPath(p, false); 2983 REPORTER_ASSERT(reporter, !iter.isClosedContour()); 2984 2985 // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not 2986 // check to see if the result is correct. 2987 for (int setNaN = 0; setNaN < 4; ++setNaN) { 2988 p.reset(); 2989 p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0); 2990 p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1); 2991 iter.setPath(p, true); 2992 iter.next(pts, false); 2993 iter.next(pts, false); 2994 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false)); 2995 } 2996 2997 p.reset(); 2998 p.quadTo(0, 0, 0, 0); 2999 iter.setPath(p, false); 3000 iter.next(pts, false); 3001 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false)); 3002 iter.setPath(p, false); 3003 iter.next(pts, false); 3004 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); 3005 3006 p.reset(); 3007 p.conicTo(0, 0, 0, 0, 0.5f); 3008 iter.setPath(p, false); 3009 iter.next(pts, false); 3010 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false)); 3011 iter.setPath(p, false); 3012 iter.next(pts, false); 3013 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); 3014 3015 p.reset(); 3016 p.cubicTo(0, 0, 0, 0, 0, 0); 3017 iter.setPath(p, false); 3018 iter.next(pts, false); 3019 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); 3020 iter.setPath(p, false); 3021 iter.next(pts, false); 3022 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); 3023 3024 p.moveTo(1, 1); // add a trailing moveto 3025 iter.setPath(p, false); 3026 iter.next(pts, false); 3027 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); 3028 iter.setPath(p, false); 3029 iter.next(pts, false); 3030 REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); 3031 3032 // The GM degeneratesegments.cpp test is more extensive 3033 3034 // Test out mixed degenerate and non-degenerate geometry with Conics 3035 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } }; 3036 SkRect r = SkRect::MakeWH(100, 100); 3037 SkRRect rr; 3038 rr.setRectRadii(r, radii); 3039 p.reset(); 3040 p.addRRect(rr); 3041 iter.setPath(p, false); 3042 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == iter.next(pts)); 3043 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts)); 3044 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == iter.next(pts)); 3045 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts)); 3046 REPORTER_ASSERT(reporter, SK_ScalarRoot2Over2 == iter.conicWeight()); 3047 } 3048 3049 static void test_raw_iter(skiatest::Reporter* reporter) { 3050 SkPath p; 3051 SkPoint pts[4]; 3052 3053 // Test an iterator with no path 3054 SkPath::RawIter noPathIter; 3055 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); 3056 // Test that setting an empty path works 3057 noPathIter.setPath(p); 3058 REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); 3059 3060 // Test an iterator with an initial empty path 3061 SkPath::RawIter iter(p); 3062 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 3063 3064 // Test that a move-only path returns the move. 3065 p.moveTo(SK_Scalar1, 0); 3066 iter.setPath(p); 3067 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3068 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); 3069 REPORTER_ASSERT(reporter, pts[0].fY == 0); 3070 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 3071 3072 // No matter how many moves we add, we should get them all back 3073 p.moveTo(SK_Scalar1*2, SK_Scalar1); 3074 p.moveTo(SK_Scalar1*3, SK_Scalar1*2); 3075 iter.setPath(p); 3076 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3077 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); 3078 REPORTER_ASSERT(reporter, pts[0].fY == 0); 3079 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3080 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); 3081 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); 3082 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3083 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); 3084 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); 3085 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 3086 3087 // Initial close is never ever stored 3088 p.reset(); 3089 p.close(); 3090 iter.setPath(p); 3091 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 3092 3093 // Move/close sequences 3094 p.reset(); 3095 p.close(); // Not stored, no purpose 3096 p.moveTo(SK_Scalar1, 0); 3097 p.close(); 3098 p.close(); // Not stored, no purpose 3099 p.moveTo(SK_Scalar1*2, SK_Scalar1); 3100 p.close(); 3101 p.moveTo(SK_Scalar1*3, SK_Scalar1*2); 3102 p.moveTo(SK_Scalar1*4, SK_Scalar1*3); 3103 p.close(); 3104 iter.setPath(p); 3105 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3106 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); 3107 REPORTER_ASSERT(reporter, pts[0].fY == 0); 3108 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); 3109 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3110 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); 3111 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); 3112 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); 3113 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3114 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); 3115 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); 3116 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); 3117 REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); 3118 REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); 3119 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); 3120 REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); 3121 3122 // Generate random paths and verify 3123 SkPoint randomPts[25]; 3124 for (int i = 0; i < 5; ++i) { 3125 for (int j = 0; j < 5; ++j) { 3126 randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j); 3127 } 3128 } 3129 3130 // Max of 10 segments, max 3 points per segment 3131 SkRandom rand(9876543); 3132 SkPoint expectedPts[31]; // May have leading moveTo 3133 SkPath::Verb expectedVerbs[22]; // May have leading moveTo 3134 SkPath::Verb nextVerb; 3135 3136 for (int i = 0; i < 500; ++i) { 3137 p.reset(); 3138 bool lastWasClose = true; 3139 bool haveMoveTo = false; 3140 SkPoint lastMoveToPt = { 0, 0 }; 3141 int numPoints = 0; 3142 int numVerbs = (rand.nextU() >> 16) % 10; 3143 int numIterVerbs = 0; 3144 for (int j = 0; j < numVerbs; ++j) { 3145 do { 3146 nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb); 3147 } while (lastWasClose && nextVerb == SkPath::kClose_Verb); 3148 switch (nextVerb) { 3149 case SkPath::kMove_Verb: 3150 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; 3151 p.moveTo(expectedPts[numPoints]); 3152 lastMoveToPt = expectedPts[numPoints]; 3153 numPoints += 1; 3154 lastWasClose = false; 3155 haveMoveTo = true; 3156 break; 3157 case SkPath::kLine_Verb: 3158 if (!haveMoveTo) { 3159 expectedPts[numPoints++] = lastMoveToPt; 3160 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; 3161 haveMoveTo = true; 3162 } 3163 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; 3164 p.lineTo(expectedPts[numPoints]); 3165 numPoints += 1; 3166 lastWasClose = false; 3167 break; 3168 case SkPath::kQuad_Verb: 3169 if (!haveMoveTo) { 3170 expectedPts[numPoints++] = lastMoveToPt; 3171 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; 3172 haveMoveTo = true; 3173 } 3174 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; 3175 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; 3176 p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]); 3177 numPoints += 2; 3178 lastWasClose = false; 3179 break; 3180 case SkPath::kConic_Verb: 3181 if (!haveMoveTo) { 3182 expectedPts[numPoints++] = lastMoveToPt; 3183 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; 3184 haveMoveTo = true; 3185 } 3186 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; 3187 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; 3188 p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1], 3189 rand.nextUScalar1() * 4); 3190 numPoints += 2; 3191 lastWasClose = false; 3192 break; 3193 case SkPath::kCubic_Verb: 3194 if (!haveMoveTo) { 3195 expectedPts[numPoints++] = lastMoveToPt; 3196 expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; 3197 haveMoveTo = true; 3198 } 3199 expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; 3200 expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; 3201 expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25]; 3202 p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1], 3203 expectedPts[numPoints + 2]); 3204 numPoints += 3; 3205 lastWasClose = false; 3206 break; 3207 case SkPath::kClose_Verb: 3208 p.close(); 3209 haveMoveTo = false; 3210 lastWasClose = true; 3211 break; 3212 default: 3213 SkDEBUGFAIL("unexpected verb"); 3214 } 3215 expectedVerbs[numIterVerbs++] = nextVerb; 3216 } 3217 3218 iter.setPath(p); 3219 numVerbs = numIterVerbs; 3220 numIterVerbs = 0; 3221 int numIterPts = 0; 3222 SkPoint lastMoveTo; 3223 SkPoint lastPt; 3224 lastMoveTo.set(0, 0); 3225 lastPt.set(0, 0); 3226 while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) { 3227 REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]); 3228 numIterVerbs++; 3229 switch (nextVerb) { 3230 case SkPath::kMove_Verb: 3231 REPORTER_ASSERT(reporter, numIterPts < numPoints); 3232 REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]); 3233 lastPt = lastMoveTo = pts[0]; 3234 numIterPts += 1; 3235 break; 3236 case SkPath::kLine_Verb: 3237 REPORTER_ASSERT(reporter, numIterPts < numPoints + 1); 3238 REPORTER_ASSERT(reporter, pts[0] == lastPt); 3239 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); 3240 lastPt = pts[1]; 3241 numIterPts += 1; 3242 break; 3243 case SkPath::kQuad_Verb: 3244 case SkPath::kConic_Verb: 3245 REPORTER_ASSERT(reporter, numIterPts < numPoints + 2); 3246 REPORTER_ASSERT(reporter, pts[0] == lastPt); 3247 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); 3248 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); 3249 lastPt = pts[2]; 3250 numIterPts += 2; 3251 break; 3252 case SkPath::kCubic_Verb: 3253 REPORTER_ASSERT(reporter, numIterPts < numPoints + 3); 3254 REPORTER_ASSERT(reporter, pts[0] == lastPt); 3255 REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); 3256 REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); 3257 REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]); 3258 lastPt = pts[3]; 3259 numIterPts += 3; 3260 break; 3261 case SkPath::kClose_Verb: 3262 lastPt = lastMoveTo; 3263 break; 3264 default: 3265 SkDEBUGFAIL("unexpected verb"); 3266 } 3267 } 3268 REPORTER_ASSERT(reporter, numIterPts == numPoints); 3269 REPORTER_ASSERT(reporter, numIterVerbs == numVerbs); 3270 } 3271 } 3272 3273 static void check_for_circle(skiatest::Reporter* reporter, 3274 const SkPath& path, 3275 bool expectedCircle, 3276 SkPathPriv::FirstDirection expectedDir) { 3277 SkRect rect = SkRect::MakeEmpty(); 3278 REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle); 3279 SkPath::Direction isOvalDir; 3280 unsigned isOvalStart; 3281 if (path.isOval(&rect, &isOvalDir, &isOvalStart)) { 3282 REPORTER_ASSERT(reporter, rect.height() == rect.width()); 3283 REPORTER_ASSERT(reporter, SkPathPriv::AsFirstDirection(isOvalDir) == expectedDir); 3284 SkPath tmpPath; 3285 tmpPath.addOval(rect, isOvalDir, isOvalStart); 3286 REPORTER_ASSERT(reporter, path == tmpPath); 3287 } 3288 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(path, expectedDir)); 3289 } 3290 3291 static void test_circle_skew(skiatest::Reporter* reporter, 3292 const SkPath& path, 3293 SkPathPriv::FirstDirection dir) { 3294 SkPath tmp; 3295 3296 SkMatrix m; 3297 m.setSkew(SkIntToScalar(3), SkIntToScalar(5)); 3298 path.transform(m, &tmp); 3299 // this matrix reverses the direction. 3300 if (SkPathPriv::kCCW_FirstDirection == dir) { 3301 dir = SkPathPriv::kCW_FirstDirection; 3302 } else { 3303 REPORTER_ASSERT(reporter, SkPathPriv::kCW_FirstDirection == dir); 3304 dir = SkPathPriv::kCCW_FirstDirection; 3305 } 3306 check_for_circle(reporter, tmp, false, dir); 3307 } 3308 3309 static void test_circle_translate(skiatest::Reporter* reporter, 3310 const SkPath& path, 3311 SkPathPriv::FirstDirection dir) { 3312 SkPath tmp; 3313 3314 // translate at small offset 3315 SkMatrix m; 3316 m.setTranslate(SkIntToScalar(15), SkIntToScalar(15)); 3317 path.transform(m, &tmp); 3318 check_for_circle(reporter, tmp, true, dir); 3319 3320 tmp.reset(); 3321 m.reset(); 3322 3323 // translate at a relatively big offset 3324 m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000)); 3325 path.transform(m, &tmp); 3326 check_for_circle(reporter, tmp, true, dir); 3327 } 3328 3329 static void test_circle_rotate(skiatest::Reporter* reporter, 3330 const SkPath& path, 3331 SkPathPriv::FirstDirection dir) { 3332 for (int angle = 0; angle < 360; ++angle) { 3333 SkPath tmp; 3334 SkMatrix m; 3335 m.setRotate(SkIntToScalar(angle)); 3336 path.transform(m, &tmp); 3337 3338 // TODO: a rotated circle whose rotated angle is not a multiple of 90 3339 // degrees is not an oval anymore, this can be improved. we made this 3340 // for the simplicity of our implementation. 3341 if (angle % 90 == 0) { 3342 check_for_circle(reporter, tmp, true, dir); 3343 } else { 3344 check_for_circle(reporter, tmp, false, dir); 3345 } 3346 } 3347 } 3348 3349 static void test_circle_mirror_x(skiatest::Reporter* reporter, 3350 const SkPath& path, 3351 SkPathPriv::FirstDirection dir) { 3352 SkPath tmp; 3353 SkMatrix m; 3354 m.reset(); 3355 m.setScaleX(-SK_Scalar1); 3356 path.transform(m, &tmp); 3357 if (SkPathPriv::kCW_FirstDirection == dir) { 3358 dir = SkPathPriv::kCCW_FirstDirection; 3359 } else { 3360 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir); 3361 dir = SkPathPriv::kCW_FirstDirection; 3362 } 3363 check_for_circle(reporter, tmp, true, dir); 3364 } 3365 3366 static void test_circle_mirror_y(skiatest::Reporter* reporter, 3367 const SkPath& path, 3368 SkPathPriv::FirstDirection dir) { 3369 SkPath tmp; 3370 SkMatrix m; 3371 m.reset(); 3372 m.setScaleY(-SK_Scalar1); 3373 path.transform(m, &tmp); 3374 3375 if (SkPathPriv::kCW_FirstDirection == dir) { 3376 dir = SkPathPriv::kCCW_FirstDirection; 3377 } else { 3378 REPORTER_ASSERT(reporter, SkPathPriv::kCCW_FirstDirection == dir); 3379 dir = SkPathPriv::kCW_FirstDirection; 3380 } 3381 3382 check_for_circle(reporter, tmp, true, dir); 3383 } 3384 3385 static void test_circle_mirror_xy(skiatest::Reporter* reporter, 3386 const SkPath& path, 3387 SkPathPriv::FirstDirection dir) { 3388 SkPath tmp; 3389 SkMatrix m; 3390 m.reset(); 3391 m.setScaleX(-SK_Scalar1); 3392 m.setScaleY(-SK_Scalar1); 3393 path.transform(m, &tmp); 3394 3395 check_for_circle(reporter, tmp, true, dir); 3396 } 3397 3398 static void test_circle_with_direction(skiatest::Reporter* reporter, 3399 SkPath::Direction inDir) { 3400 const SkPathPriv::FirstDirection dir = SkPathPriv::AsFirstDirection(inDir); 3401 SkPath path; 3402 3403 // circle at origin 3404 path.addCircle(0, 0, SkIntToScalar(20), inDir); 3405 3406 check_for_circle(reporter, path, true, dir); 3407 test_circle_rotate(reporter, path, dir); 3408 test_circle_translate(reporter, path, dir); 3409 test_circle_skew(reporter, path, dir); 3410 test_circle_mirror_x(reporter, path, dir); 3411 test_circle_mirror_y(reporter, path, dir); 3412 test_circle_mirror_xy(reporter, path, dir); 3413 3414 // circle at an offset at (10, 10) 3415 path.reset(); 3416 path.addCircle(SkIntToScalar(10), SkIntToScalar(10), 3417 SkIntToScalar(20), inDir); 3418 3419 check_for_circle(reporter, path, true, dir); 3420 test_circle_rotate(reporter, path, dir); 3421 test_circle_translate(reporter, path, dir); 3422 test_circle_skew(reporter, path, dir); 3423 test_circle_mirror_x(reporter, path, dir); 3424 test_circle_mirror_y(reporter, path, dir); 3425 test_circle_mirror_xy(reporter, path, dir); 3426 3427 // Try different starting points for the contour. 3428 for (unsigned start = 0; start < 4; ++start) { 3429 path.reset(); 3430 path.addOval(SkRect::MakeXYWH(20, 10, 5, 5), inDir, start); 3431 test_circle_rotate(reporter, path, dir); 3432 test_circle_translate(reporter, path, dir); 3433 test_circle_skew(reporter, path, dir); 3434 test_circle_mirror_x(reporter, path, dir); 3435 test_circle_mirror_y(reporter, path, dir); 3436 test_circle_mirror_xy(reporter, path, dir); 3437 } 3438 } 3439 3440 static void test_circle_with_add_paths(skiatest::Reporter* reporter) { 3441 SkPath path; 3442 SkPath circle; 3443 SkPath rect; 3444 SkPath empty; 3445 3446 const SkPath::Direction kCircleDir = SkPath::kCW_Direction; 3447 const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction; 3448 3449 circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir); 3450 rect.addRect(SkIntToScalar(5), SkIntToScalar(5), 3451 SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction); 3452 3453 SkMatrix translate; 3454 translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12)); 3455 3456 // Although all the path concatenation related operations leave 3457 // the path a circle, most mark it as a non-circle for simplicity 3458 3459 // empty + circle (translate) 3460 path = empty; 3461 path.addPath(circle, translate); 3462 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDir)); 3463 3464 // circle + empty (translate) 3465 path = circle; 3466 path.addPath(empty, translate); 3467 3468 check_for_circle(reporter, path, true, SkPathPriv::AsFirstDirection(kCircleDir)); 3469 3470 // test reverseAddPath 3471 path = circle; 3472 path.reverseAddPath(rect); 3473 check_for_circle(reporter, path, false, SkPathPriv::AsFirstDirection(kCircleDirOpposite)); 3474 } 3475 3476 static void test_circle(skiatest::Reporter* reporter) { 3477 test_circle_with_direction(reporter, SkPath::kCW_Direction); 3478 test_circle_with_direction(reporter, SkPath::kCCW_Direction); 3479 3480 // multiple addCircle() 3481 SkPath path; 3482 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); 3483 path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction); 3484 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection); 3485 3486 // some extra lineTo() would make isOval() fail 3487 path.reset(); 3488 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); 3489 path.lineTo(0, 0); 3490 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection); 3491 3492 // not back to the original point 3493 path.reset(); 3494 path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); 3495 path.setLastPt(SkIntToScalar(5), SkIntToScalar(5)); 3496 check_for_circle(reporter, path, false, SkPathPriv::kCW_FirstDirection); 3497 3498 test_circle_with_add_paths(reporter); 3499 3500 // test negative radius 3501 path.reset(); 3502 path.addCircle(0, 0, -1, SkPath::kCW_Direction); 3503 REPORTER_ASSERT(reporter, path.isEmpty()); 3504 } 3505 3506 static void test_oval(skiatest::Reporter* reporter) { 3507 SkRect rect; 3508 SkMatrix m; 3509 SkPath path; 3510 unsigned start = 0; 3511 SkPath::Direction dir = SkPath::kCCW_Direction; 3512 3513 rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50)); 3514 path.addOval(rect); 3515 3516 // Defaults to dir = CW and start = 1 3517 REPORTER_ASSERT(reporter, path.isOval(nullptr)); 3518 3519 m.setRotate(SkIntToScalar(90)); 3520 SkPath tmp; 3521 path.transform(m, &tmp); 3522 // an oval rotated 90 degrees is still an oval. The start index changes from 1 to 2. Direction 3523 // is unchanged. 3524 REPORTER_ASSERT(reporter, tmp.isOval(nullptr, &dir, &start)); 3525 REPORTER_ASSERT(reporter, 2 == start); 3526 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); 3527 3528 m.reset(); 3529 m.setRotate(SkIntToScalar(30)); 3530 tmp.reset(); 3531 path.transform(m, &tmp); 3532 // an oval rotated 30 degrees is not an oval anymore. 3533 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr)); 3534 3535 // since empty path being transformed. 3536 path.reset(); 3537 tmp.reset(); 3538 m.reset(); 3539 path.transform(m, &tmp); 3540 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr)); 3541 3542 // empty path is not an oval 3543 tmp.reset(); 3544 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr)); 3545 3546 // only has moveTo()s 3547 tmp.reset(); 3548 tmp.moveTo(0, 0); 3549 tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10)); 3550 REPORTER_ASSERT(reporter, !tmp.isOval(nullptr)); 3551 3552 // mimic WebKit's calling convention, 3553 // call moveTo() first and then call addOval() 3554 path.reset(); 3555 path.moveTo(0, 0); 3556 path.addOval(rect); 3557 REPORTER_ASSERT(reporter, path.isOval(nullptr)); 3558 3559 // copy path 3560 path.reset(); 3561 tmp.reset(); 3562 tmp.addOval(rect); 3563 path = tmp; 3564 REPORTER_ASSERT(reporter, path.isOval(nullptr, &dir, &start)); 3565 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); 3566 REPORTER_ASSERT(reporter, 1 == start); 3567 } 3568 3569 static void test_empty(skiatest::Reporter* reporter, const SkPath& p) { 3570 SkPath empty; 3571 3572 REPORTER_ASSERT(reporter, p.isEmpty()); 3573 REPORTER_ASSERT(reporter, 0 == p.countPoints()); 3574 REPORTER_ASSERT(reporter, 0 == p.countVerbs()); 3575 REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); 3576 REPORTER_ASSERT(reporter, p.isConvex()); 3577 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); 3578 REPORTER_ASSERT(reporter, !p.isInverseFillType()); 3579 REPORTER_ASSERT(reporter, p == empty); 3580 REPORTER_ASSERT(reporter, !(p != empty)); 3581 } 3582 3583 static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path, 3584 SkPath::Direction dir) { 3585 REPORTER_ASSERT(reporter, path->isConvex()); 3586 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir))); 3587 path->setConvexity(SkPath::kUnknown_Convexity); 3588 REPORTER_ASSERT(reporter, path->isConvex()); 3589 path->reset(); 3590 } 3591 3592 static void test_rrect_convexity_is_unknown(skiatest::Reporter* reporter, SkPath* path, 3593 SkPath::Direction dir) { 3594 REPORTER_ASSERT(reporter, path->isConvex()); 3595 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(*path, SkPathPriv::AsFirstDirection(dir))); 3596 path->setConvexity(SkPath::kUnknown_Convexity); 3597 REPORTER_ASSERT(reporter, path->getConvexity() == SkPath::kUnknown_Convexity); 3598 path->reset(); 3599 } 3600 3601 static void test_rrect(skiatest::Reporter* reporter) { 3602 SkPath p; 3603 SkRRect rr; 3604 SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; 3605 SkRect r = {10, 20, 30, 40}; 3606 rr.setRectRadii(r, radii); 3607 p.addRRect(rr); 3608 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3609 p.addRRect(rr, SkPath::kCCW_Direction); 3610 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); 3611 p.addRoundRect(r, &radii[0].fX); 3612 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3613 p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction); 3614 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); 3615 p.addRoundRect(r, radii[1].fX, radii[1].fY); 3616 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3617 p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction); 3618 test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); 3619 for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) { 3620 SkVector save = radii[i]; 3621 radii[i].set(0, 0); 3622 rr.setRectRadii(r, radii); 3623 p.addRRect(rr); 3624 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3625 radii[i] = save; 3626 } 3627 p.addRoundRect(r, 0, 0); 3628 SkRect returnedRect; 3629 REPORTER_ASSERT(reporter, p.isRect(&returnedRect)); 3630 REPORTER_ASSERT(reporter, returnedRect == r); 3631 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3632 SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; 3633 rr.setRectRadii(r, zeroRadii); 3634 p.addRRect(rr); 3635 bool closed; 3636 SkPath::Direction dir; 3637 REPORTER_ASSERT(reporter, p.isRect(nullptr, &closed, &dir)); 3638 REPORTER_ASSERT(reporter, closed); 3639 REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); 3640 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3641 p.addRRect(rr, SkPath::kCW_Direction); 3642 p.addRRect(rr, SkPath::kCW_Direction); 3643 REPORTER_ASSERT(reporter, !p.isConvex()); 3644 p.reset(); 3645 p.addRRect(rr, SkPath::kCCW_Direction); 3646 p.addRRect(rr, SkPath::kCCW_Direction); 3647 REPORTER_ASSERT(reporter, !p.isConvex()); 3648 p.reset(); 3649 SkRect emptyR = {10, 20, 10, 30}; 3650 rr.setRectRadii(emptyR, radii); 3651 p.addRRect(rr); 3652 REPORTER_ASSERT(reporter, p.isEmpty()); 3653 SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax}; 3654 rr.setRectRadii(largeR, radii); 3655 p.addRRect(rr); 3656 test_rrect_convexity_is_unknown(reporter, &p, SkPath::kCW_Direction); 3657 3658 // we check for non-finites 3659 SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity}; 3660 rr.setRectRadii(infR, radii); 3661 REPORTER_ASSERT(reporter, rr.isEmpty()); 3662 3663 SkRect tinyR = {0, 0, 1e-9f, 1e-9f}; 3664 p.addRoundRect(tinyR, 5e-11f, 5e-11f); 3665 test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); 3666 } 3667 3668 static void test_arc(skiatest::Reporter* reporter) { 3669 SkPath p; 3670 SkRect emptyOval = {10, 20, 30, 20}; 3671 REPORTER_ASSERT(reporter, emptyOval.isEmpty()); 3672 p.addArc(emptyOval, 1, 2); 3673 REPORTER_ASSERT(reporter, p.isEmpty()); 3674 p.reset(); 3675 SkRect oval = {10, 20, 30, 40}; 3676 p.addArc(oval, 1, 0); 3677 REPORTER_ASSERT(reporter, p.isEmpty()); 3678 p.reset(); 3679 SkPath cwOval; 3680 cwOval.addOval(oval); 3681 p.addArc(oval, 0, 360); 3682 REPORTER_ASSERT(reporter, p == cwOval); 3683 p.reset(); 3684 SkPath ccwOval; 3685 ccwOval.addOval(oval, SkPath::kCCW_Direction); 3686 p.addArc(oval, 0, -360); 3687 REPORTER_ASSERT(reporter, p == ccwOval); 3688 p.reset(); 3689 p.addArc(oval, 1, 180); 3690 REPORTER_ASSERT(reporter, p.isConvex()); 3691 REPORTER_ASSERT(reporter, SkPathPriv::CheapIsFirstDirection(p, SkPathPriv::kCW_FirstDirection)); 3692 p.setConvexity(SkPath::kUnknown_Convexity); 3693 REPORTER_ASSERT(reporter, p.isConvex()); 3694 } 3695 3696 static inline SkScalar oval_start_index_to_angle(unsigned start) { 3697 switch (start) { 3698 case 0: 3699 return 270.f; 3700 case 1: 3701 return 0.f; 3702 case 2: 3703 return 90.f; 3704 case 3: 3705 return 180.f; 3706 default: 3707 return -1.f; 3708 } 3709 } 3710 3711 static inline SkScalar canonical_start_angle(float angle) { 3712 while (angle < 0.f) { 3713 angle += 360.f; 3714 } 3715 while (angle >= 360.f) { 3716 angle -= 360.f; 3717 } 3718 return angle; 3719 } 3720 3721 static void check_oval_arc(skiatest::Reporter* reporter, SkScalar start, SkScalar sweep, 3722 const SkPath& path) { 3723 SkRect r = SkRect::MakeEmpty(); 3724 SkPath::Direction d = SkPath::kCCW_Direction; 3725 unsigned s = ~0U; 3726 bool isOval = path.isOval(&r, &d, &s); 3727 REPORTER_ASSERT(reporter, isOval); 3728 SkPath recreatedPath; 3729 recreatedPath.addOval(r, d, s); 3730 REPORTER_ASSERT(reporter, path == recreatedPath); 3731 REPORTER_ASSERT(reporter, oval_start_index_to_angle(s) == canonical_start_angle(start)); 3732 REPORTER_ASSERT(reporter, (SkPath::kCW_Direction == d) == (sweep > 0.f)); 3733 } 3734 3735 static void test_arc_ovals(skiatest::Reporter* reporter) { 3736 SkRect oval = SkRect::MakeWH(10, 20); 3737 for (SkScalar sweep : {-720.f, -540.f, -360.f, 360.f, 432.f, 720.f}) { 3738 for (SkScalar start = -360.f; start <= 360.f; start += 1.f) { 3739 SkPath path; 3740 path.addArc(oval, start, sweep); 3741 // SkPath's interfaces for inserting and extracting ovals only allow contours 3742 // to start at multiples of 90 degrees. 3743 if (std::fmod(start, 90.f) == 0) { 3744 check_oval_arc(reporter, start, sweep, path); 3745 } else { 3746 REPORTER_ASSERT(reporter, !path.isOval(nullptr)); 3747 } 3748 } 3749 // Test start angles that are nearly at valid oval start angles. 3750 for (float start : {-180.f, -90.f, 90.f, 180.f}) { 3751 for (float delta : {-SK_ScalarNearlyZero, SK_ScalarNearlyZero}) { 3752 SkPath path; 3753 path.addArc(oval, start + delta, sweep); 3754 check_oval_arc(reporter, start, sweep, path); 3755 } 3756 } 3757 } 3758 } 3759 3760 static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter, 3761 SkScalar x0, SkScalar y0) { 3762 SkPoint pts[4]; 3763 SkPath::Verb v = iter->next(pts); 3764 REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb); 3765 REPORTER_ASSERT(reporter, pts[0].fX == x0); 3766 REPORTER_ASSERT(reporter, pts[0].fY == y0); 3767 } 3768 3769 static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter, 3770 SkScalar x1, SkScalar y1) { 3771 SkPoint pts[4]; 3772 SkPath::Verb v = iter->next(pts); 3773 REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb); 3774 REPORTER_ASSERT(reporter, pts[1].fX == x1); 3775 REPORTER_ASSERT(reporter, pts[1].fY == y1); 3776 } 3777 3778 static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter, 3779 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { 3780 SkPoint pts[4]; 3781 SkPath::Verb v = iter->next(pts); 3782 REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb); 3783 REPORTER_ASSERT(reporter, pts[1].fX == x1); 3784 REPORTER_ASSERT(reporter, pts[1].fY == y1); 3785 REPORTER_ASSERT(reporter, pts[2].fX == x2); 3786 REPORTER_ASSERT(reporter, pts[2].fY == y2); 3787 } 3788 3789 static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { 3790 SkPoint pts[4]; 3791 SkPath::Verb v = iter->next(pts); 3792 REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb); 3793 } 3794 3795 static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { 3796 check_done(reporter, p, iter); 3797 p->reset(); 3798 } 3799 3800 static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p, 3801 SkScalar x0, SkScalar y0) { 3802 SkPath::RawIter iter(*p); 3803 check_move(reporter, &iter, x0, y0); 3804 check_done_and_reset(reporter, p, &iter); 3805 } 3806 3807 static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p, 3808 SkScalar x1, SkScalar y1) { 3809 SkPath::RawIter iter(*p); 3810 check_move(reporter, &iter, 0, 0); 3811 check_line(reporter, &iter, x1, y1); 3812 check_done_and_reset(reporter, p, &iter); 3813 } 3814 3815 static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p, 3816 SkScalar x1, SkScalar y1) { 3817 SkPath::RawIter iter(*p); 3818 check_move(reporter, &iter, 0, 0); 3819 check_line(reporter, &iter, x1, y1); 3820 check_done(reporter, p, &iter); 3821 } 3822 3823 static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p, 3824 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { 3825 SkPath::RawIter iter(*p); 3826 check_move(reporter, &iter, 0, 0); 3827 check_line(reporter, &iter, x1, y1); 3828 check_line(reporter, &iter, x2, y2); 3829 check_done_and_reset(reporter, p, &iter); 3830 } 3831 3832 static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p, 3833 SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { 3834 SkPath::RawIter iter(*p); 3835 check_move(reporter, &iter, 0, 0); 3836 check_quad(reporter, &iter, x1, y1, x2, y2); 3837 check_done_and_reset(reporter, p, &iter); 3838 } 3839 3840 static bool nearly_equal(const SkRect& a, const SkRect& b) { 3841 return SkScalarNearlyEqual(a.fLeft, b.fLeft) && 3842 SkScalarNearlyEqual(a.fTop, b.fTop) && 3843 SkScalarNearlyEqual(a.fRight, b.fRight) && 3844 SkScalarNearlyEqual(a.fBottom, b.fBottom); 3845 } 3846 3847 static void test_arcTo(skiatest::Reporter* reporter) { 3848 SkPath p; 3849 p.arcTo(0, 0, 1, 2, 1); 3850 check_path_is_line_and_reset(reporter, &p, 0, 0); 3851 p.arcTo(1, 2, 1, 2, 1); 3852 check_path_is_line_and_reset(reporter, &p, 1, 2); 3853 p.arcTo(1, 2, 3, 4, 0); 3854 check_path_is_line_and_reset(reporter, &p, 1, 2); 3855 p.arcTo(1, 2, 0, 0, 1); 3856 check_path_is_line_and_reset(reporter, &p, 1, 2); 3857 p.arcTo(1, 0, 1, 1, 1); 3858 SkPoint pt; 3859 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1); 3860 p.reset(); 3861 p.arcTo(1, 0, 1, -1, 1); 3862 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1); 3863 p.reset(); 3864 SkRect oval = {1, 2, 3, 4}; 3865 p.arcTo(oval, 0, 0, true); 3866 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); 3867 p.arcTo(oval, 0, 0, false); 3868 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); 3869 p.arcTo(oval, 360, 0, true); 3870 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); 3871 p.arcTo(oval, 360, 0, false); 3872 check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); 3873 3874 for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) { 3875 p.arcTo(oval, 0, sweep, false); 3876 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval)); 3877 sweep += delta; 3878 delta /= 2; 3879 } 3880 for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) { 3881 p.arcTo(oval, 0, sweep, false); 3882 REPORTER_ASSERT(reporter, nearly_equal(p.getBounds(), oval)); 3883 sweep -= delta; 3884 delta /= 2; 3885 } 3886 SkRect noOvalWidth = {1, 2, 0, 3}; 3887 p.reset(); 3888 p.arcTo(noOvalWidth, 0, 360, false); 3889 REPORTER_ASSERT(reporter, p.isEmpty()); 3890 3891 SkRect noOvalHeight = {1, 2, 3, 1}; 3892 p.reset(); 3893 p.arcTo(noOvalHeight, 0, 360, false); 3894 REPORTER_ASSERT(reporter, p.isEmpty()); 3895 } 3896 3897 static void test_addPath(skiatest::Reporter* reporter) { 3898 SkPath p, q; 3899 p.lineTo(1, 2); 3900 q.moveTo(4, 4); 3901 q.lineTo(7, 8); 3902 q.conicTo(8, 7, 6, 5, 0.5f); 3903 q.quadTo(6, 7, 8, 6); 3904 q.cubicTo(5, 6, 7, 8, 7, 5); 3905 q.close(); 3906 p.addPath(q, -4, -4); 3907 SkRect expected = {0, 0, 4, 4}; 3908 REPORTER_ASSERT(reporter, p.getBounds() == expected); 3909 p.reset(); 3910 p.reverseAddPath(q); 3911 SkRect reverseExpected = {4, 4, 8, 8}; 3912 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); 3913 } 3914 3915 static void test_addPathMode(skiatest::Reporter* reporter, bool explicitMoveTo, bool extend) { 3916 SkPath p, q; 3917 if (explicitMoveTo) { 3918 p.moveTo(1, 1); 3919 } 3920 p.lineTo(1, 2); 3921 if (explicitMoveTo) { 3922 q.moveTo(2, 1); 3923 } 3924 q.lineTo(2, 2); 3925 p.addPath(q, extend ? SkPath::kExtend_AddPathMode : SkPath::kAppend_AddPathMode); 3926 uint8_t verbs[4]; 3927 int verbcount = p.getVerbs(verbs, 4); 3928 REPORTER_ASSERT(reporter, verbcount == 4); 3929 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb); 3930 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb); 3931 REPORTER_ASSERT(reporter, verbs[2] == (extend ? SkPath::kLine_Verb : SkPath::kMove_Verb)); 3932 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kLine_Verb); 3933 } 3934 3935 static void test_extendClosedPath(skiatest::Reporter* reporter) { 3936 SkPath p, q; 3937 p.moveTo(1, 1); 3938 p.lineTo(1, 2); 3939 p.lineTo(2, 2); 3940 p.close(); 3941 q.moveTo(2, 1); 3942 q.lineTo(2, 3); 3943 p.addPath(q, SkPath::kExtend_AddPathMode); 3944 uint8_t verbs[7]; 3945 int verbcount = p.getVerbs(verbs, 7); 3946 REPORTER_ASSERT(reporter, verbcount == 7); 3947 REPORTER_ASSERT(reporter, verbs[0] == SkPath::kMove_Verb); 3948 REPORTER_ASSERT(reporter, verbs[1] == SkPath::kLine_Verb); 3949 REPORTER_ASSERT(reporter, verbs[2] == SkPath::kLine_Verb); 3950 REPORTER_ASSERT(reporter, verbs[3] == SkPath::kClose_Verb); 3951 REPORTER_ASSERT(reporter, verbs[4] == SkPath::kMove_Verb); 3952 REPORTER_ASSERT(reporter, verbs[5] == SkPath::kLine_Verb); 3953 REPORTER_ASSERT(reporter, verbs[6] == SkPath::kLine_Verb); 3954 3955 SkPoint pt; 3956 REPORTER_ASSERT(reporter, p.getLastPt(&pt)); 3957 REPORTER_ASSERT(reporter, pt == SkPoint::Make(2, 3)); 3958 REPORTER_ASSERT(reporter, p.getPoint(3) == SkPoint::Make(1, 1)); 3959 } 3960 3961 static void test_addEmptyPath(skiatest::Reporter* reporter, SkPath::AddPathMode mode) { 3962 SkPath p, q, r; 3963 // case 1: dst is empty 3964 p.moveTo(2, 1); 3965 p.lineTo(2, 3); 3966 q.addPath(p, mode); 3967 REPORTER_ASSERT(reporter, q == p); 3968 // case 2: src is empty 3969 p.addPath(r, mode); 3970 REPORTER_ASSERT(reporter, q == p); 3971 // case 3: src and dst are empty 3972 q.reset(); 3973 q.addPath(r, mode); 3974 REPORTER_ASSERT(reporter, q.isEmpty()); 3975 } 3976 3977 static void test_conicTo_special_case(skiatest::Reporter* reporter) { 3978 SkPath p; 3979 p.conicTo(1, 2, 3, 4, -1); 3980 check_path_is_line_and_reset(reporter, &p, 3, 4); 3981 p.conicTo(1, 2, 3, 4, SK_ScalarInfinity); 3982 check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4); 3983 p.conicTo(1, 2, 3, 4, 1); 3984 check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4); 3985 } 3986 3987 static void test_get_point(skiatest::Reporter* reporter) { 3988 SkPath p; 3989 SkPoint pt = p.getPoint(0); 3990 REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0)); 3991 REPORTER_ASSERT(reporter, !p.getLastPt(nullptr)); 3992 REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0)); 3993 p.setLastPt(10, 10); 3994 pt = p.getPoint(0); 3995 REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10)); 3996 REPORTER_ASSERT(reporter, p.getLastPt(nullptr)); 3997 p.rMoveTo(10, 10); 3998 REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20)); 3999 } 4000 4001 static void test_contains(skiatest::Reporter* reporter) { 4002 SkPath p; 4003 p.moveTo(SkBits2Float(0xe085e7b1), SkBits2Float(0x5f512c00)); // -7.7191e+19f, 1.50724e+19f 4004 p.conicTo(SkBits2Float(0xdfdaa221), SkBits2Float(0x5eaac338), SkBits2Float(0x60342f13), SkBits2Float(0xdf0cbb58), SkBits2Float(0x3f3504f3)); // -3.15084e+19f, 6.15237e+18f, 5.19345e+19f, -1.01408e+19f, 0.707107f 4005 p.conicTo(SkBits2Float(0x60ead799), SkBits2Float(0xdfb76c24), SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8), SkBits2Float(0x3f3504f4)); // 1.35377e+20f, -2.6434e+19f, 8.96947e+19f, -1.75139e+19f, 0.707107f 4006 p.lineTo(SkBits2Float(0x609b9872), SkBits2Float(0xdf730de8)); // 8.96947e+19f, -1.75139e+19f 4007 p.conicTo(SkBits2Float(0x6018b296), SkBits2Float(0xdeee870d), SkBits2Float(0xe008cd8e), SkBits2Float(0x5ed5b2db), SkBits2Float(0x3f3504f3)); // 4.40121e+19f, -8.59386e+18f, -3.94308e+19f, 7.69931e+18f, 0.707107f 4008 p.conicTo(SkBits2Float(0xe0d526d9), SkBits2Float(0x5fa67b31), SkBits2Float(0xe085e7b2), SkBits2Float(0x5f512c01), SkBits2Float(0x3f3504f3)); // -1.22874e+20f, 2.39925e+19f, -7.7191e+19f, 1.50724e+19f, 0.707107f 4009 // this may return true or false, depending on the platform's numerics, but it should not crash 4010 (void) p.contains(-77.2027664f, 15.3066053f); 4011 4012 p.reset(); 4013 p.setFillType(SkPath::kInverseWinding_FillType); 4014 REPORTER_ASSERT(reporter, p.contains(0, 0)); 4015 p.setFillType(SkPath::kWinding_FillType); 4016 REPORTER_ASSERT(reporter, !p.contains(0, 0)); 4017 p.moveTo(4, 4); 4018 p.lineTo(6, 8); 4019 p.lineTo(8, 4); 4020 // test on edge 4021 REPORTER_ASSERT(reporter, p.contains(6, 4)); 4022 REPORTER_ASSERT(reporter, p.contains(5, 6)); 4023 REPORTER_ASSERT(reporter, p.contains(7, 6)); 4024 // test quick reject 4025 REPORTER_ASSERT(reporter, !p.contains(4, 0)); 4026 REPORTER_ASSERT(reporter, !p.contains(0, 4)); 4027 REPORTER_ASSERT(reporter, !p.contains(4, 10)); 4028 REPORTER_ASSERT(reporter, !p.contains(10, 4)); 4029 // test various crossings in x 4030 REPORTER_ASSERT(reporter, !p.contains(5, 7)); 4031 REPORTER_ASSERT(reporter, p.contains(6, 7)); 4032 REPORTER_ASSERT(reporter, !p.contains(7, 7)); 4033 p.reset(); 4034 p.moveTo(4, 4); 4035 p.lineTo(8, 6); 4036 p.lineTo(4, 8); 4037 // test on edge 4038 REPORTER_ASSERT(reporter, p.contains(4, 6)); 4039 REPORTER_ASSERT(reporter, p.contains(6, 5)); 4040 REPORTER_ASSERT(reporter, p.contains(6, 7)); 4041 // test various crossings in y 4042 REPORTER_ASSERT(reporter, !p.contains(7, 5)); 4043 REPORTER_ASSERT(reporter, p.contains(7, 6)); 4044 REPORTER_ASSERT(reporter, !p.contains(7, 7)); 4045 p.reset(); 4046 p.moveTo(4, 4); 4047 p.lineTo(8, 4); 4048 p.lineTo(8, 8); 4049 p.lineTo(4, 8); 4050 // test on vertices 4051 REPORTER_ASSERT(reporter, p.contains(4, 4)); 4052 REPORTER_ASSERT(reporter, p.contains(8, 4)); 4053 REPORTER_ASSERT(reporter, p.contains(8, 8)); 4054 REPORTER_ASSERT(reporter, p.contains(4, 8)); 4055 p.reset(); 4056 p.moveTo(4, 4); 4057 p.lineTo(6, 8); 4058 p.lineTo(2, 8); 4059 // test on edge 4060 REPORTER_ASSERT(reporter, p.contains(5, 6)); 4061 REPORTER_ASSERT(reporter, p.contains(4, 8)); 4062 REPORTER_ASSERT(reporter, p.contains(3, 6)); 4063 p.reset(); 4064 p.moveTo(4, 4); 4065 p.lineTo(0, 6); 4066 p.lineTo(4, 8); 4067 // test on edge 4068 REPORTER_ASSERT(reporter, p.contains(2, 5)); 4069 REPORTER_ASSERT(reporter, p.contains(2, 7)); 4070 REPORTER_ASSERT(reporter, p.contains(4, 6)); 4071 // test canceling coincident edge (a smaller triangle is coincident with a larger one) 4072 p.reset(); 4073 p.moveTo(4, 0); 4074 p.lineTo(6, 4); 4075 p.lineTo(2, 4); 4076 p.moveTo(4, 0); 4077 p.lineTo(0, 8); 4078 p.lineTo(8, 8); 4079 REPORTER_ASSERT(reporter, !p.contains(1, 2)); 4080 REPORTER_ASSERT(reporter, !p.contains(3, 2)); 4081 REPORTER_ASSERT(reporter, !p.contains(4, 0)); 4082 REPORTER_ASSERT(reporter, p.contains(4, 4)); 4083 4084 // test quads 4085 p.reset(); 4086 p.moveTo(4, 4); 4087 p.quadTo(6, 6, 8, 8); 4088 p.quadTo(6, 8, 4, 8); 4089 p.quadTo(4, 6, 4, 4); 4090 REPORTER_ASSERT(reporter, p.contains(5, 6)); 4091 REPORTER_ASSERT(reporter, !p.contains(6, 5)); 4092 // test quad edge 4093 REPORTER_ASSERT(reporter, p.contains(5, 5)); 4094 REPORTER_ASSERT(reporter, p.contains(5, 8)); 4095 REPORTER_ASSERT(reporter, p.contains(4, 5)); 4096 // test quad endpoints 4097 REPORTER_ASSERT(reporter, p.contains(4, 4)); 4098 REPORTER_ASSERT(reporter, p.contains(8, 8)); 4099 REPORTER_ASSERT(reporter, p.contains(4, 8)); 4100 4101 p.reset(); 4102 const SkPoint qPts[] = {{6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}, {6, 6}}; 4103 p.moveTo(qPts[0]); 4104 for (int index = 1; index < (int) SK_ARRAY_COUNT(qPts); index += 2) { 4105 p.quadTo(qPts[index], qPts[index + 1]); 4106 } 4107 REPORTER_ASSERT(reporter, p.contains(5, 6)); 4108 REPORTER_ASSERT(reporter, !p.contains(6, 5)); 4109 // test quad edge 4110 SkPoint halfway; 4111 for (int index = 0; index < (int) SK_ARRAY_COUNT(qPts) - 2; index += 2) { 4112 SkEvalQuadAt(&qPts[index], 0.5f, &halfway, nullptr); 4113 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY)); 4114 } 4115 4116 // test conics 4117 p.reset(); 4118 const SkPoint kPts[] = {{4, 4}, {6, 6}, {8, 8}, {6, 8}, {4, 8}, {4, 6}, {4, 4}}; 4119 p.moveTo(kPts[0]); 4120 for (int index = 1; index < (int) SK_ARRAY_COUNT(kPts); index += 2) { 4121 p.conicTo(kPts[index], kPts[index + 1], 0.5f); 4122 } 4123 REPORTER_ASSERT(reporter, p.contains(5, 6)); 4124 REPORTER_ASSERT(reporter, !p.contains(6, 5)); 4125 // test conic edge 4126 for (int index = 0; index < (int) SK_ARRAY_COUNT(kPts) - 2; index += 2) { 4127 SkConic conic(&kPts[index], 0.5f); 4128 halfway = conic.evalAt(0.5f); 4129 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY)); 4130 } 4131 // test conic end points 4132 REPORTER_ASSERT(reporter, p.contains(4, 4)); 4133 REPORTER_ASSERT(reporter, p.contains(8, 8)); 4134 REPORTER_ASSERT(reporter, p.contains(4, 8)); 4135 4136 // test cubics 4137 SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}}; 4138 for (int i = 0; i < 3; ++i) { 4139 p.reset(); 4140 p.setFillType(SkPath::kEvenOdd_FillType); 4141 p.moveTo(pts[i].fX, pts[i].fY); 4142 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); 4143 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); 4144 p.close(); 4145 REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f)); 4146 REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f)); 4147 // test cubic edge 4148 SkEvalCubicAt(&pts[i], 0.5f, &halfway, nullptr, nullptr); 4149 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY)); 4150 SkEvalCubicAt(&pts[i + 3], 0.5f, &halfway, nullptr, nullptr); 4151 REPORTER_ASSERT(reporter, p.contains(halfway.fX, halfway.fY)); 4152 // test cubic end points 4153 REPORTER_ASSERT(reporter, p.contains(pts[i].fX, pts[i].fY)); 4154 REPORTER_ASSERT(reporter, p.contains(pts[i + 3].fX, pts[i + 3].fY)); 4155 REPORTER_ASSERT(reporter, p.contains(pts[i + 6].fX, pts[i + 6].fY)); 4156 } 4157 } 4158 4159 class PathRefTest_Private { 4160 public: 4161 static void TestPathRef(skiatest::Reporter* reporter) { 4162 static const int kRepeatCnt = 10; 4163 4164 sk_sp<SkPathRef> pathRef(new SkPathRef); 4165 4166 SkPathRef::Editor ed(&pathRef); 4167 4168 { 4169 ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt); 4170 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); 4171 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); 4172 REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks()); 4173 for (int i = 0; i < kRepeatCnt; ++i) { 4174 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i)); 4175 } 4176 ed.resetToSize(0, 0, 0); 4177 } 4178 4179 { 4180 ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt); 4181 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); 4182 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); 4183 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks()); 4184 for (int i = 0; i < kRepeatCnt; ++i) { 4185 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i)); 4186 } 4187 ed.resetToSize(0, 0, 0); 4188 } 4189 4190 { 4191 ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt); 4192 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); 4193 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); 4194 REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks()); 4195 for (int i = 0; i < kRepeatCnt; ++i) { 4196 REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i)); 4197 } 4198 ed.resetToSize(0, 0, 0); 4199 } 4200 4201 { 4202 SkScalar* weights = nullptr; 4203 ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights); 4204 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); 4205 REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); 4206 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights()); 4207 REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks()); 4208 REPORTER_ASSERT(reporter, weights); 4209 for (int i = 0; i < kRepeatCnt; ++i) { 4210 REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i)); 4211 } 4212 ed.resetToSize(0, 0, 0); 4213 } 4214 4215 { 4216 ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt); 4217 REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); 4218 REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints()); 4219 REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks()); 4220 for (int i = 0; i < kRepeatCnt; ++i) { 4221 REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i)); 4222 } 4223 ed.resetToSize(0, 0, 0); 4224 } 4225 } 4226 }; 4227 4228 static void test_operatorEqual(skiatest::Reporter* reporter) { 4229 SkPath a; 4230 SkPath b; 4231 REPORTER_ASSERT(reporter, a == a); 4232 REPORTER_ASSERT(reporter, a == b); 4233 a.setFillType(SkPath::kInverseWinding_FillType); 4234 REPORTER_ASSERT(reporter, a != b); 4235 a.reset(); 4236 REPORTER_ASSERT(reporter, a == b); 4237 a.lineTo(1, 1); 4238 REPORTER_ASSERT(reporter, a != b); 4239 a.reset(); 4240 REPORTER_ASSERT(reporter, a == b); 4241 a.lineTo(1, 1); 4242 b.lineTo(1, 2); 4243 REPORTER_ASSERT(reporter, a != b); 4244 a.reset(); 4245 a.lineTo(1, 2); 4246 REPORTER_ASSERT(reporter, a == b); 4247 } 4248 4249 static void compare_dump(skiatest::Reporter* reporter, const SkPath& path, bool force, 4250 bool dumpAsHex, const char* str) { 4251 SkDynamicMemoryWStream wStream; 4252 path.dump(&wStream, force, dumpAsHex); 4253 sk_sp<SkData> data = wStream.detachAsData(); 4254 REPORTER_ASSERT(reporter, data->size() == strlen(str)); 4255 if (strlen(str) > 0) { 4256 REPORTER_ASSERT(reporter, !memcmp(data->data(), str, strlen(str))); 4257 } else { 4258 REPORTER_ASSERT(reporter, data->data() == nullptr || !memcmp(data->data(), str, strlen(str))); 4259 } 4260 } 4261 4262 static void test_dump(skiatest::Reporter* reporter) { 4263 SkPath p; 4264 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n"); 4265 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n"); 4266 p.moveTo(1, 2); 4267 p.lineTo(3, 4); 4268 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n" 4269 "path.moveTo(1, 2);\n" 4270 "path.lineTo(3, 4);\n"); 4271 compare_dump(reporter, p, true, false, "path.setFillType(SkPath::kWinding_FillType);\n" 4272 "path.moveTo(1, 2);\n" 4273 "path.lineTo(3, 4);\n" 4274 "path.lineTo(1, 2);\n" 4275 "path.close();\n"); 4276 p.reset(); 4277 p.setFillType(SkPath::kEvenOdd_FillType); 4278 p.moveTo(1, 2); 4279 p.quadTo(3, 4, 5, 6); 4280 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kEvenOdd_FillType);\n" 4281 "path.moveTo(1, 2);\n" 4282 "path.quadTo(3, 4, 5, 6);\n"); 4283 p.reset(); 4284 p.setFillType(SkPath::kInverseWinding_FillType); 4285 p.moveTo(1, 2); 4286 p.conicTo(3, 4, 5, 6, 0.5f); 4287 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseWinding_FillType);\n" 4288 "path.moveTo(1, 2);\n" 4289 "path.conicTo(3, 4, 5, 6, 0.5f);\n"); 4290 p.reset(); 4291 p.setFillType(SkPath::kInverseEvenOdd_FillType); 4292 p.moveTo(1, 2); 4293 p.cubicTo(3, 4, 5, 6, 7, 8); 4294 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kInverseEvenOdd_FillType);\n" 4295 "path.moveTo(1, 2);\n" 4296 "path.cubicTo(3, 4, 5, 6, 7, 8);\n"); 4297 p.reset(); 4298 p.setFillType(SkPath::kWinding_FillType); 4299 p.moveTo(1, 2); 4300 p.lineTo(3, 4); 4301 compare_dump(reporter, p, false, true, 4302 "path.setFillType(SkPath::kWinding_FillType);\n" 4303 "path.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); // 1, 2\n" 4304 "path.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); // 3, 4\n"); 4305 p.reset(); 4306 p.moveTo(SkBits2Float(0x3f800000), SkBits2Float(0x40000000)); 4307 p.lineTo(SkBits2Float(0x40400000), SkBits2Float(0x40800000)); 4308 compare_dump(reporter, p, false, false, "path.setFillType(SkPath::kWinding_FillType);\n" 4309 "path.moveTo(1, 2);\n" 4310 "path.lineTo(3, 4);\n"); 4311 } 4312 4313 namespace { 4314 4315 class ChangeListener : public SkPathRef::GenIDChangeListener { 4316 public: 4317 ChangeListener(bool *changed) : fChanged(changed) { *fChanged = false; } 4318 ~ChangeListener() override {} 4319 void onChange() override { 4320 *fChanged = true; 4321 } 4322 private: 4323 bool* fChanged; 4324 }; 4325 4326 } 4327 4328 class PathTest_Private { 4329 public: 4330 static void TestPathTo(skiatest::Reporter* reporter) { 4331 SkPath p, q; 4332 p.lineTo(4, 4); 4333 p.reversePathTo(q); 4334 check_path_is_line(reporter, &p, 4, 4); 4335 q.moveTo(-4, -4); 4336 p.reversePathTo(q); 4337 check_path_is_line(reporter, &p, 4, 4); 4338 q.lineTo(7, 8); 4339 q.conicTo(8, 7, 6, 5, 0.5f); 4340 q.quadTo(6, 7, 8, 6); 4341 q.cubicTo(5, 6, 7, 8, 7, 5); 4342 q.close(); 4343 p.reversePathTo(q); 4344 SkRect reverseExpected = {-4, -4, 8, 8}; 4345 REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); 4346 } 4347 4348 static void TestPathrefListeners(skiatest::Reporter* reporter) { 4349 SkPath p; 4350 4351 bool changed = false; 4352 p.moveTo(0, 0); 4353 4354 // Check that listener is notified on moveTo(). 4355 4356 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed)); 4357 REPORTER_ASSERT(reporter, !changed); 4358 p.moveTo(10, 0); 4359 REPORTER_ASSERT(reporter, changed); 4360 4361 // Check that listener is notified on lineTo(). 4362 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed)); 4363 REPORTER_ASSERT(reporter, !changed); 4364 p.lineTo(20, 0); 4365 REPORTER_ASSERT(reporter, changed); 4366 4367 // Check that listener is notified on reset(). 4368 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed)); 4369 REPORTER_ASSERT(reporter, !changed); 4370 p.reset(); 4371 REPORTER_ASSERT(reporter, changed); 4372 4373 p.moveTo(0, 0); 4374 4375 // Check that listener is notified on rewind(). 4376 SkPathPriv::AddGenIDChangeListener(p, new ChangeListener(&changed)); 4377 REPORTER_ASSERT(reporter, !changed); 4378 p.rewind(); 4379 REPORTER_ASSERT(reporter, changed); 4380 4381 // Check that listener is notified when pathref is deleted. 4382 { 4383 SkPath q; 4384 q.moveTo(10, 10); 4385 SkPathPriv::AddGenIDChangeListener(q, new ChangeListener(&changed)); 4386 REPORTER_ASSERT(reporter, !changed); 4387 } 4388 // q went out of scope. 4389 REPORTER_ASSERT(reporter, changed); 4390 } 4391 }; 4392 4393 static void test_crbug_629455(skiatest::Reporter* reporter) { 4394 SkPath path; 4395 path.moveTo(0, 0); 4396 path.cubicTo(SkBits2Float(0xcdcdcd00), SkBits2Float(0xcdcdcdcd), 4397 SkBits2Float(0xcdcdcdcd), SkBits2Float(0xcdcdcdcd), 4398 SkBits2Float(0x423fcdcd), SkBits2Float(0x40ed9341)); 4399 // AKA: cubicTo(-4.31596e+08f, -4.31602e+08f, -4.31602e+08f, -4.31602e+08f, 47.951f, 7.42423f); 4400 path.lineTo(0, 0); 4401 4402 auto surface = SkSurface::MakeRasterN32Premul(100, 100); 4403 SkPaint paint; 4404 paint.setAntiAlias(true); 4405 surface->getCanvas()->drawPath(path, paint); 4406 } 4407 4408 static void test_fuzz_crbug_662952(skiatest::Reporter* reporter) { 4409 SkPath path; 4410 path.moveTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f 4411 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411c0000)); // 8.65f, 9.75f 4412 path.lineTo(SkBits2Float(0x410a6666), SkBits2Float(0x411e6666)); // 8.65f, 9.9f 4413 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411e6666)); // 8.6f, 9.9f 4414 path.lineTo(SkBits2Float(0x4109999a), SkBits2Float(0x411c0000)); // 8.6f, 9.75f 4415 path.close(); 4416 4417 auto surface = SkSurface::MakeRasterN32Premul(100, 100); 4418 SkPaint paint; 4419 paint.setAntiAlias(true); 4420 surface->getCanvas()->clipPath(path, true); 4421 surface->getCanvas()->drawRect(SkRect::MakeWH(100, 100), paint); 4422 } 4423 4424 static void test_path_crbugskia6003() { 4425 auto surface(SkSurface::MakeRasterN32Premul(500, 500)); 4426 SkCanvas* canvas = surface->getCanvas(); 4427 SkPaint paint; 4428 paint.setAntiAlias(true); 4429 SkPath path; 4430 path.moveTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f 4431 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a2999a)); // 165.9f, 81.3f 4432 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a2999a)); // 165.7f, 81.3f 4433 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x42a16666)); // 165.7f, 80.7f 4434 path.lineTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666)); // 165.7f, 79.7f 4435 // 165.7f, 79.7f, 165.8f, 79.7f, 165.8f, 79.7f 4436 path.cubicTo(SkBits2Float(0x4325b333), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), 4437 SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666)); 4438 // 165.8f, 79.7f, 165.8f, 79.7f, 165.9f, 79.7f 4439 path.cubicTo(SkBits2Float(0x4325cccc), SkBits2Float(0x429f6666), SkBits2Float(0x4325cccc), 4440 SkBits2Float(0x429f6666), SkBits2Float(0x4325e666), SkBits2Float(0x429f6666)); 4441 path.lineTo(SkBits2Float(0x4325e666), SkBits2Float(0x42a1999a)); // 165.9f, 80.8f 4442 path.close(); 4443 canvas->clipPath(path, true); 4444 canvas->drawRect(SkRect::MakeWH(500, 500), paint); 4445 } 4446 4447 static void test_fuzz_crbug_662730(skiatest::Reporter* reporter) { 4448 SkPath path; 4449 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0 4450 path.lineTo(SkBits2Float(0xd5394437), SkBits2Float(0x37373737)); // -1.2731e+13f, 1.09205e-05f 4451 path.lineTo(SkBits2Float(0x37373737), SkBits2Float(0x37373737)); // 1.09205e-05f, 1.09205e-05f 4452 path.lineTo(SkBits2Float(0x37373745), SkBits2Float(0x0001b800)); // 1.09205e-05f, 1.57842e-40f 4453 path.close(); 4454 4455 auto surface = SkSurface::MakeRasterN32Premul(100, 100); 4456 SkPaint paint; 4457 paint.setAntiAlias(true); 4458 surface->getCanvas()->drawPath(path, paint); 4459 } 4460 4461 static void test_interp(skiatest::Reporter* reporter) { 4462 SkPath p1, p2, out; 4463 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2)); 4464 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out)); 4465 REPORTER_ASSERT(reporter, p1 == out); 4466 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out)); 4467 REPORTER_ASSERT(reporter, p1 == out); 4468 p1.moveTo(0, 2); 4469 p1.lineTo(0, 4); 4470 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2)); 4471 REPORTER_ASSERT(reporter, !p1.interpolate(p2, 1, &out)); 4472 p2.moveTo(6, 0); 4473 p2.lineTo(8, 0); 4474 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2)); 4475 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0, &out)); 4476 REPORTER_ASSERT(reporter, p2 == out); 4477 REPORTER_ASSERT(reporter, p1.interpolate(p2, 1, &out)); 4478 REPORTER_ASSERT(reporter, p1 == out); 4479 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out)); 4480 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(3, 1, 4, 2)); 4481 p1.reset(); 4482 p1.moveTo(4, 4); 4483 p1.conicTo(5, 4, 5, 5, 1 / SkScalarSqrt(2)); 4484 p2.reset(); 4485 p2.moveTo(4, 2); 4486 p2.conicTo(7, 2, 7, 5, 1 / SkScalarSqrt(2)); 4487 REPORTER_ASSERT(reporter, p1.isInterpolatable(p2)); 4488 REPORTER_ASSERT(reporter, p1.interpolate(p2, 0.5f, &out)); 4489 REPORTER_ASSERT(reporter, out.getBounds() == SkRect::MakeLTRB(4, 3, 6, 5)); 4490 p2.reset(); 4491 p2.moveTo(4, 2); 4492 p2.conicTo(6, 3, 6, 5, 1); 4493 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2)); 4494 p2.reset(); 4495 p2.moveTo(4, 4); 4496 p2.conicTo(5, 4, 5, 5, 0.5f); 4497 REPORTER_ASSERT(reporter, !p1.isInterpolatable(p2)); 4498 } 4499 4500 DEF_TEST(PathInterp, reporter) { 4501 test_interp(reporter); 4502 } 4503 4504 #include "SkSurface.h" 4505 DEF_TEST(PathBigCubic, reporter) { 4506 SkPath path; 4507 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0, 0 4508 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f 4509 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), SkBits2Float(0x00000100), SkBits2Float(0x00000000), SkBits2Float(0x00000100), SkBits2Float(0x00000000)); // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0 4510 path.moveTo(0, 512); 4511 4512 // this call should not assert 4513 SkSurface::MakeRasterN32Premul(255, 255, nullptr)->getCanvas()->drawPath(path, SkPaint()); 4514 } 4515 4516 DEF_TEST(PathContains, reporter) { 4517 test_contains(reporter); 4518 } 4519 4520 DEF_TEST(Paths, reporter) { 4521 test_fuzz_crbug_647922(); 4522 test_fuzz_crbug_643933(); 4523 test_sect_with_horizontal_needs_pinning(); 4524 test_crbug_629455(reporter); 4525 test_fuzz_crbug_627414(reporter); 4526 test_path_crbug364224(); 4527 test_fuzz_crbug_662952(reporter); 4528 test_fuzz_crbug_662730(reporter); 4529 test_fuzz_crbug_662780(); 4530 test_mask_overflow(); 4531 test_path_crbugskia6003(); 4532 test_fuzz_crbug_668907(); 4533 4534 SkTSize<SkScalar>::Make(3,4); 4535 4536 SkPath p, empty; 4537 SkRect bounds, bounds2; 4538 test_empty(reporter, p); 4539 4540 REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); 4541 4542 // this triggers a code path in SkPath::operator= which is otherwise unexercised 4543 SkPath& self = p; 4544 p = self; 4545 4546 // this triggers a code path in SkPath::swap which is otherwise unexercised 4547 p.swap(self); 4548 4549 bounds.set(0, 0, SK_Scalar1, SK_Scalar1); 4550 4551 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); 4552 check_convex_bounds(reporter, p, bounds); 4553 // we have quads or cubics 4554 REPORTER_ASSERT(reporter, 4555 p.getSegmentMasks() & (kCurveSegmentMask | SkPath::kConic_SegmentMask)); 4556 REPORTER_ASSERT(reporter, !p.isEmpty()); 4557 4558 p.reset(); 4559 test_empty(reporter, p); 4560 4561 p.addOval(bounds); 4562 check_convex_bounds(reporter, p, bounds); 4563 REPORTER_ASSERT(reporter, !p.isEmpty()); 4564 4565 p.rewind(); 4566 test_empty(reporter, p); 4567 4568 p.addRect(bounds); 4569 check_convex_bounds(reporter, p, bounds); 4570 // we have only lines 4571 REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); 4572 REPORTER_ASSERT(reporter, !p.isEmpty()); 4573 4574 REPORTER_ASSERT(reporter, p != empty); 4575 REPORTER_ASSERT(reporter, !(p == empty)); 4576 4577 // do getPoints and getVerbs return the right result 4578 REPORTER_ASSERT(reporter, p.getPoints(nullptr, 0) == 4); 4579 REPORTER_ASSERT(reporter, p.getVerbs(nullptr, 0) == 5); 4580 SkPoint pts[4]; 4581 int count = p.getPoints(pts, 4); 4582 REPORTER_ASSERT(reporter, count == 4); 4583 uint8_t verbs[6]; 4584 verbs[5] = 0xff; 4585 p.getVerbs(verbs, 5); 4586 REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]); 4587 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]); 4588 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]); 4589 REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]); 4590 REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]); 4591 REPORTER_ASSERT(reporter, 0xff == verbs[5]); 4592 bounds2.set(pts, 4); 4593 REPORTER_ASSERT(reporter, bounds == bounds2); 4594 4595 bounds.offset(SK_Scalar1*3, SK_Scalar1*4); 4596 p.offset(SK_Scalar1*3, SK_Scalar1*4); 4597 REPORTER_ASSERT(reporter, bounds == p.getBounds()); 4598 4599 REPORTER_ASSERT(reporter, p.isRect(nullptr)); 4600 bounds2.setEmpty(); 4601 REPORTER_ASSERT(reporter, p.isRect(&bounds2)); 4602 REPORTER_ASSERT(reporter, bounds == bounds2); 4603 4604 // now force p to not be a rect 4605 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); 4606 p.addRect(bounds); 4607 REPORTER_ASSERT(reporter, !p.isRect(nullptr)); 4608 4609 // Test an edge case w.r.t. the bound returned by isRect (i.e., the 4610 // path has a trailing moveTo. Please see crbug.com\445368) 4611 { 4612 SkRect r; 4613 p.reset(); 4614 p.addRect(bounds); 4615 REPORTER_ASSERT(reporter, p.isRect(&r)); 4616 REPORTER_ASSERT(reporter, r == bounds); 4617 // add a moveTo outside of our bounds 4618 p.moveTo(bounds.fLeft + 10, bounds.fBottom + 10); 4619 REPORTER_ASSERT(reporter, p.isRect(&r)); 4620 REPORTER_ASSERT(reporter, r == bounds); 4621 } 4622 4623 test_operatorEqual(reporter); 4624 test_isLine(reporter); 4625 test_isRect(reporter); 4626 test_is_simple_closed_rect(reporter); 4627 test_isNestedFillRects(reporter); 4628 test_zero_length_paths(reporter); 4629 test_direction(reporter); 4630 test_convexity(reporter); 4631 test_convexity2(reporter); 4632 test_conservativelyContains(reporter); 4633 test_close(reporter); 4634 test_segment_masks(reporter); 4635 test_flattening(reporter); 4636 test_transform(reporter); 4637 test_bounds(reporter); 4638 test_iter(reporter); 4639 test_raw_iter(reporter); 4640 test_circle(reporter); 4641 test_oval(reporter); 4642 test_strokerec(reporter); 4643 test_addPoly(reporter); 4644 test_isfinite(reporter); 4645 test_isfinite_after_transform(reporter); 4646 test_islastcontourclosed(reporter); 4647 test_arb_round_rect_is_convex(reporter); 4648 test_arb_zero_rad_round_rect_is_rect(reporter); 4649 test_addrect(reporter); 4650 test_addrect_isfinite(reporter); 4651 test_tricky_cubic(); 4652 test_clipped_cubic(); 4653 test_crbug_170666(); 4654 test_crbug_493450(reporter); 4655 test_crbug_495894(reporter); 4656 test_crbug_613918(); 4657 test_bad_cubic_crbug229478(); 4658 test_bad_cubic_crbug234190(); 4659 test_gen_id(reporter); 4660 test_path_close_issue1474(reporter); 4661 test_path_to_region(reporter); 4662 test_rrect(reporter); 4663 test_arc(reporter); 4664 test_arc_ovals(reporter); 4665 test_arcTo(reporter); 4666 test_addPath(reporter); 4667 test_addPathMode(reporter, false, false); 4668 test_addPathMode(reporter, true, false); 4669 test_addPathMode(reporter, false, true); 4670 test_addPathMode(reporter, true, true); 4671 test_extendClosedPath(reporter); 4672 test_addEmptyPath(reporter, SkPath::kExtend_AddPathMode); 4673 test_addEmptyPath(reporter, SkPath::kAppend_AddPathMode); 4674 test_conicTo_special_case(reporter); 4675 test_get_point(reporter); 4676 test_contains(reporter); 4677 PathTest_Private::TestPathTo(reporter); 4678 PathRefTest_Private::TestPathRef(reporter); 4679 PathTest_Private::TestPathrefListeners(reporter); 4680 test_dump(reporter); 4681 test_path_crbug389050(reporter); 4682 test_path_crbugskia2820(reporter); 4683 test_path_crbugskia5995(); 4684 test_skbug_3469(reporter); 4685 test_skbug_3239(reporter); 4686 test_bounds_crbug_513799(reporter); 4687 test_fuzz_crbug_638223(); 4688 } 4689 4690 DEF_TEST(conservatively_contains_rect, reporter) { 4691 SkPath path; 4692 4693 path.moveTo(SkBits2Float(0x44000000), SkBits2Float(0x373938b8)); // 512, 1.10401e-05f 4694 // 1.4013e-45f, -9.22346e+18f, 3.58732e-43f, 0, 3.58732e-43f, 0 4695 path.cubicTo(SkBits2Float(0x00000001), SkBits2Float(0xdf000052), 4696 SkBits2Float(0x00000100), SkBits2Float(0x00000000), 4697 SkBits2Float(0x00000100), SkBits2Float(0x00000000)); 4698 path.moveTo(0, 0); 4699 4700 // this guy should not assert 4701 path.conservativelyContainsRect({ -211747, 12.1115f, -197893, 25.0321f }); 4702 } 4703 4704 /////////////////////////////////////////////////////////////////////////////////////////////////// 4705 4706 static void rand_path(SkPath* path, SkRandom& rand, SkPath::Verb verb, int n) { 4707 for (int i = 0; i < n; ++i) { 4708 switch (verb) { 4709 case SkPath::kLine_Verb: 4710 path->lineTo(rand.nextF()*100, rand.nextF()*100); 4711 break; 4712 case SkPath::kQuad_Verb: 4713 path->quadTo(rand.nextF()*100, rand.nextF()*100, 4714 rand.nextF()*100, rand.nextF()*100); 4715 break; 4716 case SkPath::kConic_Verb: 4717 path->conicTo(rand.nextF()*100, rand.nextF()*100, 4718 rand.nextF()*100, rand.nextF()*100, rand.nextF()*10); 4719 break; 4720 case SkPath::kCubic_Verb: 4721 path->cubicTo(rand.nextF()*100, rand.nextF()*100, 4722 rand.nextF()*100, rand.nextF()*100, 4723 rand.nextF()*100, rand.nextF()*100); 4724 break; 4725 default: 4726 SkASSERT(false); 4727 } 4728 } 4729 } 4730 4731 #include "SkPathOps.h" 4732 DEF_TEST(path_tight_bounds, reporter) { 4733 SkRandom rand; 4734 4735 const SkPath::Verb verbs[] = { 4736 SkPath::kLine_Verb, SkPath::kQuad_Verb, SkPath::kConic_Verb, SkPath::kCubic_Verb, 4737 }; 4738 for (int i = 0; i < 1000; ++i) { 4739 for (int n = 1; n <= 10; n += 9) { 4740 for (SkPath::Verb verb : verbs) { 4741 SkPath path; 4742 rand_path(&path, rand, verb, n); 4743 SkRect bounds = path.getBounds(); 4744 SkRect tight = path.computeTightBounds(); 4745 REPORTER_ASSERT(reporter, bounds.contains(tight)); 4746 4747 SkRect tight2; 4748 TightBounds(path, &tight2); 4749 REPORTER_ASSERT(reporter, nearly_equal(tight, tight2)); 4750 } 4751 } 4752 } 4753 } 4754