caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "PathOpsExtendedTest.h" |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 9 | #include "PathOpsThreadedCommon.h" |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkStream.h" |
| 15 | |
| 16 | #ifdef SK_BUILD_FOR_MAC |
| 17 | #include <sys/sysctl.h> |
| 18 | #endif |
| 19 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 20 | static const char marker[] = |
| 21 | "</div>\n" |
| 22 | "\n" |
| 23 | "<script type=\"text/javascript\">\n" |
| 24 | "\n" |
| 25 | "var testDivs = [\n"; |
| 26 | |
| 27 | static const char* opStrs[] = { |
| 28 | "kDifference_PathOp", |
| 29 | "kIntersect_PathOp", |
| 30 | "kUnion_PathOp", |
| 31 | "kXor_PathOp", |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 32 | "kReverseDifference_PathOp", |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static const char* opSuffixes[] = { |
| 36 | "d", |
| 37 | "i", |
| 38 | "u", |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 39 | "o", |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static bool gShowPath = false; |
| 43 | static bool gComparePaths = true; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 44 | static bool gComparePathsAssert = true; |
| 45 | static bool gPathStrAssert = true; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 46 | |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 47 | static void showPathContours(SkPath::Iter& iter) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 48 | uint8_t verb; |
| 49 | SkPoint pts[4]; |
| 50 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 51 | switch (verb) { |
| 52 | case SkPath::kMove_Verb: |
| 53 | SkDebugf("path.moveTo(%1.9g,%1.9g);\n", pts[0].fX, pts[0].fY); |
| 54 | continue; |
| 55 | case SkPath::kLine_Verb: |
| 56 | SkDebugf("path.lineTo(%1.9g,%1.9g);\n", pts[1].fX, pts[1].fY); |
| 57 | break; |
| 58 | case SkPath::kQuad_Verb: |
| 59 | SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n", |
| 60 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
| 61 | break; |
| 62 | case SkPath::kCubic_Verb: |
| 63 | SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n", |
| 64 | pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY); |
| 65 | break; |
| 66 | case SkPath::kClose_Verb: |
| 67 | SkDebugf("path.close();\n"); |
| 68 | break; |
| 69 | default: |
| 70 | SkDEBUGFAIL("bad verb"); |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void showPath(const SkPath& path, const char* str) { |
| 77 | SkDebugf("%s\n", !str ? "original:" : str); |
| 78 | showPath(path); |
| 79 | } |
| 80 | |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 81 | const char* fillTypeStr[] = { |
| 82 | "kWinding_FillType", |
| 83 | "kEvenOdd_FillType", |
| 84 | "kInverseWinding_FillType", |
| 85 | "kInverseEvenOdd_FillType" |
| 86 | }; |
| 87 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 88 | void showPath(const SkPath& path) { |
| 89 | SkPath::Iter iter(path, true); |
| 90 | #define SUPPORT_RECT_CONTOUR_DETECTION 0 |
| 91 | #if SUPPORT_RECT_CONTOUR_DETECTION |
| 92 | int rectCount = path.isRectContours() ? path.rectContours(NULL, NULL) : 0; |
| 93 | if (rectCount > 0) { |
| 94 | SkTDArray<SkRect> rects; |
| 95 | SkTDArray<SkPath::Direction> directions; |
| 96 | rects.setCount(rectCount); |
| 97 | directions.setCount(rectCount); |
| 98 | path.rectContours(rects.begin(), directions.begin()); |
| 99 | for (int contour = 0; contour < rectCount; ++contour) { |
| 100 | const SkRect& rect = rects[contour]; |
| 101 | SkDebugf("path.addRect(%1.9g, %1.9g, %1.9g, %1.9g, %s);\n", rect.fLeft, rect.fTop, |
| 102 | rect.fRight, rect.fBottom, directions[contour] == SkPath::kCCW_Direction |
| 103 | ? "SkPath::kCCW_Direction" : "SkPath::kCW_Direction"); |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | #endif |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 108 | SkPath::FillType fillType = path.getFillType(); |
| 109 | SkASSERT(fillType >= SkPath::kWinding_FillType && fillType <= SkPath::kInverseEvenOdd_FillType); |
| 110 | SkDebugf("path.setFillType(%s);\n", fillTypeStr[fillType]); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 111 | iter.setPath(path, true); |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 112 | showPathContours(iter); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void showPathData(const SkPath& path) { |
| 116 | SkPath::Iter iter(path, true); |
| 117 | uint8_t verb; |
| 118 | SkPoint pts[4]; |
| 119 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 120 | switch (verb) { |
| 121 | case SkPath::kMove_Verb: |
| 122 | continue; |
| 123 | case SkPath::kLine_Verb: |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 124 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY, |
| 125 | pts[1].fX, pts[1].fY); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 126 | break; |
| 127 | case SkPath::kQuad_Verb: |
| 128 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 129 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 130 | break; |
| 131 | case SkPath::kCubic_Verb: |
| 132 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 133 | pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, |
| 134 | pts[3].fX, pts[3].fY); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 135 | break; |
| 136 | case SkPath::kClose_Verb: |
| 137 | break; |
| 138 | default: |
| 139 | SkDEBUGFAIL("bad verb"); |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void showOp(const SkPathOp op) { |
| 146 | switch (op) { |
| 147 | case kDifference_PathOp: |
| 148 | SkDebugf("op difference\n"); |
| 149 | break; |
| 150 | case kIntersect_PathOp: |
| 151 | SkDebugf("op intersect\n"); |
| 152 | break; |
| 153 | case kUnion_PathOp: |
| 154 | SkDebugf("op union\n"); |
| 155 | break; |
| 156 | case kXOR_PathOp: |
| 157 | SkDebugf("op xor\n"); |
| 158 | break; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 159 | case kReverseDifference_PathOp: |
| 160 | SkDebugf("op reverse difference\n"); |
| 161 | break; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 162 | default: |
| 163 | SkASSERT(0); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void showPath(const SkPath& path, const char* str, const SkMatrix& scale) { |
| 168 | SkPath scaled; |
| 169 | SkMatrix inverse; |
| 170 | bool success = scale.invert(&inverse); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 171 | if (!success) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 172 | SkASSERT(0); |
| 173 | } |
| 174 | path.transform(inverse, &scaled); |
| 175 | showPath(scaled, str); |
| 176 | } |
| 177 | |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 178 | #if DEBUG_SHOW_TEST_NAME |
| 179 | static char hexorator(int x) { |
| 180 | if (x < 10) { |
| 181 | return x + '0'; |
| 182 | } |
| 183 | x -= 10; |
| 184 | SkASSERT(x < 26); |
| 185 | return x + 'A'; |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) { |
| 190 | #if DEBUG_SHOW_TEST_NAME |
| 191 | state->fSerialNo[0] = hexorator(state->fA); |
| 192 | state->fSerialNo[1] = hexorator(state->fB); |
| 193 | state->fSerialNo[2] = hexorator(state->fC); |
| 194 | state->fSerialNo[3] = hexorator(state->fD); |
| 195 | state->fSerialNo[4] = hexorator(a); |
| 196 | state->fSerialNo[5] = hexorator(b); |
| 197 | state->fSerialNo[6] = hexorator(c); |
| 198 | state->fSerialNo[7] = hexorator(d); |
| 199 | state->fSerialNo[8] = '\0'; |
| 200 | SkDebugf("%s\n", state->fSerialNo); |
| 201 | if (strcmp(state->fSerialNo, state->fKey) == 0) { |
| 202 | SkDebugf("%s\n", state->fPathStr); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 207 | const int bitWidth = 64; |
| 208 | const int bitHeight = 64; |
| 209 | |
| 210 | static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) { |
| 211 | SkRect larger = one.getBounds(); |
| 212 | larger.join(two.getBounds()); |
| 213 | SkScalar largerWidth = larger.width(); |
| 214 | if (largerWidth < 4) { |
| 215 | largerWidth = 4; |
| 216 | } |
| 217 | SkScalar largerHeight = larger.height(); |
| 218 | if (largerHeight < 4) { |
| 219 | largerHeight = 4; |
| 220 | } |
| 221 | SkScalar hScale = (bitWidth - 2) / largerWidth; |
| 222 | SkScalar vScale = (bitHeight - 2) / largerHeight; |
| 223 | scale.reset(); |
| 224 | scale.preScale(hScale, vScale); |
| 225 | } |
| 226 | |
| 227 | static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo, |
| 228 | int& error2x2) { |
| 229 | if (bits.width() == 0) { |
| 230 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 231 | bits.allocPixels(); |
| 232 | } |
| 233 | SkCanvas canvas(bits); |
| 234 | canvas.drawColor(SK_ColorWHITE); |
| 235 | SkPaint paint; |
| 236 | canvas.save(); |
| 237 | const SkRect& bounds1 = scaledOne.getBounds(); |
| 238 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 239 | canvas.drawPath(scaledOne, paint); |
| 240 | canvas.restore(); |
| 241 | canvas.save(); |
| 242 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
| 243 | canvas.drawPath(scaledTwo, paint); |
| 244 | canvas.restore(); |
| 245 | int errors2 = 0; |
| 246 | int errors = 0; |
| 247 | for (int y = 0; y < bitHeight - 1; ++y) { |
| 248 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 249 | uint32_t* addr2 = bits.getAddr32(0, y + 1); |
| 250 | uint32_t* addr3 = bits.getAddr32(bitWidth, y); |
| 251 | uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1); |
| 252 | for (int x = 0; x < bitWidth - 1; ++x) { |
| 253 | // count 2x2 blocks |
| 254 | bool err = addr1[x] != addr3[x]; |
| 255 | if (err) { |
| 256 | errors2 += addr1[x + 1] != addr3[x + 1] |
| 257 | && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1]; |
| 258 | errors++; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | if (errors2 >= 6 || errors > 160) { |
| 263 | SkDebugf("%s errors2=%d errors=%d\n", __FUNCTION__, errors2, errors); |
| 264 | } |
| 265 | error2x2 = errors2; |
| 266 | return errors; |
| 267 | } |
| 268 | |
| 269 | static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne, |
| 270 | SkPath& scaledTwo, int& error2x2) { |
| 271 | SkMatrix scale; |
| 272 | scaleMatrix(one, two, scale); |
| 273 | one.transform(scale, &scaledOne); |
| 274 | two.transform(scale, &scaledTwo); |
| 275 | return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2); |
| 276 | } |
| 277 | |
| 278 | bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) { |
| 279 | if (!drawPaths) { |
| 280 | return true; |
| 281 | } |
| 282 | const SkRect& bounds1 = one.getBounds(); |
| 283 | const SkRect& bounds2 = two.getBounds(); |
| 284 | SkRect larger = bounds1; |
| 285 | larger.join(bounds2); |
| 286 | SkBitmap bits; |
| 287 | char out[256]; |
| 288 | int bitWidth = SkScalarCeil(larger.width()) + 2; |
| 289 | if (bitWidth * 2 + 1 >= (int) sizeof(out)) { |
| 290 | return false; |
| 291 | } |
| 292 | int bitHeight = SkScalarCeil(larger.height()) + 2; |
| 293 | if (bitHeight >= (int) sizeof(out)) { |
| 294 | return false; |
| 295 | } |
| 296 | bits.setConfig(SkBitmap::kARGB_8888_Config, bitWidth * 2, bitHeight); |
| 297 | bits.allocPixels(); |
| 298 | SkCanvas canvas(bits); |
| 299 | canvas.drawColor(SK_ColorWHITE); |
| 300 | SkPaint paint; |
| 301 | canvas.save(); |
| 302 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 303 | canvas.drawPath(one, paint); |
| 304 | canvas.restore(); |
| 305 | canvas.save(); |
| 306 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
| 307 | canvas.drawPath(two, paint); |
| 308 | canvas.restore(); |
| 309 | for (int y = 0; y < bitHeight; ++y) { |
| 310 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 311 | int x; |
| 312 | char* outPtr = out; |
| 313 | for (x = 0; x < bitWidth; ++x) { |
| 314 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 315 | } |
| 316 | *outPtr++ = '|'; |
| 317 | for (x = bitWidth; x < bitWidth * 2; ++x) { |
| 318 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 319 | } |
| 320 | *outPtr++ = '\0'; |
| 321 | SkDebugf("%s\n", out); |
| 322 | } |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | static void showSimplifiedPath(const SkPath& one, const SkPath& two, |
| 327 | const SkPath& scaledOne, const SkPath& scaledTwo) { |
| 328 | showPath(one, "original:"); |
| 329 | showPath(two, "simplified:"); |
| 330 | drawAsciiPaths(scaledOne, scaledTwo, true); |
| 331 | } |
| 332 | |
| 333 | static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& two, |
| 334 | SkBitmap& bitmap) { |
| 335 | int errors2x2; |
| 336 | SkPath scaledOne, scaledTwo; |
| 337 | int errors = pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2); |
| 338 | if (errors2x2 == 0) { |
| 339 | return 0; |
| 340 | } |
| 341 | const int MAX_ERRORS = 9; |
| 342 | if (errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) { |
| 343 | showSimplifiedPath(one, two, scaledOne, scaledTwo); |
| 344 | } |
| 345 | if (errors2x2 > MAX_ERRORS && gComparePathsAssert) { |
| 346 | SkDebugf("%s errors=%d\n", __FUNCTION__, errors); |
| 347 | showSimplifiedPath(one, two, scaledOne, scaledTwo); |
| 348 | REPORTER_ASSERT(reporter, 0); |
| 349 | } |
| 350 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
| 351 | } |
| 352 | |
| 353 | static void showPathOpPath(const SkPath& one, const SkPath& two, const SkPath& a, const SkPath& b, |
| 354 | const SkPath& scaledOne, const SkPath& scaledTwo, const SkPathOp shapeOp, |
| 355 | const SkMatrix& scale) { |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 356 | SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs)); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 357 | showPath(a, "minuend:"); |
| 358 | SkDebugf("op: %s\n", opStrs[shapeOp]); |
| 359 | showPath(b, "subtrahend:"); |
| 360 | // the region often isn't very helpful since it approximates curves with a lot of line-tos |
| 361 | if (0) showPath(scaledOne, "region:", scale); |
| 362 | showPath(two, "op result:"); |
| 363 | drawAsciiPaths(scaledOne, scaledTwo, true); |
| 364 | } |
| 365 | |
| 366 | static int comparePaths(skiatest::Reporter* reporter, const SkPath& one, const SkPath& scaledOne, |
| 367 | const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap, |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 368 | const SkPath& a, const SkPath& b, const SkPathOp shapeOp, |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 369 | const SkMatrix& scale) { |
| 370 | int errors2x2; |
| 371 | int errors = pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2); |
| 372 | if (errors2x2 == 0) { |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 373 | if (gShowPath) { |
| 374 | showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
| 375 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | const int MAX_ERRORS = 8; |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 379 | if (gShowPath || errors2x2 == MAX_ERRORS || errors2x2 == MAX_ERRORS - 1) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 380 | showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
| 381 | } |
| 382 | if (errors2x2 > MAX_ERRORS && gComparePathsAssert) { |
| 383 | SkDebugf("%s errors=%d\n", __FUNCTION__, errors); |
| 384 | showPathOpPath(one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
| 385 | REPORTER_ASSERT(reporter, 0); |
| 386 | } |
| 387 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
| 388 | } |
| 389 | |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 390 | static int testNumber; |
| 391 | static const char* testName; |
| 392 | |
| 393 | static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) { |
| 394 | outFile.writeText(testName); |
| 395 | outFile.writeDecAsText(testNumber); |
| 396 | if (nameSuffix) { |
| 397 | outFile.writeText(nameSuffix); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix, |
| 402 | const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) { |
| 403 | outFile.writeText("<div id=\""); |
| 404 | writeTestName(nameSuffix, outFile); |
| 405 | outFile.writeText("\">\n"); |
| 406 | if (pathPrefix) { |
| 407 | outFile.writeText(pathPrefix); |
| 408 | } |
| 409 | outFile.writeText(pathStr); |
| 410 | outFile.writeText("</div>\n\n"); |
| 411 | |
| 412 | outFile.writeText(marker); |
| 413 | outFile.writeText(" "); |
| 414 | writeTestName(nameSuffix, outFile); |
| 415 | outFile.writeText(",\n\n\n"); |
| 416 | |
| 417 | outFile.writeText("static void "); |
| 418 | writeTestName(nameSuffix, outFile); |
| 419 | outFile.writeText("() {\n SkPath path"); |
| 420 | if (twoPaths) { |
| 421 | outFile.writeText(", pathB"); |
| 422 | } |
| 423 | outFile.writeText(";\n"); |
| 424 | if (pathPrefix) { |
| 425 | outFile.writeText(pathPrefix); |
| 426 | } |
| 427 | outFile.writeText(pathStr); |
| 428 | outFile.writeText(" "); |
| 429 | outFile.writeText(testFunction); |
| 430 | outFile.writeText("\n}\n\n"); |
| 431 | outFile.writeText("static void (*firstTest)() = "); |
| 432 | writeTestName(nameSuffix, outFile); |
| 433 | outFile.writeText(";\n\n"); |
| 434 | |
| 435 | outFile.writeText("static struct {\n"); |
| 436 | outFile.writeText(" void (*fun)();\n"); |
| 437 | outFile.writeText(" const char* str;\n"); |
| 438 | outFile.writeText("} tests[] = {\n"); |
| 439 | outFile.writeText(" TEST("); |
| 440 | writeTestName(nameSuffix, outFile); |
| 441 | outFile.writeText("),\n"); |
| 442 | outFile.flush(); |
| 443 | } |
| 444 | |
| 445 | bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state, |
| 446 | const char* pathStr) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 447 | SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType; |
| 448 | path.setFillType(fillType); |
| 449 | if (gShowPath) { |
| 450 | showPath(path); |
| 451 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 452 | if (!Simplify(path, &out)) { |
| 453 | SkDebugf("%s did not expect failure\n", __FUNCTION__); |
| 454 | REPORTER_ASSERT(state.fReporter, 0); |
| 455 | return false; |
| 456 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 457 | if (!gComparePaths) { |
| 458 | return true; |
| 459 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 460 | int result = comparePaths(state.fReporter, path, out, *state.fBitmap); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 461 | if (result && gPathStrAssert) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 462 | char temp[8192]; |
| 463 | sk_bzero(temp, sizeof(temp)); |
| 464 | SkMemoryWStream stream(temp, sizeof(temp)); |
| 465 | const char* pathPrefix = NULL; |
| 466 | const char* nameSuffix = NULL; |
| 467 | if (fillType == SkPath::kEvenOdd_FillType) { |
| 468 | pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n"; |
| 469 | nameSuffix = "x"; |
| 470 | } |
| 471 | const char testFunction[] = "testSimplifyx(path);"; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 472 | outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 473 | SkDebugf(temp); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 474 | REPORTER_ASSERT(state.fReporter, 0); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 475 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 476 | state.fReporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 477 | return result == 0; |
| 478 | } |
| 479 | |
| 480 | bool testSimplify(skiatest::Reporter* reporter, const SkPath& path) { |
caryclark@google.com | 0361032 | 2013-04-18 15:58:21 +0000 | [diff] [blame] | 481 | #if FORCE_RELEASE == 0 |
| 482 | showPathData(path); |
| 483 | #endif |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 484 | SkPath out; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 485 | if (!Simplify(path, &out)) { |
| 486 | SkDebugf("%s did not expect failure\n", __FUNCTION__); |
| 487 | REPORTER_ASSERT(reporter, 0); |
| 488 | return false; |
| 489 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 490 | SkBitmap bitmap; |
| 491 | int result = comparePaths(reporter, path, out, bitmap); |
| 492 | if (result && gPathStrAssert) { |
| 493 | REPORTER_ASSERT(reporter, 0); |
| 494 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 495 | reporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 496 | return result == 0; |
| 497 | } |
| 498 | |
| 499 | bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
| 500 | const SkPathOp shapeOp) { |
| 501 | #if FORCE_RELEASE == 0 |
| 502 | showPathData(a); |
| 503 | showOp(shapeOp); |
| 504 | showPathData(b); |
| 505 | #endif |
| 506 | SkPath out; |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 507 | if (!Op(a, b, shapeOp, &out) ) { |
| 508 | SkDebugf("%s did not expect failure\n", __FUNCTION__); |
| 509 | REPORTER_ASSERT(reporter, 0); |
| 510 | return false; |
| 511 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 512 | SkPath pathOut, scaledPathOut; |
| 513 | SkRegion rgnA, rgnB, openClip, rgnOut; |
| 514 | openClip.setRect(-16000, -16000, 16000, 16000); |
| 515 | rgnA.setPath(a, openClip); |
| 516 | rgnB.setPath(b, openClip); |
| 517 | rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp); |
| 518 | rgnOut.getBoundaryPath(&pathOut); |
| 519 | |
| 520 | SkMatrix scale; |
| 521 | scaleMatrix(a, b, scale); |
| 522 | SkRegion scaledRgnA, scaledRgnB, scaledRgnOut; |
| 523 | SkPath scaledA, scaledB; |
| 524 | scaledA.addPath(a, scale); |
| 525 | scaledA.setFillType(a.getFillType()); |
| 526 | scaledB.addPath(b, scale); |
| 527 | scaledB.setFillType(b.getFillType()); |
| 528 | scaledRgnA.setPath(scaledA, openClip); |
| 529 | scaledRgnB.setPath(scaledB, openClip); |
| 530 | scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp); |
| 531 | scaledRgnOut.getBoundaryPath(&scaledPathOut); |
| 532 | SkBitmap bitmap; |
| 533 | SkPath scaledOut; |
| 534 | scaledOut.addPath(out, scale); |
| 535 | scaledOut.setFillType(out.getFillType()); |
| 536 | int result = comparePaths(reporter, pathOut, scaledPathOut, out, scaledOut, bitmap, a, b, |
| 537 | shapeOp, scale); |
| 538 | if (result && gPathStrAssert) { |
| 539 | REPORTER_ASSERT(reporter, 0); |
| 540 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 541 | reporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 542 | return result == 0; |
| 543 | } |
| 544 | |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 545 | int initializeTests(skiatest::Reporter* reporter, const char* test) { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 546 | #ifdef SK_DEBUG |
| 547 | gDebugMaxWindSum = 4; |
| 548 | gDebugMaxWindValue = 4; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 549 | #endif |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 550 | testName = test; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 551 | size_t testNameSize = strlen(test); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 552 | SkFILEStream inFile("../../experimental/Intersection/op.htm"); |
| 553 | if (inFile.isValid()) { |
| 554 | SkTDArray<char> inData; |
| 555 | inData.setCount(inFile.getLength()); |
| 556 | size_t inLen = inData.count(); |
| 557 | inFile.read(inData.begin(), inLen); |
| 558 | inFile.setPath(NULL); |
| 559 | char* insert = strstr(inData.begin(), marker); |
| 560 | if (insert) { |
| 561 | insert += sizeof(marker) - 1; |
| 562 | const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1; |
| 563 | testNumber = atoi(numLoc) + 1; |
| 564 | } |
| 565 | } |
commit-bot@chromium.org | 44c661f | 2013-04-22 15:23:14 +0000 | [diff] [blame] | 566 | return reporter->allowThreaded() ? SkThreadPool::kThreadPerCore : 0; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 567 | } |
| 568 | |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 569 | void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) { |
| 570 | const char testFunction[] = "testSimplify(path);"; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 571 | const char* pathPrefix = NULL; |
| 572 | const char* nameSuffix = NULL; |
| 573 | if (pathFillType == SkPath::kEvenOdd_FillType) { |
| 574 | pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n"; |
| 575 | nameSuffix = "x"; |
| 576 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 577 | SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE); |
| 578 | outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 579 | } |
| 580 | |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 581 | void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) { |
| 582 | const char testFunction[] = "testOp(path);"; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 583 | SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes)); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 584 | const char* nameSuffix = opSuffixes[op]; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 585 | SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE); |
| 586 | outputToStream(pathStr, NULL, nameSuffix, testFunction, true, rRamStream); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count, |
| 590 | void (*firstTest)(skiatest::Reporter* ), |
| 591 | void (*stopTest)(skiatest::Reporter* ), bool reverse) { |
| 592 | size_t index; |
| 593 | if (firstTest) { |
| 594 | index = count - 1; |
| 595 | while (index > 0 && tests[index].fun != firstTest) { |
| 596 | --index; |
| 597 | } |
| 598 | #if FORCE_RELEASE == 0 |
| 599 | SkDebugf("<div id=\"%s\">\n", tests[index].str); |
| 600 | SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str); |
| 601 | #endif |
| 602 | (*tests[index].fun)(reporter); |
| 603 | } |
| 604 | index = reverse ? count - 1 : 0; |
| 605 | size_t last = reverse ? 0 : count - 1; |
| 606 | do { |
| 607 | if (tests[index].fun != firstTest) { |
| 608 | #if FORCE_RELEASE == 0 |
| 609 | SkDebugf("<div id=\"%s\">\n", tests[index].str); |
| 610 | SkDebugf(" %s [%s]\n", __FUNCTION__, tests[index].str); |
| 611 | #endif |
| 612 | (*tests[index].fun)(reporter); |
| 613 | } |
| 614 | if (tests[index].fun == stopTest) { |
| 615 | SkDebugf("lastTest\n"); |
| 616 | } |
| 617 | if (index == last) { |
| 618 | break; |
| 619 | } |
| 620 | index += reverse ? -1 : 1; |
| 621 | } while (true); |
| 622 | } |