blob: 6ea67fe89fb9e8bbe3a442c58e69bab2cb70481c [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
caryclark@google.comcffbcc32013-06-04 17:59:42 +000061#if DEBUG_SHOW_TEST_NAME
62static void showPathData(const SkPath& path) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000063 SkPath::RawIter iter(path);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000064 uint8_t verb;
65 SkPoint pts[4];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000066 SkPoint firstPt = {0, 0}, lastPt = {0, 0};
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000067 bool firstPtSet = false;
68 bool lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000069 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
70 switch (verb) {
71 case SkPath::kMove_Verb:
caryclarkdac1d172014-06-17 05:15:38 -070072 if (firstPtSet && lastPtSet && firstPt != lastPt) {
73 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
74 firstPt.fX, firstPt.fY);
75 lastPtSet = false;
76 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000077 firstPt = pts[0];
78 firstPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000079 continue;
80 case SkPath::kLine_Verb:
caryclark@google.com66089e42013-04-10 15:55:37 +000081 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", pts[0].fX, pts[0].fY,
82 pts[1].fX, pts[1].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000083 lastPt = pts[1];
84 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000085 break;
86 case SkPath::kQuad_Verb:
87 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com66089e42013-04-10 15:55:37 +000088 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 +000089 lastPt = pts[2];
90 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000091 break;
caryclark54359292015-03-26 07:52:43 -070092 case SkPath::kConic_Verb:
93 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, //weight=%1.9g\n",
94 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
95 iter.conicWeight());
96 lastPt = pts[2];
97 lastPtSet = true;
98 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000099 case SkPath::kCubic_Verb:
100 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 +0000101 pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
102 pts[3].fX, pts[3].fY);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000103 lastPt = pts[3];
104 lastPtSet = true;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000105 break;
106 case SkPath::kClose_Verb:
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000107 if (firstPtSet && lastPtSet && firstPt != lastPt) {
108 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
109 firstPt.fX, firstPt.fY);
110 }
111 firstPtSet = lastPtSet = false;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000112 break;
113 default:
114 SkDEBUGFAIL("bad verb");
115 return;
116 }
117 }
caryclarkdac1d172014-06-17 05:15:38 -0700118 if (firstPtSet && lastPtSet && firstPt != lastPt) {
119 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", lastPt.fX, lastPt.fY,
120 firstPt.fX, firstPt.fY);
121 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122}
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000123#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000124
125void showOp(const SkPathOp op) {
126 switch (op) {
caryclark54359292015-03-26 07:52:43 -0700127 case kDifference_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000128 SkDebugf("op difference\n");
129 break;
caryclark54359292015-03-26 07:52:43 -0700130 case kIntersect_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000131 SkDebugf("op intersect\n");
132 break;
caryclark54359292015-03-26 07:52:43 -0700133 case kUnion_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000134 SkDebugf("op union\n");
135 break;
caryclark54359292015-03-26 07:52:43 -0700136 case kXOR_SkPathOp:
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000137 SkDebugf("op xor\n");
138 break;
caryclark54359292015-03-26 07:52:43 -0700139 case kReverseDifference_SkPathOp:
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000140 SkDebugf("op reverse difference\n");
141 break;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000142 default:
143 SkASSERT(0);
144 }
145}
146
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000147#if DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000148static char hexorator(int x) {
149 if (x < 10) {
150 return x + '0';
151 }
152 x -= 10;
153 SkASSERT(x < 26);
154 return x + 'A';
155}
156#endif
157
158void ShowTestName(PathOpsThreadState* state, int a, int b, int c, int d) {
159#if DEBUG_SHOW_TEST_NAME
160 state->fSerialNo[0] = hexorator(state->fA);
161 state->fSerialNo[1] = hexorator(state->fB);
162 state->fSerialNo[2] = hexorator(state->fC);
163 state->fSerialNo[3] = hexorator(state->fD);
164 state->fSerialNo[4] = hexorator(a);
165 state->fSerialNo[5] = hexorator(b);
166 state->fSerialNo[6] = hexorator(c);
167 state->fSerialNo[7] = hexorator(d);
168 state->fSerialNo[8] = '\0';
169 SkDebugf("%s\n", state->fSerialNo);
170 if (strcmp(state->fSerialNo, state->fKey) == 0) {
171 SkDebugf("%s\n", state->fPathStr);
172 }
173#endif
174}
175
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000176const int bitWidth = 64;
177const int bitHeight = 64;
178
179static void scaleMatrix(const SkPath& one, const SkPath& two, SkMatrix& scale) {
180 SkRect larger = one.getBounds();
181 larger.join(two.getBounds());
182 SkScalar largerWidth = larger.width();
183 if (largerWidth < 4) {
184 largerWidth = 4;
185 }
186 SkScalar largerHeight = larger.height();
187 if (largerHeight < 4) {
188 largerHeight = 4;
189 }
190 SkScalar hScale = (bitWidth - 2) / largerWidth;
191 SkScalar vScale = (bitHeight - 2) / largerHeight;
192 scale.reset();
193 scale.preScale(hScale, vScale);
caryclark26ad22a2015-10-16 09:03:38 -0700194 larger.fLeft *= hScale;
195 larger.fRight *= hScale;
196 larger.fTop *= vScale;
197 larger.fBottom *= vScale;
198 SkScalar dx = -16000 > larger.fLeft ? -16000 - larger.fLeft
199 : 16000 < larger.fRight ? 16000 - larger.fRight : 0;
200 SkScalar dy = -16000 > larger.fTop ? -16000 - larger.fTop
201 : 16000 < larger.fBottom ? 16000 - larger.fBottom : 0;
202 scale.postTranslate(dx, dy);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000203}
204
205static int pathsDrawTheSame(SkBitmap& bits, const SkPath& scaledOne, const SkPath& scaledTwo,
206 int& error2x2) {
207 if (bits.width() == 0) {
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000208 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000209 }
210 SkCanvas canvas(bits);
211 canvas.drawColor(SK_ColorWHITE);
212 SkPaint paint;
213 canvas.save();
214 const SkRect& bounds1 = scaledOne.getBounds();
215 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
216 canvas.drawPath(scaledOne, paint);
217 canvas.restore();
218 canvas.save();
219 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
220 canvas.drawPath(scaledTwo, paint);
221 canvas.restore();
222 int errors2 = 0;
223 int errors = 0;
224 for (int y = 0; y < bitHeight - 1; ++y) {
225 uint32_t* addr1 = bits.getAddr32(0, y);
226 uint32_t* addr2 = bits.getAddr32(0, y + 1);
227 uint32_t* addr3 = bits.getAddr32(bitWidth, y);
228 uint32_t* addr4 = bits.getAddr32(bitWidth, y + 1);
229 for (int x = 0; x < bitWidth - 1; ++x) {
230 // count 2x2 blocks
231 bool err = addr1[x] != addr3[x];
232 if (err) {
233 errors2 += addr1[x + 1] != addr3[x + 1]
234 && addr2[x] != addr4[x] && addr2[x + 1] != addr4[x + 1];
235 errors++;
236 }
237 }
238 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000239 error2x2 = errors2;
240 return errors;
241}
242
243static int pathsDrawTheSame(const SkPath& one, const SkPath& two, SkBitmap& bits, SkPath& scaledOne,
244 SkPath& scaledTwo, int& error2x2) {
245 SkMatrix scale;
246 scaleMatrix(one, two, scale);
247 one.transform(scale, &scaledOne);
248 two.transform(scale, &scaledTwo);
249 return pathsDrawTheSame(bits, scaledOne, scaledTwo, error2x2);
250}
251
252bool drawAsciiPaths(const SkPath& one, const SkPath& two, bool drawPaths) {
253 if (!drawPaths) {
254 return true;
255 }
256 const SkRect& bounds1 = one.getBounds();
257 const SkRect& bounds2 = two.getBounds();
258 SkRect larger = bounds1;
259 larger.join(bounds2);
260 SkBitmap bits;
261 char out[256];
reed@google.come1ca7052013-12-17 19:22:07 +0000262 int bitWidth = SkScalarCeilToInt(larger.width()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000263 if (bitWidth * 2 + 1 >= (int) sizeof(out)) {
264 return false;
265 }
reed@google.come1ca7052013-12-17 19:22:07 +0000266 int bitHeight = SkScalarCeilToInt(larger.height()) + 2;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000267 if (bitHeight >= (int) sizeof(out)) {
268 return false;
269 }
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +0000270 bits.allocN32Pixels(bitWidth * 2, bitHeight);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000271 SkCanvas canvas(bits);
272 canvas.drawColor(SK_ColorWHITE);
273 SkPaint paint;
274 canvas.save();
275 canvas.translate(-bounds1.fLeft + 1, -bounds1.fTop + 1);
276 canvas.drawPath(one, paint);
277 canvas.restore();
278 canvas.save();
279 canvas.translate(-bounds1.fLeft + 1 + bitWidth, -bounds1.fTop + 1);
280 canvas.drawPath(two, paint);
281 canvas.restore();
282 for (int y = 0; y < bitHeight; ++y) {
283 uint32_t* addr1 = bits.getAddr32(0, y);
284 int x;
285 char* outPtr = out;
286 for (x = 0; x < bitWidth; ++x) {
287 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
288 }
289 *outPtr++ = '|';
290 for (x = bitWidth; x < bitWidth * 2; ++x) {
291 *outPtr++ = addr1[x] == (uint32_t) -1 ? '_' : 'x';
292 }
293 *outPtr++ = '\0';
294 SkDebugf("%s\n", out);
295 }
296 return true;
297}
298
caryclark54359292015-03-26 07:52:43 -0700299int comparePaths(skiatest::Reporter* reporter, const char* filename, const SkPath& one,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000300 const SkPath& two, SkBitmap& bitmap) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000301 int errors2x2;
302 SkPath scaledOne, scaledTwo;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000303 (void) pathsDrawTheSame(one, two, bitmap, scaledOne, scaledTwo, errors2x2);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000304 if (errors2x2 == 0) {
305 return 0;
306 }
307 const int MAX_ERRORS = 9;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000308 return errors2x2 > MAX_ERRORS ? errors2x2 : 0;
309}
310
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000311static SkTDArray<SkPathOp> gTestOp;
312
313static void showPathOpPath(const char* testName, const SkPath& one, const SkPath& two,
314 const SkPath& a, const SkPath& b, const SkPath& scaledOne, const SkPath& scaledTwo,
315 const SkPathOp shapeOp, const SkMatrix& scale) {
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000316 SkASSERT((unsigned) shapeOp < SK_ARRAY_COUNT(opStrs));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000317 if (!testName) {
caryclark1049f122015-04-20 08:31:59 -0700318 testName = "xOp";
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000319 }
caryclark2bec26a2016-05-26 09:01:47 -0700320 SkDebugf("static void %s_%s(skiatest::Reporter* reporter, const char* filename) {\n",
321 testName, opSuffixes[shapeOp]);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000322 *gTestOp.append() = shapeOp;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000323 SkDebugf(" SkPath path, pathB;\n");
caryclark19eb3b22014-07-18 05:08:14 -0700324 SkPathOpsDebug::ShowOnePath(a, "path", false);
325 SkPathOpsDebug::ShowOnePath(b, "pathB", false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000326 SkDebugf(" testPathOp(reporter, path, pathB, %s, filename);\n", opStrs[shapeOp]);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000327 SkDebugf("}\n");
caryclark26ad22a2015-10-16 09:03:38 -0700328 drawAsciiPaths(scaledOne, scaledTwo, true);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000329}
330
reed086eea92016-05-04 17:12:46 -0700331SK_DECLARE_STATIC_MUTEX(compareDebugOut3);
caryclark38a017b2015-05-13 10:13:17 -0700332
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000333static int comparePaths(skiatest::Reporter* reporter, const char* testName, const SkPath& one,
334 const SkPath& scaledOne, const SkPath& two, const SkPath& scaledTwo, SkBitmap& bitmap,
caryclark65f55312014-11-13 06:58:52 -0800335 const SkPath& a, const SkPath& b, const SkPathOp shapeOp, const SkMatrix& scale,
caryclark38a017b2015-05-13 10:13:17 -0700336 bool expectSuccess) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000337 int errors2x2;
caryclark65f55312014-11-13 06:58:52 -0800338 const int MAX_ERRORS = 8;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000339 (void) pathsDrawTheSame(bitmap, scaledOne, scaledTwo, errors2x2);
caryclark65f55312014-11-13 06:58:52 -0800340 if (!expectSuccess) {
caryclark38a017b2015-05-13 10:13:17 -0700341 if (errors2x2 < MAX_ERRORS) {
caryclark65f55312014-11-13 06:58:52 -0800342 REPORTER_ASSERT(reporter, 0);
343 }
344 return 0;
345 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000346 if (errors2x2 == 0) {
347 return 0;
348 }
caryclark38a017b2015-05-13 10:13:17 -0700349 if (errors2x2 >= MAX_ERRORS) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000350 SkAutoMutexAcquire autoM(compareDebugOut3);
351 showPathOpPath(testName, one, two, a, b, scaledOne, scaledTwo, shapeOp, scale);
caryclarkaec25102015-04-29 08:28:30 -0700352 SkDebugf("\n/*");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000353 REPORTER_ASSERT(reporter, 0);
caryclarkaec25102015-04-29 08:28:30 -0700354 SkDebugf(" */\n");
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000355 }
caryclark38a017b2015-05-13 10:13:17 -0700356 return errors2x2 >= MAX_ERRORS ? errors2x2 : 0;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000357}
358
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000359// Default values for when reporter->verbose() is false.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000360static int testNumber = 55;
commit-bot@chromium.org409774e2013-10-02 16:15:44 +0000361static const char* testName = "pathOpTest";
caryclark@google.com66089e42013-04-10 15:55:37 +0000362
363static void writeTestName(const char* nameSuffix, SkMemoryWStream& outFile) {
364 outFile.writeText(testName);
365 outFile.writeDecAsText(testNumber);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000366 ++testNumber;
caryclark@google.com66089e42013-04-10 15:55:37 +0000367 if (nameSuffix) {
368 outFile.writeText(nameSuffix);
369 }
370}
371
372static void outputToStream(const char* pathStr, const char* pathPrefix, const char* nameSuffix,
373 const char* testFunction, bool twoPaths, SkMemoryWStream& outFile) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000374#if 0
caryclark54359292015-03-26 07:52:43 -0700375 outFile.writeText("\n<div id=\"");
caryclark@google.com66089e42013-04-10 15:55:37 +0000376 writeTestName(nameSuffix, outFile);
377 outFile.writeText("\">\n");
378 if (pathPrefix) {
379 outFile.writeText(pathPrefix);
380 }
381 outFile.writeText(pathStr);
382 outFile.writeText("</div>\n\n");
383
384 outFile.writeText(marker);
385 outFile.writeText(" ");
386 writeTestName(nameSuffix, outFile);
387 outFile.writeText(",\n\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000388#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000389 outFile.writeText("static void ");
390 writeTestName(nameSuffix, outFile);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000391 outFile.writeText("(skiatest::Reporter* reporter) {\n SkPath path");
caryclark@google.com66089e42013-04-10 15:55:37 +0000392 if (twoPaths) {
393 outFile.writeText(", pathB");
394 }
395 outFile.writeText(";\n");
396 if (pathPrefix) {
397 outFile.writeText(pathPrefix);
398 }
399 outFile.writeText(pathStr);
400 outFile.writeText(" ");
401 outFile.writeText(testFunction);
402 outFile.writeText("\n}\n\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000403#if 0
caryclark@google.com66089e42013-04-10 15:55:37 +0000404 outFile.writeText("static void (*firstTest)() = ");
405 writeTestName(nameSuffix, outFile);
406 outFile.writeText(";\n\n");
407
408 outFile.writeText("static struct {\n");
409 outFile.writeText(" void (*fun)();\n");
410 outFile.writeText(" const char* str;\n");
411 outFile.writeText("} tests[] = {\n");
412 outFile.writeText(" TEST(");
413 writeTestName(nameSuffix, outFile);
414 outFile.writeText("),\n");
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000415#endif
caryclark@google.com66089e42013-04-10 15:55:37 +0000416 outFile.flush();
417}
418
reed086eea92016-05-04 17:12:46 -0700419SK_DECLARE_STATIC_MUTEX(simplifyDebugOut);
caryclark54359292015-03-26 07:52:43 -0700420
caryclark@google.com66089e42013-04-10 15:55:37 +0000421bool testSimplify(SkPath& path, bool useXor, SkPath& out, PathOpsThreadState& state,
422 const char* pathStr) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000423 SkPath::FillType fillType = useXor ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
424 path.setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700425 state.fReporter->bumpTestCount();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000426 if (!Simplify(path, &out)) {
427 SkDebugf("%s did not expect failure\n", __FUNCTION__);
428 REPORTER_ASSERT(state.fReporter, 0);
429 return false;
430 }
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000431 if (!state.fReporter->verbose()) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000432 return true;
433 }
halcanary96fcdcc2015-08-27 07:41:13 -0700434 int result = comparePaths(state.fReporter, nullptr, path, out, *state.fBitmap);
caryclark54359292015-03-26 07:52:43 -0700435 if (result) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000436 SkAutoMutexAcquire autoM(simplifyDebugOut);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000437 char temp[8192];
438 sk_bzero(temp, sizeof(temp));
439 SkMemoryWStream stream(temp, sizeof(temp));
halcanary96fcdcc2015-08-27 07:41:13 -0700440 const char* pathPrefix = nullptr;
441 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000442 if (fillType == SkPath::kEvenOdd_FillType) {
443 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
444 nameSuffix = "x";
445 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000446 const char testFunction[] = "testSimplify(reporter, path);";
caryclark@google.com66089e42013-04-10 15:55:37 +0000447 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, stream);
kkinnunen297aaf92015-02-19 06:32:12 -0800448 SkDebugf("%s", temp);
caryclark@google.com66089e42013-04-10 15:55:37 +0000449 REPORTER_ASSERT(state.fReporter, 0);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000450 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000451 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000452 return result == 0;
453}
454
caryclark55888e42016-07-18 10:01:36 -0700455enum class ExpectSuccess {
456 kNo,
457 kYes
458};
459
460enum class SkipAssert {
461 kNo,
462 kYes
463};
464
465enum class ExpectMatch {
466 kNo,
467 kYes
468};
469
caryclark54359292015-03-26 07:52:43 -0700470static bool inner_simplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
caryclark55888e42016-07-18 10:01:36 -0700471 ExpectSuccess expectSuccess, SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700472#if 0 && DEBUG_SHOW_TEST_NAME
caryclark@google.com03610322013-04-18 15:58:21 +0000473 showPathData(path);
474#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000475 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700476 if (!SimplifyDebug(path, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
477 SkDEBUGPARAMS(testName))) {
478 if (ExpectSuccess::kYes == expectSuccess) {
479 SkDebugf("%s did not expect %s failure\n", __FUNCTION__, filename);
480 REPORTER_ASSERT(reporter, 0);
481 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000482 return false;
caryclark55888e42016-07-18 10:01:36 -0700483 } else {
484 if (ExpectSuccess::kNo == expectSuccess) {
485 SkDebugf("%s %s unexpected success\n", __FUNCTION__, filename);
486 REPORTER_ASSERT(reporter, 0);
487 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000488 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000489 SkBitmap bitmap;
caryclark54359292015-03-26 07:52:43 -0700490 int errors = comparePaths(reporter, filename, path, out, bitmap);
caryclark55888e42016-07-18 10:01:36 -0700491 if (ExpectMatch::kNo == expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700492 if (!errors) {
493 SkDebugf("%s failing test %s now succeeds\n", __FUNCTION__, filename);
494 REPORTER_ASSERT(reporter, 0);
495 return false;
496 }
497 } else if (errors) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000498 REPORTER_ASSERT(reporter, 0);
499 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000500 reporter->bumpTestCount();
caryclark54359292015-03-26 07:52:43 -0700501 return errors == 0;
502}
503
504bool testSimplify(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
caryclark55888e42016-07-18 10:01:36 -0700505 return inner_simplify(reporter, path, filename, ExpectSuccess::kYes, SkipAssert::kNo,
506 ExpectMatch::kYes);
507}
508
509bool testSimplifyFailSkipAssert(skiatest::Reporter* reporter, const SkPath& path, const char* filename) {
510 return inner_simplify(reporter, path, filename, ExpectSuccess::kNo, SkipAssert::kYes,
511 ExpectMatch::kNo);
caryclark54359292015-03-26 07:52:43 -0700512}
513
514bool testSimplifyCheck(skiatest::Reporter* reporter, const SkPath& path, const char* filename,
515 bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700516 return inner_simplify(reporter, path, filename, checkFail ?
517 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000518}
519
caryclark@google.coma5e55922013-05-07 18:51:31 +0000520#if DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000521static void showName(const SkPath& a, const SkPath& b, const SkPathOp shapeOp) {
522 SkDebugf("\n");
523 showPathData(a);
524 showOp(shapeOp);
525 showPathData(b);
526}
527#endif
528
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000529static bool innerPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark55888e42016-07-18 10:01:36 -0700530 const SkPathOp shapeOp, const char* testName, ExpectSuccess expectSuccess,
531 SkipAssert skipAssert, ExpectMatch expectMatch) {
caryclark54359292015-03-26 07:52:43 -0700532#if 0 && DEBUG_SHOW_TEST_NAME
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000533 showName(a, b, shapeOp);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000534#endif
535 SkPath out;
caryclark55888e42016-07-18 10:01:36 -0700536 if (!OpDebug(a, b, shapeOp, &out SkDEBUGPARAMS(SkipAssert::kYes == skipAssert)
caryclarkdae6b972016-06-08 04:28:19 -0700537 SkDEBUGPARAMS(testName))) {
caryclark55888e42016-07-18 10:01:36 -0700538 if (ExpectSuccess::kYes == expectSuccess) {
539 SkDebugf("%s %s did not expect failure\n", __FUNCTION__, testName);
caryclark3f0753d2016-06-28 09:23:57 -0700540 REPORTER_ASSERT(reporter, 0);
541 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000542 return false;
caryclark55888e42016-07-18 10:01:36 -0700543 } else {
544 if (ExpectSuccess::kNo == expectSuccess) {
545 SkDebugf("%s %s unexpected success\n", __FUNCTION__, testName);
546 REPORTER_ASSERT(reporter, 0);
547 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000548 }
caryclark54359292015-03-26 07:52:43 -0700549 if (!reporter->verbose()) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000550 return true;
551 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000552 SkPath pathOut, scaledPathOut;
553 SkRegion rgnA, rgnB, openClip, rgnOut;
554 openClip.setRect(-16000, -16000, 16000, 16000);
555 rgnA.setPath(a, openClip);
556 rgnB.setPath(b, openClip);
557 rgnOut.op(rgnA, rgnB, (SkRegion::Op) shapeOp);
558 rgnOut.getBoundaryPath(&pathOut);
559
560 SkMatrix scale;
561 scaleMatrix(a, b, scale);
562 SkRegion scaledRgnA, scaledRgnB, scaledRgnOut;
563 SkPath scaledA, scaledB;
564 scaledA.addPath(a, scale);
565 scaledA.setFillType(a.getFillType());
566 scaledB.addPath(b, scale);
567 scaledB.setFillType(b.getFillType());
568 scaledRgnA.setPath(scaledA, openClip);
569 scaledRgnB.setPath(scaledB, openClip);
570 scaledRgnOut.op(scaledRgnA, scaledRgnB, (SkRegion::Op) shapeOp);
571 scaledRgnOut.getBoundaryPath(&scaledPathOut);
572 SkBitmap bitmap;
573 SkPath scaledOut;
574 scaledOut.addPath(out, scale);
575 scaledOut.setFillType(out.getFillType());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000576 int result = comparePaths(reporter, testName, pathOut, scaledPathOut, out, scaledOut, bitmap,
caryclark55888e42016-07-18 10:01:36 -0700577 a, b, shapeOp, scale, ExpectMatch::kYes == expectMatch);
caryclark@google.com66089e42013-04-10 15:55:37 +0000578 reporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000579 return result == 0;
580}
581
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000582bool testPathOp(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark65f55312014-11-13 06:58:52 -0800583 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700584 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kNo,
585 ExpectMatch::kYes);
caryclark65f55312014-11-13 06:58:52 -0800586}
587
588bool testPathOpCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
589 const SkPathOp shapeOp, const char* testName, bool checkFail) {
caryclark55888e42016-07-18 10:01:36 -0700590 return innerPathOp(reporter, a, b, shapeOp, testName, checkFail ?
591 ExpectSuccess::kYes : ExpectSuccess::kNo, SkipAssert::kNo, ExpectMatch::kNo);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000592}
593
caryclark1049f122015-04-20 08:31:59 -0700594bool testPathOpFailCheck(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
595 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700596 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kNo, SkipAssert::kNo,
597 ExpectMatch::kNo);
caryclarkdae6b972016-06-08 04:28:19 -0700598}
599
caryclark55888e42016-07-18 10:01:36 -0700600bool testPathOpSkipAssert(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclarkdae6b972016-06-08 04:28:19 -0700601 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700602 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kYes, SkipAssert::kYes,
603 ExpectMatch::kYes);
caryclark1049f122015-04-20 08:31:59 -0700604}
605
caryclark55888e42016-07-18 10:01:36 -0700606bool testPathOpFailSkipAssert(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
caryclark3f0753d2016-06-28 09:23:57 -0700607 const SkPathOp shapeOp, const char* testName) {
caryclark55888e42016-07-18 10:01:36 -0700608 return innerPathOp(reporter, a, b, shapeOp, testName, ExpectSuccess::kNo, SkipAssert::kYes,
609 ExpectMatch::kNo);
caryclark3f0753d2016-06-28 09:23:57 -0700610}
611
caryclark55888e42016-07-18 10:01:36 -0700612bool testPathOpFail(skiatest::Reporter* reporter, const SkPath& a, const SkPath& b,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000613 const SkPathOp shapeOp, const char* testName) {
614#if DEBUG_SHOW_TEST_NAME
615 showName(a, b, shapeOp);
616#endif
caryclark1049f122015-04-20 08:31:59 -0700617 SkPath orig;
618 orig.lineTo(54, 43);
619 SkPath out = orig;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000620 if (Op(a, b, shapeOp, &out) ) {
621 SkDebugf("%s test is expected to fail\n", __FUNCTION__);
622 REPORTER_ASSERT(reporter, 0);
623 return false;
624 }
caryclark1049f122015-04-20 08:31:59 -0700625 SkASSERT(out == orig);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000626 return true;
627}
628
reed086eea92016-05-04 17:12:46 -0700629SK_DECLARE_STATIC_MUTEX(gMutex);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000630
mtklein406654b2014-09-03 15:34:37 -0700631void initializeTests(skiatest::Reporter* reporter, const char* test) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000632 if (reporter->verbose()) {
633 SkAutoMutexAcquire lock(gMutex);
634 testName = test;
635 size_t testNameSize = strlen(test);
636 SkFILEStream inFile("../../experimental/Intersection/op.htm");
637 if (inFile.isValid()) {
638 SkTDArray<char> inData;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000639 inData.setCount((int) inFile.getLength());
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000640 size_t inLen = inData.count();
641 inFile.read(inData.begin(), inLen);
halcanary96fcdcc2015-08-27 07:41:13 -0700642 inFile.setPath(nullptr);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000643 char* insert = strstr(inData.begin(), marker);
644 if (insert) {
645 insert += sizeof(marker) - 1;
646 const char* numLoc = insert + 4 /* indent spaces */ + testNameSize - 1;
647 testNumber = atoi(numLoc) + 1;
648 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000649 }
650 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000651}
652
caryclark@google.com66089e42013-04-10 15:55:37 +0000653void outputProgress(char* ramStr, const char* pathStr, SkPath::FillType pathFillType) {
654 const char testFunction[] = "testSimplify(path);";
halcanary96fcdcc2015-08-27 07:41:13 -0700655 const char* pathPrefix = nullptr;
656 const char* nameSuffix = nullptr;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000657 if (pathFillType == SkPath::kEvenOdd_FillType) {
658 pathPrefix = " path.setFillType(SkPath::kEvenOdd_FillType);\n";
659 nameSuffix = "x";
660 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000661 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
662 outputToStream(pathStr, pathPrefix, nameSuffix, testFunction, false, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000663}
664
caryclark@google.com66089e42013-04-10 15:55:37 +0000665void outputProgress(char* ramStr, const char* pathStr, SkPathOp op) {
666 const char testFunction[] = "testOp(path);";
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000667 SkASSERT((size_t) op < SK_ARRAY_COUNT(opSuffixes));
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000668 const char* nameSuffix = opSuffixes[op];
caryclark@google.com66089e42013-04-10 15:55:37 +0000669 SkMemoryWStream rRamStream(ramStr, PATH_STR_SIZE);
halcanary96fcdcc2015-08-27 07:41:13 -0700670 outputToStream(pathStr, nullptr, nameSuffix, testFunction, true, rRamStream);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000671}
672
673void RunTestSet(skiatest::Reporter* reporter, TestDesc tests[], size_t count,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000674 void (*firstTest)(skiatest::Reporter* , const char* filename),
caryclark54359292015-03-26 07:52:43 -0700675 void (*skipTest)(skiatest::Reporter* , const char* filename),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000676 void (*stopTest)(skiatest::Reporter* , const char* filename), bool reverse) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000677 size_t index;
678 if (firstTest) {
679 index = count - 1;
680 while (index > 0 && tests[index].fun != firstTest) {
681 --index;
682 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000683#if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700684 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000685#endif
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) {
caryclark@google.coma5e55922013-05-07 18:51:31 +0000699 #if DEBUG_SHOW_TEST_NAME
caryclark54359292015-03-26 07:52:43 -0700700 SkDebugf("\n<div id=\"%s\">\n", tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000701 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000702 (*tests[index].fun)(reporter, tests[index].str);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000703 }
caryclark03b03ca2015-04-23 09:13:37 -0700704 if (tests[index].fun == stopTest || index == last) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000705 break;
706 }
707 index += reverse ? -1 : 1;
708 } while (true);
caryclark03b03ca2015-04-23 09:13:37 -0700709#if DEBUG_SHOW_TEST_NAME
710 SkDebugf(
711 "\n"
712 "</div>\n"
713 "\n"
714 "<script type=\"text/javascript\">\n"
715 "\n"
716 "var testDivs = [\n"
717 );
718 index = reverse ? count - 1 : 0;
719 last = reverse ? 0 : count - 1;
720 foundSkip = !skipTest;
721 do {
722 if (tests[index].fun == skipTest) {
723 foundSkip = true;
724 }
725 if (foundSkip && tests[index].fun != firstTest) {
726 SkDebugf(" %s,\n", tests[index].str);
727 }
728 if (tests[index].fun == stopTest || index == last) {
729 break;
730 }
731 index += reverse ? -1 : 1;
732 } while (true);
733#endif
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000734}