blob: 3f1a298c42007b808b3cf8cdf1ca6c5fbfa7c344 [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
8#include "PathOpsExtendedTest.h"
caryclark@google.com66089e42013-04-10 15:55:37 +00009#include "PathOpsThreadedCommon.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000012#include "SkForceLinking.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000013#include "SkMatrix.h"
mtklein1b249332015-07-07 12:21:21 -070014#include "SkMutex.h"
skia.committer@gmail.come02c5da2014-04-26 03:04:35 +000015#include "SkPaint.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000016#include "SkStream.h"
17
bungeman60e0fee2015-08-26 05:15:46 -070018#include <stdlib.h>
19
caryclark@google.com818b0cc2013-04-08 11:50:46 +000020#ifdef SK_BUILD_FOR_MAC
21#include <sys/sysctl.h>
22#endif
23
caryclark55888e42016-07-18 10:01:36 -070024bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result
25 SkDEBUGPARAMS(bool skipAssert)
26 SkDEBUGPARAMS(const char* testName));
27
28bool SimplifyDebug(const SkPath& one, SkPath* result
29 SkDEBUGPARAMS(bool skipAssert)
30 SkDEBUGPARAMS(const char* testName));
31
32
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000033__SK_FORCE_IMAGE_DECODER_LINKING;
34
caryclark65b427c2014-09-18 10:32:57 -070035DEFINE_bool2(runFail, f, false, "run tests known to fail.");
caryclark54359292015-03-26 07:52:43 -070036DEFINE_bool2(runBinary, f, false, "run tests known to fail binary sect.");
caryclark65b427c2014-09-18 10:32:57 -070037
caryclark@google.com818b0cc2013-04-08 11:50:46 +000038static const char marker[] =
39 "</div>\n"
40 "\n"
41 "<script type=\"text/javascript\">\n"
42 "\n"
43 "var testDivs = [\n";
44
45static const char* opStrs[] = {
caryclark54359292015-03-26 07:52:43 -070046 "kDifference_SkPathOp",
47 "kIntersect_SkPathOp",
48 "kUnion_SkPathOp",
caryclark55888e42016-07-18 10:01:36 -070049 "kXOR_PathOp",
caryclark54359292015-03-26 07:52:43 -070050 "kReverseDifference_SkPathOp",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000051};
52
53static const char* opSuffixes[] = {
54 "d",
55 "i",
56 "u",
caryclark@google.com66089e42013-04-10 15:55:37 +000057 "o",
caryclark55888e42016-07-18 10:01:36 -070058 "r",
caryclark@google.com818b0cc2013-04-08 11:50:46 +000059};
60
caryclarkd5b91732016-08-09 05:04:29 -070061enum class ExpectSuccess {
62 kNo,
63 kYes,
64 kFlaky
65};
66
67enum class SkipAssert {
68 kNo,
69 kYes
70};
71
72enum class ExpectMatch {
73 kNo,
74 kYes,
75 kFlaky
76};
77
caryclark@google.comcffbcc32013-06-04 17:59:42 +000078#if DEBUG_SHOW_TEST_NAME
79static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000080 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000081 uint8_t verb;
82 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000083 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000084 bool firstPtSet = false;
85 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000086 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
87 switch (verb) {
88 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070089 if (firstPtSet && lastPtSet && firstPt != lastPt) {
90 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
91 firstPt.fX, firstPt.fY);
92 lastPtSet = false;
93 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000094 firstPt = pts[0];
95 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000096 continue;
97 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000098 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
99 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000100 lastPt = pts[1];
101 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000102 break;
103 case SkPath::kQuad_Verb:
104 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000105 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000106 lastPt = pts[2];
107 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000108 break;
caryclark54359292015-03-26 07:52:43 -0700109 case SkPath::kConic_Verb:
110 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
111 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
112 iter.conicWeight());
113 lastPt = pts[2];
114 lastPtSet = true;
115 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000116 case SkPath::kCubic_Verb:
117 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +0000118 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
119 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000120 lastPt = pts[3];
121 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122 break;
123 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000124 if (firstPtSet && lastPtSet && firstPt != lastPt) {
125 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
126 firstPt.fX, firstPt.fY);
127 }
128 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000129 break;
130 default:
131 SkDEBUGFAIL("bad verb");
132 return;
133 }
134 }
caryclarkdac1d172014-06-17 05:15:38 -0700135 if (firstPtSet && lastPtSet && firstPt != lastPt) {
136 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
137 firstPt.fX, firstPt.fY);
138 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000139}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000140#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000141
142void showOp(const SkPathOp op) {
143 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700144 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000145 SkDebugf("op difference\n");
146 break;
caryclark54359292015-03-26 07:52:43 -0700147 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000148 SkDebugf("op intersect\n");
149 break;
caryclark54359292015-03-26 07:52:43 -0700150 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000151 SkDebugf("op union\n");
152 break;
caryclark54359292015-03-26 07:52:43 -0700153 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000154 SkDebugf("op xor\n");
155 break;
caryclark54359292015-03-26 07:52:43 -0700156 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000157 SkDebugf("op reverse difference\n");
158 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000159 default:
160 SkASSERT(0);
161 }
162}
163
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000164#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000165static char hexorator(int x) {
166 if (x < 10) {
167 return x + '0';
168 }
169 x -= 10;
170 SkASSERT(x < 26);
171 return x + 'A';
172}
173#endif
174
175void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
176#if DEBUG_SHOW_TEST_NAME
177 state->fSerialNo[0] = hexorator(state->fA);
178 state->fSerialNo[1] = hexorator(state->fB);
179 state->fSerialNo[2] = hexorator(state->fC);
180 state->fSerialNo[3] = hexorator(state->fD);
181 state->fSerialNo[4] = hexorator(a);
182 state->fSerialNo[5] = hexorator(b);
183 state->fSerialNo[6] = hexorator(c);
184 state->fSerialNo[7] = hexorator(d);
185 state->fSerialNo[8] = '\0';
186 SkDebugf("%s\n", state->fSerialNo);
187 if (strcmp(state->fSerialNo, state->fKey) == 0) {
188 SkDebugf("%s\n", state->fPathStr);
189 }
190#endif
191}
192
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000193const int bitWidth = 64;
194const int bitHeight = 64;
195
196static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
197 SkRect larger = one.getBounds();
198 larger.join(two.getBounds());
199 SkScalar largerWidth = larger.width();
200 if (largerWidth < 4) {
201 largerWidth = 4;
202 }
203 SkScalar largerHeight = larger.height();
204 if (largerHeight < 4) {
205 largerHeight = 4;
206 }
207 SkScalar hScale = (bitWidth - 2) / largerWidth;
208 SkScalar vScale = (bitHeight - 2) / largerHeight;
209 scale.reset();
210 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700211 larger.fLeft *= hScale;
212 larger.fRight *= hScale;
213 larger.fTop *= vScale;
214 larger.fBottom *= vScale;
215 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
216 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
217 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
218 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
219 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000220}
221
222static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
223 int& error2x2) {
224 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000225 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000226 }
227 SkCanvas canvas(bits);
228 canvas.drawColor(SK_ColorWHITE);
229 SkPaint paint;
230 canvas.save();
231 const SkRect& bounds1 = scaledOne.getBounds();
232 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
233 canvas.drawPath(scaledOne, paint);
234 canvas.restore();
235 canvas.save();
236 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
237 canvas.drawPath(scaledTwo, paint);
238 canvas.restore();
239 int errors2 = 0;
240 int errors = 0;
241 for (int y = 0; y < bitHeight - 1; ++y) {
242 uint32_t* addr1 = bits.getAddr32(0, y);
243 uint32_t* addr2 = bits.getAddr32(0, y + 1);
244 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
245 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
246 for (int x = 0; x < bitWidth - 1; ++x) {
247 // count 2x2 blocks
248 bool err = addr1[x] != addr3[x];
249 if (err) {
250 errors2 += addr1[x + 1] != addr3[x + 1]
251 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
252 errors++;
253 }
254 }
255 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000256 error2x2 = errors2;
257 return errors;
258}
259
260static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
261 SkPath& scaledTwo, int& error2x2) {
262 SkMatrix scale;
263 scaleMatrix(one, two, scale);
264 one.transform(scale, &scaledOne);
265 two.transform(scale, &scaledTwo);
266 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
267}
268
269bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
270 if (!drawPaths) {
271 return true;
272 }
273 const SkRect& bounds1 = one.getBounds();
274 const SkRect& bounds2 = two.getBounds();
275 SkRect larger = bounds1;
276 larger.join(bounds2);
277 SkBitmap bits;
278 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000279 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000280 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
281 return false;
282 }
reed@google.come1ca7052013-12-17 19:22:07 +0000283 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000284 if (bitHeight >= (int) sizeof(out)) {
285 return false;
286 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000287 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000288 SkCanvas canvas(bits);
289 canvas.drawColor(SK_ColorWHITE);
290 SkPaint paint;
291 canvas.save();
292 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
293 canvas.drawPath(one, paint);
294 canvas.restore();
295 canvas.save();
296 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
297 canvas.drawPath(two, paint);
298 canvas.restore();
299 for (int y = 0; y < bitHeight; ++y) {
300 uint32_t* addr1 = bits.getAddr32(0, y);
301 int x;
302 char* outPtr = out;
303 for (x = 0; x < bitWidth; ++x) {
304 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
305 }
306 *outPtr++ = '|';
307 for (x = bitWidth; x < bitWidth * 2; ++x) {
308 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
309 }
310 *outPtr++ = '\0';
311 SkDebugf("%s\n", out);
312 }
313 return true;
314}
315
caryclark54359292015-03-26 07:52:43 -0700316int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000317 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000318 int errors2x2;
319 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000320 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000321 if (errors2x2 == 0) {
322 return 0;
323 }
324 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000325 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
326}
327
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000328static SkTDArray<SkPathOp> gTestOp;
329
330static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
331 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
332 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000333 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000334 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700335 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000336 }
caryclark2bec26a2016-05-26 09:01:47 -0700337 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
338 testName, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000339 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000340 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700341 SkPathOpsDebug::ShowOnePath(a, "path", false);
342 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000343 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000344 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700345 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000346}
347
reed086eea92016-05-04 17:12:46 -0700348SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700349
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000350static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
351 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800352 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclarkd5b91732016-08-09 05:04:29 -0700353 ExpectMatch expectMatch) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000354 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800355 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000356 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclarkd5b91732016-08-09 05:04:29 -0700357 if (ExpectMatch::kNo == expectMatch) {
caryclark38a017b2015-05-13 10:13:17 -0700358 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800359 REPORTER_ASSERT(reporter, 0);
360 }
361 return 0;
362 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000363 if (errors2x2 == 0) {
364 return 0;
365 }
caryclarkd5b91732016-08-09 05:04:29 -0700366 if (ExpectMatch::kYes == expectMatch && errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000367 SkAutoMutexAcquire autoM(compareDebugOut3);
368 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700369 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000370 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700371 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000372 }
caryclark38a017b2015-05-13 10:13:17 -0700373 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000374}
375
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000376// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000377static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000378static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000379
380static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
381 outFile.writeText(testName);
382 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000383 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000384 if (nameSuffix) {
385 outFile.writeText(nameSuffix);
386 }
387}
388
389static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
390 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000391#if 0
caryclark54359292015-03-26 07:52:43 -0700392 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000393 writeTestName(nameSuffix, outFile);
394 outFile.writeText("\">\n");
395 if (pathPrefix) {
396 outFile.writeText(pathPrefix);
397 }
398 outFile.writeText(pathStr);
399 outFile.writeText("</div>\n\n");
400
401 outFile.writeText(marker);
402 outFile.writeText(" ");
403 writeTestName(nameSuffix, outFile);
404 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000405#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000406 outFile.writeText("static void ");
407 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000408 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000409 if (twoPaths) {
410 outFile.writeText(", pathB");
411 }
412 outFile.writeText(";\n");
413 if (pathPrefix) {
414 outFile.writeText(pathPrefix);
415 }
416 outFile.writeText(pathStr);
417 outFile.writeText(" ");
418 outFile.writeText(testFunction);
419 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000420#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000421 outFile.writeText("static void (*firstTest)() = ");
422 writeTestName(nameSuffix, outFile);
423 outFile.writeText(";\n\n");
424
425 outFile.writeText("static struct {\n");
426 outFile.writeText(" void (*fun)();\n");
427 outFile.writeText(" const char* str;\n");
428 outFile.writeText("} tests[] = {\n");
429 outFile.writeText(" TEST(");
430 writeTestName(nameSuffix, outFile);
431 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000432#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000433 outFile.flush();
434}
435
reed086eea92016-05-04 17:12:46 -0700436SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700437
caryclark@google.com66089e42013-04-10 15:55:37 +0000438bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
439 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000440 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
441 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700442 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000443 if (!Simplify(path, &out)) {
444 SkDebugf("%s did not expect failure\n", __FUNCTION__);
445 REPORTER_ASSERT(state.fReporter, 0);
446 return false;
447 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000448 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000449 return true;
450 }
halcanary96fcdcc2015-08-27 07:41:13 -0700451 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700452 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000453 SkAutoMutexAcquire autoM(simplifyDebugOut);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000454 char temp[8192];
455 sk_bzero(temp, sizeof(temp));
456 SkMemoryWStream stream(temp, sizeof(temp));
halcanary96fcdcc2015-08-27 07:41:13 -0700457 const char* pathPrefix = nullptr;
458 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000459 if (fillType == SkPath::kEvenOdd_FillType) {
460 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
461 nameSuffix = "x";
462 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000463 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000464 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800465 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000466 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000467 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000468 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000469 return result == 0;
470}
471
caryclark54359292015-03-26 07:52:43 -0700472static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700473 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700474#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000475 showPathData(path);
476#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000477 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700478 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
479 SkDEBUGPARAMS(testName))) {
480 if (ExpectSuccess::kYes == expectSuccess) {
481 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
482 REPORTER_ASSERT(reporter, 0);
483 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000484 return false;
caryclark55888e42016-07-18 10:01:36 -0700485 } else {
486 if (ExpectSuccess::kNo == expectSuccess) {
487 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
488 REPORTER_ASSERT(reporter, 0);
489 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000490 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000491 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700492 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700493 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700494 if (!errors) {
495 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
496 REPORTER_ASSERT(reporter, 0);
497 return false;
498 }
caryclarkd5b91732016-08-09 05:04:29 -0700499 } else if (ExpectMatch::kYes == expectMatch && errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000500 REPORTER_ASSERT(reporter, 0);
501 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000502 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700503 return errors == 0;
504}
505
506bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700507 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
508 ExpectMatch::kYes);
509}
510
caryclark30b9fdd2016-08-31 14:36:29 -0700511bool testSimplifyFuzz(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
512 return inner_simplify(reporter, path, filename, ExpectSuccess::kFlaky, SkipAssert::kYes,
513 ExpectMatch::kFlaky);
caryclark54359292015-03-26 07:52:43 -0700514}
515
516bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
517 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700518 return inner_simplify(reporter, path, filename, checkFail ?
519 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000520}
521
caryclark@google.coma5e55922013-05-07 18:51:31 +0000522#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000523static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
524 SkDebugf("\n");
525 showPathData(a);
526 showOp(shapeOp);
527 showPathData(b);
528}
529#endif
530
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000531static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700532 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
533 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700534#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000535 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000536#endif
537 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700538 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700539 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700540 if (ExpectSuccess::kYes == expectSuccess) {
541 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700542 REPORTER_ASSERT(reporter, 0);
543 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000544 return false;
caryclark55888e42016-07-18 10:01:36 -0700545 } else {
546 if (ExpectSuccess::kNo == expectSuccess) {
547 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
548 REPORTER_ASSERT(reporter, 0);
549 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000550 }
caryclark54359292015-03-26 07:52:43 -0700551 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000552 return true;
553 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000554 SkPath pathOut, scaledPathOut;
555 SkRegion rgnA, rgnB, openClip, rgnOut;
556 openClip.setRect(-16000, -16000, 16000, 16000);
557 rgnA.setPath(a, openClip);
558 rgnB.setPath(b, openClip);
559 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
560 rgnOut.getBoundaryPath(&pathOut);
561
562 SkMatrix scale;
563 scaleMatrix(a, b, scale);
564 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
565 SkPath scaledA, scaledB;
566 scaledA.addPath(a, scale);
567 scaledA.setFillType(a.getFillType());
568 scaledB.addPath(b, scale);
569 scaledB.setFillType(b.getFillType());
570 scaledRgnA.setPath(scaledA, openClip);
571 scaledRgnB.setPath(scaledB, openClip);
572 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
573 scaledRgnOut.getBoundaryPath(&scaledPathOut);
574 SkBitmap bitmap;
575 SkPath scaledOut;
576 scaledOut.addPath(out, scale);
577 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000578 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclarkd5b91732016-08-09 05:04:29 -0700579 a, b, shapeOp, scale, expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000580 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000581 return result == 0;
582}
583
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000584bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800585 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700586 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
587 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800588}
589
590bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
591 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700592 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
593 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000594}
595
caryclark30b9fdd2016-08-31 14:36:29 -0700596bool testPathOpFuzz(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkd5b91732016-08-09 05:04:29 -0700597 const SkPathOp shapeOp, const char* testName) {
598 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kFlaky, SkipAssert::kYes,
599 ExpectMatch::kFlaky);
600}
601
caryclark55888e42016-07-18 10:01:36 -0700602bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000603 const SkPathOp shapeOp, const char* testName) {
604#if DEBUG_SHOW_TEST_NAME
605 showName(a, b, shapeOp);
606#endif
caryclark1049f122015-04-20 08:31:59 -0700607 SkPath orig;
608 orig.lineTo(54, 43);
609 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000610 if (Op(a, b, shapeOp, &out) ) {
611 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
612 REPORTER_ASSERT(reporter, 0);
613 return false;
614 }
caryclark1049f122015-04-20 08:31:59 -0700615 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000616 return true;
617}
618
reed086eea92016-05-04 17:12:46 -0700619SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000620
mtklein406654b2014-09-03 15:34:37 -0700621void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000622 if (reporter->verbose()) {
623 SkAutoMutexAcquire lock(gMutex);
624 testName = test;
625 size_t testNameSize = strlen(test);
626 SkFILEStream inFile("../../experimental/Intersection/op.htm");
627 if (inFile.isValid()) {
628 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000629 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000630 size_t inLen = inData.count();
631 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700632 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000633 char* insert = strstr(inData.begin(), marker);
634 if (insert) {
635 insert += sizeof(marker) - 1;
636 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
637 testNumber = atoi(numLoc) + 1;
638 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000639 }
640 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000641}
642
caryclark@google.com66089e42013-04-10 15:55:37 +0000643void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
644 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700645 const char* pathPrefix = nullptr;
646 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000647 if (pathFillType == SkPath::kEvenOdd_FillType) {
648 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
649 nameSuffix = "x";
650 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000651 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
652 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000653}
654
caryclark@google.com66089e42013-04-10 15:55:37 +0000655void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
656 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000657 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000658 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000659 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
halcanary96fcdcc2015-08-27 07:41:13 -0700660 outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000661}
662
663void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000664 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700665 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000666 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000667 size_t index;
668 if (firstTest) {
669 index = count - 1;
670 while (index > 0 && tests[index].fun != firstTest) {
671 --index;
672 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000673#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700674 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000675#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000676 (*tests[index].fun)(reporter, tests[index].str);
677 if (tests[index].fun == stopTest) {
678 return;
679 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000680 }
681 index = reverse ? count - 1 : 0;
682 size_t last = reverse ? 0 : count - 1;
caryclark54359292015-03-26 07:52:43 -0700683 bool foundSkip = !skipTest;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000684 do {
caryclark54359292015-03-26 07:52:43 -0700685 if (tests[index].fun == skipTest) {
686 foundSkip = true;
687 }
688 if (foundSkip && tests[index].fun != firstTest) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000689 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700690 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000691 #endif
caryclark30b9fdd2016-08-31 14:36:29 -0700692 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000693 }
caryclark03b03ca2015-04-23 09:13:37 -0700694 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000695 break;
696 }
697 index += reverse ? -1 : 1;
698 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700699#if DEBUG_SHOW_TEST_NAME
700 SkDebugf(
701 "\n"
702 "</div>\n"
703 "\n"
704 "<script type=\"text/javascript\">\n"
705 "\n"
706 "var testDivs = [\n"
707 );
708 index = reverse ? count - 1 : 0;
709 last = reverse ? 0 : count - 1;
710 foundSkip = !skipTest;
711 do {
712 if (tests[index].fun == skipTest) {
713 foundSkip = true;
714 }
715 if (foundSkip && tests[index].fun != firstTest) {
716 SkDebugf(" %s,\n", tests[index].str);
717 }
718 if (tests[index].fun == stopTest || index == last) {
719 break;
720 }
721 index += reverse ? -1 : 1;
722 } while (true);
723#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000724}