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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkMatrix.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkRegion.h" |
| 13 | #include "include/core/SkStream.h" |
| 14 | #include "include/private/SkMutex.h" |
| 15 | #include "include/utils/SkParsePath.h" |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 16 | #include "src/core/SkPathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "tests/PathOpsDebug.h" |
| 18 | #include "tests/PathOpsExtendedTest.h" |
| 19 | #include "tests/PathOpsThreadedCommon.h" |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 20 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 22 | #include <vector> |
| 23 | #include <string> |
| 24 | #include <algorithm> |
| 25 | |
| 26 | std::vector<std::string> gUniqueNames; |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 27 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 28 | #ifdef SK_BUILD_FOR_MAC |
| 29 | #include <sys/sysctl.h> |
| 30 | #endif |
| 31 | |
Cary Clark | 389c557 | 2017-04-14 07:46:07 -0400 | [diff] [blame] | 32 | // std::to_string isn't implemented on android |
| 33 | #include <sstream> |
| 34 | |
| 35 | template <typename T> |
| 36 | std::string std_to_string(T value) |
| 37 | { |
| 38 | std::ostringstream os ; |
| 39 | os << value ; |
| 40 | return os.str() ; |
| 41 | } |
| 42 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 43 | bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result |
| 44 | SkDEBUGPARAMS(bool skipAssert) |
| 45 | SkDEBUGPARAMS(const char* testName)); |
| 46 | |
| 47 | bool SimplifyDebug(const SkPath& one, SkPath* result |
| 48 | SkDEBUGPARAMS(bool skipAssert) |
| 49 | SkDEBUGPARAMS(const char* testName)); |
| 50 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 51 | static const char marker[] = |
| 52 | "</div>\n" |
| 53 | "\n" |
| 54 | "<script type=\"text/javascript\">\n" |
| 55 | "\n" |
| 56 | "var testDivs = [\n"; |
| 57 | |
| 58 | static const char* opStrs[] = { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 59 | "kDifference_SkPathOp", |
| 60 | "kIntersect_SkPathOp", |
| 61 | "kUnion_SkPathOp", |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 62 | "kXOR_PathOp", |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 63 | "kReverseDifference_SkPathOp", |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static const char* opSuffixes[] = { |
| 67 | "d", |
| 68 | "i", |
| 69 | "u", |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 70 | "o", |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 71 | "r", |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 74 | enum class ExpectSuccess { |
| 75 | kNo, |
| 76 | kYes, |
| 77 | kFlaky |
| 78 | }; |
| 79 | |
| 80 | enum class SkipAssert { |
| 81 | kNo, |
| 82 | kYes |
| 83 | }; |
| 84 | |
| 85 | enum class ExpectMatch { |
| 86 | kNo, |
| 87 | kYes, |
| 88 | kFlaky |
| 89 | }; |
| 90 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 91 | void showOp(const SkPathOp op) { |
| 92 | switch (op) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 93 | case kDifference_SkPathOp: |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 94 | SkDebugf("op difference\n"); |
| 95 | break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 96 | case kIntersect_SkPathOp: |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 97 | SkDebugf("op intersect\n"); |
| 98 | break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 99 | case kUnion_SkPathOp: |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 100 | SkDebugf("op union\n"); |
| 101 | break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 102 | case kXOR_SkPathOp: |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 103 | SkDebugf("op xor\n"); |
| 104 | break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 105 | case kReverseDifference_SkPathOp: |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 106 | SkDebugf("op reverse difference\n"); |
| 107 | break; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 108 | default: |
| 109 | SkASSERT(0); |
| 110 | } |
| 111 | } |
| 112 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 113 | const int bitWidth = 64; |
| 114 | const int bitHeight = 64; |
| 115 | |
| 116 | static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) { |
| 117 | SkRect larger = one.getBounds(); |
| 118 | larger.join(two.getBounds()); |
| 119 | SkScalar largerWidth = larger.width(); |
| 120 | if (largerWidth < 4) { |
| 121 | largerWidth = 4; |
| 122 | } |
| 123 | SkScalar largerHeight = larger.height(); |
| 124 | if (largerHeight < 4) { |
| 125 | largerHeight = 4; |
| 126 | } |
| 127 | SkScalar hScale = (bitWidth - 2) / largerWidth; |
| 128 | SkScalar vScale = (bitHeight - 2) / largerHeight; |
| 129 | scale.reset(); |
| 130 | scale.preScale(hScale, vScale); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 131 | larger.fLeft *= hScale; |
| 132 | larger.fRight *= hScale; |
| 133 | larger.fTop *= vScale; |
| 134 | larger.fBottom *= vScale; |
| 135 | SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft |
| 136 | : 16000 < larger.fRight ? 16000 - larger.fRight : 0; |
| 137 | SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop |
| 138 | : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0; |
| 139 | scale.postTranslate(dx, dy); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo, |
| 143 | int& error2x2) { |
| 144 | if (bits.width() == 0) { |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 145 | bits.allocN32Pixels(bitWidth * 2, bitHeight); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 146 | } |
| 147 | SkCanvas canvas(bits); |
| 148 | canvas.drawColor(SK_ColorWHITE); |
| 149 | SkPaint paint; |
| 150 | canvas.save(); |
| 151 | const SkRect& bounds1 = scaledOne.getBounds(); |
| 152 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 153 | canvas.drawPath(scaledOne, paint); |
| 154 | canvas.restore(); |
| 155 | canvas.save(); |
| 156 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
| 157 | canvas.drawPath(scaledTwo, paint); |
| 158 | canvas.restore(); |
| 159 | int errors2 = 0; |
| 160 | int errors = 0; |
| 161 | for (int y = 0; y < bitHeight - 1; ++y) { |
| 162 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 163 | uint32_t* addr2 = bits.getAddr32(0, y + 1); |
| 164 | uint32_t* addr3 = bits.getAddr32(bitWidth, y); |
| 165 | uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1); |
| 166 | for (int x = 0; x < bitWidth - 1; ++x) { |
| 167 | // count 2x2 blocks |
| 168 | bool err = addr1[x] != addr3[x]; |
| 169 | if (err) { |
| 170 | errors2 += addr1[x + 1] != addr3[x + 1] |
| 171 | && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1]; |
| 172 | errors++; |
| 173 | } |
| 174 | } |
| 175 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 176 | error2x2 = errors2; |
| 177 | return errors; |
| 178 | } |
| 179 | |
| 180 | static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne, |
| 181 | SkPath& scaledTwo, int& error2x2) { |
| 182 | SkMatrix scale; |
| 183 | scaleMatrix(one, two, scale); |
| 184 | one.transform(scale, &scaledOne); |
| 185 | two.transform(scale, &scaledTwo); |
| 186 | return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2); |
| 187 | } |
| 188 | |
| 189 | bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) { |
| 190 | if (!drawPaths) { |
| 191 | return true; |
| 192 | } |
| 193 | const SkRect& bounds1 = one.getBounds(); |
| 194 | const SkRect& bounds2 = two.getBounds(); |
| 195 | SkRect larger = bounds1; |
| 196 | larger.join(bounds2); |
| 197 | SkBitmap bits; |
| 198 | char out[256]; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 199 | int bitWidth = SkScalarCeilToInt(larger.width()) + 2; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 200 | if (bitWidth * 2 + 1 >= (int) sizeof(out)) { |
| 201 | return false; |
| 202 | } |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 203 | int bitHeight = SkScalarCeilToInt(larger.height()) + 2; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 204 | if (bitHeight >= (int) sizeof(out)) { |
| 205 | return false; |
| 206 | } |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 207 | bits.allocN32Pixels(bitWidth * 2, bitHeight); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 208 | SkCanvas canvas(bits); |
| 209 | canvas.drawColor(SK_ColorWHITE); |
| 210 | SkPaint paint; |
| 211 | canvas.save(); |
| 212 | canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1); |
| 213 | canvas.drawPath(one, paint); |
| 214 | canvas.restore(); |
| 215 | canvas.save(); |
| 216 | canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1); |
| 217 | canvas.drawPath(two, paint); |
| 218 | canvas.restore(); |
| 219 | for (int y = 0; y < bitHeight; ++y) { |
| 220 | uint32_t* addr1 = bits.getAddr32(0, y); |
| 221 | int x; |
| 222 | char* outPtr = out; |
| 223 | for (x = 0; x < bitWidth; ++x) { |
| 224 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 225 | } |
| 226 | *outPtr++ = '|'; |
| 227 | for (x = bitWidth; x < bitWidth * 2; ++x) { |
| 228 | *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x'; |
| 229 | } |
| 230 | *outPtr++ = '\0'; |
| 231 | SkDebugf("%s\n", out); |
| 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 236 | int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one, |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 237 | const SkPath& two, SkBitmap& bitmap) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 238 | int errors2x2; |
| 239 | SkPath scaledOne, scaledTwo; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 240 | (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 241 | if (errors2x2 == 0) { |
| 242 | return 0; |
| 243 | } |
| 244 | const int MAX_ERRORS = 9; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 245 | return errors2x2 > MAX_ERRORS ? errors2x2 : 0; |
| 246 | } |
| 247 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 248 | static SkTDArray<SkPathOp> gTestOp; |
| 249 | |
Cary Clark | 59d5a0e | 2017-01-23 14:38:52 +0000 | [diff] [blame] | 250 | static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two, |
| 251 | const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo, |
| 252 | const SkPathOp shapeOp, const SkMatrix& scale) { |
| 253 | SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 254 | if (!testName) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 255 | testName = "xOp"; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 256 | } |
caryclark | 2bec26a | 2016-05-26 09:01:47 -0700 | [diff] [blame] | 257 | SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n", |
| 258 | testName, opSuffixes[shapeOp]); |
Cary Clark | 59d5a0e | 2017-01-23 14:38:52 +0000 | [diff] [blame] | 259 | *gTestOp.append() = shapeOp; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 260 | SkDebugf(" SkPath path, pathB;\n"); |
caryclark | 19eb3b2 | 2014-07-18 05:08:14 -0700 | [diff] [blame] | 261 | SkPathOpsDebug::ShowOnePath(a, "path", false); |
| 262 | SkPathOpsDebug::ShowOnePath(b, "pathB", false); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 263 | SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 264 | SkDebugf("}\n"); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 265 | drawAsciiPaths(scaledOne, scaledTwo, true); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 266 | } |
| 267 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 268 | static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one, |
| 269 | const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap, |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 270 | const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale, |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 271 | ExpectMatch expectMatch) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 272 | static SkMutex& compareDebugOut3 = *(new SkMutex); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 273 | int errors2x2; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 274 | const int MAX_ERRORS = 8; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 275 | (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2); |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 276 | if (ExpectMatch::kNo == expectMatch) { |
caryclark | 38a017b | 2015-05-13 10:13:17 -0700 | [diff] [blame] | 277 | if (errors2x2 < MAX_ERRORS) { |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, 0); |
| 279 | } |
| 280 | return 0; |
| 281 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 282 | if (errors2x2 == 0) { |
| 283 | return 0; |
| 284 | } |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 285 | if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 286 | SkAutoMutexExclusive autoM(compareDebugOut3); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 287 | showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale); |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 288 | SkDebugf("\n/*"); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 289 | REPORTER_ASSERT(reporter, 0); |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 290 | SkDebugf(" */\n"); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 291 | } |
caryclark | 38a017b | 2015-05-13 10:13:17 -0700 | [diff] [blame] | 292 | return errors2x2 >= MAX_ERRORS ? errors2x2 : 0; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 293 | } |
| 294 | |
commit-bot@chromium.org | 409774e | 2013-10-02 16:15:44 +0000 | [diff] [blame] | 295 | // Default values for when reporter->verbose() is false. |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 296 | static int testNumber = 55; |
commit-bot@chromium.org | 409774e | 2013-10-02 16:15:44 +0000 | [diff] [blame] | 297 | static const char* testName = "pathOpTest"; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 298 | |
Cary Clark | 389c557 | 2017-04-14 07:46:07 -0400 | [diff] [blame] | 299 | static void appendTestName(const char* nameSuffix, std::string& out) { |
| 300 | out += testName; |
| 301 | out += std_to_string(testNumber); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 302 | ++testNumber; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 303 | if (nameSuffix) { |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 304 | out.append(nameSuffix); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 308 | static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix, |
Cary Clark | 389c557 | 2017-04-14 07:46:07 -0400 | [diff] [blame] | 309 | const char* testFunction, bool twoPaths, std::string& out) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 310 | #if 0 |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 311 | out.append("\n<div id=\""); |
| 312 | appendTestName(nameSuffix, out); |
| 313 | out.append("\">\n"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 314 | if (pathPrefix) { |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 315 | out.append(pathPrefix); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 316 | } |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 317 | out.append(pathStr); |
| 318 | out.append("</div>\n\n"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 319 | |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 320 | out.append(marker); |
| 321 | out.append(" "); |
| 322 | appendTestName(nameSuffix, out); |
| 323 | out.append(",\n\n\n"); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 324 | #endif |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 325 | out.append("static void "); |
| 326 | appendTestName(nameSuffix, out); |
| 327 | out.append("(skiatest::Reporter* reporter) {\n SkPath path"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 328 | if (twoPaths) { |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 329 | out.append(", pathB"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 330 | } |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 331 | out.append(";\n"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 332 | if (pathPrefix) { |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 333 | out.append(pathPrefix); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 334 | } |
Cary Clark | 389c557 | 2017-04-14 07:46:07 -0400 | [diff] [blame] | 335 | out += pathStr; |
| 336 | out += " "; |
| 337 | out += testFunction; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 338 | #if 0 |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 339 | out.append("static void (*firstTest)() = "); |
| 340 | appendTestName(nameSuffix, out); |
| 341 | out.append(";\n\n"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 342 | |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 343 | out.append("static struct {\n"); |
| 344 | out.append(" void (*fun)();\n"); |
| 345 | out.append(" const char* str;\n"); |
| 346 | out.append("} tests[] = {\n"); |
| 347 | out.append(" TEST("); |
| 348 | appendTestName(nameSuffix, out); |
| 349 | out.append("),\n"); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 350 | #endif |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Cary Clark | da6289c | 2018-08-27 16:10:28 -0400 | [diff] [blame] | 353 | void markTestFlakyForPathKit() { |
| 354 | if (PathOpsDebug::gJson) { |
| 355 | SkASSERT(!PathOpsDebug::gMarkJsonFlaky); |
| 356 | PathOpsDebug::gMarkJsonFlaky = true; |
| 357 | } |
| 358 | } |
| 359 | |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 360 | bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state, |
| 361 | const char* pathStr) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 362 | static SkMutex& simplifyDebugOut = *(new SkMutex); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 363 | SkPathFillType fillType = useXor ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 364 | path.setFillType(fillType); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 365 | state.fReporter->bumpTestCount(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 366 | if (!Simplify(path, &out)) { |
| 367 | SkDebugf("%s did not expect failure\n", __FUNCTION__); |
| 368 | REPORTER_ASSERT(state.fReporter, 0); |
| 369 | return false; |
| 370 | } |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 371 | if (!state.fReporter->verbose()) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 372 | return true; |
| 373 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 374 | int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 375 | if (result) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 376 | SkAutoMutexExclusive autoM(simplifyDebugOut); |
Cary Clark | 389c557 | 2017-04-14 07:46:07 -0400 | [diff] [blame] | 377 | std::string str; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 378 | const char* pathPrefix = nullptr; |
| 379 | const char* nameSuffix = nullptr; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 380 | if (fillType == SkPathFillType::kEvenOdd) { |
| 381 | pathPrefix = " path.setFillType(SkPathFillType::kEvenOdd);\n"; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 382 | nameSuffix = "x"; |
| 383 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 384 | const char testFunction[] = "testSimplify(reporter, path);"; |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 385 | appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str); |
Mike Reed | ff80c2a | 2017-01-07 11:16:28 -0500 | [diff] [blame] | 386 | SkDebugf("%s", str.c_str()); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 387 | REPORTER_ASSERT(state.fReporter, 0); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 388 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 389 | state.fReporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 390 | return result == 0; |
| 391 | } |
| 392 | |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 393 | static void json_status(ExpectSuccess expectSuccess, ExpectMatch expectMatch, bool opSucceeded) { |
| 394 | fprintf(PathOpsDebug::gOut, " \"expectSuccess\": \"%s\",\n", |
| 395 | ExpectSuccess::kNo == expectSuccess ? "no" : |
| 396 | ExpectSuccess::kYes == expectSuccess ? "yes" : "flaky"); |
Cary Clark | da6289c | 2018-08-27 16:10:28 -0400 | [diff] [blame] | 397 | if (PathOpsDebug::gMarkJsonFlaky) { |
| 398 | expectMatch = ExpectMatch::kFlaky; |
| 399 | PathOpsDebug::gMarkJsonFlaky = false; |
| 400 | } |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 401 | fprintf(PathOpsDebug::gOut, " \"expectMatch\": \"%s\",\n", |
| 402 | ExpectMatch::kNo == expectMatch ? "no" : |
| 403 | ExpectMatch::kYes == expectMatch ? "yes" : "flaky"); |
| 404 | fprintf(PathOpsDebug::gOut, " \"succeeded\": %s,\n", opSucceeded ? "true" : "false"); |
| 405 | } |
| 406 | |
| 407 | static void json_path_out(const SkPath& path, const char* pathName, const char* fillTypeName, |
| 408 | bool lastField) { |
| 409 | char const * const gFillTypeStrs[] = { |
| 410 | "Winding", |
| 411 | "EvenOdd", |
| 412 | "InverseWinding", |
| 413 | "InverseEvenOdd", |
| 414 | }; |
Cary Clark | 9c9611f | 2018-08-08 13:17:25 -0400 | [diff] [blame] | 415 | if (PathOpsDebug::gOutputSVG) { |
| 416 | SkString svg; |
| 417 | SkParsePath::ToSVGString(path, &svg); |
| 418 | fprintf(PathOpsDebug::gOut, " \"%s\": \"%s\",\n", pathName, svg.c_str()); |
| 419 | } else { |
Cary Clark | 9c9611f | 2018-08-08 13:17:25 -0400 | [diff] [blame] | 420 | // MOVE, LINE, QUAD, CONIC, CUBIC, CLOSE |
| 421 | const int verbConst[] = { 0, 1, 2, 3, 4, 5 }; |
| 422 | const int pointIndex[] = { 0, 1, 1, 1, 1, 0 }; |
| 423 | const int pointCount[] = { 1, 2, 3, 3, 4, 0 }; |
| 424 | fprintf(PathOpsDebug::gOut, " \"%s\": [", pathName); |
| 425 | bool first = true; |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 426 | for (auto [verb, points, w] : SkPathPriv::Iterate(path)) { |
Cary Clark | 9c9611f | 2018-08-08 13:17:25 -0400 | [diff] [blame] | 427 | if (first) { |
| 428 | first = false; |
| 429 | } else { |
| 430 | fprintf(PathOpsDebug::gOut, ",\n "); |
| 431 | } |
| 432 | int verbIndex = (int) verb; |
| 433 | fprintf(PathOpsDebug::gOut, "[%d", verbConst[verbIndex]); |
| 434 | for (int i = pointIndex[verbIndex]; i < pointCount[verbIndex]; ++i) { |
| 435 | fprintf(PathOpsDebug::gOut, ", \"0x%08x\", \"0x%08x\"", |
| 436 | SkFloat2Bits(points[i].fX), SkFloat2Bits(points[i].fY)); |
| 437 | } |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 438 | if (SkPathVerb::kConic == verb) { |
| 439 | fprintf(PathOpsDebug::gOut, ", \"0x%08x\"", SkFloat2Bits(*w)); |
Cary Clark | 9c9611f | 2018-08-08 13:17:25 -0400 | [diff] [blame] | 440 | } |
| 441 | fprintf(PathOpsDebug::gOut, "]"); |
Chris Dalton | 957189b | 2020-05-07 12:47:26 -0600 | [diff] [blame] | 442 | } |
Cary Clark | 9c9611f | 2018-08-08 13:17:25 -0400 | [diff] [blame] | 443 | fprintf(PathOpsDebug::gOut, "],\n"); |
| 444 | } |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 445 | fprintf(PathOpsDebug::gOut, " \"fillType%s\": \"k%s_FillType\"%s", fillTypeName, |
| 446 | gFillTypeStrs[(int) path.getFillType()], lastField ? "\n}" : ",\n"); |
| 447 | } |
| 448 | |
| 449 | static bool check_for_duplicate_names(const char* testName) { |
| 450 | if (PathOpsDebug::gCheckForDuplicateNames) { |
| 451 | if (gUniqueNames.end() != std::find(gUniqueNames.begin(), gUniqueNames.end(), |
| 452 | std::string(testName))) { |
| 453 | SkDebugf(""); // convenience for setting breakpoints |
| 454 | } |
| 455 | gUniqueNames.push_back(std::string(testName)); |
| 456 | return true; |
| 457 | } |
| 458 | return false; |
| 459 | } |
| 460 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 461 | static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename, |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 462 | ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) { |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 463 | if (PathOpsDebug::gJson) { |
| 464 | if (check_for_duplicate_names(filename)) { |
| 465 | return true; |
| 466 | } |
| 467 | if (!PathOpsDebug::gOutFirst) { |
| 468 | fprintf(PathOpsDebug::gOut, ",\n"); |
| 469 | } |
| 470 | PathOpsDebug::gOutFirst = false; |
| 471 | fprintf(PathOpsDebug::gOut, "\"%s\": {\n", filename); |
| 472 | json_path_out(path, "path", "", false); |
| 473 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 474 | SkPath out; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 475 | if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert) |
| 476 | SkDEBUGPARAMS(testName))) { |
| 477 | if (ExpectSuccess::kYes == expectSuccess) { |
| 478 | SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename); |
| 479 | REPORTER_ASSERT(reporter, 0); |
| 480 | } |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 481 | if (PathOpsDebug::gJson) { |
| 482 | json_status(expectSuccess, expectMatch, false); |
| 483 | fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}"); |
| 484 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 485 | return false; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 486 | } else { |
| 487 | if (ExpectSuccess::kNo == expectSuccess) { |
| 488 | SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename); |
| 489 | REPORTER_ASSERT(reporter, 0); |
| 490 | } |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 491 | if (PathOpsDebug::gJson) { |
| 492 | json_status(expectSuccess, expectMatch, true); |
| 493 | json_path_out(out, "out", "Out", true); |
| 494 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 495 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 496 | SkBitmap bitmap; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 497 | int errors = comparePaths(reporter, filename, path, out, bitmap); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 498 | if (ExpectMatch::kNo == expectMatch) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 499 | if (!errors) { |
| 500 | SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename); |
| 501 | REPORTER_ASSERT(reporter, 0); |
| 502 | return false; |
| 503 | } |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 504 | } else if (ExpectMatch::kYes == expectMatch && errors) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 505 | REPORTER_ASSERT(reporter, 0); |
| 506 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 507 | reporter->bumpTestCount(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 508 | return errors == 0; |
| 509 | } |
| 510 | |
| 511 | bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 512 | return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo, |
| 513 | ExpectMatch::kYes); |
| 514 | } |
| 515 | |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 516 | bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) { |
| 517 | return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes, |
| 518 | ExpectMatch::kFlaky); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename, |
| 522 | bool checkFail) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 523 | return inner_simplify(reporter, path, filename, checkFail ? |
| 524 | ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 527 | bool testSimplifyFail(skiatest::Reporter* reporter, const SkPath& path, const char* filename) { |
| 528 | return inner_simplify(reporter, path, filename, |
| 529 | ExpectSuccess::kNo, SkipAssert::kYes, ExpectMatch::kNo); |
| 530 | } |
| 531 | |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 532 | static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 533 | const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess, |
| 534 | SkipAssert skipAssert, ExpectMatch expectMatch) { |
Cary Clark | f394912 | 2018-08-07 16:38:21 -0400 | [diff] [blame] | 535 | if (PathOpsDebug::gJson) { |
Cary Clark | 4533f3d | 2018-08-08 09:48:09 -0400 | [diff] [blame] | 536 | if (check_for_duplicate_names(testName)) { |
| 537 | return true; |
| 538 | } |
Cary Clark | f394912 | 2018-08-07 16:38:21 -0400 | [diff] [blame] | 539 | if (!PathOpsDebug::gOutFirst) { |
| 540 | fprintf(PathOpsDebug::gOut, ",\n"); |
| 541 | } |
| 542 | PathOpsDebug::gOutFirst = false; |
| 543 | fprintf(PathOpsDebug::gOut, "\"%s\": {\n", testName); |
| 544 | json_path_out(a, "p1", "1", false); |
| 545 | json_path_out(b, "p2", "2", false); |
| 546 | fprintf(PathOpsDebug::gOut, " \"op\": \"%s\",\n", opStrs[shapeOp]); |
| 547 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 548 | SkPath out; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 549 | if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert) |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 550 | SkDEBUGPARAMS(testName))) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 551 | if (ExpectSuccess::kYes == expectSuccess) { |
| 552 | SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName); |
caryclark | 3f0753d | 2016-06-28 09:23:57 -0700 | [diff] [blame] | 553 | REPORTER_ASSERT(reporter, 0); |
| 554 | } |
Cary Clark | f394912 | 2018-08-07 16:38:21 -0400 | [diff] [blame] | 555 | if (PathOpsDebug::gJson) { |
| 556 | json_status(expectSuccess, expectMatch, false); |
| 557 | fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}"); |
| 558 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 559 | return false; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 560 | } else { |
| 561 | if (ExpectSuccess::kNo == expectSuccess) { |
| 562 | SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName); |
| 563 | REPORTER_ASSERT(reporter, 0); |
| 564 | } |
Cary Clark | f394912 | 2018-08-07 16:38:21 -0400 | [diff] [blame] | 565 | if (PathOpsDebug::gJson) { |
| 566 | json_status(expectSuccess, expectMatch, true); |
| 567 | json_path_out(out, "out", "Out", true); |
| 568 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 569 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 570 | if (!reporter->verbose()) { |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 573 | SkPath pathOut, scaledPathOut; |
| 574 | SkRegion rgnA, rgnB, openClip, rgnOut; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 575 | openClip.setRect({-16000, -16000, 16000, 16000}); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 576 | rgnA.setPath(a, openClip); |
| 577 | rgnB.setPath(b, openClip); |
| 578 | rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp); |
| 579 | rgnOut.getBoundaryPath(&pathOut); |
| 580 | |
| 581 | SkMatrix scale; |
| 582 | scaleMatrix(a, b, scale); |
| 583 | SkRegion scaledRgnA, scaledRgnB, scaledRgnOut; |
| 584 | SkPath scaledA, scaledB; |
| 585 | scaledA.addPath(a, scale); |
| 586 | scaledA.setFillType(a.getFillType()); |
| 587 | scaledB.addPath(b, scale); |
| 588 | scaledB.setFillType(b.getFillType()); |
| 589 | scaledRgnA.setPath(scaledA, openClip); |
| 590 | scaledRgnB.setPath(scaledB, openClip); |
| 591 | scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp); |
| 592 | scaledRgnOut.getBoundaryPath(&scaledPathOut); |
| 593 | SkBitmap bitmap; |
| 594 | SkPath scaledOut; |
| 595 | scaledOut.addPath(out, scale); |
| 596 | scaledOut.setFillType(out.getFillType()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 597 | int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap, |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 598 | a, b, shapeOp, scale, expectMatch); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 599 | reporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 600 | return result == 0; |
| 601 | } |
| 602 | |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 603 | bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 604 | const SkPathOp shapeOp, const char* testName) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 605 | return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo, |
| 606 | ExpectMatch::kYes); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
| 610 | const SkPathOp shapeOp, const char* testName, bool checkFail) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 611 | return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ? |
| 612 | ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 613 | } |
| 614 | |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 615 | bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
caryclark | d5b9173 | 2016-08-09 05:04:29 -0700 | [diff] [blame] | 616 | const SkPathOp shapeOp, const char* testName) { |
| 617 | return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes, |
| 618 | ExpectMatch::kFlaky); |
| 619 | } |
| 620 | |
Cary Clark | 59d5a0e | 2017-01-23 14:38:52 +0000 | [diff] [blame] | 621 | bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b, |
| 622 | const SkPathOp shapeOp, const char* testName) { |
Cary Clark | 59d5a0e | 2017-01-23 14:38:52 +0000 | [diff] [blame] | 623 | SkPath orig; |
| 624 | orig.lineTo(54, 43); |
| 625 | SkPath out = orig; |
| 626 | if (Op(a, b, shapeOp, &out) ) { |
| 627 | SkDebugf("%s test is expected to fail\n", __FUNCTION__); |
| 628 | REPORTER_ASSERT(reporter, 0); |
| 629 | return false; |
| 630 | } |
| 631 | SkASSERT(out == orig); |
| 632 | return true; |
| 633 | } |
| 634 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 635 | void initializeTests(skiatest::Reporter* reporter, const char* test) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 636 | static SkMutex& mu = *(new SkMutex); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 637 | if (reporter->verbose()) { |
Herb Derby | 9c71e7b | 2019-06-17 14:40:42 -0400 | [diff] [blame] | 638 | SkAutoMutexExclusive lock(mu); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 639 | testName = test; |
| 640 | size_t testNameSize = strlen(test); |
| 641 | SkFILEStream inFile("../../experimental/Intersection/op.htm"); |
| 642 | if (inFile.isValid()) { |
| 643 | SkTDArray<char> inData; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 644 | inData.setCount((int) inFile.getLength()); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 645 | size_t inLen = inData.count(); |
| 646 | inFile.read(inData.begin(), inLen); |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 647 | inFile.close(); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 648 | char* insert = strstr(inData.begin(), marker); |
| 649 | if (insert) { |
| 650 | insert += sizeof(marker) - 1; |
| 651 | const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1; |
| 652 | testNumber = atoi(numLoc) + 1; |
| 653 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 658 | void PathOpsThreadState::outputProgress(const char* pathStr, SkPathFillType pathFillType) { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 659 | const char testFunction[] = "testSimplify(path);"; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 660 | const char* pathPrefix = nullptr; |
| 661 | const char* nameSuffix = nullptr; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 662 | if (pathFillType == SkPathFillType::kEvenOdd) { |
| 663 | pathPrefix = " path.setFillType(SkPathFillType::kEvenOdd);\n"; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 664 | nameSuffix = "x"; |
| 665 | } |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 666 | appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Mike Reed | ff80c2a | 2017-01-07 11:16:28 -0500 | [diff] [blame] | 669 | void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 670 | const char testFunction[] = "testOp(path);"; |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 671 | SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes)); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 672 | const char* nameSuffix = opSuffixes[op]; |
Ben Wagner | 561c1b0 | 2017-01-09 15:54:34 -0500 | [diff] [blame] | 673 | appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count, |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 677 | void (*firstTest)(skiatest::Reporter* , const char* filename), |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 678 | void (*skipTest)(skiatest::Reporter* , const char* filename), |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 679 | void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 680 | size_t index; |
| 681 | if (firstTest) { |
| 682 | index = count - 1; |
| 683 | while (index > 0 && tests[index].fun != firstTest) { |
| 684 | --index; |
| 685 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 686 | (*tests[index].fun)(reporter, tests[index].str); |
| 687 | if (tests[index].fun == stopTest) { |
| 688 | return; |
| 689 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 690 | } |
| 691 | index = reverse ? count - 1 : 0; |
| 692 | size_t last = reverse ? 0 : count - 1; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 693 | bool foundSkip = !skipTest; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 694 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 695 | if (tests[index].fun == skipTest) { |
| 696 | foundSkip = true; |
| 697 | } |
| 698 | if (foundSkip && tests[index].fun != firstTest) { |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 699 | (*tests[index].fun)(reporter, tests[index].str); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 700 | } |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 701 | if (tests[index].fun == stopTest || index == last) { |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 702 | break; |
| 703 | } |
| 704 | index += reverse ? -1 : 1; |
| 705 | } while (true); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 706 | } |