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