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