blob: 2ceb5fa9c85a11b07469d00173b07facf9d4937b [file] [log] [blame]
caryclark@google.com818b0cc2013-04-08 11:50:46 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Dalton957189b2020-05-07 12:47:26 -060016#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "tests/PathOpsDebug.h"
18#include "tests/PathOpsExtendedTest.h"
19#include "tests/PathOpsThreadedCommon.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000020
bungeman60e0fee2015-08-26 05:15:46 -070021#include <stdlib.h>
Cary Clark4533f3d2018-08-08 09:48:09 -040022#include <vector>
23#include <string>
24#include <algorithm>
25
26std::vector<std::string> gUniqueNames;
bungeman60e0fee2015-08-26 05:15:46 -070027
caryclark@google.com818b0cc2013-04-08 11:50:46 +000028#ifdef SK_BUILD_FOR_MAC
29#include <sys/sysctl.h>
30#endif
31
Cary Clark389c5572017-04-14 07:46:07 -040032// std::to_string isn't implemented on android
33#include <sstream>
34
35template <typename T>
36std::string std_to_string(T value)
37{
38 std::ostringstream os ;
39 os << value ;
40 return os.str() ;
41}
42
caryclark55888e42016-07-18 10:01:36 -070043bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
44 SkDEBUGPARAMS(bool skipAssert)
45 SkDEBUGPARAMS(const char* testName));
46
47bool SimplifyDebug(const SkPath& one, SkPath* result
48 SkDEBUGPARAMS(bool skipAssert)
49 SkDEBUGPARAMS(const char* testName));
50
caryclark@google.com818b0cc2013-04-08 11:50:46 +000051static const char marker[] =
52 "</div>\n"
53 "\n"
54 "<script type=\"text/javascript\">\n"
55 "\n"
56 "var testDivs = [\n";
57
58static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070059 "kDifference_SkPathOp",
60 "kIntersect_SkPathOp",
61 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070062 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070063 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000064};
65
66static const char* opSuffixes[] = {
67 "d",
68 "i",
69 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000070 "o",
caryclark55888e42016-07-18 10:01:36 -070071 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000072};
73
caryclarkd5b91732016-08-09 05:04:29 -070074enum class ExpectSuccess {
75 kNo,
76 kYes,
77 kFlaky
78};
79
80enum class SkipAssert {
81 kNo,
82 kYes
83};
84
85enum class ExpectMatch {
86 kNo,
87 kYes,
88 kFlaky
89};
90
caryclark@google.com818b0cc2013-04-08 11:50:46 +000091void showOp(const SkPathOp op) {
92 switch (op) {
caryclark54359292015-03-26 07:52:43 -070093 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +000094 SkDebugf("op difference\n");
95 break;
caryclark54359292015-03-26 07:52:43 -070096 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +000097 SkDebugf("op intersect\n");
98 break;
caryclark54359292015-03-26 07:52:43 -070099 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000100 SkDebugf("op union\n");
101 break;
caryclark54359292015-03-26 07:52:43 -0700102 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000103 SkDebugf("op xor\n");
104 break;
caryclark54359292015-03-26 07:52:43 -0700105 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000106 SkDebugf("op reverse difference\n");
107 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000108 default:
109 SkASSERT(0);
110 }
111}
112
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000113const int bitWidth = 64;
114const int bitHeight = 64;
115
116static 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);
caryclark26ad22a2015-10-16 09:03:38 -0700131 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.com818b0cc2013-04-08 11:50:46 +0000140}
141
142static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
143 int& error2x2) {
144 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000145 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000146 }
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.com818b0cc2013-04-08 11:50:46 +0000176 error2x2 = errors2;
177 return errors;
178}
179
180static 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
189bool 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.come1ca7052013-12-17 19:22:07 +0000199 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000200 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
201 return false;
202 }
reed@google.come1ca7052013-12-17 19:22:07 +0000203 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000204 if (bitHeight >= (int) sizeof(out)) {
205 return false;
206 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000207 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000208 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
caryclark54359292015-03-26 07:52:43 -0700236int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000237 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000238 int errors2x2;
239 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000240 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000241 if (errors2x2 == 0) {
242 return 0;
243 }
244 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000245 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
246}
247
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000248static SkTDArray<SkPathOp> gTestOp;
249
Cary Clark59d5a0e2017-01-23 14:38:52 +0000250static 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.org4431e772014-04-14 17:08:59 +0000254 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700255 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000256 }
caryclark2bec26a2016-05-26 09:01:47 -0700257 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
258 testName, opSuffixes[shapeOp]);
Cary Clark59d5a0e2017-01-23 14:38:52 +0000259 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000260 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700261 SkPathOpsDebug::ShowOnePath(a, "path", false);
262 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000263 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000264 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700265 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000266}
267
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000268static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
269 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800270 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700271 ExpectMatch expectMatch) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400272 static SkMutex& compareDebugOut3 = *(new SkMutex);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000273 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800274 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000275 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700276 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700277 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800278 REPORTER_ASSERT(reporter, 0);
279 }
280 return 0;
281 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000282 if (errors2x2 == 0) {
283 return 0;
284 }
caryclarkd5b91732016-08-09 05:04:29 -0700285 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400286 SkAutoMutexExclusive autoM(compareDebugOut3);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000287 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700288 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000289 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700290 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000291 }
caryclark38a017b2015-05-13 10:13:17 -0700292 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000293}
294
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000295// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000296static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000297static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000298
Cary Clark389c5572017-04-14 07:46:07 -0400299static void appendTestName(const char* nameSuffix, std::string& out) {
300 out += testName;
301 out += std_to_string(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000302 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000303 if (nameSuffix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500304 out.append(nameSuffix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000305 }
306}
307
Ben Wagner561c1b02017-01-09 15:54:34 -0500308static void appendTest(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
Cary Clark389c5572017-04-14 07:46:07 -0400309 const char* testFunction, bool twoPaths, std::string& out) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000310#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500311 out.append("\n<div id=\"");
312 appendTestName(nameSuffix, out);
313 out.append("\">\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000314 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500315 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000316 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500317 out.append(pathStr);
318 out.append("</div>\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000319
Ben Wagner561c1b02017-01-09 15:54:34 -0500320 out.append(marker);
321 out.append(" ");
322 appendTestName(nameSuffix, out);
323 out.append(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000324#endif
Ben Wagner561c1b02017-01-09 15:54:34 -0500325 out.append("static void ");
326 appendTestName(nameSuffix, out);
327 out.append("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000328 if (twoPaths) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500329 out.append(", pathB");
caryclark@google.com66089e42013-04-10 15:55:37 +0000330 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500331 out.append(";\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000332 if (pathPrefix) {
Ben Wagner561c1b02017-01-09 15:54:34 -0500333 out.append(pathPrefix);
caryclark@google.com66089e42013-04-10 15:55:37 +0000334 }
Cary Clark389c5572017-04-14 07:46:07 -0400335 out += pathStr;
336 out += " ";
337 out += testFunction;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000338#if 0
Ben Wagner561c1b02017-01-09 15:54:34 -0500339 out.append("static void (*firstTest)() = ");
340 appendTestName(nameSuffix, out);
341 out.append(";\n\n");
caryclark@google.com66089e42013-04-10 15:55:37 +0000342
Ben Wagner561c1b02017-01-09 15:54:34 -0500343 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.org4431e772014-04-14 17:08:59 +0000350#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000351}
352
Cary Clarkda6289c2018-08-27 16:10:28 -0400353void markTestFlakyForPathKit() {
354 if (PathOpsDebug::gJson) {
355 SkASSERT(!PathOpsDebug::gMarkJsonFlaky);
356 PathOpsDebug::gMarkJsonFlaky = true;
357 }
358}
359
caryclark@google.com66089e42013-04-10 15:55:37 +0000360bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
361 const char* pathStr) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400362 static SkMutex& simplifyDebugOut = *(new SkMutex);
Mike Reed7d34dc72019-11-26 12:17:17 -0500363 SkPathFillType fillType = useXor ? SkPathFillType::kEvenOdd : SkPathFillType::kWinding;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000364 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700365 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000366 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.com8d0a5242013-07-16 16:11:16 +0000371 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000372 return true;
373 }
halcanary96fcdcc2015-08-27 07:41:13 -0700374 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700375 if (result) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400376 SkAutoMutexExclusive autoM(simplifyDebugOut);
Cary Clark389c5572017-04-14 07:46:07 -0400377 std::string str;
halcanary96fcdcc2015-08-27 07:41:13 -0700378 const char* pathPrefix = nullptr;
379 const char* nameSuffix = nullptr;
Mike Reed7d34dc72019-11-26 12:17:17 -0500380 if (fillType == SkPathFillType::kEvenOdd) {
381 pathPrefix = " path.setFillType(SkPathFillType::kEvenOdd);\n";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000382 nameSuffix = "x";
383 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000384 const char testFunction[] = "testSimplify(reporter, path);";
Ben Wagner561c1b02017-01-09 15:54:34 -0500385 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, str);
Mike Reedff80c2a2017-01-07 11:16:28 -0500386 SkDebugf("%s", str.c_str());
caryclark@google.com66089e42013-04-10 15:55:37 +0000387 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000388 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000389 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000390 return result == 0;
391}
392
Cary Clark4533f3d2018-08-08 09:48:09 -0400393static 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 Clarkda6289c2018-08-27 16:10:28 -0400397 if (PathOpsDebug::gMarkJsonFlaky) {
398 expectMatch = ExpectMatch::kFlaky;
399 PathOpsDebug::gMarkJsonFlaky = false;
400 }
Cary Clark4533f3d2018-08-08 09:48:09 -0400401 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
407static 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 Clark9c9611f2018-08-08 13:17:25 -0400415 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 Clark9c9611f2018-08-08 13:17:25 -0400420 // 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 Dalton957189b2020-05-07 12:47:26 -0600426 for (auto [verb, points, w] : SkPathPriv::Iterate(path)) {
Cary Clark9c9611f2018-08-08 13:17:25 -0400427 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 Dalton957189b2020-05-07 12:47:26 -0600438 if (SkPathVerb::kConic == verb) {
439 fprintf(PathOpsDebug::gOut, ", \"0x%08x\"", SkFloat2Bits(*w));
Cary Clark9c9611f2018-08-08 13:17:25 -0400440 }
441 fprintf(PathOpsDebug::gOut, "]");
Chris Dalton957189b2020-05-07 12:47:26 -0600442 }
Cary Clark9c9611f2018-08-08 13:17:25 -0400443 fprintf(PathOpsDebug::gOut, "],\n");
444 }
Cary Clark4533f3d2018-08-08 09:48:09 -0400445 fprintf(PathOpsDebug::gOut, " \"fillType%s\": \"k%s_FillType\"%s", fillTypeName,
446 gFillTypeStrs[(int) path.getFillType()], lastField ? "\n}" : ",\n");
447}
448
449static 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
caryclark54359292015-03-26 07:52:43 -0700461static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700462 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
Cary Clark4533f3d2018-08-08 09:48:09 -0400463 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.com818b0cc2013-04-08 11:50:46 +0000474 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700475 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 Clark4533f3d2018-08-08 09:48:09 -0400481 if (PathOpsDebug::gJson) {
482 json_status(expectSuccess, expectMatch, false);
483 fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}");
484 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000485 return false;
caryclark55888e42016-07-18 10:01:36 -0700486 } else {
487 if (ExpectSuccess::kNo == expectSuccess) {
488 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
489 REPORTER_ASSERT(reporter, 0);
490 }
Cary Clark4533f3d2018-08-08 09:48:09 -0400491 if (PathOpsDebug::gJson) {
492 json_status(expectSuccess, expectMatch, true);
493 json_path_out(out, "out", "Out", true);
494 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000495 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000496 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700497 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700498 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700499 if (!errors) {
500 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
501 REPORTER_ASSERT(reporter, 0);
502 return false;
503 }
caryclarkd5b91732016-08-09 05:04:29 -0700504 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000505 REPORTER_ASSERT(reporter, 0);
506 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000507 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700508 return errors == 0;
509}
510
511bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700512 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
513 ExpectMatch::kYes);
514}
515
caryclark30b9fdd2016-08-31 14:36:29 -0700516bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
517 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
518 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700519}
520
521bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
522 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700523 return inner_simplify(reporter, path, filename, checkFail ?
524 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000525}
526
Cary Clark2587f412018-07-24 12:40:10 -0400527bool 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.com8d0a5242013-07-16 16:11:16 +0000532static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700533 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
534 SkipAssert skipAssert, ExpectMatch expectMatch) {
Cary Clarkf3949122018-08-07 16:38:21 -0400535 if (PathOpsDebug::gJson) {
Cary Clark4533f3d2018-08-08 09:48:09 -0400536 if (check_for_duplicate_names(testName)) {
537 return true;
538 }
Cary Clarkf3949122018-08-07 16:38:21 -0400539 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.com818b0cc2013-04-08 11:50:46 +0000548 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700549 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700550 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700551 if (ExpectSuccess::kYes == expectSuccess) {
552 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700553 REPORTER_ASSERT(reporter, 0);
554 }
Cary Clarkf3949122018-08-07 16:38:21 -0400555 if (PathOpsDebug::gJson) {
556 json_status(expectSuccess, expectMatch, false);
557 fprintf(PathOpsDebug::gOut, " \"out\": \"\"\n}");
558 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000559 return false;
caryclark55888e42016-07-18 10:01:36 -0700560 } else {
561 if (ExpectSuccess::kNo == expectSuccess) {
562 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
563 REPORTER_ASSERT(reporter, 0);
564 }
Cary Clarkf3949122018-08-07 16:38:21 -0400565 if (PathOpsDebug::gJson) {
566 json_status(expectSuccess, expectMatch, true);
567 json_path_out(out, "out", "Out", true);
568 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000569 }
caryclark54359292015-03-26 07:52:43 -0700570 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000571 return true;
572 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000573 SkPath pathOut, scaledPathOut;
574 SkRegion rgnA, rgnB, openClip, rgnOut;
Mike Reed92b33352019-08-24 19:39:13 -0400575 openClip.setRect({-16000, -16000, 16000, 16000});
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000576 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.org4431e772014-04-14 17:08:59 +0000597 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700598 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000599 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000600 return result == 0;
601}
602
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000603bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800604 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700605 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
606 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800607}
608
609bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
610 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700611 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
612 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000613}
614
caryclark30b9fdd2016-08-31 14:36:29 -0700615bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700616 const SkPathOp shapeOp, const char* testName) {
617 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
618 ExpectMatch::kFlaky);
619}
620
Cary Clark59d5a0e2017-01-23 14:38:52 +0000621bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
622 const SkPathOp shapeOp, const char* testName) {
Cary Clark59d5a0e2017-01-23 14:38:52 +0000623 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
mtklein406654b2014-09-03 15:34:37 -0700635void initializeTests(skiatest::Reporter* reporter, const char* test) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400636 static SkMutex& mu = *(new SkMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000637 if (reporter->verbose()) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400638 SkAutoMutexExclusive lock(mu);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000639 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.org4431e772014-04-14 17:08:59 +0000644 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000645 size_t inLen = inData.count();
646 inFile.read(inData.begin(), inLen);
Ben Wagner4d1955c2017-03-10 13:08:15 -0500647 inFile.close();
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000648 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.com818b0cc2013-04-08 11:50:46 +0000654 }
655 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000656}
657
Mike Reed7d34dc72019-11-26 12:17:17 -0500658void PathOpsThreadState::outputProgress(const char* pathStr, SkPathFillType pathFillType) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000659 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700660 const char* pathPrefix = nullptr;
661 const char* nameSuffix = nullptr;
Mike Reed7d34dc72019-11-26 12:17:17 -0500662 if (pathFillType == SkPathFillType::kEvenOdd) {
663 pathPrefix = " path.setFillType(SkPathFillType::kEvenOdd);\n";
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000664 nameSuffix = "x";
665 }
Ben Wagner561c1b02017-01-09 15:54:34 -0500666 appendTest(pathStr, pathPrefix, nameSuffix, testFunction, false, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000667}
668
Mike Reedff80c2a2017-01-07 11:16:28 -0500669void PathOpsThreadState::outputProgress(const char* pathStr, SkPathOp op) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000670 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000671 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000672 const char* nameSuffix = opSuffixes[op];
Ben Wagner561c1b02017-01-09 15:54:34 -0500673 appendTest(pathStr, nullptr, nameSuffix, testFunction, true, fPathStr);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000674}
675
676void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000677 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700678 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000679 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000680 size_t index;
681 if (firstTest) {
682 index = count - 1;
683 while (index > 0 && tests[index].fun != firstTest) {
684 --index;
685 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000686 (*tests[index].fun)(reporter, tests[index].str);
687 if (tests[index].fun == stopTest) {
688 return;
689 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000690 }
691 index = reverse ? count - 1 : 0;
692 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700693 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000694 do {
caryclark54359292015-03-26 07:52:43 -0700695 if (tests[index].fun == skipTest) {
696 foundSkip = true;
697 }
698 if (foundSkip && tests[index].fun != firstTest) {
caryclark30b9fdd2016-08-31 14:36:29 -0700699 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000700 }
caryclark03b03ca2015-04-23 09:13:37 -0700701 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000702 break;
703 }
704 index += reverse ? -1 : 1;
705 } while (true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000706}