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